Skip to content

Commit bdc8153

Browse files
brkyvzhvanhovell
authored andcommitted
[SPARK-18465] Add 'IF EXISTS' clause to 'UNCACHE' to not throw exceptions when table doesn't exist
## What changes were proposed in this pull request? While this behavior is debatable, consider the following use case: ```sql UNCACHE TABLE foo; CACHE TABLE foo AS SELECT * FROM bar ``` The command above fails the first time you run it. But I want to run the command above over and over again, and I don't want to change my code just for the first run of it. The issue is that subsequent `CACHE TABLE` commands do not overwrite the existing table. Now we can do: ```sql UNCACHE TABLE IF EXISTS foo; CACHE TABLE foo AS SELECT * FROM bar ``` ## How was this patch tested? Unit tests Author: Burak Yavuz <[email protected]> Closes #15896 from brkyvz/uncache.
1 parent 702cd40 commit bdc8153

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ statement
142142
| REFRESH TABLE tableIdentifier #refreshTable
143143
| REFRESH .*? #refreshResource
144144
| CACHE LAZY? TABLE tableIdentifier (AS? query)? #cacheTable
145-
| UNCACHE TABLE tableIdentifier #uncacheTable
145+
| UNCACHE TABLE (IF EXISTS)? tableIdentifier #uncacheTable
146146
| CLEAR CACHE #clearCache
147147
| LOAD DATA LOCAL? INPATH path=STRING OVERWRITE? INTO TABLE
148148
tableIdentifier partitionSpec? #loadData

sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder {
233233
* Create an [[UncacheTableCommand]] logical plan.
234234
*/
235235
override def visitUncacheTable(ctx: UncacheTableContext): LogicalPlan = withOrigin(ctx) {
236-
UncacheTableCommand(visitTableIdentifier(ctx.tableIdentifier))
236+
UncacheTableCommand(visitTableIdentifier(ctx.tableIdentifier), ctx.EXISTS != null)
237237
}
238238

239239
/**

sql/core/src/main/scala/org/apache/spark/sql/execution/command/cache.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.apache.spark.sql.execution.command
1919

2020
import org.apache.spark.sql.{Dataset, Row, SparkSession}
2121
import org.apache.spark.sql.catalyst.TableIdentifier
22+
import org.apache.spark.sql.catalyst.analysis.NoSuchTableException
2223
import org.apache.spark.sql.catalyst.plans.QueryPlan
2324
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
2425

@@ -49,10 +50,17 @@ case class CacheTableCommand(
4950
}
5051

5152

52-
case class UncacheTableCommand(tableIdent: TableIdentifier) extends RunnableCommand {
53+
case class UncacheTableCommand(
54+
tableIdent: TableIdentifier,
55+
ifExists: Boolean) extends RunnableCommand {
5356

5457
override def run(sparkSession: SparkSession): Seq[Row] = {
55-
sparkSession.catalog.uncacheTable(tableIdent.quotedString)
58+
val tableId = tableIdent.quotedString
59+
try {
60+
sparkSession.catalog.uncacheTable(tableId)
61+
} catch {
62+
case _: NoSuchTableException if ifExists => // don't throw
63+
}
5664
Seq.empty[Row]
5765
}
5866
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,16 @@ class CachedTableSuite extends QueryTest with SQLTestUtils with TestHiveSingleto
101101
sql("DROP TABLE IF EXISTS nonexistantTable")
102102
}
103103

104-
test("correct error on uncache of nonexistant tables") {
104+
test("uncache of nonexistant tables") {
105+
// make sure table doesn't exist
106+
intercept[NoSuchTableException](spark.table("nonexistantTable"))
105107
intercept[NoSuchTableException] {
106108
spark.catalog.uncacheTable("nonexistantTable")
107109
}
108110
intercept[NoSuchTableException] {
109111
sql("UNCACHE TABLE nonexistantTable")
110112
}
113+
sql("UNCACHE TABLE IF EXISTS nonexistantTable")
111114
}
112115

113116
test("no error on uncache of non-cached table") {

0 commit comments

Comments
 (0)