You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2022/07/20 03:18:49 UTC

[spark] branch master updated: [SPARK-39810][SQL] Catalog.tableExists should handle nested namespace

This is an automated email from the ASF dual-hosted git repository.

wenchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 0cc96f76d8a [SPARK-39810][SQL] Catalog.tableExists should handle nested namespace
0cc96f76d8a is described below

commit 0cc96f76d8a4858aee09e1fa32658da3ae76d384
Author: Rui Wang <ru...@databricks.com>
AuthorDate: Wed Jul 20 11:18:34 2022 +0800

    [SPARK-39810][SQL] Catalog.tableExists should handle nested namespace
    
    ### What changes were proposed in this pull request?
    
    Let CatalogImpl.tableExists to reuse CatalogImpl.getTable code.
    
    ### Why are the changes needed?
    
    Currently `tableExists` assume input is only have 3 name parts which is wrong assumption (namespace could be an array). The `getTable` implementation is correct. So we can re-use `getTable` code and if the getTable succeeds, then `tableExists` can return true.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No
    ### How was this patch tested?
    
    UT
    
    Closes #37220 from amaliujia/SPARK-39810.
    
    Authored-by: Rui Wang <ru...@databricks.com>
    Signed-off-by: Wenchen Fan <we...@databricks.com>
---
 .../scala/org/apache/spark/sql/internal/CatalogImpl.scala    | 12 ++++--------
 .../scala/org/apache/spark/sql/internal/CatalogSuite.scala   | 12 ++++++++++++
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/sql/core/src/main/scala/org/apache/spark/sql/internal/CatalogImpl.scala b/sql/core/src/main/scala/org/apache/spark/sql/internal/CatalogImpl.scala
index 8ca11f620a5..e11b349777e 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/internal/CatalogImpl.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/internal/CatalogImpl.scala
@@ -28,7 +28,7 @@ import org.apache.spark.sql.catalyst.catalog._
 import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
 import org.apache.spark.sql.catalyst.plans.logical.{CreateTable, LocalRelation, RecoverPartitions, ShowFunctions, ShowNamespaces, ShowTables, SubqueryAlias, TableSpec, View}
 import org.apache.spark.sql.catalyst.util.CharVarcharUtils
-import org.apache.spark.sql.connector.catalog.{CatalogManager, CatalogPlugin, Identifier, SupportsNamespaces, TableCatalog}
+import org.apache.spark.sql.connector.catalog.{CatalogManager, CatalogPlugin, SupportsNamespaces, TableCatalog}
 import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.{CatalogHelper, IdentifierHelper, MultipartIdentifierHelper, TransformHelper}
 import org.apache.spark.sql.errors.QueryCompilationErrors
 import org.apache.spark.sql.execution.datasources.{DataSource, LogicalRelation}
@@ -493,14 +493,10 @@ class CatalogImpl(sparkSession: SparkSession) extends Catalog {
    */
   override def tableExists(tableName: String): Boolean = {
     try {
-      val tableIdent = sparkSession.sessionState.sqlParser.parseTableIdentifier(tableName)
-      tableExists(tableIdent.database.orNull, tableIdent.table)
+      getTable(tableName)
+      true
     } catch {
-      case e: org.apache.spark.sql.catalyst.parser.ParseException =>
-        val ident = sparkSession.sessionState.sqlParser.parseMultipartIdentifier(tableName)
-        val catalog =
-          sparkSession.sessionState.catalogManager.catalog(ident(0)).asTableCatalog
-        catalog.tableExists(Identifier.of(Array(ident(1)), ident(2)))
+      case e: AnalysisException => false
     }
   }
 
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/internal/CatalogSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/internal/CatalogSuite.scala
index f3133026836..0de48325d98 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/internal/CatalogSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/internal/CatalogSuite.scala
@@ -122,6 +122,7 @@ class CatalogSuite extends SharedSparkSession with AnalysisTest with BeforeAndAf
   override def afterEach(): Unit = {
     try {
       sessionCatalog.reset()
+      spark.sessionState.catalogManager.reset()
     } finally {
       super.afterEach()
     }
@@ -769,6 +770,17 @@ class CatalogSuite extends SharedSparkSession with AnalysisTest with BeforeAndAf
     assert(spark.catalog.tableExists(Array(catalogName, dbName, tableName).mkString(".")))
   }
 
+  test("SPARK-39810: Catalog.tableExists should handle nested namespace") {
+    val tableSchema = new StructType().add("i", "int")
+    val catalogName = "testcat"
+    val dbName = "my_db2.my_db3"
+    val tableName = "my_table2"
+    assert(!spark.catalog.tableExists(Array(catalogName, dbName, tableName).mkString(".")))
+    createTable(tableName, dbName, catalogName, classOf[FakeV2Provider].getName, tableSchema,
+      Map.empty[String, String], "")
+    assert(spark.catalog.tableExists(Array(catalogName, dbName, tableName).mkString(".")))
+  }
+
   test("three layer namespace compatibility - database exists") {
     val catalogName = "testcat"
     val dbName = "my_db"


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org