fix: FilterPushdown incorrectly remaps filters through ProjectionExec with duplicate column names#21247
Open
yashrb24 wants to merge 3 commits intoapache:mainfrom
Open
fix: FilterPushdown incorrectly remaps filters through ProjectionExec with duplicate column names#21247yashrb24 wants to merge 3 commits intoapache:mainfrom
yashrb24 wants to merge 3 commits intoapache:mainfrom
Conversation
… with duplicate column names collect_reverse_alias used column_with_name() which returns the first match for a given name. When a projection has duplicate column names (e.g. two "id" columns from both sides of a join), both entries get the same HashMap key and the second silently overwrites the first. gather_filters_for_pushdown used FilterRemapper::try_remap which calls index_of(), also returning the first match. A filter on id@2 (second occurrence) gets silently rewritten to id@0. Fix 1: Use enumerate index in collect_reverse_alias instead of column_with_name. Projection expressions are positionally aligned with the output schema. Fix 2: Replace FilterRemapper::try_remap with direct validation against the alias map's exact (name, index) keys via collect_columns.
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
ProjectionExec::gather_filters_for_pushdownsilently rewrites filter predicates to the wrong source column when the output schema contains duplicate column names — a structure that arises above joins where both sides share a column name. Two functions use name-only schema lookups (column_with_nameandindex_of) that always return the first match, which is incorrect when duplicate names exist:collect_reverse_alias— HashMap key collision causes the second duplicate to overwrite the first.FilterRemapper::try_remap—index_ofsilently rewrites column indices from non-first duplicates to position 0.This code path is not exercised through normal SQL because the logical optimizer's
PushDownFilterresolves qualified column references and pushes filters below projections before the physical plan is created. However, it affects any direct construction of physical plans.What changes are included in this PR?
collect_reverse_alias: Useenumerate()index instead ofcolumn_with_name(). Projection expressions are positionally aligned with the output schema, soidxis the correct output column index.gather_filters_for_pushdown: ReplaceFilterRemapper::try_remap(which usesindex_of) with direct validation against the alias map's exact(name, index)keys. ThePhysicalColumnRewriteralready does an exact-key lookup, sotry_remapwas both redundant and wrong for this case.Are these changes tested?
Yes. A regression test is added that constructs the exact physical plan structure triggering the bug (FilterExec → ProjectionExec with duplicate column names → HashJoinExec), runs the FilterPushdown optimizer, and verifies the optimized plan returns correct results (3 rows instead of the previous 0).
Are there any user-facing changes?
No API changes. Fixes incorrect query results for physical plans with duplicate column names in projections.