Skip to content

Commit 2b113a2

Browse files
committed
[SPARK-22686][SQL] DROP TABLE IF EXISTS should not throw AnalysisException
1 parent 3887b7e commit 2b113a2

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,8 @@ class Analyzer(
635635
try {
636636
catalog.lookupRelation(tableIdentWithDb)
637637
} catch {
638-
case _: NoSuchTableException =>
639-
u.failAnalysis(s"Table or view not found: ${tableIdentWithDb.unquotedString}")
638+
case e: NoSuchTableException =>
639+
u.failAnalysisWithCause(s"Table or view not found: ${tableIdentWithDb.unquotedString}", e)
640640
// If the database is defined and that database is not found, throw an AnalysisException.
641641
// Note that if the database is not defined, it is possible we are looking up a temp view.
642642
case e: NoSuchDatabaseException =>

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/package.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ package object analysis {
4141
def failAnalysis(msg: String): Nothing = {
4242
throw new AnalysisException(msg, t.origin.line, t.origin.startPosition)
4343
}
44+
45+
def failAnalysisWithCause(msg: String, cause: Throwable): Nothing = {
46+
throw new AnalysisException(msg, t.origin.line, t.origin.startPosition, None, Some(cause))
47+
}
4448
}
4549

4650
/** Catches any AnalysisExceptions thrown by `f` and attaches `t`'s position if any. */

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ case class DropTableCommand(
206206
try {
207207
sparkSession.sharedState.cacheManager.uncacheQuery(sparkSession.table(tableName))
208208
} catch {
209-
case _: NoSuchTableException if ifExists =>
209+
case ae: AnalysisException
210+
if ifExists && ae.cause.nonEmpty && ae.getCause.isInstanceOf[NoSuchTableException] =>
210211
case NonFatal(e) => log.warn(e.toString, e)
211212
}
212213
catalog.refreshTable(tableName)

0 commit comments

Comments
 (0)