Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion datafusion/spark/src/function/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

pub mod repeat;
pub mod shuffle;
pub mod spark_array;

Expand All @@ -24,6 +25,7 @@ use std::sync::Arc;

make_udf_function!(spark_array::SparkArray, array);
make_udf_function!(shuffle::SparkShuffle, shuffle);
make_udf_function!(repeat::SparkArrayRepeat, array_repeat);

pub mod expr_fn {
use datafusion_functions::export_functions;
Expand All @@ -34,8 +36,13 @@ pub mod expr_fn {
"Returns a random permutation of the given array.",
args
));
export_functions!((
array_repeat,
"returns an array containing element count times.",
element count
));
}

pub fn functions() -> Vec<Arc<ScalarUDF>> {
vec![array(), shuffle()]
vec![array(), shuffle(), array_repeat()]
}
128 changes: 128 additions & 0 deletions datafusion/spark/src/function/array/repeat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use arrow::datatypes::{DataType, Field};
use datafusion_common::utils::take_function_args;
use datafusion_common::{Result, ScalarValue, exec_err};
use datafusion_expr::{
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
};
use datafusion_functions_nested::repeat::ArrayRepeat;
use std::any::Any;
use std::sync::Arc;

use crate::function::null_utils::{
NullMaskResolution, apply_null_mask, compute_null_mask,
};

/// Spark-compatible `array_repeat` expression. The difference with DataFusion's `array_repeat` is the handling of NULL inputs: in spark if any input is NULL, the result is NULL.
/// <https://spark.apache.org/docs/latest/api/sql/index.html#array_repeat>
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct SparkArrayRepeat {
signature: Signature,
}

impl Default for SparkArrayRepeat {
fn default() -> Self {
Self::new()
}
}

impl SparkArrayRepeat {
pub fn new() -> Self {
Self {
signature: Signature::user_defined(Volatility::Immutable),
}
}
}

impl ScalarUDFImpl for SparkArrayRepeat {
fn as_any(&self) -> &dyn Any {
self
}

fn name(&self) -> &str {
"array_repeat"
}

fn signature(&self) -> &Signature {
&self.signature
}

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::List(Arc::new(Field::new_list_field(
arg_types[0].clone(),
true,
))))
}

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
spark_array_repeat(args)
}

fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
let [first_type, second_type] = take_function_args(self.name(), arg_types)?;

// Coerce the second argument to Int64/UInt64 if it's a numeric type
let second = match second_type {
DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 => {
DataType::Int64
}
DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 => {
DataType::UInt64
}
_ => return exec_err!("count must be an integer type"),
};

Ok(vec![first_type.clone(), second])
}
}

/// This is a Spark-specific wrapper around DataFusion's array_repeat that returns NULL
/// if any argument is NULL (Spark behavior), whereas DataFusion's array_repeat ignores NULLs.
fn spark_array_repeat(args: ScalarFunctionArgs) -> Result<ColumnarValue> {
let ScalarFunctionArgs {
args: arg_values,
arg_fields,
number_rows,
return_field,
config_options,
} = args;
let return_type = return_field.data_type().clone();

// Step 1: Check for NULL mask in incoming args
let null_mask = compute_null_mask(&arg_values, number_rows)?;

// If any argument is null then return NULL immediately
if matches!(null_mask, NullMaskResolution::ReturnNull) {
return Ok(ColumnarValue::Scalar(ScalarValue::try_from(return_type)?));
}

// Step 2: Delegate to DataFusion's array_repeat
let array_repeat_func = ArrayRepeat::new();
let func_args = ScalarFunctionArgs {
args: arg_values,
arg_fields,
number_rows,
return_field,
config_options,
};
let result = array_repeat_func.invoke_with_args(func_args)?;

// Step 3: Apply NULL mask to result
apply_null_mask(result, null_mask, &return_type)
}
1 change: 1 addition & 0 deletions datafusion/spark/src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod lambda;
pub mod map;
pub mod math;
pub mod misc;
mod null_utils;
pub mod predicate;
pub mod string;
pub mod r#struct;
Expand Down
122 changes: 122 additions & 0 deletions datafusion/spark/src/function/null_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use arrow::array::Array;
use arrow::buffer::NullBuffer;
use arrow::datatypes::DataType;
use datafusion_common::{Result, ScalarValue};
use datafusion_expr::ColumnarValue;
use std::sync::Arc;

