Skip to content

[SPARK-52488] [SQL] Strip alias before wrapping outer references under HAVING #51186

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

Conversation

mihailoale-db
Copy link
Contributor

What changes were proposed in this pull request?

For the following query:

SELECT col1 AS alias
FROM values(named_struct('a', 1))
GROUP BY col1
HAVING (
SELECT col1.a = 1
);

this is the resulting analyzed plan:

Filter cast(scalar-subquery#8847 [alias#8846] as boolean)
:  +- Project [(outer(alias#8846).a = 1) AS (outer(col1).a AS a = 1)#8867]
:     +- OneRowRelation
+- Aggregate [col1#8865], [col1#8865 AS alias#8846]
   +- LocalRelation [col1#8865]

As it can be seen, we have outer(col1).a AS a in the Alias name for col1.a = 1 which is redundant and should be removed. It doesn't affect the output schema so changing the Alias name here is safe.

After the change, plan looks like:

Filter cast(scalar-subquery#x [alias#x] as boolean)
:  +- Project [(outer(alias#x).a = 1) AS (outer(col1).a = 1)#x]
:     +- OneRowRelation
+- Aggregate [col1#x], [col1#x AS alias#x]
   +- LocalRelation [col1#x]

Why are the changes needed?

To keep the compatibility between fixed-point and single-pass implementations.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Tests added in this PR.

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

No.

@github-actions github-actions bot added the SQL label Jun 16, 2025
@cloud-fan
Copy link
Contributor

thanks, merging to master!

@cloud-fan cloud-fan closed this in 3185b9e Jun 18, 2025
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