cloud-fan commented on a change in pull request #25771: [SPARK-28970][SQL] 
Implement USE CATALOG/NAMESPACE for Data Source V2
URL: https://github.com/apache/spark/pull/25771#discussion_r329294509
 
 

 ##########
 File path: 
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala
 ##########
 @@ -830,6 +830,70 @@ class DataSourceV2SQLSuite
     assert(df.collect().map(_.getAs[String](0)).sorted === expected.sorted)
   }
 
+  test("UseCatalog: use catalog with v2 catalog") {
+    val catalogManager = spark.sessionState.catalogManager
+    assert(catalogManager.currentCatalog.name() == "session")
+
+    sql("USE CATALOG testcat")
+    assert(catalogManager.currentCatalog.name() == "testcat")
+  }
+
+  test("UseCatalog: v2 catalog does not exist") {
+    val exception = intercept[AnalysisException] {
+      sql("USE CATALOG unknown")
+    }
+    assert(exception.getMessage.contains("v2 catalog 'unknown' cannot be 
loaded"))
+  }
+
+  test("Use: basic tests with USE statements") {
+    val catalogManager = spark.sessionState.catalogManager
+
+    // Validate the initial current catalog and namespace.
+    assert(catalogManager.currentCatalog.name() == "session")
+    assert(catalogManager.currentNamespace === Array("default"))
+
+    // The following implicitly creates namespaces.
+    sql("CREATE TABLE testcat.ns1.ns1_1.table (id bigint) USING foo")
+    sql("CREATE TABLE testcat2.ns2.ns2_2.table (id bigint) USING foo")
+    sql("CREATE TABLE testcat2.ns3.ns3_3.table (id bigint) USING foo")
+
+    // Catalog is explicitly specified as 'testcat'.
+    sql("USE ns1.ns1_1 IN testcat")
+    assert(catalogManager.currentCatalog.name() == "testcat")
+    assert(catalogManager.currentNamespace === Array("ns1", "ns1_1"))
+
+    // Catalog is resolved to 'testcat2'.
+    sql("USE testcat2.ns2.ns2_2")
 
 Review comment:
   image you have a namepace `abc` in the current catalog, and a catalog called 
`abc`, `USE NAMESPACE abc` can set current namespace to `abc` without ambiguity.
   
   Actually this reminds me that we don't need `USE CATALOG` as `USE 
catalogName` can set current catalog without ambiguity. So we can simplify it a 
bit more:
   ```
   USE NAMESPACE? multipartIdentifier
   ```
   ```
   def visitUse(ctx: UseContext): LogicalPlan = withOrigin(ctx) {
     val nameParts = visitMultipartIdentifier(ctx.multipartIdentifier)
     UseStatement(ctx.NAMESPACE != null, nameParts)
   }
   ...
   case class UseStatement(isSetNamespace: Boolean, nameParts: Seq[String])
   ```
   
   And the rule is simple:
   1. USE NAMESPACE only sets the current namespace
   2. USE sets the current catalog if the first name part is a catalog name. 
This doesn't change even if there is a namespace with the same name. Catalog 
has the highest priority during name resolution.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to