Skip to content

fix: Keep same-named correlated columns apart when decorrelating subqueries - #26236

Draft
thobe wants to merge 1 commit into
apache:mainfrom
thobe:exists-correlated-column-collision
Draft

thobe wants to merge 1 commit into
apache:mainfrom
thobe:exists-correlated-column-collision

Conversation

@thobe

@thobe thobe commented Oct 12, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

See issue #26234.

What changes are included in this PR?

When a correlated subquery refers to the outer query from two of its relations through columns with the same name, SubqueryAlias gives those columns unique aliases (k, k:1), but replace_qualified_name rewrote the pulled-up join filter by column name only, so both columns referred to the first one and the query returned wrong results. This affected EXISTS, NOT EXISTS, IN and scalar subqueries.

Map each correlated column to the name it has in the alias's schema, found by its position in the decorrelated subquery's schema. Columns the schema only has under a derived table's alias keep the name-based mapping.

What is the testing strategy for this PR?

Introducing new tests for the affected subqueries.

Are there any user-facing changes?

Correcting the behaviour of queries that previously produced the wrong results.

…ueries

When a correlated subquery refers to the outer query from two of its
relations through columns with the same name, `SubqueryAlias` gives those
columns unique aliases (`k`, `k:1`), but `replace_qualified_name` rewrote the
pulled-up join filter by column name only, so both columns referred to the
first one and the query returned wrong results. This affected EXISTS, NOT
EXISTS, IN and scalar subqueries.

Map each correlated column to the name it has in the alias's schema, found
by its position in the decorrelated subquery's schema. Columns the schema
only has under a derived table's alias keep the name-based mapping.
@github-actions

Copy link
Copy Markdown

Marked as draft after 14 days without activity: merge conflicts.

Please address these items and mark Ready for review when ready.

@alamb alamb 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 for your contribution; Sorry I don't think this is the right approach

As I mentioned elsewhere (sorry for the repeated content) I don't think we should be trying to patch these subquery cases to make them work; Instead I think we should make the query error otherwise i worry we just open ourselves up to more wrong results bugs. To make it work and have confidence it will be correct, I think we need a more general approach than patches. See here for details

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.

Wrong results: Correlated subquery confuses same-named columns of two of its relations

2 participants