Skip to content

Commit 453455c

Browse files
committed
[SPARK-13974][SQL] sub-query names do not need to be globally unique while generate SQL
## What changes were proposed in this pull request? We only need to make sub-query names unique every time we generate a SQL string, but not all the time. This PR moves the `newSubqueryName` method to `class SQLBuilder` and remove `object SQLBuilder`. also addressed 2 minor comments in #11696 ## How was this patch tested? existing tests. Author: Wenchen Fan <wenchen@databricks.com> Closes #11783 from cloud-fan/tmp.
1 parent 1614485 commit 453455c

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
5454

5555
def this(df: DataFrame) = this(df.queryExecution.analyzed, df.sqlContext)
5656

57+
private val nextSubqueryId = new AtomicLong(0)
58+
private def newSubqueryName(): String = s"gen_subquery_${nextSubqueryId.getAndIncrement()}"
59+
5760
def toSQL: String = {
5861
val canonicalizedPlan = Canonicalizer.execute(logicalPlan)
5962
val outputNames = logicalPlan.output.map(_.name)
@@ -64,7 +67,7 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
6467
val finalName = if (qualifiers.length == 1) {
6568
qualifiers.head
6669
} else {
67-
SQLBuilder.newSubqueryName
70+
newSubqueryName()
6871
}
6972

7073
// Canonicalizer will remove all naming information, we should add it back by adding an extra
@@ -254,16 +257,16 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
254257
}
255258

256259
private def generateToSQL(g: Generate): String = {
257-
val columnAliases = g.generatorOutput.map(_.sql).mkString(",")
260+
val columnAliases = g.generatorOutput.map(_.sql).mkString(", ")
258261

259262
val childSQL = if (g.child == OneRowRelation) {
260263
// This only happens when we put UDTF in project list and there is no FROM clause. Because we
261264
// always generate LATERAL VIEW for `Generate`, here we use a trick to put a dummy sub-query
262265
// after FROM clause, so that we can generate a valid LATERAL VIEW SQL string.
263266
// For example, if the original SQL is: "SELECT EXPLODE(ARRAY(1, 2))", we will convert in to
264267
// LATERAL VIEW format, and generate:
265-
// SELECT col FROM (SELECT 1) sub-q0 LATERAL VIEW EXPLODE(ARRAY(1, 2)) sub_q1 AS col
266-
s"(SELECT 1) ${SQLBuilder.newSubqueryName}"
268+
// SELECT col FROM (SELECT 1) sub_q0 LATERAL VIEW EXPLODE(ARRAY(1, 2)) sub_q1 AS col
269+
s"(SELECT 1) ${newSubqueryName()}"
267270
} else {
268271
toSQL(g.child)
269272
}
@@ -283,7 +286,7 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
283286
"LATERAL VIEW",
284287
if (g.outer) "OUTER" else "",
285288
g.generator.sql,
286-
SQLBuilder.newSubqueryName,
289+
newSubqueryName(),
287290
"AS",
288291
columnAliases
289292
)
@@ -477,7 +480,7 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
477480
}
478481

479482
private def addSubquery(plan: LogicalPlan): SubqueryAlias = {
480-
SubqueryAlias(SQLBuilder.newSubqueryName, plan)
483+
SubqueryAlias(newSubqueryName(), plan)
481484
}
482485

483486
private def addSubqueryIfNeeded(plan: LogicalPlan): LogicalPlan = plan match {
@@ -514,9 +517,3 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: SQLContext) extends Loggi
514517
}
515518
}
516519
}
517-
518-
object SQLBuilder {
519-
private val nextSubqueryId = new AtomicLong(0)
520-
521-
private def newSubqueryName: String = s"gen_subquery_${nextSubqueryId.getAndIncrement()}"
522-
}

0 commit comments

Comments
 (0)