Skip to content

Commit 9d28909

Browse files
panbingkunMaxGekk
authored andcommitted
[SPARK-40788][SQL][TESTS] Check error classes in CreateNamespaceParserSuite
### What changes were proposed in this pull request? This PR aims to replace 'intercept' with 'Check error classes' in CreateNamespaceParserSuite. ### Why are the changes needed? The changes improve the error framework. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? By running the modified test suite: ``` $ build/sbt "test:testOnly *CreateNamespaceParserSuite" ``` Closes #38246 from panbingkun/SPARK-40788. Authored-by: panbingkun <[email protected]> Signed-off-by: Max Gekk <[email protected]>
1 parent e54e3ab commit 9d28909

File tree

1 file changed

+69
-25
lines changed

1 file changed

+69
-25
lines changed

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

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@
1717

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

20+
import org.apache.spark.SparkThrowable
2021
import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedNamespace}
2122
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan
23+
import org.apache.spark.sql.catalyst.parser.ParseException
2224
import org.apache.spark.sql.catalyst.plans.logical.CreateNamespace
25+
import org.apache.spark.sql.test.SharedSparkSession
2326

24-
class CreateNamespaceParserSuite extends AnalysisTest {
25-
test("create namespace -- backward compatibility with DATABASE/DBPROPERTIES") {
27+
class CreateNamespaceParserSuite extends AnalysisTest with SharedSparkSession {
28+
29+
private def parseException(sqlText: String): SparkThrowable = {
30+
intercept[ParseException](sql(sqlText).collect())
31+
}
32+
33+
test("create namespace - backward compatibility with DATABASE/DBPROPERTIES") {
2634
val expected = CreateNamespace(
2735
UnresolvedNamespace(Seq("a", "b", "c")),
2836
ifNotExists = true,
@@ -52,40 +60,79 @@ class CreateNamespaceParserSuite extends AnalysisTest {
5260
expected)
5361
}
5462

55-
test("create namespace -- check duplicates") {
63+
test("create namespace - check duplicates") {
5664
def createNamespace(duplicateClause: String): String = {
57-
s"""
58-
|CREATE NAMESPACE IF NOT EXISTS a.b.c
65+
s"""CREATE NAMESPACE IF NOT EXISTS a.b.c
5966
|$duplicateClause
60-
|$duplicateClause
61-
""".stripMargin
67+
|$duplicateClause""".stripMargin
6268
}
69+
6370
val sql1 = createNamespace("COMMENT 'namespace_comment'")
71+
checkError(
72+
exception = parseException(sql1),
73+
errorClass = "_LEGACY_ERROR_TEMP_0041",
74+
parameters = Map("clauseName" -> "COMMENT"),
75+
context = ExpectedContext(
76+
fragment = sql1,
77+
start = 0,
78+
stop = 91))
79+
6480
val sql2 = createNamespace("LOCATION '/home/user/db'")
81+
checkError(
82+
exception = parseException(sql2),
83+
errorClass = "_LEGACY_ERROR_TEMP_0041",
84+
parameters = Map("clauseName" -> "LOCATION"),
85+
context = ExpectedContext(
86+
fragment = sql2,
87+
start = 0,
88+
stop = 85))
89+
6590
val sql3 = createNamespace("WITH PROPERTIES ('a'='a', 'b'='b', 'c'='c')")
66-
val sql4 = createNamespace("WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')")
91+
checkError(
92+
exception = parseException(sql3),
93+
errorClass = "_LEGACY_ERROR_TEMP_0041",
94+
parameters = Map("clauseName" -> "WITH PROPERTIES"),
95+
context = ExpectedContext(
96+
fragment = sql3,
97+
start = 0,
98+
stop = 123))
6799

68-
intercept(sql1, "Found duplicate clauses: COMMENT")
69-
intercept(sql2, "Found duplicate clauses: LOCATION")
70-
intercept(sql3, "Found duplicate clauses: WITH PROPERTIES")
71-
intercept(sql4, "Found duplicate clauses: WITH DBPROPERTIES")
100+
val sql4 = createNamespace("WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')")
101+
checkError(
102+
exception = parseException(sql4),
103+
errorClass = "_LEGACY_ERROR_TEMP_0041",
104+
parameters = Map("clauseName" -> "WITH DBPROPERTIES"),
105+
context = ExpectedContext(
106+
fragment = sql4,
107+
start = 0,
108+
stop = 127))
72109
}
73110

74111
test("create namespace - property values must be set") {
75-
intercept(
76-
"CREATE NAMESPACE a.b.c WITH PROPERTIES('key_without_value', 'key_with_value'='x')",
77-
"Operation not allowed: Values must be specified for key(s): [key_without_value]")
112+
val sql = "CREATE NAMESPACE a.b.c WITH PROPERTIES('key_without_value', 'key_with_value'='x')"
113+
checkError(
114+
exception = parseException(sql),
115+
errorClass = "_LEGACY_ERROR_TEMP_0035",
116+
parameters = Map("message" -> "Values must be specified for key(s): [key_without_value]"),
117+
context = ExpectedContext(
118+
fragment = sql,
119+
start = 0,
120+
stop = 80))
78121
}
79122

80-
test("create namespace -- either PROPERTIES or DBPROPERTIES is allowed") {
123+
test("create namespace - either PROPERTIES or DBPROPERTIES is allowed") {
81124
val sql =
82-
s"""
83-
|CREATE NAMESPACE IF NOT EXISTS a.b.c
125+
s"""CREATE NAMESPACE IF NOT EXISTS a.b.c
84126
|WITH PROPERTIES ('a'='a', 'b'='b', 'c'='c')
85-
|WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')
86-
""".stripMargin
87-
intercept(sql, "The feature is not supported: " +
88-
"set PROPERTIES and DBPROPERTIES at the same time.")
127+
|WITH DBPROPERTIES ('a'='a', 'b'='b', 'c'='c')""".stripMargin
128+
checkError(
129+
exception = parseException(sql),
130+
errorClass = "UNSUPPORTED_FEATURE.SET_PROPERTIES_AND_DBPROPERTIES",
131+
parameters = Map.empty,
132+
context = ExpectedContext(
133+
fragment = sql,
134+
start = 0,
135+
stop = 125))
89136
}
90137

91138
test("create namespace - support for other types in PROPERTIES") {
@@ -106,7 +153,4 @@ class CreateNamespaceParserSuite extends AnalysisTest {
106153
"c" -> "true",
107154
"location" -> "/home/user/db")))
108155
}
109-
110-
private def intercept(sqlCommand: String, messages: String*): Unit =
111-
interceptParseException(parsePlan)(sqlCommand, messages: _*)()
112156
}

0 commit comments

Comments
 (0)