-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-51257][SQL][TESTS] Add order-by-alias.sql
### What changes were proposed in this pull request? Added a new test, `order-by-alias.sql`, which covers the cases where `ORDER BY` is done using an alias. ### Why are the changes needed? To improve the testing coverage of `ORDER BY` feature. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Added tests. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #50007 from mihailoale-db/orderbyalias. Authored-by: mihailoale-db <mihailo.aleksic@databricks.com> Signed-off-by: Max Gekk <max.gekk@gmail.com>
- Loading branch information
1 parent
26febf7
commit cecafac
Showing
3 changed files
with
482 additions
and
0 deletions.
There are no files selected for viewing
214 changes: 214 additions & 0 deletions
214
sql/core/src/test/resources/sql-tests/analyzer-results/order-by-alias.sql.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
-- Automatically generated by SQLQueryTestSuite | ||
-- !query | ||
CREATE OR REPLACE TEMPORARY VIEW testData AS SELECT * FROM VALUES | ||
(1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2), (null, 1), (3, null), (null, null) | ||
AS testData(a, b) | ||
-- !query analysis | ||
CreateViewCommand `testData`, SELECT * FROM VALUES | ||
(1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2), (null, 1), (3, null), (null, null) | ||
AS testData(a, b), false, true, LocalTempView, UNSUPPORTED, true | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT a FROM testData ORDER BY A | ||
-- !query analysis | ||
Sort [A#x ASC NULLS FIRST], true | ||
+- Project [a#x] | ||
+- SubqueryAlias testdata | ||
+- View (`testData`, [a#x, b#x]) | ||
+- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT a as alias FROM testData ORDER BY ALIAS | ||
-- !query analysis | ||
Sort [ALIAS#x ASC NULLS FIRST], true | ||
+- Project [a#x AS alias#x] | ||
+- SubqueryAlias testdata | ||
+- View (`testData`, [a#x, b#x]) | ||
+- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT a AS k FROM testData ORDER BY 'k' | ||
-- !query analysis | ||
Sort [k ASC NULLS FIRST], true | ||
+- Project [a#x AS k#x] | ||
+- SubqueryAlias testdata | ||
+- View (`testData`, [a#x, b#x]) | ||
+- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT 1 AS k FROM testData ORDER BY 'k' | ||
-- !query analysis | ||
Sort [k ASC NULLS FIRST], true | ||
+- Project [1 AS k#x] | ||
+- SubqueryAlias testdata | ||
+- View (`testData`, [a#x, b#x]) | ||
+- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT concat_ws(' ', a, b) FROM testData ORDER BY `concat_ws( , a, b)` | ||
-- !query analysis | ||
Sort [concat_ws( , a, b)#x ASC NULLS FIRST], true | ||
+- Project [concat_ws( , cast(a#x as string), cast(b#x as string)) AS concat_ws( , a, b)#x] | ||
+- SubqueryAlias testdata | ||
+- View (`testData`, [a#x, b#x]) | ||
+- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT 1 AS a FROM testData ORDER BY a | ||
-- !query analysis | ||
Sort [a#x ASC NULLS FIRST], true | ||
+- Project [1 AS a#x] | ||
+- SubqueryAlias testdata | ||
+- View (`testData`, [a#x, b#x]) | ||
+- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT 1 AS a FROM testData ORDER BY `a` | ||
-- !query analysis | ||
Sort [a#x ASC NULLS FIRST], true | ||
+- Project [1 AS a#x] | ||
+- SubqueryAlias testdata | ||
+- View (`testData`, [a#x, b#x]) | ||
+- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT 1 ORDER BY `1` | ||
-- !query analysis | ||
Sort [1#x ASC NULLS FIRST], true | ||
+- Project [1 AS 1#x] | ||
+- OneRowRelation | ||
|
||
|
||
-- !query | ||
SELECT a, b FROM testData ORDER BY a, (SELECT b FROM testData LIMIT 1) | ||
-- !query analysis | ||
Sort [a#x ASC NULLS FIRST, scalar-subquery#x [] ASC NULLS FIRST], true | ||
: +- GlobalLimit 1 | ||
: +- LocalLimit 1 | ||
: +- Project [b#x] | ||
: +- SubqueryAlias testdata | ||
: +- View (`testData`, [a#x, b#x]) | ||
: +- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
: +- Project [a#x, b#x] | ||
: +- SubqueryAlias testData | ||
: +- LocalRelation [a#x, b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testdata | ||
+- View (`testData`, [a#x, b#x]) | ||
+- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT a FROM testData ORDER BY (SELECT b FROM testData) | ||
-- !query analysis | ||
Sort [scalar-subquery#x [] ASC NULLS FIRST], true | ||
: +- Project [b#x] | ||
: +- SubqueryAlias testdata | ||
: +- View (`testData`, [a#x, b#x]) | ||
: +- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
: +- Project [a#x, b#x] | ||
: +- SubqueryAlias testData | ||
: +- LocalRelation [a#x, b#x] | ||
+- Project [a#x] | ||
+- SubqueryAlias testdata | ||
+- View (`testData`, [a#x, b#x]) | ||
+- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x] | ||
+- Project [a#x, b#x] | ||
+- SubqueryAlias testData | ||
+- LocalRelation [a#x, b#x] | ||
|
||
|
||
-- !query | ||
SELECT a, b FROM testData ORDER BY a, b IN (SELECT a FROM testData) | ||
-- !query analysis | ||
org.apache.spark.sql.catalyst.ExtendedAnalysisException | ||
{ | ||
"errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.UNSUPPORTED_IN_EXISTS_SUBQUERY", | ||
"sqlState" : "0A000", | ||
"messageParameters" : { | ||
"treeNode" : "Sort [a#x ASC NULLS FIRST, b#x IN (list#x []) ASC NULLS FIRST], true\n: +- Project [a#x]\n: +- SubqueryAlias testdata\n: +- View (`testData`, [a#x, b#x])\n: +- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x]\n: +- Project [a#x, b#x]\n: +- SubqueryAlias testData\n: +- LocalRelation [a#x, b#x]\n+- Project [a#x, b#x]\n +- SubqueryAlias testdata\n +- View (`testData`, [a#x, b#x])\n +- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x]\n +- Project [a#x, b#x]\n +- SubqueryAlias testData\n +- LocalRelation [a#x, b#x]\n" | ||
}, | ||
"queryContext" : [ { | ||
"objectType" : "", | ||
"objectName" : "", | ||
"startIndex" : 41, | ||
"stopIndex" : 67, | ||
"fragment" : "IN (SELECT a FROM testData)" | ||
} ] | ||
} | ||
|
||
|
||
-- !query | ||
SELECT a, b FROM testData ORDER BY a, EXISTS(SELECT b FROM testData) | ||
-- !query analysis | ||
org.apache.spark.sql.catalyst.ExtendedAnalysisException | ||
{ | ||
"errorClass" : "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.UNSUPPORTED_IN_EXISTS_SUBQUERY", | ||
"sqlState" : "0A000", | ||
"messageParameters" : { | ||
"treeNode" : "Sort [a#x ASC NULLS FIRST, exists#x [] ASC NULLS FIRST], true\n: +- Project [b#x]\n: +- SubqueryAlias testdata\n: +- View (`testData`, [a#x, b#x])\n: +- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x]\n: +- Project [a#x, b#x]\n: +- SubqueryAlias testData\n: +- LocalRelation [a#x, b#x]\n+- Project [a#x, b#x]\n +- SubqueryAlias testdata\n +- View (`testData`, [a#x, b#x])\n +- Project [cast(a#x as int) AS a#x, cast(b#x as int) AS b#x]\n +- Project [a#x, b#x]\n +- SubqueryAlias testData\n +- LocalRelation [a#x, b#x]\n" | ||
}, | ||
"queryContext" : [ { | ||
"objectType" : "", | ||
"objectName" : "", | ||
"startIndex" : 39, | ||
"stopIndex" : 68, | ||
"fragment" : "EXISTS(SELECT b FROM testData)" | ||
} ] | ||
} | ||
|
||
|
||
-- !query | ||
SELECT a AS k, c FROM testData ORDER BY k | ||
-- !query analysis | ||
org.apache.spark.sql.catalyst.ExtendedAnalysisException | ||
{ | ||
"errorClass" : "UNRESOLVED_COLUMN.WITH_SUGGESTION", | ||
"sqlState" : "42703", | ||
"messageParameters" : { | ||
"objectName" : "`c`", | ||
"proposal" : "`a`, `b`" | ||
}, | ||
"queryContext" : [ { | ||
"objectType" : "", | ||
"objectName" : "", | ||
"startIndex" : 16, | ||
"stopIndex" : 16, | ||
"fragment" : "c" | ||
} ] | ||
} |
37 changes: 37 additions & 0 deletions
37
sql/core/src/test/resources/sql-tests/inputs/order-by-alias.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- Test data. | ||
CREATE OR REPLACE TEMPORARY VIEW testData AS SELECT * FROM VALUES | ||
(1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2), (null, 1), (3, null), (null, null) | ||
AS testData(a, b); | ||
|
||
-- ORDER BY alias should work with case insensitive names | ||
SELECT a FROM testData ORDER BY A; | ||
|
||
-- Aliases in SELECT could be used in ORDER BY | ||
SELECT a as alias FROM testData ORDER BY ALIAS; | ||
|
||
-- ORDER BY literal | ||
SELECT a AS k FROM testData ORDER BY 'k'; | ||
SELECT 1 AS k FROM testData ORDER BY 'k'; | ||
|
||
-- ORDER BY alias with the function name | ||
SELECT concat_ws(' ', a, b) FROM testData ORDER BY `concat_ws( , a, b)`; | ||
|
||
-- ORDER BY column with name same as an alias used in the project list | ||
SELECT 1 AS a FROM testData ORDER BY a; | ||
SELECT 1 AS a FROM testData ORDER BY `a`; | ||
|
||
-- ORDER BY implicit alias | ||
SELECT 1 ORDER BY `1`; | ||
|
||
-- ORDER BY with expression subqueries | ||
SELECT a, b FROM testData ORDER BY a, (SELECT b FROM testData LIMIT 1); | ||
|
||
-- ORDER BY more than one row | ||
SELECT a FROM testData ORDER BY (SELECT b FROM testData); | ||
|
||
-- Unsupported expressions in ORDER BY | ||
SELECT a, b FROM testData ORDER BY a, b IN (SELECT a FROM testData); | ||
SELECT a, b FROM testData ORDER BY a, EXISTS(SELECT b FROM testData); | ||
|
||
-- ORDER BY alias with invalid col in SELECT list | ||
SELECT a AS k, c FROM testData ORDER BY k; |
Oops, something went wrong.