Skip to content

Commit bea871f

Browse files
committed
Address review comments
1 parent 53e9934 commit bea871f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
9494
case Distinct(p: Project) =>
9595
projectToSQL(p, isDistinct = true)
9696

97-
case p@ Project(_, g: Generate) =>
97+
case p @ Project(_, g: Generate) =>
9898
generateToSQL(p)
9999

100100
case g: Generate =>
@@ -311,18 +311,19 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
311311
(w.child.output ++ w.windowExpressions).map(_.sql).mkString(", "),
312312
if (w.child == OneRowRelation) "" else "FROM",
313313
toSQL(w.child)
314+
)
314315
}
315316

316-
/* This function handles the SQL generation for generators.
317+
/**
318+
* This function handles the SQL generation for generators.
317319
* sample plan :
318320
* +- Project [mycol2#192]
319321
* +- Generate explode(myCol#191), true, false, Some(mytable2), [mycol2#192]
320322
* +- Generate explode(array(array(1, 2, 3))), true, false, Some(mytable), [mycol#191]
321323
* +- MetastoreRelation default, src, None
322-
*
323324
*/
324325
private def generateToSQL(plan: Generate): String = {
325-
val columnAliases = plan.generatorOutput.map(a => quoteIdentifier(a.name)).mkString(",")
326+
val columnAliases = plan.generatorOutput.map(_.sql).mkString(",")
326327
val generatorAlias = if (plan.qualifier.isEmpty) "" else plan.qualifier.get
327328
val outerClause = if (plan.outer) "OUTER" else ""
328329
build(
@@ -342,8 +343,8 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
342343
}
343344

344345
private def generateToSQL(plan: Project): String = {
345-
// assert if child is a generate or not.
346346
val generate = plan.child.asInstanceOf[Generate]
347+
assert(generate.join == true || plan.projectList.size == 1)
347348
// Generators that appear in projection list will be expressed as LATERAL VIEW.
348349
// A qualifier is needed for a LATERAL VIEw.
349350
val generatorAlias: String = generate.qualifier.getOrElse(SQLBuilder.newGeneratorName)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,15 @@ class LogicalPlanToSQLSuite extends SQLBuilderTest with SQLTestUtils {
585585
checkHiveQl("SELECT key, json_tuple(jstring, 'f1', 'f2', 'f3', 'f4', 'f5') FROM parquet_t3")
586586
}
587587

588+
test("Lateral view with join") {
589+
checkHiveQl(
590+
"""
591+
|SELECT gencol, explode(array(1,2,3)), x1.key
592+
|FROM (t1 LATERAL VIEW OUTER explode(value) gentab as gencol), t1 as x1
593+
""".stripMargin
594+
)
595+
}
596+
588597
test("SQL generation for lateral views") {
589598
// Filter and OUTER clause
590599
checkHiveQl(

0 commit comments

Comments
 (0)