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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package org.apache.spark.sql.execution.datasources.v2

import java.util.Locale

import scala.util.control.NonFatal

import org.apache.spark.internal.LogKeys.OPTIONS
import org.apache.spark.internal.MDC
import org.apache.spark.sql.catalyst.{InternalRow, TableIdentifier}
Expand All @@ -28,8 +30,10 @@ import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap
import org.apache.spark.sql.classic.Dataset
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.MultipartIdentifierHelper
import org.apache.spark.sql.execution.command.CreateViewCommand
import org.apache.spark.sql.connector.catalog.Identifier
import org.apache.spark.sql.execution.command.{CreateViewCommand, DropTempViewCommand}
import org.apache.spark.storage.StorageLevel
import org.apache.spark.util.Utils

trait BaseCacheTableExec extends LeafV2CommandExec {
def relationName: String
Expand All @@ -53,7 +57,16 @@ trait BaseCacheTableExec extends LeafV2CommandExec {

if (!isLazy) {
// Performs eager caching.
df.count()
try {
df.count()
} catch {
case NonFatal(e) =>
// If the query fails, we should remove the cached table.
Utils.tryLogNonFatalError {
session.sharedState.cacheManager.uncacheQuery(session, planToCache, cascade = false)
}
throw e
}
}

Seq.empty
Expand Down Expand Up @@ -99,7 +112,15 @@ case class CacheTableAsSelectExec(
isAnalyzed = true,
referredTempFunctions = referredTempFunctions
).run(session)
super.run()
try {
super.run()
} catch {
case NonFatal(e) =>
Utils.tryLogNonFatalError {
DropTempViewCommand(Identifier.of(Array.empty, tempViewName)).run(session)
}
throw e
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import scala.concurrent.duration._

import org.apache.commons.io.FileUtils

import org.apache.spark.CleanerListener
import org.apache.spark.{CleanerListener, SparkRuntimeException}
import org.apache.spark.executor.DataReadMethod._
import org.apache.spark.executor.DataReadMethod.DataReadMethod
import org.apache.spark.scheduler.{SparkListener, SparkListenerEvent, SparkListenerJobStart}
Expand Down Expand Up @@ -1833,4 +1833,13 @@ class CachedTableSuite extends QueryTest with SQLTestUtils
}
}
}

test("SPARK-52684: Atomicity of cache table on error") {
withTempView("SPARK_52684") {
intercept[SparkRuntimeException] {
spark.sql("CACHE TABLE SPARK_52684 AS SELECT raise_error('SPARK-52684') AS c1")
}
assert(!spark.catalog.tableExists("SPARK_52684"))
}
}
}