fix: support type coercion for MAP literals with NULL values in VALUES lists#23521
Open
PG1204 wants to merge 2 commits into
Open
fix: support type coercion for MAP literals with NULL values in VALUES lists#23521PG1204 wants to merge 2 commits into
PG1204 wants to merge 2 commits into
Conversation
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
A bare
MAP {'k1': NULL}literal infers aNullvalue type, producingMap("entries": Struct("key": Utf8, "value": Null)). Placing it in a VALUES list alongside concretely-typed map rows failed with:Error during planning: Inconsistent data type across values list at row 1 column 0.
This is because
type_union_resolution_coercion(the pairwise worker behindtype_union_resolution, which the VALUES builder uses to widen row types) had recursion arms forDictionary,RunEndEncoded,Struct, andList(vialist_coercion), but none forMap. Scalar NULL inference across a VALUES list (VALUES (1), (NULL)) already worked; the same inference just never reached into a map's value type. This gap was noted in #23406, where the tests had to use aCAST(NULL AS BIGINT)workaround.What changes are included in this PR?
datafusion/expr-common/src/type_coercion/binary.rs: wire the existingmap_coercionhelper (already used bycomparison_coercionandtype_union_coercion) intotype_union_resolution_coercion's fallback chain. The recursion into the map's key/value types then applies the exact same rules as scalar VALUES coercion (null_coercionfor NULL inference,binary_numeric_coercionfor numeric widening), symmetrically in either row order.test_type_union_resolution_map) covering Null + Int64 value types in both orders, Int64 + Null + Float64 widening, and Map vs. Struct still refusing to unify.datafusion/sqllogictest/test_files/map.sltcovering: the issue reproducer, the reversed row order, all-NULL values (resulting typeMap("entries": Struct("key": Utf8, "value": Null)), verified viaarrow_typeof), multi-key rows with one NULL value, NULL scattered across three rows, numeric widening to Float64, two-level nested maps, NULL round-tripping as NULL throughSELECT, andINSERT INTO ... VALUES.(MAP {'k': 1}), (MAP {'k': 'hello'})follows the scalar VALUES rule (coerce to the numeric type, then fail withCast error: Cannot cast string 'hello' to value of Int64 type, which is the same behavior asVALUES (1), ('hello')).CAST(NULL AS BIGINT)workaround is no longer required (those tests are left unchanged).Are these changes tested?
Yes. New unit test in
datafusion-expr-commonand new sqllogictests inmap.sltas described above. The full sqllogictest suite (492 files) passes locally with zero regressions, as docargo test -p datafusion-expr -p datafusion-expr-common,cargo fmt, andcargo clippy --workspace --all-targets -- -D warnings.Are there any user-facing changes?
VALUESlists (and other contexts that usetype_union_resolution, such asCASE/COALESCE/array literals) now unify Map types whose key/value types are coercible, including bare NULL map values. No API changes; previously failing queries now succeed, and incompatible-type errors are preserved.