Skip to content

Commit 6df9efd

Browse files
committed
address PR comments
1 parent 4629166 commit 6df9efd

File tree

5 files changed

+46
-48
lines changed

5 files changed

+46
-48
lines changed

sql/core/src/test/scala/org/apache/spark/sql/execution/command/AlterNamespaceSetLocationSuiteBase.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.spark.sql.execution.command
1919

2020
import org.apache.spark.sql.{AnalysisException, QueryTest}
21+
import org.apache.spark.sql.connector.catalog.SupportsNamespaces
2122

2223
/**
2324
* This base suite contains unified tests for the `ALTER NAMESPACE ... SET LOCATION` command that
@@ -36,6 +37,8 @@ import org.apache.spark.sql.{AnalysisException, QueryTest}
3637
trait AlterNamespaceSetLocationSuiteBase extends QueryTest with DDLCommandTestUtils {
3738
override val command = "ALTER NAMESPACE ... SET LOCATION"
3839

40+
protected def namespace: String
41+
3942
protected def notFoundMsgPrefix: String
4043

4144
test("Namespace does not exist") {
@@ -45,4 +48,27 @@ trait AlterNamespaceSetLocationSuiteBase extends QueryTest with DDLCommandTestUt
4548
}.getMessage
4649
assert(message.contains(s"$notFoundMsgPrefix '$ns' not found"))
4750
}
51+
52+
// Hive catalog does not support "ALTER NAMESPACE ... SET LOCATION", thus
53+
// this is called from non-Hive v1 and v2 tests.
54+
protected def runBasicTest(): Unit = {
55+
val ns = s"$catalog.$namespace"
56+
withNamespace(ns) {
57+
sql(s"CREATE NAMESPACE IF NOT EXISTS $ns COMMENT " +
58+
"'test namespace' LOCATION '/tmp/loc_test_1'")
59+
sql(s"ALTER NAMESPACE $ns SET LOCATION '/tmp/loc_test_2'")
60+
// v1 command stores the location as URI ("file:/tmp/loc_test_2") whereas
61+
// v2 command stores the location as string ("/tmp/loc_test_2").
62+
assert(getLocation(ns).contains("/tmp/loc_test_2"))
63+
}
64+
}
65+
66+
protected def getLocation(namespace: String): String = {
67+
val locationRow = sql(s"DESCRIBE NAMESPACE EXTENDED $namespace")
68+
.toDF("key", "value")
69+
.where(s"key like '${SupportsNamespaces.PROP_LOCATION.capitalize}%'")
70+
.collect()
71+
assert(locationRow.length == 1)
72+
locationRow(0).getString(1)
73+
}
4874
}

sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.apache.spark.{SparkException, SparkFiles}
2828
import org.apache.spark.internal.config
2929
import org.apache.spark.sql.{AnalysisException, QueryTest, Row, SaveMode}
3030
import org.apache.spark.sql.catalyst.{FunctionIdentifier, QualifiedTableName, TableIdentifier}
31-
import org.apache.spark.sql.catalyst.analysis.{FunctionRegistry, NoSuchDatabaseException, NoSuchFunctionException, TableFunctionRegistry, TempTableAlreadyExistsException}
31+
import org.apache.spark.sql.catalyst.analysis.{FunctionRegistry, NoSuchFunctionException, TableFunctionRegistry, TempTableAlreadyExistsException}
3232
import org.apache.spark.sql.catalyst.catalog._
3333
import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
3434
import org.apache.spark.sql.connector.catalog.SupportsNamespaces.PROP_OWNER
@@ -776,17 +776,6 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
776776
Row("Comment", "") ::
777777
Row("Location", CatalogUtils.URIToString(location)) ::
778778
Row("Properties", "((a,a), (b,b), (c,c), (d,d))") :: Nil)
779-
780-
withTempDir { tmpDir =>
781-
intercept[NoSuchDatabaseException] {
782-
sql(s"ALTER DATABASE `db-not-exist` SET LOCATION '${tmpDir.toURI}'")
783-
}
784-
785-
val e3 = intercept[IllegalArgumentException] {
786-
sql(s"ALTER DATABASE $dbName SET LOCATION ''")
787-
}
788-
assert(e3.getMessage.contains("Can not create a Path from an empty string"))
789-
}
790779
} finally {
791780
catalog.reset()
792781
}

sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/AlterNamespaceSetLocationSuite.scala

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package org.apache.spark.sql.execution.command.v1
1919

20-
import org.apache.hadoop.fs.Path
21-
2220
import org.apache.spark.sql.execution.command
2321

2422
/**
@@ -33,14 +31,15 @@ import org.apache.spark.sql.execution.command
3331
*/
3432
trait AlterNamespaceSetLocationSuiteBase extends command.AlterNamespaceSetLocationSuiteBase
3533
with command.TestsV1AndV2Commands {
34+
override def namespace: String = "db"
3635
override def notFoundMsgPrefix: String = "Database"
3736

38-
test ("Empty location string") {
39-
val ns = "db1"
37+
test("Empty location string") {
38+
val ns = s"$catalog.$namespace"
4039
withNamespace(ns) {
41-
sql(s"CREATE NAMESPACE $catalog.$ns")
40+
sql(s"CREATE NAMESPACE $ns")
4241
val message = intercept[IllegalArgumentException] {
43-
sql(s"ALTER DATABASE $catalog.$ns SET LOCATION ''")
42+
sql(s"ALTER NAMESPACE $ns SET LOCATION ''")
4443
}.getMessage
4544
assert(message.contains("Can not create a Path from an empty string"))
4645
}
@@ -55,17 +54,7 @@ class AlterNamespaceSetLocationSuite extends AlterNamespaceSetLocationSuiteBase
5554
with CommandSuiteBase {
5655
override def commandVersion: String = super[AlterNamespaceSetLocationSuiteBase].commandVersion
5756

58-
test("basic v1 test") {
59-
val ns = "db1"
60-
withNamespace(ns) {
61-
sql(s"CREATE NAMESPACE $catalog.$ns")
62-
withTempDir { tmpDir =>
63-
sql(s"ALTER NAMESPACE $catalog.$ns SET LOCATION '${tmpDir.toURI}'")
64-
val sessionCatalog = spark.sessionState.catalog
65-
val uriInCatalog = sessionCatalog.getDatabaseMetadata(ns).locationUri
66-
assert("file" === uriInCatalog.getScheme)
67-
assert(new Path(tmpDir.getPath).toUri.getPath === uriInCatalog.getPath)
68-
}
69-
}
57+
test("basic test") {
58+
runBasicTest()
7059
}
7160
}

sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/AlterNamespaceSetLocationSuite.scala

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,27 @@
1717

1818
package org.apache.spark.sql.execution.command.v2
1919

20-
import org.apache.spark.sql.Row
21-
import org.apache.spark.sql.connector.catalog.SupportsNamespaces
2220
import org.apache.spark.sql.execution.command
23-
import org.apache.spark.util.Utils
2421

2522
/**
2623
* The class contains tests for the `ALTER NAMESPACE ... SET LOCATION` command to check V2 table
2724
* catalogs.
2825
*/
2926
class AlterNamespaceSetLocationSuite extends command.AlterNamespaceSetLocationSuiteBase
3027
with CommandSuiteBase {
28+
override def namespace: String = "ns1.ns2"
3129
override def notFoundMsgPrefix: String = "Namespace"
3230

33-
test("basic v2 test") {
34-
val ns = "ns1.ns2"
35-
withNamespace(s"$catalog.$ns") {
36-
sql(s"CREATE NAMESPACE IF NOT EXISTS $catalog.$ns COMMENT " +
37-
"'test namespace' LOCATION '/tmp/ns_test_1'")
38-
sql(s"ALTER NAMESPACE $catalog.$ns SET LOCATION '/tmp/ns_test_2'")
39-
val descriptionDf = sql(s"DESCRIBE NAMESPACE EXTENDED $catalog.$ns")
40-
assert(descriptionDf.collect() === Seq(
41-
Row("Namespace Name", "ns2"),
42-
Row(SupportsNamespaces.PROP_COMMENT.capitalize, "test namespace"),
43-
Row(SupportsNamespaces.PROP_LOCATION.capitalize, "/tmp/ns_test_2"),
44-
Row(SupportsNamespaces.PROP_OWNER.capitalize, Utils.getCurrentUserName()),
45-
Row("Properties", ""))
46-
)
31+
test("basic test") {
32+
runBasicTest()
33+
}
34+
35+
test("Empty location string is allowed") {
36+
val ns = s"$catalog.$namespace"
37+
withNamespace(ns) {
38+
sql(s"CREATE NAMESPACE $ns")
39+
sql(s"ALTER NAMESPACE $ns SET LOCATION ''")
40+
assert(getLocation(ns) === "")
4741
}
4842
}
4943
}

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/command/AlterNamespaceSetLocationSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AlterNamespaceSetLocationSuite extends v1.AlterNamespaceSetLocationSuiteBa
2929
override def commandVersion: String = super[AlterNamespaceSetLocationSuiteBase].commandVersion
3030

3131
test("Hive catalog not supported") {
32-
val ns = "db1"
32+
val ns = s"$catalog.$namespace"
3333
withNamespace(ns) {
3434
sql(s"CREATE NAMESPACE $ns")
3535
val e = intercept[AnalysisException] {

0 commit comments

Comments
 (0)