Fix/support duplicate column names #6543#21126
Open
RafaelHerrero wants to merge 2 commits intoapache:mainfrom
Open
Fix/support duplicate column names #6543#21126RafaelHerrero wants to merge 2 commits intoapache:mainfrom
RafaelHerrero wants to merge 2 commits intoapache:mainfrom
Conversation
Add a deduplication pass in the SQL planner that auto-suffixes
duplicate expression names with :{count} before projection, so
queries like SELECT x, x or TPC-DS Q39 no longer error.
The fix is scoped to the SQL path only. The Rust API
(LogicalPlanBuilder::project) still rejects duplicates via
validate_unique_names, keeping optimizer invariants intact.
alamb
reviewed
Mar 24, 2026
Contributor
alamb
left a comment
There was a problem hiding this comment.
Thank you @RafaelHerrero -- this looks really nice. My only concern is that this may slow down planning (as now it has to create a bunch more strings). I'll run some benchmarks to be sure
Contributor
|
run benchmark sql_planner |
|
🤖 Criterion benchmark running (GKE) | trigger |
|
🤖 Criterion benchmark completed (GKE) | trigger Details
Resource Usagebase (merge-base)
branch
|
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
We're building a SQL engine on top of DataFusion and hit this while running TPC-DS benchmarks — Q39 fails during planning with:
The underlying issue is that
CASTis transparent toschema_name(), so both expressions resolve toinv1.cov. But this also affects simpler cases likeSELECT 1, 1orSELECT x, x FROM t— all of which PostgreSQL, Trino, and SQLite handle without errors.Looking at the issue discussion, @alamb suggested adding auto-aliases in the SQL planner:
That's what this PR does.
TPC-DS Q39 reproduction
The query joins two CTEs that both produce columns named
cov,mean, etc. When the planner applies implicit casts during type coercion, the cast-wrapped and original expressions end up with the same schema name:What changes are included in this PR?
A dedup pass in
SqlToRelthat runs right afterprepare_select_exprs()and beforeself.project(). It detects duplicateschema_name()values and wraps the second (and subsequent) occurrences in anAliaswith a:{N}suffix:The actual code is small (~45 lines of logic across 2 files):
datafusion/sql/src/utils.rs— newdeduplicate_select_expr_names()functiondatafusion/sql/src/select.rs— one call site betweenprepare_select_exprs()andself.project()I intentionally kept this scoped to the SQL planner only:
validate_unique_names("Projections")inbuilder.rsis untouched, so the Rust API (LogicalPlanBuilder::project) still rejects duplicatesvalidate_unique_names("Windows")is unchangedKnown limitation:
SELECT *, x FROM tstill errors whenxoverlaps with*, because wildcard expansion happens after this dedup pass (insideproject_with_validation). Happy to address that in a follow-up if desired.Are these changes tested?
New sqllogictest file (
duplicate_column_alias.slt) with 13 test cases covering:iszero(0.0), iszero(-0.0)(reported in the issue by @jatin510)query errortestUpdated existing tests in
sql_integration.rs(5 tests),aggregate.slt, andunnest.sltthat previously asserted the "Projections require unique" error.Are there any user-facing changes?
Yes, this is a behavior change:
:{N}suffix in the output (e.g.,cov,cov:1)