Skip to content

Commit f4e17cd

Browse files
ravipesalamarmbrus
authored andcommitted
[SPARK-3834][SQL] Backticks not correctly handled in subquery aliases
The queries like SELECT a.key FROM (SELECT key FROM src) \`a\` does not work as backticks in subquery aliases are not handled properly. This PR fixes that. Author : ravipesala ravindra.pesalahuawei.com Author: ravipesala <[email protected]> Closes apache#2737 from ravipesala/SPARK-3834 and squashes the following commits: 0e0ab98 [ravipesala] Fixing issue in backtick handling for subquery aliases Conflicts: sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala
1 parent 64945f8 commit f4e17cd

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ private[hive] object HiveQl {
658658
def nodeToRelation(node: Node): LogicalPlan = node match {
659659
case Token("TOK_SUBQUERY",
660660
query :: Token(alias, Nil) :: Nil) =>
661-
Subquery(alias, nodeToPlan(query))
661+
Subquery(cleanIdentifier(alias), nodeToPlan(query))
662662

663663
case Token(laterViewToken(isOuter), selectClause :: relationClause :: Nil) =>
664664
val Token("TOK_SELECT",

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,30 @@ class SQLQuerySuite extends QueryTest {
4747
GROUP BY key, value
4848
ORDER BY value) a""").collect().toSeq)
4949
}
50+
51+
test("double nested data") {
52+
sparkContext.parallelize(Nested1(Nested2(Nested3(1))) :: Nil).registerTempTable("nested")
53+
checkAnswer(
54+
sql("SELECT f1.f2.f3 FROM nested"),
55+
1)
56+
}
57+
58+
test("test CTAS") {
59+
checkAnswer(sql("CREATE TABLE test_ctas_123 AS SELECT key, value FROM src"), Seq.empty[Row])
60+
checkAnswer(
61+
sql("SELECT key, value FROM test_ctas_123 ORDER BY key"),
62+
sql("SELECT key, value FROM src ORDER BY key").collect().toSeq)
63+
}
64+
65+
test("SPARK-3708 Backticks aren't handled correctly is aliases") {
66+
checkAnswer(
67+
sql("SELECT k FROM (SELECT `key` AS `k` FROM src) a"),
68+
sql("SELECT `key` FROM src").collect().toSeq)
69+
}
70+
71+
test("SPARK-3834 Backticks not correctly handled in subquery aliases") {
72+
checkAnswer(
73+
sql("SELECT a.key FROM (SELECT key FROM src) `a`"),
74+
sql("SELECT `key` FROM src").collect().toSeq)
75+
}
5076
}

0 commit comments

Comments
 (0)