Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 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 @@ -1770,38 +1770,6 @@ class DDLParserSuite extends AnalysisTest {
"location" -> "/home/user/db")))
}

test("set namespace properties") {
comparePlans(
parsePlan("ALTER DATABASE a.b.c SET PROPERTIES ('a'='a', 'b'='b', 'c'='c')"),
SetNamespaceProperties(
UnresolvedNamespace(Seq("a", "b", "c")), Map("a" -> "a", "b" -> "b", "c" -> "c")))

comparePlans(
parsePlan("ALTER SCHEMA a.b.c SET PROPERTIES ('a'='a')"),
SetNamespaceProperties(
UnresolvedNamespace(Seq("a", "b", "c")), Map("a" -> "a")))

comparePlans(
parsePlan("ALTER NAMESPACE a.b.c SET PROPERTIES ('b'='b')"),
SetNamespaceProperties(
UnresolvedNamespace(Seq("a", "b", "c")), Map("b" -> "b")))

comparePlans(
parsePlan("ALTER DATABASE a.b.c SET DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')"),
SetNamespaceProperties(
UnresolvedNamespace(Seq("a", "b", "c")), Map("a" -> "a", "b" -> "b", "c" -> "c")))

comparePlans(
parsePlan("ALTER SCHEMA a.b.c SET DBPROPERTIES ('a'='a')"),
SetNamespaceProperties(
UnresolvedNamespace(Seq("a", "b", "c")), Map("a" -> "a")))

comparePlans(
parsePlan("ALTER NAMESPACE a.b.c SET DBPROPERTIES ('b'='b')"),
SetNamespaceProperties(
UnresolvedNamespace(Seq("a", "b", "c")), Map("b" -> "b")))
}

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 @@ -1224,54 +1224,6 @@ class DataSourceV2SQLSuite
}
}

test("ALTER NAMESPACE .. SET PROPERTIES using v2 catalog") {
withNamespace("testcat.ns1.ns2") {
sql("CREATE NAMESPACE IF NOT EXISTS testcat.ns1.ns2 COMMENT " +
"'test namespace' LOCATION '/tmp/ns_test' WITH PROPERTIES ('a'='a','b'='b','c'='c')")
sql("ALTER NAMESPACE testcat.ns1.ns2 SET PROPERTIES ('a'='b','b'='a')")
val descriptionDf = sql("DESCRIBE NAMESPACE EXTENDED testcat.ns1.ns2")
assert(descriptionDf.collect() === Seq(
Row("Namespace Name", "ns2"),
Row(SupportsNamespaces.PROP_COMMENT.capitalize, "test namespace"),
Row(SupportsNamespaces.PROP_LOCATION.capitalize, "file:/tmp/ns_test"),
Row(SupportsNamespaces.PROP_OWNER.capitalize, defaultUser),
Row("Properties", "((a,b), (b,a), (c,c))"))
)
}
}

