Skip to content
Prev Previous commit
Next Next commit
add more tests
  • Loading branch information
imback82 committed Oct 18, 2019
commit 08728d3bc2778b4ccd2a023bfa476b0fa61b4e83
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ case class CreateNamespaceExec(
private var properties: Map[String, String])
extends V2CommandExec {
override protected def run(): Seq[InternalRow] = {
val ns = namespace.toArray
if (ifNotExists && catalog.namespaceExists(ns)) {
throw new NamespaceAlreadyExistsException(ns)
if (ifNotExists && catalog.namespaceExists(namespace.toArray)) {
return Seq.empty
}

// Add any additional properties.
Expand All @@ -50,9 +49,7 @@ case class CreateNamespaceExec(
properties += ("locationSpec" -> CatalogUtils.stringToURI(locationSpec.get).toString)
}

// Note that even if ifNotExists is set to false, createNamespace can still throw
// NamespaceAlreadyExistsException depending on the SupportsNamespaces implementation.
catalog.createNamespace(ns, properties.asJava)
catalog.createNamespace(namespace.toArray, properties.asJava)

Seq.empty
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import scala.collection.mutable
import org.apache.spark.sql.{AnalysisException, Strategy}
import org.apache.spark.sql.catalyst.expressions.{And, AttributeReference, AttributeSet, Expression, PredicateHelper, SubqueryExpression}
import org.apache.spark.sql.catalyst.planning.PhysicalOperation
import org.apache.spark.sql.catalyst.plans.logical.{AlterTable, AppendData, CreateTableAsSelect, CreateV2Table, DeleteFromTable, DescribeTable, DropTable, LogicalPlan, OverwriteByExpression, OverwritePartitionsDynamic, Repartition, ReplaceTable, ReplaceTableAsSelect, SetCatalogAndNamespace, ShowNamespaces, ShowTables}
import org.apache.spark.sql.catalyst.plans.logical.{AlterTable, AppendData, CreateNamespace, CreateTableAsSelect, CreateV2Table, DeleteFromTable, DescribeTable, DropTable, LogicalPlan, OverwriteByExpression, OverwritePartitionsDynamic, Repartition, ReplaceTable, ReplaceTableAsSelect, SetCatalogAndNamespace, ShowNamespaces, ShowTables}
import org.apache.spark.sql.connector.catalog.{StagingTableCatalog, TableCapability}
import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsPushDownFilters, SupportsPushDownRequiredColumns}
import org.apache.spark.sql.connector.read.streaming.{ContinuousStream, MicroBatchStream}
Expand Down Expand Up @@ -289,6 +289,9 @@ object DataSourceV2Strategy extends Strategy with PredicateHelper {
case AlterTable(catalog, ident, _, changes) =>
AlterTableExec(catalog, ident, changes) :: Nil

case CreateNamespace(catalog, namespace, ifNotExists, comment, locationSpec, properties) =>
CreateNamespaceExec(catalog, namespace, ifNotExists, comment, locationSpec, properties) :: Nil

case r: ShowNamespaces =>
ShowNamespacesExec(r.output, r.catalog, r.namespace, r.pattern) :: Nil

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql.connector
import scala.collection.JavaConverters._

import org.apache.spark.sql._
import org.apache.spark.sql.catalyst.analysis.{CannotReplaceMissingTableException, NoSuchDatabaseException, NoSuchTableException, TableAlreadyExistsException}
import org.apache.spark.sql.catalyst.analysis.{CannotReplaceMissingTableException, NamespaceAlreadyExistsException, NoSuchDatabaseException, NoSuchTableException, TableAlreadyExistsException}
import org.apache.spark.sql.connector.catalog._
import org.apache.spark.sql.connector.catalog.CatalogManager.SESSION_CATALOG_NAME
import org.apache.spark.sql.internal.SQLConf
Expand Down Expand Up @@ -764,12 +764,30 @@ class DataSourceV2SQLSuite
assert(expected === df.collect())
}

test("CreateNameSpace: session catalog") {
test("CreateNameSpace: basic tests") {
// Session catalog is used.
sql("CREATE NAMESPACE ns")
testShowNamespaces("SHOW NAMESPACES", Seq("default", "ns"))

// V2 non-session catalog is used.
sql("CREATE NAMESPACE testcat.ns1.ns2")
testShowNamespaces("SHOW NAMESPACES testcat", Seq("ns1"))
testShowNamespaces("SHOW NAMESPACES IN testcat", Seq("ns1"))
testShowNamespaces("SHOW NAMESPACES IN testcat.ns1", Seq("ns1.ns2"))

// TODO: Add tests for validating namespace metadata when DESCRIBE NAMESPACE is available.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will create a JIRA for this and follow up.

}

test("CreateNameSpace: test handling of 'IF NOT EXIST'") {
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("ShowNamespaces: show root namespaces with default v2 catalog") {
Expand Down