Skip to content

Commit 96ddd55

Browse files
authored
fix: derive custom nullability for spark map_from_arrays (#19275)
## 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 #19160 - Part of #19144 ## 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. --> ## What changes are included in this PR? - Spark `map_from_arrays` now uses `return_field_from_args` to handle nullability <!-- 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? - New unit tests are added - All previous tests pass <!-- 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 aebae99 commit 96ddd55

1 file changed

Lines changed: 79 additions & 9 deletions

File tree

datafusion/spark/src/function/map/map_from_arrays.rs

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ use crate::function::map::utils::{
2323
};
2424
use arrow::array::{Array, ArrayRef, NullArray};
2525
use arrow::compute::kernels::cast;
26-
use arrow::datatypes::DataType;
26+
use arrow::datatypes::{DataType, Field, FieldRef};
2727
use datafusion_common::utils::take_function_args;
28-
use datafusion_common::Result;
29-
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};
28+
use datafusion_common::{internal_err, Result};
29+
use datafusion_expr::{
30+
ColumnarValue, ReturnFieldArgs, ScalarUDFImpl, Signature, Volatility,
31+
};
3032
use datafusion_functions::utils::make_scalar_function;
33+
use std::sync::Arc;
3134

3235
/// Spark-compatible `map_from_arrays` expression
3336
/// <https://spark.apache.org/docs/latest/api/sql/index.html#map_from_arrays>
@@ -63,12 +66,23 @@ impl ScalarUDFImpl for MapFromArrays {
6366
&self.signature
6467
}
6568

66-
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
67-
let [key_type, value_type] = take_function_args("map_from_arrays", arg_types)?;
68-
Ok(map_type_from_key_value_types(
69-
get_element_type(key_type)?,
70-
get_element_type(value_type)?,
71-
))
69+
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
70+
internal_err!("return_field_from_args should be used instead")
71+
}
72+
73+
fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
74+
let [keys_field, values_field] = args.arg_fields else {
75+
return internal_err!("map_from_arrays expects exactly 2 arguments");
76+
};
77+
78+
let map_type = map_type_from_key_value_types(
79+
get_element_type(keys_field.data_type())?,
80+
get_element_type(values_field.data_type())?,
81+
);
82+
// Spark marks map_from_arrays as null intolerant, so the output is
83+
// nullable if either input is nullable.
84+
let nullable = keys_field.is_nullable() || values_field.is_nullable();
85+
Ok(Arc::new(Field::new(self.name(), map_type, nullable)))
7286
}
7387

7488
fn invoke_with_args(
@@ -103,3 +117,59 @@ fn map_from_arrays_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
103117
values.nulls(),
104118
)
105119
}
120+
121+
#[cfg(test)]
122+
mod tests {
123+
use super::*;
124+
use arrow::datatypes::Field;
125+
use datafusion_expr::ReturnFieldArgs;
126+
127+
#[test]
128+
fn test_map_from_arrays_nullability_and_type() {
129+
let func = MapFromArrays::new();
130+
131+
let keys_field: FieldRef = Arc::new(Field::new(
132+
"keys",
133+
DataType::List(Arc::new(Field::new("item", DataType::Int32, false))),
134+
false,
135+
));
136+
let values_field: FieldRef = Arc::new(Field::new(
137+
"values",
138+
DataType::List(Arc::new(Field::new("item", DataType::Utf8, true))),
139+
false,
140+
));
141+
142+
let out = func
143+
.return_field_from_args(ReturnFieldArgs {
144+
arg_fields: &[Arc::clone(&keys_field), Arc::clone(&values_field)],
145+
scalar_arguments: &[None, None],
146+
})
147+
.expect("return_field_from_args should succeed");
148+
149+
let expected_type =
150+
map_type_from_key_value_types(&DataType::Int32, &DataType::Utf8);
151+
assert_eq!(out.data_type(), &expected_type);
152+
assert!(
153+
!out.is_nullable(),
154+
"map_from_arrays should be non-nullable when both inputs are non-nullable"
155+
);
156+
157+
let nullable_keys: FieldRef = Arc::new(Field::new(
158+
"keys",
159+
DataType::List(Arc::new(Field::new("item", DataType::Int32, false))),
160+
true,
161+
));
162+
163+
let out_nullable = func
164+
.return_field_from_args(ReturnFieldArgs {
165+
arg_fields: &[nullable_keys, values_field],
166+
scalar_arguments: &[None, None],
167+
})
168+
.expect("return_field_from_args should succeed");
169+
170+
assert!(
171+
out_nullable.is_nullable(),
172+
"map_from_arrays should be nullable when any input is nullable"
173+
);
174+
}
175+
}

0 commit comments

Comments
 (0)