Skip to content

Commit 385f6d4

Browse files
committed
[SPARK-13698] Fix Analysis Exceptions when Using Backticks in Generate
1 parent f19228e commit 385f6d4

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/CatalystQl.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,10 +999,16 @@ https://cwiki.apache.org/confluence/display/Hive/Enhanced+Aggregation%2C+Cube%2C
999999
}
10001000

10011001
val attributes = clauses.collect {
1002-
case Token(a, Nil) => UnresolvedAttribute(a.toLowerCase)
1002+
case Token(a, Nil) => UnresolvedAttribute(cleanIdentifier(a.toLowerCase))
10031003
}
10041004

1005-
Generate(generator, join = true, outer = outer, Some(alias.toLowerCase), attributes, child)
1005+
Generate(
1006+
generator,
1007+
join = true,
1008+
outer = outer,
1009+
Some(cleanIdentifier(alias.toLowerCase)),
1010+
attributes,
1011+
child)
10061012
}
10071013

10081014
protected def nodeToGenerator(node: ASTNode): Generator = noParseRule("Generator", node)

sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveQlSuite.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,13 @@ class HiveQlSuite extends SparkFunSuite with BeforeAndAfterAll {
208208
|USING 'cat' AS (`thing1` int, `thing2` string) FROM `default`.`parquet_t1`) AS t
209209
""".stripMargin)
210210
}
211+
212+
test("use backticks in output of Generator") {
213+
val plan = parser.parsePlan(
214+
"""SELECT `gentab2`.`gencol2`
215+
|FROM `default`.`src`
216+
|LATERAL VIEW explode(array(array(1, 2, 3))) `gentab1` AS `gencol1`
217+
|LATERAL VIEW explode(`gentab1`.`gencol1`) `gentab2` AS `gencol2`
218+
""".stripMargin)
219+
}
211220
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
254254
checkAnswer(
255255
sql("SELECT ints FROM nestedArray LATERAL VIEW explode(a.b) a AS ints"),
256256
Row(1) :: Row(2) :: Row(3) :: Nil)
257+
258+
checkAnswer(
259+
sql("SELECT `ints` FROM nestedArray LATERAL VIEW explode(a.b) `a` AS `ints`"),
260+
Row(1) :: Row(2) :: Row(3) :: Nil)
261+
262+
checkAnswer(
263+
sql("SELECT `a`.`ints` FROM nestedArray LATERAL VIEW explode(a.b) `a` AS `ints`"),
264+
Row(1) :: Row(2) :: Row(3) :: Nil)
257265
}
258266

259267
test("SPARK-4512 Fix attribute reference resolution error when using SORT BY") {

0 commit comments

Comments
 (0)