Skip to content

[SPARK-2594][SQL] Support CACHE TABLE <name> AS SELECT ... #2397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
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 @@ -151,7 +151,7 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
EXCEPT ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Except(q1, q2)} |
UNION ~ opt(DISTINCT) ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Distinct(Union(q1, q2)) }
)
| insert | cache
| insert | cache | unCache
)

protected lazy val select: Parser[LogicalPlan] =
Expand Down Expand Up @@ -183,9 +183,17 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
}

protected lazy val cache: Parser[LogicalPlan] =
(CACHE ^^^ true | UNCACHE ^^^ false) ~ TABLE ~ ident ^^ {
case doCache ~ _ ~ tableName => CacheCommand(tableName, doCache)
CACHE ~ TABLE ~> ident ~ opt(AS ~> select) <~ opt(";") ^^ {
case tableName ~ None =>
CacheCommand(tableName, true)
case tableName ~ Some(plan) =>
CacheTableAsSelectCommand(tableName, plan)
}

protected lazy val unCache: Parser[LogicalPlan] =
UNCACHE ~ TABLE ~> ident <~ opt(";") ^^ {
case tableName => CacheCommand(tableName, false)
}

protected lazy val projections: Parser[Seq[Expression]] = repsep(projection, ",")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ case class DescribeCommand(
AttributeReference("data_type", StringType, nullable = false)(),
AttributeReference("comment", StringType, nullable = false)())
}

/**
* Returned for the "CACHE TABLE tableName AS SELECT .." command.
*/
case class CacheTableAsSelectCommand(tableName: String, plan: LogicalPlan) extends Command
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ private[sql] abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
Seq(execution.ExplainCommand(logicalPlan, plan.output, extended)(context))
case logical.CacheCommand(tableName, cache) =>
Seq(execution.CacheCommand(tableName, cache)(context))
case logical.CacheTableAsSelectCommand(tableName, plan) =>
Seq(execution.CacheTableAsSelectCommand(tableName, plan))
case _ => Nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,21 @@ case class DescribeCommand(child: SparkPlan, output: Seq[Attribute])(
child.output.map(field => Row(field.name, field.dataType.toString, null))
}
}

/**
* :: DeveloperApi ::
*/
@DeveloperApi
case class CacheTableAsSelectCommand(tableName: String, logicalPlan: LogicalPlan)
extends LeafNode with Command {

override protected[sql] lazy val sideEffectResult = {
import sqlContext._
logicalPlan.registerTempTable(tableName)
cacheTable(tableName)
Seq.empty[Row]
}

override def output: Seq[Attribute] = Seq.empty

}
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,17 @@ class CachedTableSuite extends QueryTest {
}
assert(!TestSQLContext.isCached("testData"), "Table 'testData' should not be cached")
}

test("CACHE TABLE tableName AS SELECT Star Table") {
TestSQLContext.sql("CACHE TABLE testCacheTable AS SELECT * FROM testData")
TestSQLContext.sql("SELECT * FROM testCacheTable WHERE key = 1").collect()
assert(TestSQLContext.isCached("testCacheTable"), "Table 'testCacheTable' should be cached")
TestSQLContext.uncacheTable("testCacheTable")
}

test("'CACHE TABLE tableName AS SELECT ..'") {
TestSQLContext.sql("CACHE TABLE testCacheTable AS SELECT * FROM testData")
assert(TestSQLContext.isCached("testCacheTable"), "Table 'testCacheTable' should be cached")
TestSQLContext.uncacheTable("testCacheTable")
}
}
30 changes: 20 additions & 10 deletions sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ private[hive] object HiveQl {
SetCommand(Some(key), Some(value))
}
} else if (sql.trim.toLowerCase.startsWith("cache table")) {
CacheCommand(sql.trim.drop(12).trim, true)
sql.trim.drop(12).trim.split(" ").toSeq match {
case Seq(tableName) =>
CacheCommand(tableName, true)
case Seq(tableName, _, select @ _*) =>
CacheTableAsSelectCommand(tableName, createPlan(select.mkString(" ").trim))
}
} else if (sql.trim.toLowerCase.startsWith("uncache table")) {
CacheCommand(sql.trim.drop(14).trim, false)
} else if (sql.trim.toLowerCase.startsWith("add jar")) {
Expand All @@ -243,15 +248,7 @@ private[hive] object HiveQl {
} else if (sql.trim.startsWith("!")) {
ShellCommand(sql.drop(1))
} else {
val tree = getAst(sql)
if (nativeCommands contains tree.getText) {
NativeCommand(sql)
} else {
nodeToPlan(tree) match {
case NativePlaceholder => NativeCommand(sql)
case other => other
}
}
createPlan(sql)
}
} catch {
case e: Exception => throw new ParseException(sql, e)
Expand All @@ -262,6 +259,19 @@ private[hive] object HiveQl {
""".stripMargin)
}
}

/** Creates LogicalPlan for a given HiveQL string. */
def createPlan(sql: String) = {
val tree = getAst(sql)
if (nativeCommands contains tree.getText) {
NativeCommand(sql)
} else {
nodeToPlan(tree) match {
case NativePlaceholder => NativeCommand(sql)
case other => other
}
}
}

def parseDdl(ddl: String): Seq[Attribute] = {
val tree =
Expand Down