Skip to content

Commit 7fd6d53

Browse files
dongjoon-hyuncloud-fan
authored andcommitted
[SPARK-22686][SQL] DROP TABLE IF EXISTS should not show AnalysisException
## What changes were proposed in this pull request? During [SPARK-22488](#19713) to fix view resolution issue, there occurs a regression at `2.2.1` and `master` branch like the following. This PR fixes that. ```scala scala> spark.version res2: String = 2.2.1 scala> sql("DROP TABLE IF EXISTS t").show 17/12/04 21:01:06 WARN DropTableCommand: org.apache.spark.sql.AnalysisException: Table or view not found: t; org.apache.spark.sql.AnalysisException: Table or view not found: t; ``` ## How was this patch tested? Manual. Author: Dongjoon Hyun <[email protected]> Closes #19888 from dongjoon-hyun/SPARK-22686. (cherry picked from commit 82183f7) Signed-off-by: Wenchen Fan <[email protected]>
1 parent 5b63000 commit 7fd6d53

File tree

1 file changed

+13
-7
lines changed
  • sql/core/src/main/scala/org/apache/spark/sql/execution/command

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,20 @@ case class DropTableCommand(
199199
case _ =>
200200
}
201201
}
202-
try {
203-
sparkSession.sharedState.cacheManager.uncacheQuery(sparkSession.table(tableName))
204-
} catch {
205-
case _: NoSuchTableException if ifExists =>
206-
case NonFatal(e) => log.warn(e.toString, e)
202+
203+
if (catalog.isTemporaryTable(tableName) || catalog.tableExists(tableName)) {
204+
try {
205+
sparkSession.sharedState.cacheManager.uncacheQuery(sparkSession.table(tableName))
206+
} catch {
207+
case NonFatal(e) => log.warn(e.toString, e)
208+
}
209+
catalog.refreshTable(tableName)
210+
catalog.dropTable(tableName, ifExists, purge)
211+
} else if (ifExists) {
212+
// no-op
213+
} else {
214+
throw new AnalysisException(s"Table or view not found: ${tableName.identifier}")
207215
}
208-
catalog.refreshTable(tableName)
209-
catalog.dropTable(tableName, ifExists, purge)
210216
Seq.empty[Row]
211217
}
212218
}

0 commit comments

Comments
 (0)