imback82 commented on a change in pull request #35093:
URL: https://github.com/apache/spark/pull/35093#discussion_r777856554



##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/execution/command/CreateNamespaceSuiteBase.scala
##########
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.command
+
+import scala.collection.JavaConverters._
+
+import org.apache.hadoop.fs.Path
+
+import org.apache.spark.sql.{AnalysisException, QueryTest}
+import org.apache.spark.sql.catalyst.parser.ParseException
+import org.apache.spark.sql.connector.catalog.{CatalogPlugin, CatalogV2Util, 
SupportsNamespaces}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * This base suite contains unified tests for the `CREATE NAMESPACE` command 
that check V1 and V2
+ * table catalogs. The tests that cannot run for all supported catalogs are 
located in more
+ * specific test suites:
+ *
+ *   - V2 table catalog tests: 
`org.apache.spark.sql.execution.command.v2.CreateNamespaceSuite`
+ *   - V1 table catalog tests:
+ *     `org.apache.spark.sql.execution.command.v1.CreateNamespaceSuiteBase`
+ *     - V1 In-Memory catalog: 
`org.apache.spark.sql.execution.command.v1.CreateNamespaceSuite`
+ *     - V1 Hive External catalog:
+*        `org.apache.spark.sql.hive.execution.command.CreateNamespaceSuite`
+ */
+trait CreateNamespaceSuiteBase extends QueryTest with DDLCommandTestUtils {
+  import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+
+  override val command = "Create NAMESPACE"
+
+  protected def namespace: String
+
+  protected def namespaceArray: Array[String] = namespace.split('.')
+
+  protected def notFoundMsgPrefix: String
+
+  protected def alreadyExistErrorMessage: String = s"$notFoundMsgPrefix 
'$namespace' already exists"
+
+  test("basic") {
+    val ns = s"$catalog.$namespace"
+    withNamespace(ns) {
+      sql(s"CREATE NAMESPACE $ns")
+      
assert(getCatalog(catalog).asNamespaceCatalog.namespaceExists(namespaceArray))
+    }
+  }
+
+  test("namespace with location") {
+    val ns = s"$catalog.$namespace"
+    withNamespace(ns) {
+      withTempDir { tmpDir =>
+        // The generated temp path is not qualified.
+        val path = tmpDir.getCanonicalPath
+        assert(!path.startsWith("file:/"))
+
+        val e = intercept[IllegalArgumentException] {
+          sql(s"CREATE NAMESPACE $ns Location ''")
+        }
+        assert(e.getMessage.contains("Can not create a Path from an empty 
string"))
+
+        val uri = new Path(path).toUri
+        sql(s"CREATE NAMESPACE $ns Location '$uri'")
+
+        // Make sure the location is qualified.
+        val expected = makeQualifiedPath(tmpDir.toString)
+        assert("file" === expected.getScheme)
+        assert(new Path(getNamespaceLocation(catalog, namespaceArray)).toUri 
=== expected)
+      }
+    }
+  }
+
+  test("Namespace already exists") {
+    val ns = s"$catalog.$namespace"
+    withNamespace(ns) {
+      sql(s"CREATE NAMESPACE $ns")
+
+      // TODO: HiveExternalCatalog throws DatabaseAlreadyExistsException.
+      val e = intercept[AnalysisException] {
+        sql(s"CREATE NAMESPACE $ns")
+      }
+      assert(e.getMessage.contains(alreadyExistErrorMessage))
+    }
+  }
+
+  test("test handling of 'IF NOT EXIST'") {
+    val ns = s"$catalog.$namespace"
+    withNamespace(ns) {
+      sql(s"CREATE NAMESPACE IF NOT EXISTS $ns")
+
+      // The namespace already exists, so this should fail.
+      // TODO: non-Hive catalogs throw NamespaceAlreadyExistsException.

Review comment:
       This is a new TODO similar to `// TODO: HiveExternalCatalog throws 
DatabaseAlreadyExistsException.` Basically, hive catalog throws a different 
exception (and a slightly different message, requiring 
`alreadyExistErrorMessage`).

##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala
##########
@@ -1093,93 +1093,6 @@ class DataSourceV2SQLSuite
       " only SessionCatalog supports this command."))
   }
 
-  test("CreateNameSpace: basic tests") {
-    // Session catalog is used.
-    withNamespace("ns") {
-      sql("CREATE NAMESPACE ns")
-      testShowNamespaces("SHOW NAMESPACES", Seq("default", "ns"))
-    }
-
-    // V2 non-session catalog is used.
-    withNamespace("testcat.ns1.ns2") {
-      sql("CREATE NAMESPACE testcat.ns1.ns2")
-      testShowNamespaces("SHOW NAMESPACES IN testcat", Seq("ns1"))
-      testShowNamespaces("SHOW NAMESPACES IN testcat.ns1", Seq("ns1.ns2"))
-    }
-
-    withNamespace("testcat.test") {
-      withTempDir { tmpDir =>
-        val path = tmpDir.getCanonicalPath
-        sql(s"CREATE NAMESPACE testcat.test LOCATION '$path'")
-        val metadata =
-          
catalog("testcat").asNamespaceCatalog.loadNamespaceMetadata(Array("test")).asScala
-        val catalogPath = metadata(SupportsNamespaces.PROP_LOCATION)
-        assert(catalogPath.equals(catalogPath))
-      }
-    }
-  }
-
-  test("CreateNameSpace: test handling of 'IF NOT EXIST'") {
-    withNamespace("testcat.ns1") {
-      sql("CREATE NAMESPACE IF NOT EXISTS testcat.ns1")
-
-      // The 'ns1' namespace already exists, so this should fail.
-      val exception = intercept[NamespaceAlreadyExistsException] {
-        sql("CREATE NAMESPACE testcat.ns1")
-      }
-      assert(exception.getMessage.contains("Namespace 'ns1' already exists"))
-
-      // The following will be no-op since the namespace already exists.
-      sql("CREATE NAMESPACE IF NOT EXISTS testcat.ns1")
-    }
-  }
-
-  test("CreateNameSpace: reserved properties") {
-    import SupportsNamespaces._
-    withSQLConf((SQLConf.LEGACY_PROPERTY_NON_RESERVED.key, "false")) {
-      CatalogV2Util.NAMESPACE_RESERVED_PROPERTIES.filterNot(_ == 
PROP_COMMENT).foreach { key =>
-        val exception = intercept[ParseException] {
-          sql(s"CREATE NAMESPACE testcat.reservedTest WITH 
DBPROPERTIES('$key'='dummyVal')")
-        }
-        assert(exception.getMessage.contains(s"$key is a reserved namespace 
property"))
-      }
-    }
-    withSQLConf((SQLConf.LEGACY_PROPERTY_NON_RESERVED.key, "true")) {
-      CatalogV2Util.NAMESPACE_RESERVED_PROPERTIES.filterNot(_ == 
PROP_COMMENT).foreach { key =>
-        withNamespace("testcat.reservedTest") {
-          sql(s"CREATE NAMESPACE testcat.reservedTest WITH 
DBPROPERTIES('$key'='foo')")
-          assert(sql("DESC NAMESPACE EXTENDED testcat.reservedTest")
-            .toDF("k", "v")
-            .where("k='Properties'")
-            .where("v=''")
-            .count == 1, s"$key is a reserved namespace property and ignored")
-          val meta =
-            
catalog("testcat").asNamespaceCatalog.loadNamespaceMetadata(Array("reservedTest"))
-          assert(meta.get(key) == null || !meta.get(key).contains("foo"),
-            "reserved properties should not have side effects")
-        }
-      }
-    }
-  }
-
-  test("SPARK-37456: Location in CreateNamespace should be qualified") {

Review comment:
       This should be covered in `test("namespace with location")` for all 
catalogs.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



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

Reply via email to