andygrove commented on code in PR #401:
URL: https://github.com/apache/datafusion-spark/pull/401#discussion_r4669573556
##########
crates/spark/src/function/array/array_contains.rs:
##########
@@ -72,6 +81,65 @@ impl ScalarUDFImpl for SparkArrayContains {
}
}
+/// Casts the array element and the search element to their tightest common
type, and rejects the
+/// argument types that Spark's `ArrayContains.checkInputDataTypes` rejects.
+fn coerce_array_and_element(
+ name: &str,
+ array: &DataType,
+ element: &DataType,
+) -> Result<Vec<DataType>> {
+ if array.is_null() || element.is_null() {
+ return null_type_err(name);
+ }
+ let (item, large) = match array {
+ DataType::List(field) | DataType::FixedSizeList(field, _) => (field,
false),
+ DataType::LargeList(field) => (field, true),
+ other => {
+ return plan_err!(
+ "[DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE] The first parameter
of `{name}` \
+ requires the \"ARRAY\" type, however it has the type \"{}\".
SQLSTATE: 42K09",
+ spark_sql_type(other)
+ );
+ }
+ };
+ // Spark's `ArrayContains` calls the non-ANSI
`TypeCoercion.findTightestCommonType`.
+ let common = tightest_common_type(item.data_type(), element, false)
+ .filter(|_| struct_field_types_match(item.data_type(), element));
+ let Some(common) = common else {
+ return plan_err!(
+ "[DATATYPE_MISMATCH.ARRAY_FUNCTION_DIFF_TYPES] Input to `{name}`
should have \
+ been \"ARRAY\" followed by a value with same element type, but
it's \
+ [\"{}\", \"{}\"]. SQLSTATE: 42K09",
+ spark_sql_type(array),
+ spark_sql_type(element)
+ );
+ };
+ if common.is_null() {
+ return null_type_err(name);
+ }
+ if !is_orderable(&common) {
+ return plan_err!(
+ "[DATATYPE_MISMATCH.INVALID_ORDERING_TYPE] The `{name}` does not
support \
+ ordering on type \"{}\". SQLSTATE: 42K09",
+ spark_sql_type(&common)
+ );
+ }
+ let item = Arc::new(Field::new(item.name(), common.clone(), true));
+ let array = if large {
+ DataType::LargeList(item)
+ } else {
+ DataType::List(item)
+ };
+ Ok(vec![array, common])
+}
+
+fn null_type_err<T>(name: &str) -> Result<T> {
+ plan_err!(
+ "[DATATYPE_MISMATCH.NULL_TYPE] Null typed values cannot be used as
arguments of \
+ `{name}`. SQLSTATE: 42K09"
+ )
+}
Review Comment:
This block is the same as `coerce_array_and_element` and `null_type_err` in
`array_position.rs`, apart from the element NULL check. `array_remove` and
`array_insert` have near-copies as well. #353 already plans to move this into
`type_coercion.rs` and have `array_contains` use it. Would you be up for doing
that here? The helper could take the common-type function as a parameter, since
`array_contains` needs `wider_type` and the others need `tightest_common_type`.
That way the copies can't drift apart again.
##########
crates/spark/src/function/array/array_contains.rs:
##########
@@ -72,6 +81,65 @@ impl ScalarUDFImpl for SparkArrayContains {
}
}
+/// Casts the array element and the search element to their tightest common
type, and rejects the
+/// argument types that Spark's `ArrayContains.checkInputDataTypes` rejects.
+fn coerce_array_and_element(
+ name: &str,
+ array: &DataType,
+ element: &DataType,
+) -> Result<Vec<DataType>> {
+ if array.is_null() || element.is_null() {
+ return null_type_err(name);
+ }
+ let (item, large) = match array {
+ DataType::List(field) | DataType::FixedSizeList(field, _) => (field,
false),
+ DataType::LargeList(field) => (field, true),
+ other => {
+ return plan_err!(
+ "[DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE] The first parameter
of `{name}` \
+ requires the \"ARRAY\" type, however it has the type \"{}\".
SQLSTATE: 42K09",
+ spark_sql_type(other)
+ );
+ }
+ };
+ // Spark's `ArrayContains` calls the non-ANSI
`TypeCoercion.findTightestCommonType`.
+ let common = tightest_common_type(item.data_type(), element, false)
+ .filter(|_| struct_field_types_match(item.data_type(), element));
Review Comment:
What should happen with structs whose field types differ? `SELECT
array_contains(array(named_struct('a', CAST(1 AS INT))), named_struct('a',
CAST(1 AS BIGINT)))` returns `true` on `main` and in Spark 4.2 with ANSI on,
which is Spark's default. Spark returns `ARRAY_FUNCTION_DIFF_TYPES` only with
ANSI off. With this filter it is always an error, so ANSI users lose a query
that works today.
One option is to skip `struct_field_types_match` when `enable_ansi_mode` is
on, using `with_updated_config` the way `greatest_least.rs` does. Another is to
leave struct coercion as it is in this PR and handle it separately. Either way,
an `.slt` case would pin the behavior down.
##########
crates/spark/src/function/array/array_contains.rs:
##########
@@ -72,6 +81,65 @@ impl ScalarUDFImpl for SparkArrayContains {
}
}
+/// Casts the array element and the search element to their tightest common
type, and rejects the
+/// argument types that Spark's `ArrayContains.checkInputDataTypes` rejects.
+fn coerce_array_and_element(
+ name: &str,
+ array: &DataType,
+ element: &DataType,
+) -> Result<Vec<DataType>> {
+ if array.is_null() || element.is_null() {
+ return null_type_err(name);
+ }
+ let (item, large) = match array {
+ DataType::List(field) | DataType::FixedSizeList(field, _) => (field,
false),
+ DataType::LargeList(field) => (field, true),
+ other => {
+ return plan_err!(
+ "[DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE] The first parameter
of `{name}` \
+ requires the \"ARRAY\" type, however it has the type \"{}\".
SQLSTATE: 42K09",
+ spark_sql_type(other)
+ );
+ }
+ };
+ // Spark's `ArrayContains` calls the non-ANSI
`TypeCoercion.findTightestCommonType`.
+ let common = tightest_common_type(item.data_type(), element, false)
Review Comment:
I think the comment here may have come over from `array_position`. In Spark
3.5 and 4.2, `ArrayContains.inputTypes` calls
`TypeCoercion.findWiderTypeWithoutStringPromotionForTwo`, not
`findTightestCommonType`. The difference shows up with decimals. For example,
PySpark 4.2 returns `true` for `SELECT array_contains(array(array(1)),
array(CAST(1.0 AS DECIMAL(2,1))))` and for `SELECT array_contains(array(1, 2),
CAST(1.0 AS DECIMAL(2,1)))`. With this change both fail with
`ARRAY_FUNCTION_DIFF_TYPES`, and the first one returns `true` on `main` today.
We already have `type_coercion::wider_type`, which ports the Spark function.
When I swapped it in locally, all of these matched Spark and the unit tests
still passed. Could you switch to it and add these cases to the `.slt`?
##########
crates/sqllogictest/test_files/array/array_contains.slt:
##########
@@ -39,17 +39,33 @@ SELECT array_contains(array(1, NULL, 3), 3);
----
true
-# Null array -> null
-# xfail spark: Spark rejects an untyped NULL argument (NULL_TYPE),
datafusion-spark returns NULL
(https://github.com/apache/datafusion-spark/issues/367)
-query B
+# Untyped NULL array is rejected at planning time, as in Spark.
+query error \[DATATYPE_MISMATCH\.NULL_TYPE\] Null typed values cannot be used
as arguments of `array_contains`
SELECT array_contains(NULL, 1);
+
+# Untyped NULL element is rejected at planning time, as in Spark.
+query error \[DATATYPE_MISMATCH\.NULL_TYPE\] Null typed values cannot be used
as arguments of `array_contains`
+SELECT array_contains(array(1, 2, 3), NULL);
+
+# Both untyped NULL arguments are rejected at planning time.
+query error \[DATATYPE_MISMATCH\.NULL_TYPE\] Null typed values cannot be used
as arguments of `array_contains`
Review Comment:
Could you add a "Type coercion" section here, like the one in
`array_position.slt`? It would cover the error cases the unit tests check
(`array_contains(1, 1)`, `array(1, 2)` vs `'1'`, a map element) and the
widening cases (INT vs BIGINT, decimals, nested arrays, structs). The PySpark
runner checks `.slt` files against real Spark, so these would then be verified
on every run. One thing to watch is that `array(1, 2)` is BIGINT here, so use
`CAST(.. AS INT)` where you mean INT.
##########
crates/spark/src/function/array/array_contains.rs:
##########
@@ -194,4 +262,130 @@ mod tests {
.unwrap();
assert_eq!(result.as_boolean(),
&BooleanArray::from(vec![Some(false)]));
}
+
+ #[test]
+ fn test_coerce_types_untyped_null_array() {
+ let udf = SparkArrayContains::new();
+ let err = udf
+ .coerce_types(&[DataType::Null, DataType::Int32])
+ .unwrap_err();
+ assert!(
+ err.to_string()
+ .contains("[DATATYPE_MISMATCH.NULL_TYPE] Null typed values
cannot be used as arguments of `array_contains`. SQLSTATE: 42K09")
+ );
+ }
+
+ #[test]
+ fn test_coerce_types_untyped_null_element() {
+ let udf = SparkArrayContains::new();
+ let err = udf
+ .coerce_types(&[
+ DataType::List(Arc::new(Field::new("item", DataType::Int32,
true))),
+ DataType::Null,
+ ])
+ .unwrap_err();
+ assert!(
+ err.to_string()
+ .contains("[DATATYPE_MISMATCH.NULL_TYPE] Null typed values
cannot be used as arguments of `array_contains`. SQLSTATE: 42K09")
+ );
+ }
+
+ #[test]
+ fn test_coerce_types_both_untyped_null() {
+ let udf = SparkArrayContains::new();
+ let err = udf
+ .coerce_types(&[DataType::Null, DataType::Null])
+ .unwrap_err();
+ assert!(
+ err.to_string()
+ .contains("[DATATYPE_MISMATCH.NULL_TYPE] Null typed values
cannot be used as arguments of `array_contains`. SQLSTATE: 42K09")
+ );
+ }
+
+ #[test]
+ fn test_coerce_types_valid_typed_nulls() {
Review Comment:
This test passes `[List<Int32>, Int32]`, so it checks plain same-type
coercion. A typed NULL doesn't show up at the type level. Maybe rename it to
something like `test_coerce_types_same_type`? The typed-NULL behavior is
already covered well in the `.slt`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]