Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-46640][FOLLOW-UP] Consider the whole expression tree when excluding subquery references #50570

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nikhilsheoran-db
Copy link
Contributor

@nikhilsheoran-db nikhilsheoran-db commented Apr 11, 2025

What changes were proposed in this pull request?

  • Context: In RemoveRedundantAliases, we exclude outerAttrs referenced by subquery expressions when trying to identify aliases that can be removed. Failure to do so could lead to scenarios where certain part of the plans (for example: the join conditions in exists subquery) would end up with duplicated expression ID(s). A fix for this was merged here in the PR here - [SPARK-46640][SQL] Fix RemoveRedundantAlias by excluding subquery attributes #44645.

  • The fix there only accounted for the plan.expressions to be a SubqueryExpression.

  • We can have scenarios where the SubqueryExpression is wrapped in other expressions -- for example, Alias < CaseWhen < (SubqueryExpression).

  • To correctly account for these, need to traverse the whole expression tree and collect outer references from these.

  • Consider plans of the form:

Project [CASE WHEN exists#2 [a#1 && (a#1 = a#0)] THEN 1 ELSE 2 END AS result#3]
:  +- LocalRelation <empty>, [a#0]
+- Project [a#0 AS a#1]
  +- LocalRelation <empty>, [a#0]
  • The current rule would have rewritten this as follows. Note the join condition with conflicting expression ID(s) a#0 = a#0
Project [CASE WHEN exists#2 [a#0 && (a#0 = a#0)] THEN 1 ELSE 2 END AS result#3]
:  +- LocalRelation <empty>, [a#0]
+- LocalRelation <empty>, [a#0]

Why are the changes needed?

  • Queries that would have run successfully with this rule disabled would fail.

Does this PR introduce any user-facing change?

No

How was this patch tested?

  • Added unit tests

Was this patch authored or co-authored using generative AI tooling?

No

@github-actions github-actions bot added the SQL label Apr 11, 2025
@nikhilsheoran-db
Copy link
Contributor Author

cc: @agubichev @cloud-fan to take a look.

Copy link
Contributor

@cloud-fan cloud-fan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

nikhilsheoran-db and others added 2 commits April 13, 2025 22:06
…mizer/Optimizer.scala

Co-authored-by: Wenchen Fan <cloud0fan@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants