Subodh-17 opened a new pull request, #401:
URL: https://github.com/apache/datafusion-spark/pull/401
<!--
SPDX-License-Identifier: Apache-2.0
SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under
one or more contributor license agreements
-->
## Which issue does this PR close?
- Closes #367.
## Rationale for this change
In Apache Spark, `array_contains` rejects untyped `NULL` inputs during query
analysis / planning with error class `[DATATYPE_MISMATCH.NULL_TYPE]`:
```
[DATATYPE_MISMATCH.NULL_TYPE] Cannot resolve "array_contains(NULL, 1)" due
to data type mismatch: Null typed values cannot be used as arguments of
"array_contains". SQLSTATE: 42K09
```
Previously, `datafusion-spark` configured `SparkArrayContains` with
DataFusion's generic `Signature::array_and_element`, which bypassed
analysis-time validation for untyped `NULL` and returned `NULL` at runtime
(annotated as `xfail spark` in
`crates/sqllogictest/test_files/array/array_contains.slt`).
Typed NULL expressions, such as `CAST(NULL AS ARRAY<INT>)` or `CAST(NULL AS
INT)`, are valid in Spark and return `NULL` at runtime.
## What changes are included in this PR?
1. Configured `SparkArrayContains` with
`Signature::user_defined(Volatility::Immutable)`.
2. Implemented `ScalarUDFImpl::coerce_types` for `SparkArrayContains`
aligning with Spark's `ArrayContains.checkInputDataTypes`:
- Symmetrically rejects untyped `DataType::Null` arguments at planning
time with `[DATATYPE_MISMATCH.NULL_TYPE] Null typed values cannot be used as
arguments of `array_contains`. SQLSTATE: 42K09`.
- Validates array types (`List`, `LargeList`, `FixedSizeList`), raising
`[DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE]` if the first argument is not an
array.
- Coerces array element and search element via `tightest_common_type(...,
false)` and `struct_field_types_match`, raising
`[DATATYPE_MISMATCH.ARRAY_FUNCTION_DIFF_TYPES]` on mismatch.
- Checks `is_orderable(&common)`, raising
`[DATATYPE_MISMATCH.INVALID_ORDERING_TYPE]` for unorderable types (e.g. Map,
Interval).
3. Preserves typed NULL expressions (`CAST(NULL AS ...)`), which pass
analysis and evaluate to `NULL` at execution time.
4. Added 10 Rust unit tests in
`crates/spark/src/function/array/array_contains.rs` covering all type coercion
and error paths.
5. Updated `crates/sqllogictest/test_files/array/array_contains.slt` to
remove the two `# xfail spark` markers, assert `DATATYPE_MISMATCH.NULL_TYPE`
errors for untyped NULLs, and assert `NULL` for typed NULLs.
## Are these changes tested?
- **Unit tests**: Added 10 tests in
`crates/spark/src/function/array/array_contains.rs` verifying untyped NULL
rejection (array, element, both), typed NULL expressions, type widening, type
mismatch, non-array first argument, unorderable element type, and arity
validation.
- **Sqllogictests**: Updated
`crates/sqllogictest/test_files/array/array_contains.slt` and verified with
`cargo test -p datafusion-spark-sqllogictest -- array_contains` across both
`simplify` and `evaluate` modes.
- Verified against Apache Spark 4.2.0 behavior as documented in issue #367.
## Are there any user-facing changes?
`array_contains` now rejects untyped `NULL` arguments at query planning time
with `[DATATYPE_MISMATCH.NULL_TYPE] ... SQLSTATE: 42K09`, matching Apache Spark
behavior.
--
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]