File tree Expand file tree Collapse file tree 3 files changed +17
-10
lines changed
core/src/main/scala/org/apache/spark/sql/execution/command
hive/src/test/scala/org/apache/spark/sql/hive Expand file tree Collapse file tree 3 files changed +17
-10
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ license: |
4949 * ` MSCK REPAIR TABLE `
5050 * ` LOAD DATA `
5151 * ` REFRESH TABLE `
52+ * ` TRUNCATE TABLE `
5253 * and the method ` spark.catalog.refreshTable `
5354 In Spark 3.1 and earlier, table refreshing leaves dependents uncached.
5455
Original file line number Diff line number Diff line change @@ -561,16 +561,9 @@ case class TruncateTableCommand(
561561 }
562562 }
563563 }
564- // After deleting the data, invalidate the table to make sure we don't keep around a stale
565- // file relation in the metastore cache.
566- spark.sessionState.refreshTable(tableName.unquotedString)
567- // Also try to drop the contents of the table from the columnar cache
568- try {
569- spark.sharedState.cacheManager.uncacheQuery(spark.table(table.identifier), cascade = true )
570- } catch {
571- case NonFatal (e) =>
572- log.warn(s " Exception when attempting to uncache table $tableIdentWithDB" , e)
573- }
564+ // After deleting the data, refresh the table to make sure we don't keep around a stale
565+ // file relation in the metastore cache and cached table data in the cache manager.
566+ spark.catalog.refreshTable(tableIdentWithDB)
574567
575568 if (table.stats.nonEmpty) {
576569 // empty table after truncation
Original file line number Diff line number Diff line change @@ -501,4 +501,17 @@ class CachedTableSuite extends QueryTest with SQLTestUtils with TestHiveSingleto
501501 }
502502 }
503503 }
504+
505+ test(" SPARK-34215: keep table cached after truncation" ) {
506+ withTable(" tbl" ) {
507+ sql(" CREATE TABLE tbl (c0 int)" )
508+ sql(" INSERT INTO tbl SELECT 0" )
509+ sql(" CACHE TABLE tbl" )
510+ assert(spark.catalog.isCached(" tbl" ))
511+ checkAnswer(sql(" SELECT * FROM tbl" ), Row (0 ))
512+ sql(" TRUNCATE TABLE tbl" )
513+ assert(spark.catalog.isCached(" tbl" ))
514+ checkAnswer(sql(" SELECT * FROM tbl" ), Seq .empty)
515+ }
516+ }
504517}
You can’t perform that action at this time.
0 commit comments