Skip to content

Commit 19db09e

Browse files
resolve names for generator in projection
1 parent e8f0e01 commit 19db09e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,12 @@ class Analyzer(
576576
/** Extracts a [[Generator]] expression and any names assigned by aliases to their output. */
577577
private object AliasedGenerator {
578578
def unapply(e: Expression): Option[(Generator, Seq[String])] = e match {
579+
case Alias(g: Generator, name) if g.elementTypes.size > 1 => {
580+
// In project, we probably give a default name which is not correct
581+
// e.g. SELECT explode(map(key, value)) FROM src;
582+
// Let's ignore the specified names for generator in project list
583+
Some((g, Nil))
584+
}
579585
case Alias(g: Generator, name) => Some((g, name :: Nil))
580586
case MultiAlias(g: Generator, names) => Some(g, names)
581587
case _ => None

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,13 @@ class SQLQuerySuite extends QueryTest {
555555
val col = df("val")
556556
}
557557

558+
test("resolve udtf without alias in projection") {
559+
val rdd = sparkContext.makeRDD((1 to 2).map(i => s"""{"a":[$i, ${i + 1}]}"""))
560+
jsonRDD(rdd).registerTempTable("data")
561+
checkAnswer(sql("SELECT explode(map(1, 1)) FROM data LIMIT 1"), Row(1, 1) :: Nil)
562+
checkAnswer(sql("SELECT explode(array(1, 1)) FROM data LIMIT 1"), Row(1) :: Nil)
563+
}
564+
558565
test("logical.Project should not be resolved if it contains aggregates or generators") {
559566
// This test is used to test the fix of SPARK-5875.
560567
// The original issue was that Project's resolved will be true when it contains

0 commit comments

Comments
 (0)