Skip to content

Commit 08e43d3

Browse files
committed
[SPARK-52684][SQL] Make CACHE TABLE Commands atomic while encountering execution errors
### What changes were proposed in this pull request? This PR makes CACHE TABLE commands atomic while encountering execution errors ### Why are the changes needed? For now, when an AnalysisException occurs, no cache or view will be created, but an execution one occurs, a view or an erroneous 'cache' is created. ### Does this PR introduce _any_ user-facing change? Yes, but it's a bugfix. It only affects rare corner case that a user leverages this bug to create an erroneous 'cache'/view for some particular purposes ### How was this patch tested? new tests ### Was this patch authored or co-authored using generative AI tooling? no Closes #51374 from yaooqinn/SPARK-52684. Authored-by: Kent Yao <yao@apache.org> Signed-off-by: Kent Yao <yao@apache.org>
1 parent b44389c commit 08e43d3

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/CacheTableExec.scala

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package org.apache.spark.sql.execution.datasources.v2
1919

2020
import java.util.Locale
2121

22+
import scala.util.control.NonFatal
23+
2224
import org.apache.spark.internal.LogKeys.OPTIONS
2325
import org.apache.spark.internal.MDC
2426
import org.apache.spark.sql.catalyst.{InternalRow, TableIdentifier}
@@ -28,8 +30,10 @@ import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
2830
import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap
2931
import org.apache.spark.sql.classic.Dataset
3032
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.MultipartIdentifierHelper
31-
import org.apache.spark.sql.execution.command.CreateViewCommand
33+
import org.apache.spark.sql.connector.catalog.Identifier
34+
import org.apache.spark.sql.execution.command.{CreateViewCommand, DropTempViewCommand}
3235
import org.apache.spark.storage.StorageLevel
36+
import org.apache.spark.util.Utils
3337

3438
trait BaseCacheTableExec extends LeafV2CommandExec {
3539
def relationName: String
@@ -53,7 +57,16 @@ trait BaseCacheTableExec extends LeafV2CommandExec {
5357

5458
if (!isLazy) {
5559
// Performs eager caching.
56-
df.count()
60+
try {
61+
df.count()
62+
} catch {
63+
case NonFatal(e) =>
64+
// If the query fails, we should remove the cached table.
65+
Utils.tryLogNonFatalError {
66+
session.sharedState.cacheManager.uncacheQuery(session, planToCache, cascade = false)
67+
}
68+
throw e
69+
}
5770
}
5871

5972
Seq.empty
@@ -99,7 +112,15 @@ case class CacheTableAsSelectExec(
99112
isAnalyzed = true,
100113
referredTempFunctions = referredTempFunctions
101114
).run(session)
102-
super.run()
115+
try {
116+
super.run()
117+
} catch {
118+
case NonFatal(e) =>
119+
Utils.tryLogNonFatalError {
120+
DropTempViewCommand(Identifier.of(Array.empty, tempViewName)).run(session)
121+
}
122+
throw e
123+
}
103124
}
104125
}
105126

sql/core/src/test/scala/org/apache/spark/sql/CachedTableSuite.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import scala.concurrent.duration._
2727

2828
import org.apache.commons.io.FileUtils
2929

30-
import org.apache.spark.CleanerListener
30+
import org.apache.spark.{CleanerListener, SparkRuntimeException}
3131
import org.apache.spark.executor.DataReadMethod._
3232
import org.apache.spark.executor.DataReadMethod.DataReadMethod
3333
import org.apache.spark.scheduler.{SparkListener, SparkListenerEvent, SparkListenerJobStart}
@@ -1833,4 +1833,13 @@ class CachedTableSuite extends QueryTest with SQLTestUtils
18331833
}
18341834
}
18351835
}
1836+
1837+
test("SPARK-52684: Atomicity of cache table on error") {
1838+
withTempView("SPARK_52684") {
1839+
intercept[SparkRuntimeException] {
1840+
spark.sql("CACHE TABLE SPARK_52684 AS SELECT raise_error('SPARK-52684') AS c1")
1841+
}
1842+
assert(!spark.catalog.tableExists("SPARK_52684"))
1843+
}
1844+
}
18361845
}

0 commit comments

Comments
 (0)