Skip to content

Commit 9fe9ec7

Browse files
authored
fix: spark crc32 custom nullability (#19271)
## 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 #19157 ## Rationale for this change <!-- 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. --> The `crc32` UDF was using the default return_type implementation which does not preserve nullability information [Spark CRC32](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/hash.scala#L213-L240) * Only returns the data type (Int64) * Doesn't consider nullability of inputs * Would always mark output as non-nullable ## What changes are included in this PR? <!-- 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. --> * Implemented `return_field_from_args`: Creates a field with Int64 type and correctly propagates nullability from input fields and scalar arguments * Updated `return_type`: Now returns an error directing users to use return_field_from_args instead * Added necessary imports: `Field`, `FieldRef`, and `ReturnFieldArgs` to support the new implementation * Added comprehensive nullability tests: Verifies that nullable inputs, non-nullable inputs, and null scalar literals are handled correctly ## Are these changes tested? <!-- 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)? --> * Non-nullable Binary input produces non-nullable Int64 output * Nullable Binary input produces nullable Int64 output * Null scalar literal (e.g., crc32(NULL)) produces nullable Int64 output * Data type is correctly set to Int64 in all cases ## 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. --> This is a bug fix that corrects schema metadata only, it does not change the actual computation or introduce any breaking changes to the API.
1 parent 1acaf7a commit 9fe9ec7

File tree

1 file changed

+53
-4
lines changed
  • datafusion/spark/src/function/hash

1 file changed

+53
-4
lines changed

datafusion/spark/src/function/hash/crc32.rs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::any::Any;
1919
use std::sync::Arc;
2020

2121
use arrow::array::{ArrayRef, Int64Array};
22-
use arrow::datatypes::DataType;
22+
use arrow::datatypes::{DataType, Field, FieldRef};
2323
use crc32fast::Hasher;
2424
use datafusion_common::cast::{
2525
as_binary_array, as_binary_view_array, as_fixed_size_binary_array,
@@ -29,8 +29,8 @@ use datafusion_common::types::{NativeType, logical_string};
2929
use datafusion_common::utils::take_function_args;
3030
use datafusion_common::{Result, internal_err};
3131
use datafusion_expr::{
32-
Coercion, ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature,
33-
TypeSignatureClass, Volatility,
32+
Coercion, ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl,
33+
Signature, TypeSignatureClass, Volatility,
3434
};
3535
use datafusion_functions::utils::make_scalar_function;
3636

@@ -75,7 +75,16 @@ impl ScalarUDFImpl for SparkCrc32 {
7575
}
7676

7777
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
78-
Ok(DataType::Int64)
78+
internal_err!("return_field_from_args should be used instead")
79+
}
80+
81+
fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
82+
let nullable = args.arg_fields.iter().any(|f| f.is_nullable())
83+
|| args
84+
.scalar_arguments
85+
.iter()
86+
.any(|scalar| scalar.is_some_and(|s| s.is_null()));
87+
Ok(Arc::new(Field::new(self.name(), DataType::Int64, nullable)))
7988
}
8089

8190
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
@@ -122,3 +131,43 @@ fn spark_crc32(args: &[ArrayRef]) -> Result<ArrayRef> {
122131
}
123132
}
124133
}
134+
135+
#[cfg(test)]
136+
mod tests {
137+
use super::*;
138+
use datafusion_common::ScalarValue;
139+
140+
#[test]
141+
fn test_crc32_nullability() -> Result<()> {
142+
let crc32_func = SparkCrc32::new();
143+
144+
// non-nullable field should produce non-nullable output
145+
let field_not_null = Arc::new(Field::new("data", DataType::Binary, false));
146+
let result = crc32_func.return_field_from_args(ReturnFieldArgs {
147+
arg_fields: std::slice::from_ref(&field_not_null),
148+
scalar_arguments: &[None],
149+
})?;
150+
assert!(!result.is_nullable());
151+
assert_eq!(result.data_type(), &DataType::Int64);
152+
153+
// nullable field should produce nullable output
154+
let field_nullable = Arc::new(Field::new("data", DataType::Binary, true));
155+
let result = crc32_func.return_field_from_args(ReturnFieldArgs {
156+
arg_fields: &[field_nullable],
157+
scalar_arguments: &[None],
158+
})?;
159+
assert!(result.is_nullable());
160+
assert_eq!(result.data_type(), &DataType::Int64);
161+
162+
// null scalar value - user input literal NULL
163+
let scalar_null = ScalarValue::Binary(None);
164+
let result = crc32_func.return_field_from_args(ReturnFieldArgs {
165+
arg_fields: &[field_not_null],
166+
scalar_arguments: &[Some(&scalar_null)],
167+
})?;
168+
assert!(result.is_nullable());
169+
assert_eq!(result.data_type(), &DataType::Int64);
170+
171+
Ok(())
172+
}
173+
}

0 commit comments

Comments
 (0)