You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[SPARK-52488][SQL] Strip alias before wrapping outer references under HAVING
### 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.
Closes#51186 from mihailoale-db/stripaliasbeforewrapouterreference.
Authored-by: mihailoale-db <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
0 commit comments