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
13 changes: 9 additions & 4 deletions sql/core/src/main/scala/org/apache/spark/sql/SQLContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,15 @@ class SQLContext(@transient val sparkContext: SparkContext)
/** Caches the specified table in-memory. */
def cacheTable(tableName: String): Unit = {
val currentTable = catalog.lookupRelation(None, tableName)
val useCompression =
sparkContext.conf.getBoolean("spark.sql.inMemoryColumnarStorage.compressed", false)
val asInMemoryRelation =
InMemoryRelation(useCompression, executePlan(currentTable).executedPlan)
val asInMemoryRelation = EliminateAnalysisOperators(currentTable.logicalPlan) match {
case _: InMemoryRelation =>
currentTable.logicalPlan

case _ =>
val useCompression =
sparkContext.conf.getBoolean("spark.sql.inMemoryColumnarStorage.compressed", false)
InMemoryRelation(useCompression, executePlan(currentTable).executedPlan)
}

catalog.registerTable(None, tableName, asInMemoryRelation)
}
Expand Down
20 changes: 20 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.spark.sql

import org.apache.spark.sql.catalyst.analysis.EliminateAnalysisOperators
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.columnar.{InMemoryColumnarTableScan, InMemoryRelation}
import org.apache.spark.sql.test._

/* Implicits */
Expand Down Expand Up @@ -405,4 +407,22 @@ class SQLQuerySuite extends QueryTest {
clear()
}

test("SPARK-1669: cacheTable should be idempotent") {
assume(!table("testData").logicalPlan.isInstanceOf[InMemoryRelation])

cacheTable("testData")
EliminateAnalysisOperators(table("testData").logicalPlan) match {
case _: InMemoryRelation =>
case _ =>
fail("testData should be cached")
}

cacheTable("testData")
EliminateAnalysisOperators(table("testData").logicalPlan) match {
case InMemoryRelation(_, _, _: InMemoryColumnarTableScan) =>
fail("cacheTable is not idempotent")

case _ =>
}
}
}