Skip to content

Commit

Permalink
[SPARK-17150][SQL] Support SQL generation for inline tables
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?
This patch adds support for SQL generation for inline tables. With this, it would be possible to create a view that depends on inline tables.

## How was this patch tested?
Added a test case in LogicalPlanToSQLSuite.

Author: petermaxlee <[email protected]>

Closes #14709 from petermaxlee/SPARK-17150.

(cherry picked from commit 45d40d9)
Signed-off-by: Wenchen Fan <[email protected]>
  • Loading branch information
petermaxlee authored and cloud-fan committed Aug 20, 2016
1 parent 379b127 commit f7458c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package org.apache.spark.sql.catalyst.plans.logical

import org.apache.spark.sql.Row
import org.apache.spark.sql.catalyst.{analysis, CatalystTypeConverters, InternalRow}
import org.apache.spark.sql.catalyst.expressions.Attribute
import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow}
import org.apache.spark.sql.catalyst.analysis
import org.apache.spark.sql.catalyst.expressions.{Attribute, Literal}
import org.apache.spark.sql.types.{StructField, StructType}

object LocalRelation {
Expand Down Expand Up @@ -75,4 +76,16 @@ case class LocalRelation(output: Seq[Attribute], data: Seq[InternalRow] = Nil)

override lazy val statistics =
Statistics(sizeInBytes = output.map(_.dataType.defaultSize).sum * data.length)

def toSQL(inlineTableName: String): String = {
require(data.nonEmpty)
val types = output.map(_.dataType)
val rows = data.map { row =>
val cells = row.toSeq(types).zip(types).map { case (v, tpe) => Literal(v, tpe).sql }
cells.mkString("(", ", ", ")")
}
"VALUES " + rows.mkString(", ") +
" AS " + inlineTableName +
output.map(_.name).mkString("(", ", ", ")")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ class SQLBuilder private (
case p: ScriptTransformation =>
scriptTransformationToSQL(p)

case p: LocalRelation =>
p.toSQL(newSubqueryName())

case OneRowRelation =>
""

Expand Down
4 changes: 4 additions & 0 deletions sql/hive/src/test/resources/sqlgen/inline_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- This file is automatically generated by LogicalPlanToSQLSuite.
select * from values ("one", 1), ("two", 2), ("three", null) as data(a, b) where b > 1
--------------------------------------------------------------------------------
SELECT `gen_attr_0` AS `a`, `gen_attr_1` AS `b` FROM (SELECT `gen_attr_0`, `gen_attr_1` FROM (VALUES ("one", 1), ("two", 2), ("three", CAST(NULL AS INT)) AS gen_subquery_0(gen_attr_0, gen_attr_1)) AS data WHERE (`gen_attr_1` > 1)) AS data
Original file line number Diff line number Diff line change
Expand Up @@ -1102,4 +1102,12 @@ class LogicalPlanToSQLSuite extends SQLBuilderTest with SQLTestUtils {
checkSQL("select * from orc_t", "select_orc_table")
}
}

test("inline tables") {
checkSQL(
"""
|select * from values ("one", 1), ("two", 2), ("three", null) as data(a, b) where b > 1
""".stripMargin,
"inline_tables")
}
}

0 comments on commit f7458c7

Please sign in to comment.