Skip to content

Commit 9e01fe2

Browse files
gatorsmilehvanhovell
authored andcommitted
[SPARK-13535][SQL] Fix Analysis Exceptions when Using Backticks in Transform Clause
#### What changes were proposed in this pull request? ```SQL FROM (FROM test SELECT TRANSFORM(key, value) USING 'cat' AS (`thing1` int, thing2 string)) t SELECT thing1 + 1 ``` This query returns an analysis error, like: ``` Failed to analyze query: org.apache.spark.sql.AnalysisException: cannot resolve '`thing1`' given input columns: [`thing1`, thing2]; line 3 pos 7 'Project [unresolvedalias(('thing1 + 1), None)] +- SubqueryAlias t +- ScriptTransformation [key#2,value#3], cat, [`thing1`#6,thing2#7], HiveScriptIOSchema(List(),List(),Some(org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe),Some(org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe),List((field.delim, )),List((field.delim, )),Some(org.apache.hadoop.hive.ql.exec.TextRecordReader),Some(org.apache.hadoop.hive.ql.exec.TextRecordWriter),false) +- SubqueryAlias test +- Project [_1#0 AS key#2,_2#1 AS value#3] +- LocalRelation [_1#0,_2#1], [[1,1],[2,2],[3,3],[4,4],[5,5]] ``` The backpacks of \`thing1\` should be cleaned before entering Parser/Analyzer. This PR fixes this issue. #### How was this patch tested? Added a test case and modified an existing test case Author: gatorsmile <gatorsmile@gmail.com> Closes apache#11415 from gatorsmile/scriptTransform.
1 parent d6969ff commit 9e01fe2

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,11 @@ private[hive] class HiveQl(conf: ParserConf) extends SparkQl(conf) with Logging
583583

584584
val (output, schemaLess) = outputClause match {
585585
case Token("TOK_ALIASLIST", aliases) :: Nil =>
586-
(aliases.map { case Token(name, Nil) => AttributeReference(name, StringType)() },
587-
false)
586+
(aliases.map { case Token(name, Nil) =>
587+
AttributeReference(cleanIdentifier(name), StringType)() }, false)
588588
case Token("TOK_TABCOLLIST", attributes) :: Nil =>
589589
(attributes.map { case Token("TOK_TABCOL", Token(name, Nil) :: dataType :: Nil) =>
590-
AttributeReference(name, nodeToDataType(dataType))() }, false)
590+
AttributeReference(cleanIdentifier(name), nodeToDataType(dataType))() }, false)
591591
case Nil =>
592592
(List(AttributeReference("key", StringType)(),
593593
AttributeReference("value", StringType)()), true)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,12 @@ class HiveQlSuite extends SparkFunSuite with BeforeAndAfterAll {
200200

201201
assert(plan.children.head.asInstanceOf[Generate].generator.isInstanceOf[JsonTuple])
202202
}
203+
204+
test("use backticks in output of Script Transform") {
205+
val plan = parser.parsePlan(
206+
"""SELECT `t`.`thing1`
207+
|FROM (SELECT TRANSFORM (`parquet_t1`.`key`, `parquet_t1`.`value`)
208+
|USING 'cat' AS (`thing1` int, `thing2` string) FROM `default`.`parquet_t1`) AS t
209+
""".stripMargin)
210+
}
203211
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
731731
data.toDF("key", "value").registerTempTable("test")
732732
checkAnswer(
733733
sql("""FROM
734-
|(FROM test SELECT TRANSFORM(key, value) USING 'cat' AS (thing1 int, thing2 string)) t
734+
|(FROM test SELECT TRANSFORM(key, value) USING 'cat' AS (`thing1` int, thing2 string)) t
735735
|SELECT thing1 + 1
736736
""".stripMargin), (2 to 6).map(i => Row(i)))
737737
}

0 commit comments

Comments
 (0)