Skip to content

[den-55] fix: reconcile Struct fields during type coercion (#27536) - #221

Merged
Jason8Ni merged 2 commits into
den-55from
Jason8Ni/den-55-reconcile-struct-coercion
Nov 24, 2026
Merged

Jason8Ni merged 2 commits into
den-55from
Jason8Ni/den-55-reconcile-struct-coercion

Conversation

@Jason8Ni

@Jason8Ni Jason8Ni commented Nov 23, 2026 •

Copy link
Copy Markdown

Which issue does this PR close?

Backports apache/datafusion#27536 to den-55 (upstream main only; not in a release yet).

Rationale for this change

When struct arguments differ only in child nullability or metadata, coercion kept each argument's own child fields, so one call could see two different struct types:

SELECT [(1, NULL), (1, 2)];        -- panicked in make_array (MutableArrayData)
SELECT coalesce((1, NULL), (1, 2)); -- planning error: multiple data types

Tuples built from table columns hit the same panic because their children carry different PARQUET:field_id metadata.

What changes are included in this PR?

Are these changes tested?

Yes:

  • fix: reconcile Struct fields during type coercion apache/datafusion#27536's tests: core/tests/sql/select.rs, the make_array unit test, and coalesce.slt. Also the ported helper's unit tests and the related sqllogictests (struct, array, case, nvl, map, union).
  • DEN against this branch with its workaround removed: unit tests and SLT mem/distributed/clickhouse pass, with no panics.

Are there any user-facing changes?

Queries that mix tuples differing only in NULL fields or child metadata (array literals, make_array, coalesce) now return results instead of panicking or failing to plan. Conflicting child extension types now fail at planning time. array_append, array_prepend, array_resize, and array_has use a different coercion path and are not fixed here (upstream either).

Related to apache#21982. This is the lower layer of the constructor metadata
work in apache#27502; it fixes shared Struct coercion independently of
constructor field propagation.

Struct arguments can be assigned different child nullability or metadata
even when a function needs one common type. For example, COALESCE with a
non-nullable child in one argument and a nullable child in another can
fail with `Cannot cast nullable struct field 'v' to non-nullable field`.

Use one Struct reconciliation path behind the existing coercion entry
points. It preserves existing field-order choices, merges child
nullability and metadata by field name, ignores untyped NULL
contributors, and recurses into directly nested Structs. Changed child
types lose their extension annotations while retaining ordinary
metadata; conflicting surviving extension names produce a planning
error.

Route the existing array coercion adapter through that result so it
cannot swallow a metadata error and retry storage-only inference. Keep
its function-specific error for ordinary unification failures and its
public signature. Remove both extension metadata keys directly at the
call site, adding `arrow-schema` as a direct workspace dependency of
`datafusion-expr-common` at the existing pinned Arrow version (`60.1.0`,
`default-features = false`).

SQL regressions over registered Struct columns check planned schemas and
executed arrays—types, metadata, values, and root/child NULL
validity—across COALESCE's simplified CASE path, a volatile expression
that retains native COALESCE execution, and `make_array`. They cover
extension preservation at unchanged child types, extension stripping on
child type promotion, ordinary metadata precedence, ignoring untyped
NULL-child metadata while widening child nullability, top-level and
nested field-order rules, and conflicting child extension planning
errors. SQLLogicTests cover pure-SQL Struct COALESCE coercion and
planning errors when comma-containing field names differ. A focused unit
test retains coverage for the public array-coercion adapter's
caller-named error contract.

The lower layer at `f03a6ec95` passed formatting, all-target/all-feature
clippy, 62 coercion tests, 162 nested-function tests, 20 SELECT tests,
`coalesce.slt`/`struct.slt`, and the extended workspace suite (13,453
tests passed, 8 ignored, and 307 SQLLogicTest files completed).

Struct arguments resolve to one compatible type, including child
nullability and metadata. COALESCE accepts the nullable-child case
above. Conflicting surviving child extension names fail during planning
instead of being silently discarded by the array coercion fallback.

Port `datafusion_common::metadata::coerced_fields_metadata` and its unit
tests verbatim from apache#24865, which this change calls but
the fork lacks. Nothing else calls it yet, so COALESCE, NVL2, and CASE
return-field metadata behavior is unchanged.

Adapt the new tests to arrow 59, which has no `arrow::datatypes::Metadata`
builder: `select.rs` aliases it to `HashMap<String, String>` with a small
`with` helper, and the `make_array` unit test builds `HashMap`s directly.
The coercion result calls `FieldMetadata::to_hashmap()` because arrow 59's
`Field::with_metadata` takes a `HashMap`. Keep the existing `plan_err`
import in `make_array.rs`, which the fork still uses.

Conflicts in `select.rs` and `coalesce.slt` came from intermediate
upstream tests (apache#27143 and the volatile COALESCE tests for
apache#25477); only the tests added by this change are kept.

(cherry picked from commit 04b9108)
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T22:07:19.144021Z d8c8f41 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codecov-commenter

codecov-commenter commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.75806% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.28%. Comparing base (ad56f24) to head (8a7db3f).

Files with missing lines Patch % Lines
datafusion/expr-common/src/type_coercion/binary.rs 90.16% 10 Missing and 2 partials ⚠️
datafusion/functions-nested/src/make_array.rs 96.15% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           den-55     #221    +/-   ##
========================================
  Coverage   81.27%   81.28%            
========================================
  Files        1111     1111            
  Lines      390309   390505   +196     
  Branches   390309   390505   +196     
========================================
+ Hits       317233   317429   +196     
+ Misses      54460    54458     -2     
- Partials    18616    18618     +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

cargo audit fails on den-55 for RUSTSEC-2026-0538 (quinn-udp 0.5.14,
receive loop can spin after a truncated datagram) and RUSTSEC-2026-0592
(url 2.5.8, stack overflow in Url::origin() on nested blob: URLs). The
audit workflow only runs on PRs that touch Cargo.toml/Cargo.lock, so the
existing den-55 lockfile had not been re-audited since these advisories
were published.

Lockfile-only patch bumps: quinn-udp 0.5.14 -> 0.5.16, url 2.5.8 ->
2.5.9 (with form_urlencoded 1.2.2 -> 1.2.3). quinn-udp 0.5.16 no longer
pulls windows-sys 0.60, so its unused Windows target crates drop out.
@Jason8Ni
Jason8Ni merged commit 4dc8b1d into den-55 Nov 24, 2026
74 checks passed
@Jason8Ni
Jason8Ni deleted the Jason8Ni/den-55-reconcile-struct-coercion branch November 24, 2026 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants