Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1686,90 +1686,6 @@ class DDLParserSuite extends AnalysisTest {
ShowViews(UnresolvedNamespace(Seq("ns1")), Some("*test*")))
}

test("create namespace -- backward compatibility with DATABASE/DBPROPERTIES") {
val expected = CreateNamespace(
UnresolvedDBObjectName(Seq("a", "b", "c"), true),
ifNotExists = true,
Map(
"a" -> "a",
"b" -> "b",
"c" -> "c",
"comment" -> "namespace_comment",
"location" -> "/home/user/db"))

comparePlans(
parsePlan(
"""
|CREATE NAMESPACE IF NOT EXISTS a.b.c
|WITH PROPERTIES ('a'='a', 'b'='b', 'c'='c')
|COMMENT 'namespace_comment' LOCATION '/home/user/db'
""".stripMargin),
expected)

comparePlans(
parsePlan(
"""
|CREATE DATABASE IF NOT EXISTS a.b.c
|WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')
|COMMENT 'namespace_comment' LOCATION '/home/user/db'
""".stripMargin),
expected)
}

test("create namespace -- check duplicates") {
def createDatabase(duplicateClause: String): String = {
s"""
|CREATE NAMESPACE IF NOT EXISTS a.b.c
|$duplicateClause
|$duplicateClause
""".stripMargin
}
val sql1 = createDatabase("COMMENT 'namespace_comment'")
val sql2 = createDatabase("LOCATION '/home/user/db'")
val sql3 = createDatabase("WITH PROPERTIES ('a'='a', 'b'='b', 'c'='c')")
val sql4 = createDatabase("WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')")

intercept(sql1, "Found duplicate clauses: COMMENT")
intercept(sql2, "Found duplicate clauses: LOCATION")
intercept(sql3, "Found duplicate clauses: WITH PROPERTIES")
intercept(sql4, "Found duplicate clauses: WITH DBPROPERTIES")
}

test("create namespace - property values must be set") {
assertUnsupported(
sql = "CREATE NAMESPACE a.b.c WITH PROPERTIES('key_without_value', 'key_with_value'='x')",
containsThesePhrases = Seq("key_without_value"))
}

test("create namespace -- either PROPERTIES or DBPROPERTIES is allowed") {
val sql =
s"""
|CREATE NAMESPACE IF NOT EXISTS a.b.c
|WITH PROPERTIES ('a'='a', 'b'='b', 'c'='c')
|WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')
""".stripMargin
intercept(sql, "Either PROPERTIES or DBPROPERTIES is allowed")
}

test("create namespace - support for other types in PROPERTIES") {
val sql =
"""
|CREATE NAMESPACE a.b.c
|LOCATION '/home/user/db'
|WITH PROPERTIES ('a'=1, 'b'=0.1, 'c'=TRUE)
""".stripMargin
comparePlans(
parsePlan(sql),
CreateNamespace(
UnresolvedDBObjectName(Seq("a", "b", "c"), true),
ifNotExists = false,
Map(
"a" -> "1",
"b" -> "0.1",
"c" -> "true",
"location" -> "/home/user/db")))
}

