Skip to content

[branch-56] fix: infer UNION output types before analysis (backport #27139) - #27178

Merged
mbutrovich merged 1 commit into
apache:branch-56from
alamb:alamb/backport-27139
Nov 6, 2026
Merged

mbutrovich merged 1 commit into
apache:branch-56from
alamb:alamb/backport-27139

Conversation

@alamb

@alamb alamb commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

## Which issue does this PR close?

- Closes apache#27023.
- Part of apache#14380. UNION inputs still need analyzer casts.

## Rationale for this change

`DataFrame::schema()` previously reported the first UNION input's type
while execution used the common type. UNION construction now infers the
common output type.

## What changes are included in this PR?

- Infer UNION and CASE result types before analysis, with cast insertion
deferred to the analyzer.
- Derive final UNION types from actual inputs with the shared
`coerce_union_type` helper.
- A branch-56 backport remains a maintainer decision because public
inference behavior and one executed Arrow type change.

Synthetic nested CASE planning (`ci-bench`, SQL through physical
planning):

| Depth | Main (µs) | Patch (µs) | Extra (µs) |
| --- | --- | --- | --- |
| 8 | 101.2 | 105.5 | +4.3 |
| 32 | 270.1 | 317.6 | +47.5 |
| 64 | 502.8 | 684.0 | +181.2 |

These three points suggest roughly quadratic extra work. Extrapolating
that trend to depth 1,000 gives about 45 ms of extra planning. The
benchmark did not measure depth 1,000.

## What is the testing strategy for this PR?

Focused core and expression tests (316), 64 doctests, five SLT files,
all-features Clippy, Rustdoc, and formatting pass.

## Are there any user-facing changes?

In this query, `x` changes from `Dictionary(Int32, Utf8)` to `Utf8`;
decoded values remain unchanged. Rustdoc describes UNION and CASE schema
inference.

```sql
SELECT 1 AS y
UNION ALL BY NAME
SELECT arrow_cast('a', 'Dictionary(Int32, Utf8)') AS x;
```

(cherry picked from commit 13bd9d1)
@github-actions github-actions Bot added logical-expr Logical plan and expressions optimizer Optimizer rules core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Nov 6, 2026
@alamb alamb mentioned this pull request Nov 6, 2026
28 of 54 tasks

@mbutrovich mbutrovich left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a clean cherry-pick, approved pending CI. Thanks @alamb!

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.15663% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.26%. Comparing base (7711800) to head (0ef09b7).
⚠️ Report is 2 commits behind head on branch-56.

Files with missing lines Patch % Lines
datafusion/expr/src/logical_plan/plan.rs 82.55% 8 Missing and 7 partials ⚠️
datafusion/expr/src/expr_schema.rs 97.05% 0 Missing and 1 partial ⚠️
datafusion/expr/src/type_coercion/other.rs 92.85% 1 Missing ⚠️
datafusion/optimizer/src/analyzer/type_coercion.rs 96.87% 0 Missing and 1 partial ⚠️
Flag Coverage Δ
all-tests 83.26% <89.15%> (+<0.01%) ⬆️
sqllogictests 60.73% <84.04%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@@            Coverage Diff             @@
##           branch-56   #27178   +/-   ##
==========================================
  Coverage      83.26%   83.26%           
==========================================
  Files           1169     1169           
  Lines         474209   474299   +90     
  Branches      474209   474299   +90     
==========================================
+ Hits          394853   394935   +82     
- Misses         55458    55459    +1     
- Partials       23898    23905    +7     
Files with missing lines Coverage Δ
datafusion/expr/src/logical_plan/builder.rs 86.35% <ø> (ø)
datafusion/expr/src/expr_schema.rs 92.88% <97.05%> (+0.14%) ⬆️
datafusion/expr/src/type_coercion/other.rs 69.56% <92.85%> (+10.19%) ⬆️
datafusion/optimizer/src/analyzer/type_coercion.rs 89.02% <96.87%> (-0.36%) ⬇️
datafusion/expr/src/logical_plan/plan.rs 79.44% <82.55%> (+0.14%) ⬆️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mbutrovich
mbutrovich merged commit 2277aa4 into apache:branch-56 Nov 6, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate logical-expr Logical plan and expressions optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants