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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ trait ColumnResolutionHelper extends Logging with DataTypeErrorsBase {
case u @ UnresolvedHaving(_, agg: Aggregate) =>
agg.resolveChildren(nameParts, conf.resolver)
.orElse(u.resolveChildren(nameParts, conf.resolver))
.map(wrapOuterReference)
.map {
case alias: Alias =>
wrapOuterReference(alias.child)
case other => wrapOuterReference(other)
}
case other =>
other.resolveChildren(nameParts, conf.resolver).map(wrapOuterReference)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,63 @@ Project [((sum(v) + 1) + min(v))#xL]
+- Project [k#x, v#x]
+- SubqueryAlias hav
+- LocalRelation [k#x, v#x]


-- !query
SELECT col1 AS alias
FROM values(1)
GROUP BY col1
HAVING (
SELECT col1 = 1
)
-- !query analysis
Filter cast(scalar-subquery#x [alias#x] as boolean)
: +- Project [(outer(alias#x) = 1) AS (outer(col1) = 1)#x]
: +- OneRowRelation
+- Aggregate [col1#x], [col1#x AS alias#x]
+- LocalRelation [col1#x]


-- !query
SELECT col1 AS alias
FROM values(named_struct('a', 1))
GROUP BY col1
HAVING (
SELECT col1.a = 1
)
-- !query analysis
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]


-- !query
SELECT col1 AS alias
FROM values(array(1))
GROUP BY col1
HAVING (
SELECT col1[0] = 1
)
-- !query analysis
Filter cast(scalar-subquery#x [alias#x] as boolean)
: +- Project [(outer(alias#x)[0] = 1) AS (outer(col1)[0] = 1)#x]
: +- OneRowRelation
+- Aggregate [col1#x], [col1#x AS alias#x]
+- LocalRelation [col1#x]


-- !query
SELECT col1 AS alias
FROM values(map('a', 1))
GROUP BY col1
HAVING (
SELECT col1[0] = 1
)
-- !query analysis
Filter cast(scalar-subquery#x [alias#x] as boolean)
: +- Project [(outer(alias#x)[cast(0 as string)] = 1) AS (outer(col1)[0] = 1)#x]
: +- OneRowRelation
+- Aggregate [col1#x], [col1#x AS alias#x]
+- LocalRelation [col1#x]
29 changes: 29 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/having.sql
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,32 @@ SELECT 1 + SUM(v) FROM hav HAVING SUM(v) + 1;
SELECT SUM(v) + 1 FROM hav HAVING 1 + SUM(v);
SELECT MAX(v) + SUM(v) FROM hav HAVING SUM(v) + MAX(v);
SELECT SUM(v) + 1 + MIN(v) FROM hav HAVING 1 + 1 + 1 + MIN(v) + 1 + SUM(v);

-- HAVING with outer reference to alias in outer project list
SELECT col1 AS alias
FROM values(1)
GROUP BY col1
HAVING (
SELECT col1 = 1
);

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

SELECT col1 AS alias
FROM values(array(1))
GROUP BY col1
HAVING (
SELECT col1[0] = 1
);

SELECT col1 AS alias
FROM values(map('a', 1))
GROUP BY col1
HAVING (
SELECT col1[0] = 1
);
52 changes: 52 additions & 0 deletions sql/core/src/test/resources/sql-tests/results/having.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,55 @@ SELECT SUM(v) + 1 + MIN(v) FROM hav HAVING 1 + 1 + 1 + MIN(v) + 1 + SUM(v)
struct<((sum(v) + 1) + min(v)):bigint>
-- !query output
13


-- !query
SELECT col1 AS alias
FROM values(1)
GROUP BY col1
HAVING (
SELECT col1 = 1
)
-- !query schema
struct<alias:int>
-- !query output
1


-- !query
SELECT col1 AS alias
FROM values(named_struct('a', 1))
GROUP BY col1
HAVING (
SELECT col1.a = 1
)
-- !query schema
struct<alias:struct<a:int>>
-- !query output
{"a":1}


-- !query
SELECT col1 AS alias
FROM values(array(1))
GROUP BY col1
HAVING (
SELECT col1[0] = 1
)
-- !query schema
struct<alias:array<int>>
-- !query output
[1]


-- !query
SELECT col1 AS alias
FROM values(map('a', 1))
GROUP BY col1
HAVING (
SELECT col1[0] = 1
)
-- !query schema
struct<alias:map<string,int>>
-- !query output