Skip to content

Commit ea9f354

Browse files
authored
fix: derive custom nullability for spark bit_shift (#19222)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #19149. - Part of #19144 ## Rationale for this change As stated in the original issue the UDF uses the default is_nullable which is always true which is not the case. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - `bit_shift` now reports schema using `return_field_from_args` - Added unit tests <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? - All original tests pass - Added new unit tests for the changes <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 1a6df66 commit ea9f354

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

datafusion/spark/src/function/bitwise/bit_shift.rs

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use std::sync::Arc;
2121
use arrow::array::{ArrayRef, ArrowPrimitiveType, AsArray, Int32Array, PrimitiveArray};
2222
use arrow::compute;
2323
use arrow::datatypes::{
24-
ArrowNativeType, DataType, Int32Type, Int64Type, UInt32Type, UInt64Type,
24+
ArrowNativeType, DataType, Field, FieldRef, Int32Type, Int64Type, UInt32Type,
25+
UInt64Type,
2526
};
2627
use datafusion_common::types::{
2728
logical_int16, logical_int32, logical_int64, logical_int8, logical_uint16,
@@ -30,8 +31,8 @@ use datafusion_common::types::{
3031
use datafusion_common::utils::take_function_args;
3132
use datafusion_common::{internal_err, Result};
3233
use datafusion_expr::{
33-
Coercion, ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature,
34-
TypeSignatureClass, Volatility,
34+
Coercion, ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl,
35+
Signature, TypeSignature, TypeSignatureClass, Volatility,
3536
};
3637
use datafusion_functions::utils::make_scalar_function;
3738

@@ -275,8 +276,14 @@ impl ScalarUDFImpl for SparkBitShift {
275276
&self.signature
276277
}
277278

278-
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
279-
Ok(arg_types[0].clone())
279+
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
280+
internal_err!("return_field_from_args should be used instead")
281+
}
282+
283+
fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
284+
let nullable = args.arg_fields.iter().any(|f| f.is_nullable());
285+
let data_type = args.arg_fields[0].data_type().clone();
286+
Ok(Arc::new(Field::new(self.name(), data_type, nullable)))
280287
}
281288

282289
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
@@ -286,3 +293,58 @@ impl ScalarUDFImpl for SparkBitShift {
286293
make_scalar_function(inner, vec![])(&args.args)
287294
}
288295
}
296+
297+
#[cfg(test)]
298+
mod tests {
299+
use super::*;
300+
use arrow::datatypes::Field;
301+
use datafusion_expr::ReturnFieldArgs;
302+
303+
#[test]
304+
fn test_bit_shift_nullability() -> Result<()> {
305+
let func = SparkBitShift::left();
306+
307+
let non_nullable_value: FieldRef =
308+
Arc::new(Field::new("value", DataType::Int64, false));
309+
let non_nullable_shift: FieldRef =
310+
Arc::new(Field::new("shift", DataType::Int32, false));
311+
312+
let out = func.return_field_from_args(ReturnFieldArgs {
313+
arg_fields: &[
314+
Arc::clone(&non_nullable_value),
315+
Arc::clone(&non_nullable_shift),
316+
],
317+
scalar_arguments: &[None, None],
318+
})?;
319+
320+
assert_eq!(out.data_type(), non_nullable_value.data_type());
321+
assert!(
322+
!out.is_nullable(),
323+
"shift result should be non-nullable when both inputs are non-nullable"
324+
);
325+
326+
let nullable_value: FieldRef =
327+
Arc::new(Field::new("value", DataType::Int64, true));
328+
let out_nullable_value = func.return_field_from_args(ReturnFieldArgs {
329+
arg_fields: &[Arc::clone(&nullable_value), Arc::clone(&non_nullable_shift)],
330+
scalar_arguments: &[None, None],
331+
})?;
332+
assert!(
333+
out_nullable_value.is_nullable(),
334+
"shift result should be nullable when value is nullable"
335+
);
336+
337+
let nullable_shift: FieldRef =
338+
Arc::new(Field::new("shift", DataType::Int32, true));
339+
let out_nullable_shift = func.return_field_from_args(ReturnFieldArgs {
340+
arg_fields: &[non_nullable_value, nullable_shift],
341+
scalar_arguments: &[None, None],
342+
})?;
343+
assert!(
344+
out_nullable_shift.is_nullable(),
345+
"shift result should be nullable when shift is nullable"
346+
);
347+
348+
Ok(())
349+
}
350+
}

0 commit comments

Comments
 (0)