Skip to content

Cherry-pick apache/datafusion#26790 - #225

Merged
alexanderbianchi merged 1 commit into
branch-55from
alex.bianchi/cherry-pick/apache-pr-26790-20261124
Nov 24, 2026
Merged

alexanderbianchi merged 1 commit into
branch-55from
alex.bianchi/cherry-pick/apache-pr-26790-20261124

Conversation

@alexanderbianchi

Copy link
Copy Markdown

Which issue does this PR close?

Backport of apache#26790 to branch-55.

Rationale for this change

When UNION inputs differ and fields are non-nullable, constructing the output schema can repeat the same field metadata merge for many candidate inputs.

What changes are included in this PR?

Select the first nullable field (or first field if none are nullable) before merging metadata once per output column. Preserve metadata precedence; use a HashMap for the Arrow 59 test fixture. Stacked on #224 because it extends the preceding change's union_schema implementation and benchmark.

Are these changes tested?

All 22 datafusion-physical-plan union::tests passed on this branch, including the metadata/nullability cases. The benchmark target compiled. Workspace formatting passed.

Are there any user-facing changes?

Planning performance only; no change to output schemas or rows. No local timing claim.

@codecov-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.18605% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.22%. Comparing base (b516542) to head (e46e643).
⚠️ Report is 2 commits behind head on branch-55.

Files with missing lines Patch % Lines
datafusion/physical-plan/src/union.rs 94.18% 0 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           branch-55     #225   +/-   ##
==========================================
  Coverage      81.22%   81.22%           
==========================================
  Files           1112     1112           
  Lines         390954   391023   +69     
  Branches      390954   391023   +69     
==========================================
+ Hits          317539   317604   +65     
+ Misses         54446    54445    -1     
- Partials       18969    18974    +5     

☔ 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.

Base automatically changed from alex.bianchi/cherry-pick/apache-pr-24389-20261124 to branch-55 November 24, 2026 14:28
Related to apache#17261. Complements the fast path for equal schemas added in

Benchmark coverage is reviewed in apache#26791. This PR is stacked on its
benchmark-only commit `cfc6594b5`; the [optimization-only
diff](apache/datafusion@cfc6594...c5f9d3e)
contains the schema change and regression test.

Constructing a union with unequal input schemas can take quadratic time
in the number of children. The fallback merges every input's field
metadata before checking whether that candidate field is nullable. With
non-nullable fields, the same full merge is repeated for every child.

Select the first nullable field (or the first field when none are
nullable) before merging metadata. This makes the fallback linear in
child count for a fixed number of fields and metadata entries per input.
The shared helper also serves `InterleaveExec`.

- Merge metadata once per output field, preserving the selected field,
leftmost name, and existing metadata conflict precedence.
- Add a test that constructs and executes unions with non-nullable
inputs, first/middle/last nullable inputs, conflicting and distinct
metadata keys, and checks output rows.

Benchmarks use upstream `c18e5d974` plus the benchmark extension, Rust
1.99.1, `release-nonlto`, and an Apple M5 Pro. The table averages two
interleaved runs per binary, with 20 Criterion samples per run. Schemas
have 10 fields and two metadata entries per field.

| Non-nullable schema case | Children | Upstream | Patched | Speedup |
| --- | ---: | ---: | ---: | ---: |
| Last child's metadata differs | 10 | 141 µs | 62 µs | 2.3× |
| Last child's metadata differs | 100 | 9.24 ms | 0.66 ms | 14.1× |
| Last child's metadata differs | 1,000 | 877 ms | 7.40 ms | 118.6× |
| Field names differ | 10 | 140 µs | 60 µs | 2.3× |
| Field names differ | 100 | 9.38 ms | 0.66 ms | 14.3× |
| Field names differ | 1,000 | 866 ms | 7.36 ms | 117.6× |

The controls with nullable fields or equal schemas at 4,000 children
varied from -0.8% to +6.3% on this shared machine. Background builds
affected some measurements, so the larger SQL cases with sorted unions
were also checked in a tighter alternating sequence, with four runs per
binary:

| SQL planning case | Upstream | Patched | Change |
| --- | ---: | ---: | ---: |
| 50 columns, Int64 | 38.61 ms | 38.40 ms | -0.5% |
| 50 columns, UInt64 | 139.48 ms | 139.63 ms | +0.1% |

These results establish the constructor improvement independently of
workloads. The original extended logical-planning benchmark only calls
logical optimization and does not exercise this physical schema helper.

To run the benchmark cases from apache#26791:

```sh
cargo bench --profile release-nonlto -p datafusion-physical-plan --bench union_schema -- union_exec_try_new_non_nullable
```

Faster construction of unions and interleaves with unequal schemas and
non-nullable fields. Schema behavior and public APIs are preserved.

AI assistance: OpenAI Codex.

(cherry picked from commit 7ca68cf)

Ported to branch-55 (arrow 59): arrow::datatypes::Metadata does not exist there, so the
new test builds field metadata as a HashMap<String, String>, and the unused Field import
is dropped.

(cherry picked from commit 0c03aae)
(cherry picked from commit 83e6630)
@alexanderbianchi
alexanderbianchi force-pushed the alex.bianchi/cherry-pick/apache-pr-26790-20261124 branch from 7dce033 to e46e643 Compare November 24, 2026 14:28
@alexanderbianchi
alexanderbianchi merged commit a348975 into branch-55 Nov 24, 2026
34 checks passed
@alexanderbianchi
alexanderbianchi deleted the alex.bianchi/cherry-pick/apache-pr-26790-20261124 branch November 24, 2026 14:28
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