-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20841][SQL] Support table column aliases in FROM clause #18079
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -472,7 +472,7 @@ identifierComment | |
| ; | ||
|
|
||
| relationPrimary | ||
| : tableIdentifier sample? (AS? strictIdentifier)? #tableName | ||
| : tableIdentifier sample? tableAlias #tableName | ||
| | '(' queryNoWith ')' sample? (AS? strictIdentifier) #aliasedQuery | ||
| | '(' relation ')' sample? (AS? strictIdentifier)? #aliasedRelation | ||
| | inlineTable #inlineTableDefault2 | ||
|
|
@@ -711,7 +711,7 @@ nonReserved | |
| | ADD | ||
| | OVER | PARTITION | RANGE | ROWS | PRECEDING | FOLLOWING | CURRENT | ROW | LAST | FIRST | AFTER | ||
| | MAP | ARRAY | STRUCT | ||
| | LATERAL | WINDOW | REDUCE | TRANSFORM | USING | SERDE | SERDEPROPERTIES | RECORDREADER | ||
|
||
| | LATERAL | WINDOW | REDUCE | TRANSFORM | SERDE | SERDEPROPERTIES | RECORDREADER | ||
| | DELIMITED | FIELDS | TERMINATED | COLLECTION | ITEMS | KEYS | ESCAPED | LINES | SEPARATED | ||
| | EXTENDED | REFRESH | CLEAR | CACHE | UNCACHE | LAZY | GLOBAL | TEMPORARY | OPTIONS | ||
| | GROUPING | CUBE | ROLLUP | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -131,8 +131,9 @@ object ResolveTableValuedFunctions extends Rule[LogicalPlan] { | |
| val outputAttrs = resolvedFunc.output | ||
| // Checks if the number of the aliases is equal to expected one | ||
| if (u.outputNames.size != outputAttrs.size) { | ||
| u.failAnalysis(s"expected ${outputAttrs.size} columns but " + | ||
| s"found ${u.outputNames.size} columns") | ||
| u.failAnalysis(s"Number of given aliases does not match number of output columns. " + | ||
| s"Function name: ${u.functionName}; number of aliases: " + | ||
| s"${u.outputNames.size}; number of output columns: ${outputAttrs.size}.") | ||
| } | ||
|
||
| val aliases = outputAttrs.zip(u.outputNames).map { | ||
| case (attr, name) => Alias(attr, name)() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,7 @@ class TableIdentifierParserSuite extends SparkFunSuite { | |
| "insert", "int", "into", "is", "lateral", "like", "local", "none", "null", | ||
| "of", "order", "out", "outer", "partition", "percent", "procedure", "range", "reads", "revoke", | ||
| "rollup", "row", "rows", "set", "smallint", "table", "timestamp", "to", "trigger", | ||
| "true", "truncate", "update", "user", "using", "values", "with", "regexp", "rlike", | ||
|
||
| "true", "truncate", "update", "user", "values", "with", "regexp", "rlike", | ||
| "bigint", "binary", "boolean", "current_date", "current_timestamp", "date", "double", "float", | ||
| "int", "smallint", "timestamp", "at") | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| -- Test data. | ||
| CREATE OR REPLACE TEMPORARY VIEW testData AS SELECT * FROM VALUES (1, 1), (1, 2), (2, 1) AS testData(a, b); | ||
|
|
||
| -- Table column aliases in FROM clause | ||
| SELECT * FROM testData AS t(col1, col2) WHERE col1 = 1; | ||
|
|
||
| SELECT * FROM testData AS t(col1, col2) WHERE col1 = 2; | ||
|
|
||
| SELECT col1 AS k, SUM(col2) FROM testData AS t(col1, col2) GROUP BY k; | ||
|
|
||
| -- Aliasing the wrong number of columns in the FROM clause | ||
| SELECT * FROM testData AS t(col1, col2, col3); | ||
|
|
||
| SELECT * FROM testData AS t(col1); | ||
|
|
||
| -- Check alias duplication | ||
| SELECT a AS col1, b AS col2 FROM testData AS t(c, d); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| -- Automatically generated by SQLQueryTestSuite | ||
| -- Number of queries: 7 | ||
|
|
||
|
|
||
| -- !query 0 | ||
| CREATE OR REPLACE TEMPORARY VIEW testData AS SELECT * FROM VALUES (1, 1), (1, 2), (2, 1) AS testData(a, b) | ||
| -- !query 0 schema | ||
| struct<> | ||
| -- !query 0 output | ||
|
|
||
|
|
||
|
|
||
| -- !query 1 | ||
| SELECT * FROM testData AS t(col1, col2) WHERE col1 = 1 | ||
| -- !query 1 schema | ||
| struct<col1:int,col2:int> | ||
| -- !query 1 output | ||
| 1 1 | ||
| 1 2 | ||
|
|
||
|
|
||
| -- !query 2 | ||
| SELECT * FROM testData AS t(col1, col2) WHERE col1 = 2 | ||
| -- !query 2 schema | ||
| struct<col1:int,col2:int> | ||
| -- !query 2 output | ||
| 2 1 | ||
|
|
||
|
|
||
| -- !query 3 | ||
| SELECT col1 AS k, SUM(col2) FROM testData AS t(col1, col2) GROUP BY k | ||
| -- !query 3 schema | ||
| struct<k:int,sum(col2):bigint> | ||
| -- !query 3 output | ||
| 1 3 | ||
| 2 1 | ||
|
|
||
|
|
||
| -- !query 4 | ||
| SELECT * FROM testData AS t(col1, col2, col3) | ||
| -- !query 4 schema | ||
| struct<> | ||
| -- !query 4 output | ||
| org.apache.spark.sql.AnalysisException | ||
| Number of column aliases does not match number of columns. Table name: testData; number of column aliases: 3; number of columns: 2.; line 1 pos 14 | ||
|
|
||
|
|
||
| -- !query 5 | ||
| SELECT * FROM testData AS t(col1) | ||
| -- !query 5 schema | ||
| struct<> | ||
| -- !query 5 output | ||
| org.apache.spark.sql.AnalysisException | ||
| Number of column aliases does not match number of columns. Table name: testData; number of column aliases: 1; number of columns: 2.; line 1 pos 14 | ||
|
|
||
|
|
||
| -- !query 6 | ||
| SELECT a AS col1, b AS col2 FROM testData AS t(c, d) | ||
| -- !query 6 schema | ||
| struct<> | ||
| -- !query 6 output | ||
| org.apache.spark.sql.AnalysisException | ||
| cannot resolve '`a`' given input columns: [c, d]; line 1 pos 7 |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: keep it unchanged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this
USINGentry caused a failure inPlanParserSuite;https://github.com/apache/spark/blob/master/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala#L336