Pastoray opened a new pull request, #420:
URL: https://github.com/apache/datafusion-spark/pull/420
<!--
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?
- Part of #388.
## Rationale for this change
`if` and `array` coerced their arguments with DataFusion's rules rather than
Spark's, so
they resolved mixed-type arguments differently from Spark. For example,
`array(1::INT, 'a')`
is `ARRAY<STRING>` in Spark, and with ANSI mode on `array(1::INT, '2')` is
`ARRAY<BIGINT>`,
because ANSI casts the string to the other type. Neither function followed
`enable_ansi_mode`, and a mismatch such as `if(true, 1::INT, array(1::INT))`
did not raise
Spark's `DATA_DIFF_TYPES` error.
## What changes are included in this PR?
- `type_coercion.rs`: ports Spark's `findWiderCommonType`,
`findWiderTypeForTwo` (with both
the `TypeCoercion` and `AnsiTypeCoercion` string promotion) and
`findWiderTypeWithoutStringPromotion`.
- New helper `merge_arg_types`: computes the common type of a range of
arguments, casts them
to it and raises `DATA_DIFF_TYPES` if there is none. It writes the type
list in the format
of the Spark expression: `["INT", "ARRAY<INT>"]` for `If`, `("INT" or
"ARRAY<INT>")` for
`CreateArray` (`TypeUtils.checkForSameTypeInputExpr`).
- `if` and `array` use the helper and follow
`datafusion.execution.enable_ansi_mode`.
- `greatest`/`least` call the ported `findWiderTypeWithoutStringPromotion`
instead of their
own fold. Behavior is unchanged.
- Supporting fixes needed by the new rules: dictionary types are treated as
their value
type, and `force_nullable` marks a string cast to a non-string, non-binary
type (which ANSI
promotion produces) as nullable, like Spark's `Cast.forceNullable`.
## Are these changes tested?
Yes:
- Unit tests in `type_coercion.rs`: string promotion with and without ANSI,
argument order,
nested types and map keys, dictionaries, decimals, empty/NULL inputs, and
both error formats.
- New `.slt` cases in `conditional/if.slt` and `array/array.slt`, with ANSI
mode on and off.
The new cases fail before this change and pass after it.
The rules and error messages were checked against the Spark v4.2.0 source:
`TypeCoercion`,
`AnsiTypeCoercion`, `AnsiStringPromotionTypeCoercion`, `TypeCoercionHelper`,
`TypeUtils`,
`If`, `CreateArray`, `Cast.forceNullable` and `error-conditions.json`. I
wrote the expected
results by hand from those rules and then ran them. I did not run them
against a real Spark
with the PySpark runner.
`cargo fmt --all`, `cargo clippy --all-targets --all-features -- -D
warnings`,
`cargo test -p datafusion-spark` and `cargo test -p
datafusion-spark-sqllogictest` all pass.
## Are there any user-facing changes?
Yes, in how `if` and `array` coerce mixed-type arguments: they now follow
Spark's rules,
which depend on ANSI mode, and report Spark's `DATA_DIFF_TYPES` error when
there is no common
type. The new helper functions are `pub(crate)`; `SparkIf` and `SparkArray`
gain a private
field and keep their public constructors, so there are no breaking changes
to the public API.
--
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]