From 8bca8d35e04e582f73052411e42811a8c90329de Mon Sep 17 00:00:00 2001 From: Wenchen Fan Date: Mon, 27 Feb 2017 16:26:46 -0800 Subject: [PATCH] drop the table cache after inserting into a data source table --- .../InsertIntoHadoopFsRelationCommand.scala | 3 +++ .../scala/org/apache/spark/sql/CachedTableSuite.scala | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/InsertIntoHadoopFsRelationCommand.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/InsertIntoHadoopFsRelationCommand.scala index 652bcc8331936..052a2f88754f6 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/InsertIntoHadoopFsRelationCommand.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/InsertIntoHadoopFsRelationCommand.scala @@ -148,6 +148,9 @@ case class InsertIntoHadoopFsRelationCommand( options = options) fileIndex.foreach(_.refresh()) + catalogTable.foreach { table => + sparkSession.sharedState.cacheManager.uncacheQuery(sparkSession.table(table.identifier)) + } } else { logInfo("Skipping insertion into a relation that already exists.") } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/CachedTableSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/CachedTableSuite.scala index 1af1a3652971c..851db6849f49f 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/CachedTableSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/CachedTableSuite.scala @@ -634,4 +634,15 @@ class CachedTableSuite extends QueryTest with SQLTestUtils with SharedSQLContext assert(getNumInMemoryRelations(cachedPlan2) == 4) } } + + test("SPARK-19756: drop the table cache after inserting into a data source table") { + withTable("t") { + Seq(1 -> "a").toDF("i", "j").write.saveAsTable("t") + spark.catalog.cacheTable("t") + checkAnswer(spark.table("t"), Row(1, "a")) + + sql("INSERT INTO t SELECT 2, 'b'") + checkAnswer(spark.table("t"), Row(1, "a") :: Row(2, "b") :: Nil) + } + } }