Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}