Skip to content

fix: requalify LATERAL filters through derived-table aliases - #26044

Open
canyang25 wants to merge 5 commits into
apache:mainfrom
canyang25:fix/25837-lateral-derived-alias
Open

canyang25 wants to merge 5 commits into
apache:mainfrom
canyang25:fix/25837-lateral-derived-alias

Conversation

@canyang25

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

A LATERAL subquery whose correlated filter goes through a derived-table alias failed to plan. When the filter was pulled up through the SubqueryAlias, it kept the inner table's qualifier. If the alias differed from the table name, the resulting join condition referenced a column the rewritten subquery no longer exposed, so the query errored instead of returning rows.

What changes are included in this PR?

When decorrelation pulls a correlated filter up through a SubqueryAlias, the filter's column references are requalified to the alias, so the join condition matches the subquery's output schema.

What is the testing strategy for this PR?

New cases in lateral_join.slt cover a correlated LATERAL filter through a derived-table alias that differs from the table name. They fail on main and pass with this change. Existing optimizer tests pass, and cargo fmt and cargo clippy are clean.

Are there any user-facing changes?

Queries of this shape now plan and return results instead of erroring. No public API changes.

This change was made with help from Cursor (Grok). I reviewed and tested it.

A correlated filter pulled through a SubqueryAlias kept the inner
table qualifier. When that alias differed from the table name, the
join condition named a column the rewritten subquery no longer had.

Fixes apache#25837

Assisted by Cursor (Grok).
@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Oct 5, 2026
@kumarUjjawal
kumarUjjawal self-requested a review October 5, 2026 06:59

@kumarUjjawal kumarUjjawal 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.

Thank you @canyang25 for working on this

Left a comment please take a look.

Comment thread datafusion/optimizer/src/decorrelate.rs Outdated
if let Expr::Column(col) = &expr
&& cols.contains(col)
{
let new_col = Column::new(Some(alias.clone()), col.name.clone());

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.

Keeping col.name is incorrect when pull-up adds a correlation column whose name matches another projected column. SubqueryAlias::try_new disambiguates duplicate field names using suffixes such as id:1, so changing only the qualifier can target a different column.

Use the rebuilt alias’s actual input-to-output column mapping for both the filters and propagated correlation columns, and add a collision regression case.

Pull-up can add a correlation column whose name matches another
projected column. SubqueryAlias suffixes the duplicate (id:1), so
requalifying only the qualifier targets the wrong column. Map both
the join filters and the propagated correlation columns through the
rebuilt alias's positional output names.

Assisted by Cursor (Grok).
The lateral join and EXISTS/IN rewrites wrapped the pulled-up subquery
with SubqueryAlias::try_new, then requalified filters by keeping the
inner column name. When try_new suffixes a duplicate (id, id:1, id:2),
that name is an earlier column. Both rewrites now use the same
positional input-to-output mapping as alias_output_column, and a
correlated column missing from the pre-alias schema is an error.

Assisted by Cursor (Grok).
requalify_filter mapped both the pulled-up correlation predicate and the
user's ON clause through the pre-alias schema. ON is already bound to the
alias output. When an inner column shares that qualifier, the map sends
ON s.id to a later id:1. Pull-up only appends columns, and those are the
only names unique_field_aliases suffixes, so the planner's ON names stay
valid. EXISTS, IN, and scalar decorrelation only positionally rewrite
correlation columns, not a user predicate that already uses output names.

Assisted by Cursor (Grok).
@canyang25

Copy link
Copy Markdown
Author

Thanks @kumarUjjawal, good catch. The fix now uses the alias's real input-to-output column mapping, so a renamed duplicate like id:1 is handled correctly. This covers LATERAL, EXISTS/IN, and scalar subqueries. The user's ON clause is left unchanged. I added collision tests to lateral_join.slt and subquery.slt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LATERAL subquery fails with a schema error when a derived table alias differs from the table name

2 participants