Skip to content

Commit 82183f7

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.
1 parent 59aa3d5 commit 82183f7

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
@@ -203,14 +203,20 @@ case class DropTableCommand(
203203
case _ =>
204204
}
205205
}
206-
try {
207-
sparkSession.sharedState.cacheManager.uncacheQuery(sparkSession.table(tableName))
208-
} catch {
209-
case _: NoSuchTableException if ifExists =>
210-
case NonFatal(e) => log.warn(e.toString, e)
206+
207+
if (catalog.isTemporaryTable(tableName) || catalog.tableExists(tableName)) {
208+
try {
209+
sparkSession.sharedState.cacheManager.uncacheQuery(sparkSession.table(tableName))
210+
} catch {
211+
case NonFatal(e) => log.warn(e.toString, e)
212+
}
213+
catalog.refreshTable(tableName)
214+
catalog.dropTable(tableName, ifExists, purge)
215+
} else if (ifExists) {
216+
// no-op
217+
} else {
218+
throw new AnalysisException(s"Table or view not found: ${tableName.identifier}")
211219
}
212-
catalog.refreshTable(tableName)
213-
catalog.dropTable(tableName, ifExists, purge)
214220
Seq.empty[Row]
215221
}
216222
}

0 commit comments

Comments
 (0)