pub(crate) enum NullMaskResolution {
/// Return NULL as the result (e.g., scalar inputs with at least one NULL)
ReturnNull,
/// No null mask needed (e.g., all scalar inputs are non-NULL)
NoMask,
/// Null mask to apply for arrays
Apply(NullBuffer),
}

/// Compute NULL mask for the arguments using NullBuffer::union
pub(crate) fn compute_null_mask(
args: &[ColumnarValue],
number_rows: usize,
) -> Result<NullMaskResolution> {
// Check if all arguments are scalars
let all_scalars = args
.iter()
.all(|arg| matches!(arg, ColumnarValue::Scalar(_)));

if all_scalars {
// For scalars, check if any is NULL
for arg in args {
if let ColumnarValue::Scalar(scalar) = arg
&& scalar.is_null()
{
return Ok(NullMaskResolution::ReturnNull);
}
}
// No NULLs in scalars
Ok(NullMaskResolution::NoMask)
} else {
// For arrays, compute NULL mask for each row using NullBuffer::union
let array_len = args
.iter()
.find_map(|arg| match arg {
ColumnarValue::Array(array) => Some(array.len()),
_ => None,
})
.unwrap_or(number_rows);

// Convert all scalars to arrays for uniform processing
let arrays: Result<Vec<_>> = args
.iter()
.map(|arg| match arg {
ColumnarValue::Array(array) => Ok(Arc::clone(array)),
ColumnarValue::Scalar(scalar) => scalar.to_array_of_size(array_len),
})
.collect();
let arrays = arrays?;

// Use NullBuffer::union to combine all null buffers
let combined_nulls = arrays
.iter()
.map(|arr| arr.nulls())
.fold(None, |acc, nulls| NullBuffer::union(acc.as_ref(), nulls));

match combined_nulls {
Some(nulls) => Ok(NullMaskResolution::Apply(nulls)),
None => Ok(NullMaskResolution::NoMask),
}
}
}

/// Apply NULL mask to the result using NullBuffer::union
pub(crate) fn apply_null_mask(
result: ColumnarValue,
null_mask: NullMaskResolution,
return_type: &DataType,
) -> Result<ColumnarValue> {
match (result, null_mask) {
// Scalar with ReturnNull mask means return NULL of the correct type
(ColumnarValue::Scalar(_), NullMaskResolution::ReturnNull) => {
Ok(ColumnarValue::Scalar(ScalarValue::try_from(return_type)?))
}
// Scalar without mask, return as-is
(scalar @ ColumnarValue::Scalar(_), NullMaskResolution::NoMask) => Ok(scalar),
// Array with NULL mask - use NullBuffer::union to combine nulls
(ColumnarValue::Array(array), NullMaskResolution::Apply(null_mask)) => {
// Combine the result's existing nulls with our computed null mask
let combined_nulls = NullBuffer::union(array.nulls(), Some(&null_mask));

// Create new array with combined nulls
let new_array = array
.into_data()
.into_builder()
.nulls(combined_nulls)
.build()?;

Ok(ColumnarValue::Array(Arc::new(arrow::array::make_array(
new_array,
))))
}
// Array without NULL mask, return as-is
(array @ ColumnarValue::Array(_), NullMaskResolution::NoMask) => Ok(array),
// Edge cases that shouldn't happen in practice
(scalar, _) => Ok(scalar),
}
}
Loading