test("ALTER NAMESPACE .. SET PROPERTIES reserved properties") {
import SupportsNamespaces._
withSQLConf((SQLConf.LEGACY_PROPERTY_NON_RESERVED.key, "false")) {
CatalogV2Util.NAMESPACE_RESERVED_PROPERTIES.filterNot(_ == PROP_COMMENT).foreach { key =>
withNamespace("testcat.reservedTest") {
sql("CREATE NAMESPACE testcat.reservedTest")
val exception = intercept[ParseException] {
sql(s"ALTER NAMESPACE testcat.reservedTest SET PROPERTIES ('$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")
sql(s"ALTER NAMESPACE testcat.reservedTest SET PROPERTIES ('$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("ALTER NAMESPACE .. SET LOCATION using v2 catalog") {
withNamespace("testcat.ns1.ns2") {
sql("CREATE NAMESPACE IF NOT EXISTS testcat.ns1.ns2 COMMENT " +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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, UnresolvedNamespace}
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan
import org.apache.spark.sql.catalyst.parser.ParseException
import org.apache.spark.sql.catalyst.plans.logical.SetNamespaceProperties

class AlterNamespaceSetPropertiesParserSuite extends AnalysisTest {
test("set namespace properties") {
Seq("DATABASE", "SCHEMA", "NAMESPACE").foreach { nsToken =>
Seq("PROPERTIES", "DBPROPERTIES").foreach { propToken =>
comparePlans(
parsePlan(s"ALTER $nsToken a.b.c SET $propToken ('a'='a', 'b'='b', 'c'='c')"),
SetNamespaceProperties(
UnresolvedNamespace(Seq("a", "b", "c")), Map("a" -> "a", "b" -> "b", "c" -> "c")))

comparePlans(
parsePlan(s"ALTER $nsToken a.b.c SET $propToken ('a'='a')"),
SetNamespaceProperties(
UnresolvedNamespace(Seq("a", "b", "c")), Map("a" -> "a")))
}
}
}

test("property values must be set") {
val e = intercept[ParseException] {
parsePlan("ALTER NAMESPACE my_db SET PROPERTIES('key_without_value', 'key_with_value'='x')")
}
assert(e.getMessage.contains(
"Operation not allowed: Values must be specified for key(s): [key_without_value]"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.{AnalysisException, QueryTest}
import org.apache.spark.sql.catalyst.parser.ParseException
import org.apache.spark.sql.connector.catalog.{CatalogV2Util, SupportsNamespaces}
import org.apache.spark.sql.internal.SQLConf

/**
* This base suite contains unified tests for the `ALTER NAMESPACE ... SET PROPERTIES` 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.AlterNamespaceSetPropertiesSuite`
* - V1 table catalog tests:
* `org.apache.spark.sql.execution.command.v1.AlterNamespaceSetPropertiesSuiteBase`
* - V1 In-Memory catalog:
* `org.apache.spark.sql.execution.command.v1.AlterNamespaceSetPropertiesSuite`
* - V1 Hive External catalog:
* `org.apache.spark.sql.hive.execution.command.AlterNamespaceSetPropertiesSuite`
*/
trait AlterNamespaceSetPropertiesSuiteBase extends QueryTest with DDLCommandTestUtils {
override val command = "ALTER NAMESPACE ... SET PROPERTIES"

protected def namespace: String

protected def notFoundMsgPrefix: String

test("Namespace does not exist") {
val ns = "not_exist"
val message = intercept[AnalysisException] {
sql(s"ALTER DATABASE $catalog.$ns SET PROPERTIES ('d'='d')")
}.getMessage
assert(message.contains(s"$notFoundMsgPrefix '$ns' not found"))
}

test("basic test") {
val ns = s"$catalog.$namespace"
withNamespace(ns) {
sql(s"CREATE NAMESPACE $ns")
assert(getProperties(ns) === "")
sql(s"ALTER NAMESPACE $ns SET PROPERTIES ('a'='a', 'b'='b', 'c'='c')")
assert(getProperties(ns) === "((a,a), (b,b), (c,c))")
sql(s"ALTER DATABASE $ns SET PROPERTIES ('d'='d')")
assert(getProperties(ns) === "((a,a), (b,b), (c,c), (d,d))")
Comment thread
imback82 marked this conversation as resolved.
}
}

test("test with properties set while creating namespace") {
val ns = s"$catalog.$namespace"
withNamespace(ns) {
sql(s"CREATE NAMESPACE $ns WITH PROPERTIES ('a'='a','b'='b','c'='c')")
assert(getProperties(ns) === "((a,a), (b,b), (c,c))")
sql(s"ALTER NAMESPACE $ns SET PROPERTIES ('a'='b', 'b'='a')")
assert(getProperties(ns) === "((a,b), (b,a), (c,c))")
}
}

test("test reserved properties") {
import SupportsNamespaces._
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
val ns = s"$catalog.$namespace"
withSQLConf((SQLConf.LEGACY_PROPERTY_NON_RESERVED.key, "false")) {
CatalogV2Util.NAMESPACE_RESERVED_PROPERTIES.filterNot(_ == PROP_COMMENT).foreach { key =>
withNamespace(ns) {
sql(s"CREATE NAMESPACE $ns")
val exception = intercept[ParseException] {
sql(s"ALTER NAMESPACE $ns SET PROPERTIES ('$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(ns) {
sql(s"CREATE NAMESPACE $ns")
assert(getProperties(ns) === "")
sql(s"ALTER NAMESPACE $ns SET PROPERTIES ('$key'='foo')")
assert(getProperties(ns) === "", s"$key is a reserved namespace property and ignored")
val meta = spark.sessionState.catalogManager.catalog(catalog)
.asNamespaceCatalog.loadNamespaceMetadata(namespace.split('.'))
assert(meta.get(key) == null || !meta.get(key).contains("foo"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this a behavior difference between v1 and v2? null vs empty string

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was taken from DataSourceV2SQLSuite.scala, but further looking into this, there seems to be a difference.

In the above loop, the key will be either location or owner, and loadNamespaceMetadata returns the following:

key v1 catalog v2 catalog
location non-null null
owner null non-null

I think the null case is interesting.

  1. v2 catalog returns null for location because CREATE NAMESPACE doesn't create a default location if not specified, where as v1 catalog creates a default location. This is expected for v2 catalog, right?
  2. v1 catalog returns null for owner since the following doesn't set owner to metadata:
    def toMetadata: util.Map[String, String] = {
    val metadata = mutable.HashMap[String, String]()
    catalogDatabase.properties.foreach {
    case (key, value) => metadata.put(key, value)
    }
    metadata.put(SupportsNamespaces.PROP_LOCATION, catalogDatabase.locationUri.toString)
    metadata.put(SupportsNamespaces.PROP_COMMENT, catalogDatabase.description)
    . Do you know if this was intentional?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For the location, I think it's OK. The catalog implementation should decide the default location (or even no location if the source is not file-based). We should accept this difference.

For the owner, it seems a bug that V2SessionCatalog does not propagate the owner field.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For the owner, the difference comes because v2 command always adds the owner when the namespace is created:

val ownership =
Map(PROP_OWNER -> Utils.getCurrentUserName())
catalog.createNamespace(ns, (properties ++ ownership).asJava)

, whereas for v1 command doesn't add the owner when the database is created.

Instead, for v1 Hive catalog, the user property is inserted when the database is retrieved:

override def getDatabase(dbName: String): CatalogDatabase = withHiveState {
Option(shim.getDatabase(client, dbName)).map { d =>
val params = Option(d.getParameters).map(_.asScala.toMap).getOrElse(Map()) ++
Map(PROP_OWNER -> shim.getDatabaseOwnerName(d))

Meanwhile, the v1 in-memory catalog implementation doesn't add the owner when the database is retrieved, so we see null owner above. (and the owner is a part of the property, so updating V2SessionCatalog doesn't really address the issue).

One thing we can do is to update the v1 in-memory catalog to add the owner when the database is created or retrieved, but it is still not consistent since adding the owner is a responsibility of the command in v2, but a responsibility of the catalog in v1. Any thoughts?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It may be too risky to change the v1 behavior now (Hive metastore fills the owner field). Let's just update the v1 in-memory catalog to fill the owner field as well.

@yaooqinn which one do you think should set the owner field? Spark or the underlying catalog?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

for v1 and v2 database and table creation, we both respect the sparkUser now

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

for alter properties and if it's not an explicitly ower change, we shall respect the catalog settings

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added an owner while creating a database in InMemoryCatalog, explicitly added a location while creating a namespace in the test (to handle the difference for v2 catalog), and removed the meta.get(key) == null check.

"reserved properties should not have side effects")
}
}
}
}

protected def getProperties(namespace: String): String = {
val propsRow = sql(s"DESCRIBE NAMESPACE EXTENDED $namespace")
.toDF("key", "value")
.where(s"key like 'Properties%'")
.collect()
assert(propsRow.length == 1)
propsRow(0).getString(1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ class DDLParserSuite extends AnalysisTest with SharedSparkSession {
ShowCurrentNamespaceCommand())
}

test("alter database - property values must be set") {
assertUnsupported(
sql = "ALTER DATABASE my_db SET DBPROPERTIES('key_without_value', 'key_with_value'='x')",
containsThesePhrases = Seq("key_without_value"))
}

test("insert overwrite directory") {
val v1 = "INSERT OVERWRITE DIRECTORY '/tmp/file' USING parquet SELECT 1 as a"
parser.parsePlan(v1) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
}
}

test("Alter/Describe Database") {
test("Describe Database") {
val catalog = spark.sessionState.catalog
val databaseNames = Seq("db1", "`database`")

Expand All @@ -760,9 +760,7 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
val dbNameWithoutBackTicks = cleanIdentifier(dbName)
val location = getDBPath(dbNameWithoutBackTicks)

sql(s"CREATE DATABASE $dbName")

sql(s"ALTER DATABASE $dbName SET DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')")
sql(s"CREATE DATABASE $dbName WITH PROPERTIES ('a'='a', 'b'='b', 'c'='c')")

checkAnswer(
sql(s"DESCRIBE DATABASE EXTENDED $dbName").toDF("key", "value")
Expand All @@ -771,36 +769,12 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
Row("Comment", "") ::
Row("Location", CatalogUtils.URIToString(location)) ::
Row("Properties", "((a,a), (b,b), (c,c))") :: Nil)

sql(s"ALTER DATABASE $dbName SET DBPROPERTIES ('d'='d')")

checkAnswer(
sql(s"DESCRIBE DATABASE EXTENDED $dbName").toDF("key", "value")
.where("key not like 'Owner%'"), // filter for consistency with in-memory catalog
Row("Namespace Name", dbNameWithoutBackTicks) ::
Row("Comment", "") ::
Row("Location", CatalogUtils.URIToString(location)) ::
Row("Properties", "((a,a), (b,b), (c,c), (d,d))") :: Nil)
} finally {
catalog.reset()
}
}
}

test("Alter Database - database does not exists") {
val databaseNames = Seq("db1", "`database`")

databaseNames.foreach { dbName =>
val dbNameWithoutBackTicks = cleanIdentifier(dbName)
assert(!spark.sessionState.catalog.databaseExists(dbNameWithoutBackTicks))

val message = intercept[AnalysisException] {
sql(s"ALTER DATABASE $dbName SET DBPROPERTIES ('d'='d')")
}.getMessage
assert(message.contains(s"Database '$dbNameWithoutBackTicks' not found"))
}
}

test("create table in default db") {
val catalog = spark.sessionState.catalog
val tableIdent1 = TableIdentifier("tab1", None)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.v1

import org.apache.spark.sql.execution.command

/**
* This base suite contains unified tests for the `ALTER NAMESPACE ... SET PROPERTIES` command that
* checks V1 table catalogs. The tests that cannot run for all V1 catalogs are located in more
* specific test suites:
*
* - V1 In-Memory catalog:
* `org.apache.spark.sql.execution.command.v1.AlterNamespaceSetPropertiesSuite`
* - V1 Hive External catalog:
* `org.apache.spark.sql.hive.execution.command.AlterNamespaceSetPropertiesSuite`
*/
trait AlterNamespaceSetPropertiesSuiteBase extends command.AlterNamespaceSetPropertiesSuiteBase
with command.TestsV1AndV2Commands {
override def namespace: String = "db"
override def notFoundMsgPrefix: String = "Database"
}

/**
* The class contains tests for the `ALTER NAMESPACE ... SET PROPERTIES` command to
* check V1 In-Memory table catalog.
*/
class AlterNamespaceSetPropertiesSuite extends AlterNamespaceSetPropertiesSuiteBase
with CommandSuiteBase {
override def commandVersion: String = super[AlterNamespaceSetPropertiesSuiteBase].commandVersion
}
Loading