Repository navigation
Conversation
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).
kumarUjjawal
left a comment
There was a problem hiding this comment.
Thank you @canyang25 for working on this
Left a comment please take a look.
| if let Expr::Column(col) = &expr | ||
| && cols.contains(col) | ||
| { | ||
| let new_col = Column::new(Some(alias.clone()), col.name.clone()); |
There was a problem hiding this comment.
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).
|
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. |
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.sltcover a correlated LATERAL filter through a derived-table alias that differs from the table name. They fail onmainand pass with this change. Existing optimizer tests pass, andcargo fmtandcargo clippyare 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.