fix: make_array accepts inputs differing only in nested-field nullability#22658
fix: make_array accepts inputs differing only in nested-field nullability#22658schenksj wants to merge 2 commits into
Conversation
…lity `make_array` panicked with "Arrays with inconsistent types passed to MutableArrayData" when combining values whose types are identical except for the nullability of a nested field (e.g. a struct field that is non-nullable when built from a literal but nullable when read from a column). This blocks native execution of plans that construct structs from multiple sources (Delta Lake CDC writes, UNION within arrays). Following the precedent of Spark, Postgres, and Arrow's own `concat`, relax the strict element-type equality by widening nullable flags to `true` at every nesting level: - `array_array` now computes a merged element type that is a supertype of all arguments (OR-ing nullable flags via `Field::try_merge`) and cheaply casts each argument up to it before building the list, so `MutableArrayData` no longer sees inconsistent types. - `coerce_types_inner` widens the per-argument struct types produced by `try_type_union_resolution_with_struct` to a single common type so the declared return type matches the value produced at runtime. Adds a unit test reproducing the original panic and sqllogictest coverage for `make_array` over (nested) structs. Closes apache#22366 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Thank you for opening this pull request! Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch). Details |
| [[31, 32, 33, 34, 35, 26, 37, 38, 39, 40]] [[31, 32, 33, 34, 35, 26, 37, 38, 39, 40], [8, 9]] [[31, 32, 33, 34, 35, 26, 37, 38, 39, 40], [50, 51, 52]] | ||
|
|
||
|
|
||
| # make_array over structs built from a literal (non-nullable fields) and a |
There was a problem hiding this comment.
these tests succeed on main without this fix
| /// must succeed (widening nullable flags) rather than panic in | ||
| /// `MutableArrayData`. | ||
| #[test] | ||
| fn make_array_relaxes_nested_field_nullability() { |
There was a problem hiding this comment.
we might need to move this test somewhere like here instead
https://github.com/apache/datafusion/blob/main/datafusion/core/tests/sql/select.rs
so that we go through the signature machinery properly; preferably this would just be an SLT test but i can't figure a way to get the non-nullable+nullable mix working, see related issue:
| return plan_err!("Array requires at least one argument"); | ||
| } | ||
|
|
||
| // Widen the element type so inputs that differ only in nested-field |
There was a problem hiding this comment.
the changes to array_array is only because this is what we're testing in the unit test i think? ideally we'd only need to fix this in the coerce function, instead of having it twice (correct me if i'm wrong here 😅)
| // a single common type, both so the declared return type matches the | ||
| // value produced at runtime and so the arguments can actually be | ||
| // combined. See issue #22366. | ||
| if let Some(merged) = merge_nullability(unified.iter().cloned()) { |
There was a problem hiding this comment.
i do wonder if we're better off trying to integrate this fix within try_type_union_resolution_with_struct itself 🤔
Which issue does this PR close?
Rationale for this change
make_arraypanics withArrays with inconsistent types passed to MutableArrayDatawhen combining values whose types are identical except for the nullability of a nested field — for example a struct field that is non-nullable when constructed from a literal but nullable when read from a column.This blocks native execution of plans that construct structs from multiple sources (Delta Lake CDC writes,
UNIONwithin arrays), forcing workarounds that sacrifice performance.Spark, Postgres, and Arrow's own
concatall handle this by widening nullable flags rather than enforcing strict type equality. This PR bringsmake_arrayin line with that precedent: inputs that differ only in nested-field nullability are accepted, and the result widens nullable flags totrueat every nesting level.What changes are included in this PR?
merge_nullability, which OR-s nullable flags at every nesting level (struct fields, list elements, ...) using Arrow'sField::try_merge, and returnsNone(preserving prior behavior) for structurally-incompatible inputs.array_array(the runtime shared by bothmake_arrayand Spark'sarray) computes a merged element type that is a supertype of all arguments and cheaply casts each argument up to it before building the list, soMutableArrayDatano longer sees inconsistent types.coerce_types_innerwidens the per-argument struct types produced bytry_type_union_resolution_with_structto a single common type, so the declared return type matches the value produced at runtime.Are these changes tested?
Yes:
make_array_relaxes_nested_field_nullability) reproduces the original panic at themake_array_innerboundary and asserts it now succeeds.array/make_array.sltformake_arrayover flat and nested structs.Note: the SQL planner already normalizes nested-field nullability for struct construction from SQL literals/columns, so the panic is reached from sources with declared non-null nested schemas (e.g. Delta Lake CDC); the unit test exercises that path directly.
Are there any user-facing changes?
make_array(and Sparkarray) now succeed on inputs that previously panicked. There are no breaking API changes; the result type simply widens nested nullable flags where inputs disagree.