test("analyze table statistics") {
comparePlans(parsePlan("analyze table a.b.c compute statistics"),
AnalyzeTable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import scala.collection.JavaConverters._

import org.apache.spark.sql._
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.{CannotReplaceMissingTableException, NamespaceAlreadyExistsException, NoSuchDatabaseException, NoSuchNamespaceException, TableAlreadyExistsException}
import org.apache.spark.sql.catalyst.analysis.{CannotReplaceMissingTableException, NoSuchDatabaseException, NoSuchNamespaceException, TableAlreadyExistsException}
import org.apache.spark.sql.catalyst.parser.ParseException
import org.apache.spark.sql.catalyst.util.DateTimeUtils
import org.apache.spark.sql.connector.catalog._
Expand Down Expand Up @@ -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") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

withNamespace("testcat.ns1.ns2") {
val e = intercept[IllegalArgumentException] {
sql("CREATE NAMESPACE testcat.ns1.ns2 LOCATION ''")
}
assert(e.getMessage.contains("Can not create a Path from an empty string"))

sql("CREATE NAMESPACE testcat.ns1.ns2 LOCATION '/tmp/ns_test'")
val descriptionDf = sql("DESCRIBE NAMESPACE EXTENDED testcat.ns1.ns2")
assert(descriptionDf.collect() === Seq(
Row("Namespace Name", "ns2"),
Row(SupportsNamespaces.PROP_LOCATION.capitalize, "file:/tmp/ns_test"),
Row(SupportsNamespaces.PROP_OWNER.capitalize, defaultUser),
Row("Properties", ""))
)
}
}

test("create/replace/alter table - reserved properties") {
import TableCatalog._
withSQLConf((SQLConf.LEGACY_PROPERTY_NON_RESERVED.key, "false")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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 org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedDBObjectName}
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan
import org.apache.spark.sql.catalyst.plans.logical.CreateNamespace

class CreateNamespaceParserSuite extends AnalysisTest {
test("create namespace -- backward compatibility with DATABASE/DBPROPERTIES") {
val expected = CreateNamespace(
UnresolvedDBObjectName(Seq("a", "b", "c"), true),
ifNotExists = true,
Map(
"a" -> "a",
"b" -> "b",
"c" -> "c",
"comment" -> "namespace_comment",
"location" -> "/home/user/db"))

comparePlans(
parsePlan(
"""
|CREATE NAMESPACE IF NOT EXISTS a.b.c
|WITH PROPERTIES ('a'='a', 'b'='b', 'c'='c')
|COMMENT 'namespace_comment' LOCATION '/home/user/db'
""".stripMargin),
expected)

comparePlans(
parsePlan(
"""
|CREATE DATABASE IF NOT EXISTS a.b.c
|WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')
|COMMENT 'namespace_comment' LOCATION '/home/user/db'
""".stripMargin),
expected)
}

test("create namespace -- check duplicates") {
def createNamespace(duplicateClause: String): String = {
s"""
|CREATE NAMESPACE IF NOT EXISTS a.b.c
|$duplicateClause
|$duplicateClause
""".stripMargin
}
val sql1 = createNamespace("COMMENT 'namespace_comment'")
val sql2 = createNamespace("LOCATION '/home/user/db'")
val sql3 = createNamespace("WITH PROPERTIES ('a'='a', 'b'='b', 'c'='c')")
val sql4 = createNamespace("WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')")

intercept(sql1, "Found duplicate clauses: COMMENT")
intercept(sql2, "Found duplicate clauses: LOCATION")
intercept(sql3, "Found duplicate clauses: WITH PROPERTIES")
intercept(sql4, "Found duplicate clauses: WITH DBPROPERTIES")
}

test("create namespace - property values must be set") {
intercept(
"CREATE NAMESPACE a.b.c WITH PROPERTIES('key_without_value', 'key_with_value'='x')",
"Operation not allowed: Values must be specified for key(s): [key_without_value]")
}

test("create namespace -- either PROPERTIES or DBPROPERTIES is allowed") {
val sql =
s"""
|CREATE NAMESPACE IF NOT EXISTS a.b.c
|WITH PROPERTIES ('a'='a', 'b'='b', 'c'='c')
|WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')
""".stripMargin
intercept(sql, "Either PROPERTIES or DBPROPERTIES is allowed")
}

test("create namespace - support for other types in PROPERTIES") {
val sql =
"""
|CREATE NAMESPACE a.b.c
|LOCATION '/home/user/db'
|WITH PROPERTIES ('a'=1, 'b'=0.1, 'c'=TRUE)
""".stripMargin
comparePlans(
parsePlan(sql),
CreateNamespace(
UnresolvedDBObjectName(Seq("a", "b", "c"), true),
ifNotExists = false,
Map(
"a" -> "1",
"b" -> "0.1",
"c" -> "true",
"location" -> "/home/user/db")))
}

private def intercept(sqlCommand: String, messages: String*): Unit =
interceptParseException(parsePlan)(sqlCommand, messages: _*)
}
Loading