Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -1829,23 +1829,6 @@ class DDLParserSuite extends AnalysisTest {
UnresolvedNamespace(Seq("a", "b", "c")), Map("b" -> "b")))
}

test("set namespace location") {
comparePlans(
parsePlan("ALTER DATABASE a.b.c SET LOCATION '/home/user/db'"),
SetNamespaceLocation(
UnresolvedNamespace(Seq("a", "b", "c")), "/home/user/db"))

comparePlans(
parsePlan("ALTER SCHEMA a.b.c SET LOCATION '/home/user/db'"),
SetNamespaceLocation(
UnresolvedNamespace(Seq("a", "b", "c")), "/home/user/db"))

comparePlans(
parsePlan("ALTER NAMESPACE a.b.c SET LOCATION '/home/user/db'"),
SetNamespaceLocation(
UnresolvedNamespace(Seq("a", "b", "c")), "/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 @@ -1284,22 +1284,6 @@ class DataSourceV2SQLSuite
}
}

test("ALTER NAMESPACE .. SET LOCATION using v2 catalog") {
withNamespace("testcat.ns1.ns2") {
sql("CREATE NAMESPACE IF NOT EXISTS testcat.ns1.ns2 COMMENT " +
"'test namespace' LOCATION '/tmp/ns_test_1'")
sql("ALTER NAMESPACE testcat.ns1.ns2 SET LOCATION '/tmp/ns_test_2'")
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, "/tmp/ns_test_2"),
Row(SupportsNamespaces.PROP_OWNER.capitalize, defaultUser),
Row("Properties", ""))
)
}
}

private def testShowNamespaces(
sqlText: String,
expected: Seq[String]): Unit = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.plans.logical.SetNamespaceLocation

class AlterNamespaceSetLocationParserSuite extends AnalysisTest {
test("set namespace location") {
comparePlans(
parsePlan("ALTER DATABASE a.b.c SET LOCATION '/home/user/db'"),
SetNamespaceLocation(
UnresolvedNamespace(Seq("a", "b", "c")), "/home/user/db"))

comparePlans(
parsePlan("ALTER SCHEMA a.b.c SET LOCATION '/home/user/db'"),
SetNamespaceLocation(
UnresolvedNamespace(Seq("a", "b", "c")), "/home/user/db"))

comparePlans(
parsePlan("ALTER NAMESPACE a.b.c SET LOCATION '/home/user/db'"),
SetNamespaceLocation(
UnresolvedNamespace(Seq("a", "b", "c")), "/home/user/db"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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}

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

protected def notFoundMsgPrefix: String

test("Namespace does not exist") {
val ns = "not_exist"
val message = intercept[AnalysisException] {
sql(s"ALTER DATABASE $catalog.$ns SET LOCATION 'loc'")
}.getMessage
assert(message.contains(s"$notFoundMsgPrefix '$ns' not found"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -778,18 +778,6 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
Row("Properties", "((a,a), (b,b), (c,c), (d,d))") :: Nil)

withTempDir { tmpDir =>
if (isUsingHiveMetastore) {
val e1 = intercept[AnalysisException] {
sql(s"ALTER DATABASE $dbName SET LOCATION '${tmpDir.toURI}'")
}
assert(e1.getMessage.contains("does not support altering database location"))
} else {
sql(s"ALTER DATABASE $dbName SET LOCATION '${tmpDir.toURI}'")
val uriInCatalog = catalog.getDatabaseMetadata(dbNameWithoutBackTicks).locationUri
assert("file" === uriInCatalog.getScheme)
assert(new Path(tmpDir.getPath).toUri.getPath === uriInCatalog.getPath)
}

intercept[NoSuchDatabaseException] {
sql(s"ALTER DATABASE `db-not-exist` SET LOCATION '${tmpDir.toURI}'")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.hadoop.fs.Path

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

/**
* This base suite contains unified tests for the `ALTER NAMESPACE ... SET LOCATION` 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.AlterNamespaceSetLocationSuite`
* - V1 Hive External catalog:
* `org.apache.spark.sql.hive.execution.command.AlterNamespaceSetLocationSuite`
*/
trait AlterNamespaceSetLocationSuiteBase extends command.AlterNamespaceSetLocationSuiteBase
with command.TestsV1AndV2Commands {
override def notFoundMsgPrefix: String = "Database"

test ("Empty location string") {
val ns = "db1"
withNamespace(ns) {
sql(s"CREATE NAMESPACE $catalog.$ns")
val message = intercept[IllegalArgumentException] {
sql(s"ALTER DATABASE $catalog.$ns SET LOCATION ''")
}.getMessage
assert(message.contains("Can not create a Path from an empty string"))
}
}
}

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

test("basic v1 test") {
val ns = "db1"
withNamespace(ns) {
sql(s"CREATE NAMESPACE $catalog.$ns")
withTempDir { tmpDir =>
sql(s"ALTER NAMESPACE $catalog.$ns SET LOCATION '${tmpDir.toURI}'")
val sessionCatalog = spark.sessionState.catalog
val uriInCatalog = sessionCatalog.getDatabaseMetadata(ns).locationUri
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cloud-fan This check is used in v1 session catalog (existing code), but this can be unified with v2 test if we use DESCRIBE NAMESPACE, WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

yea let's unify it, so that we can move this test to the base trait.

assert("file" === uriInCatalog.getScheme)
assert(new Path(tmpDir.getPath).toUri.getPath === uriInCatalog.getPath)
}
}
}
}
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.v2

import org.apache.spark.sql.Row
import org.apache.spark.sql.connector.catalog.SupportsNamespaces
import org.apache.spark.sql.execution.command
import org.apache.spark.util.Utils

/**
* The class contains tests for the `ALTER NAMESPACE ... SET LOCATION` command to check V2 table
* catalogs.
*/
class AlterNamespaceSetLocationSuite extends command.AlterNamespaceSetLocationSuiteBase
with CommandSuiteBase {
override def notFoundMsgPrefix: String = "Namespace"

test("basic v2 test") {
val ns = "ns1.ns2"
withNamespace(s"$catalog.$ns") {
sql(s"CREATE NAMESPACE IF NOT EXISTS $catalog.$ns COMMENT " +
"'test namespace' LOCATION '/tmp/ns_test_1'")
sql(s"ALTER NAMESPACE $catalog.$ns SET LOCATION '/tmp/ns_test_2'")
val descriptionDf = sql(s"DESCRIBE NAMESPACE EXTENDED $catalog.$ns")
assert(descriptionDf.collect() === Seq(
Row("Namespace Name", "ns2"),
Row(SupportsNamespaces.PROP_COMMENT.capitalize, "test namespace"),
Row(SupportsNamespaces.PROP_LOCATION.capitalize, "/tmp/ns_test_2"),
Row(SupportsNamespaces.PROP_OWNER.capitalize, Utils.getCurrentUserName()),
Row("Properties", ""))
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.hive.execution.command

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

/**
* The class contains tests for the `ALTER NAMESPACE ... SET LOCATION` command to check
* V1 Hive external table catalog.
*/
class AlterNamespaceSetLocationSuite extends v1.AlterNamespaceSetLocationSuiteBase
with CommandSuiteBase {
override def commandVersion: String = super[AlterNamespaceSetLocationSuiteBase].commandVersion

test("Hive catalog not supported") {
val ns = "db1"
withNamespace(ns) {
sql(s"CREATE NAMESPACE $ns")
val e = intercept[AnalysisException] {
sql(s"ALTER DATABASE $ns SET LOCATION 'loc'")
}
assert(e.getMessage.contains("does not support altering database location"))
}
}
}