From 1a4971d8a16b5bc624cef584271243bf64a51941 Mon Sep 17 00:00:00 2001 From: Wenchen Fan Date: Wed, 17 Mar 2021 16:36:50 +0800 Subject: [PATCH] [SPARK-34770][SQL] InMemoryCatalog.tableExists should not fail if database doesn't exist ### What changes were proposed in this pull request? This PR updates `InMemoryCatalog.tableExists` to return false if database doesn't exist, instead of failing. The new behavior is consistent with `HiveExternalCatalog` which is used in production, so this bug mostly only affects tests. ### Why are the changes needed? bug fix ### Does this PR introduce _any_ user-facing change? no ### How was this patch tested? a new test Closes #31860 from cloud-fan/catalog. Authored-by: Wenchen Fan Signed-off-by: Wenchen Fan --- .../apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala | 3 +-- .../spark/sql/catalyst/catalog/SessionCatalogSuite.scala | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala index 1c4db8746c46a..ab7e49581a9f4 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala @@ -342,8 +342,7 @@ class InMemoryCatalog( } override def tableExists(db: String, table: String): Boolean = synchronized { - requireDbExists(db) - catalog(db).tables.contains(table) + catalog.contains(db) && catalog(db).tables.contains(table) } override def listTables(db: String): Seq[String] = synchronized { diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala index ce06a76b8c1b6..be663eaae341c 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala @@ -704,6 +704,9 @@ abstract class SessionCatalogSuite extends AnalysisTest with Eventually { catalog.createTempView("tbl3", tempTable, overrideIfExists = false) // tableExists should not check temp view. assert(!catalog.tableExists(TableIdentifier("tbl3"))) + + // If database doesn't exist, return false instead of failing. + assert(!catalog.tableExists(TableIdentifier("tbl1", Some("non-exist")))) } }