Skip to content

Commit 584f90c

Browse files
MaxGekkHyukjinKwon
authored andcommitted
[SPARK-33067][SQL][TESTS][FOLLOWUP] Check error messages in JDBCTableCatalogSuite
### What changes were proposed in this pull request? Get error message from the expected exception, and check that they are reasonable. ### Why are the changes needed? To improve tests by expecting particular error messages. ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? By running `JDBCTableCatalogSuite`. Closes #29957 from MaxGekk/jdbcv2-negative-tests-followup. Authored-by: Max Gekk <[email protected]> Signed-off-by: HyukjinKwon <[email protected]>
1 parent 57ed5a8 commit 584f90c

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/v2/jdbc/JDBCTableCatalogSuite.scala

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import java.util.Properties
2222
import org.apache.spark.SparkConf
2323
import org.apache.spark.sql.{AnalysisException, QueryTest, Row}
2424
import org.apache.spark.sql.catalyst.analysis.NoSuchTableException
25+
import org.apache.spark.sql.catalyst.parser.ParseException
2526
import org.apache.spark.sql.test.SharedSparkSession
2627
import org.apache.spark.sql.types._
2728
import org.apache.spark.util.Utils
@@ -75,10 +76,14 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
7576
checkAnswer(sql("SHOW TABLES IN h2.test"), Seq(Row("test", "to_drop"), Row("test", "people")))
7677
sql("DROP TABLE h2.test.to_drop")
7778
checkAnswer(sql("SHOW TABLES IN h2.test"), Seq(Row("test", "people")))
78-
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
79-
intercept[NoSuchTableException] {
79+
Seq(
80+
"h2.test.not_existing_table" -> "Table test.not_existing_table not found",
81+
"h2.bad_test.not_existing_table" -> "Table bad_test.not_existing_table not found"
82+
).foreach { case (table, expectedMsg) =>
83+
val msg = intercept[NoSuchTableException] {
8084
sql(s"DROP TABLE $table")
81-
}
85+
}.getMessage
86+
assert(msg.contains(expectedMsg))
8287
}
8388
}
8489

@@ -96,10 +101,14 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
96101
Seq(Row("test", "dst_table"), Row("test", "people")))
97102
}
98103
// Rename not existing table or namespace
99-
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
100-
intercept[org.h2.jdbc.JdbcSQLException] {
104+
Seq(
105+
"h2.test.not_existing_table" -> "Table \"not_existing_table\" not found",
106+
"h2.bad_test.not_existing_table" -> "Schema \"bad_test\" not found"
107+
).foreach { case (table, expectedMsg) =>
108+
val msg = intercept[org.h2.jdbc.JdbcSQLException] {
101109
sql(s"ALTER TABLE $table RENAME TO test.dst_table")
102-
}
110+
}.getMessage
111+
assert(msg.contains(expectedMsg))
103112
}
104113
// Rename to an existing table
105114
withTable("h2.test.dst_table") {
@@ -110,9 +119,10 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
110119
withConnection { conn =>
111120
conn.prepareStatement("""CREATE TABLE "test"."src_table" (id INTEGER)""").executeUpdate()
112121
}
113-
intercept[org.h2.jdbc.JdbcSQLException] {
114-
sql("ALTER TABLE h2.test.src_table RENAME TO h2.test.dst_table")
115-
}
122+
val msg = intercept[org.h2.jdbc.JdbcSQLException] {
123+
sql("ALTER TABLE h2.test.src_table RENAME TO test.dst_table")
124+
}.getMessage
125+
assert(msg.contains("Table \"dst_table\" already exists"))
116126
}
117127
}
118128
}
@@ -124,9 +134,10 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
124134
.add("ID", IntegerType)
125135
assert(t.schema === expectedSchema)
126136
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
127-
intercept[AnalysisException] {
128-
spark.table(s"h2.$table").schema
129-
}
137+
val msg = intercept[AnalysisException] {
138+
spark.table(table).schema
139+
}.getMessage
140+
assert(msg.contains("Table or view not found"))
130141
}
131142
}
132143

@@ -140,13 +151,15 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
140151
}
141152
withTable("h2.test.new_table") {
142153
sql("CREATE TABLE h2.test.new_table(i INT, j STRING) USING _")
143-
intercept[AnalysisException] {
154+
val msg = intercept[AnalysisException] {
144155
sql("CREATE TABLE h2.test.new_table(i INT, j STRING) USING _")
145-
}
156+
}.getMessage
157+
assert(msg.contains("Table test.new_table already exists"))
146158
}
147-
intercept[org.h2.jdbc.JdbcSQLException] {
159+
val msg = intercept[org.h2.jdbc.JdbcSQLException] {
148160
sql("CREATE TABLE h2.bad_test.new_table(i INT, j STRING) USING _")
149-
}
161+
}.getMessage
162+
assert(msg.contains("Schema \"bad_test\" not found"))
150163
}
151164

152165
test("alter table ... add column") {
@@ -164,15 +177,17 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
164177
expectedSchema = expectedSchema.add("C3", DoubleType)
165178
assert(t.schema === expectedSchema)
166179
// Add already existing column
167-
intercept[AnalysisException] {
180+
val msg = intercept[AnalysisException] {
168181
sql("ALTER TABLE h2.test.alt_table ADD COLUMNS (C3 DOUBLE)")
169-
}
182+
}.getMessage
183+
assert(msg.contains("Cannot add column, because C3 already exists"))
170184
}
171185
// Add a column to not existing table and namespace
172186
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
173-
intercept[AnalysisException] {
187+
val msg = intercept[AnalysisException] {
174188
sql(s"ALTER TABLE $table ADD COLUMNS (C4 STRING)")
175-
}
189+
}.getMessage
190+
assert(msg.contains("Table not found"))
176191
}
177192
}
178193

@@ -186,15 +201,17 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
186201
.add("C0", IntegerType)
187202
assert(t.schema === expectedSchema)
188203
// Rename to already existing column
189-
intercept[AnalysisException] {
204+
val msg = intercept[AnalysisException] {
190205
sql("ALTER TABLE h2.test.alt_table RENAME COLUMN C TO C0")
191-
}
206+
}.getMessage
207+
assert(msg.contains("Cannot rename column, because C0 already exists"))
192208
}
193209
// Rename a column in not existing table and namespace
194210
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
195-
intercept[AnalysisException] {
211+
val msg = intercept[AnalysisException] {
196212
sql(s"ALTER TABLE $table RENAME COLUMN ID TO C")
197-
}
213+
}.getMessage
214+
assert(msg.contains("Table not found"))
198215
}
199216
}
200217

@@ -206,15 +223,17 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
206223
val expectedSchema = new StructType().add("C2", IntegerType)
207224
assert(t.schema === expectedSchema)
208225
// Drop not existing column
209-
intercept[AnalysisException] {
226+
val msg = intercept[AnalysisException] {
210227
sql("ALTER TABLE h2.test.alt_table DROP COLUMN bad_column")
211-
}
228+
}.getMessage
229+
assert(msg.contains("Cannot delete missing field bad_column in test.alt_table schema"))
212230
}
213231
// Drop a column to not existing table and namespace
214232
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
215-
intercept[AnalysisException] {
233+
val msg = intercept[AnalysisException] {
216234
sql(s"ALTER TABLE $table DROP COLUMN C1")
217-
}
235+
}.getMessage
236+
assert(msg.contains("Table not found"))
218237
}
219238
}
220239

@@ -226,19 +245,22 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
226245
val expectedSchema = new StructType().add("ID", DoubleType)
227246
assert(t.schema === expectedSchema)
228247
// Update not existing column
229-
intercept[AnalysisException] {
248+
val msg1 = intercept[AnalysisException] {
230249
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN bad_column TYPE DOUBLE")
231-
}
250+
}.getMessage
251+
assert(msg1.contains("Cannot update missing field bad_column in test.alt_table schema"))
232252
// Update column to wrong type
233-
intercept[AnalysisException] {
253+
val msg2 = intercept[ParseException] {
234254
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN id TYPE bad_type")
235-
}
255+
}.getMessage
256+
assert(msg2.contains("DataType bad_type is not supported"))
236257
}
237258
// Update column type in not existing table and namespace
238259
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
239-
intercept[AnalysisException] {
260+
val msg = intercept[AnalysisException] {
240261
sql(s"ALTER TABLE $table ALTER COLUMN id TYPE DOUBLE")
241-
}
262+
}.getMessage
263+
assert(msg.contains("Table not found"))
242264
}
243265
}
244266

@@ -250,35 +272,39 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
250272
val expectedSchema = new StructType().add("ID", IntegerType, nullable = true)
251273
assert(t.schema === expectedSchema)
252274
// Update nullability of not existing column
253-
intercept[AnalysisException] {
275+
val msg = intercept[AnalysisException] {
254276
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN bad_column DROP NOT NULL")
255-
}
277+
}.getMessage
278+
assert(msg.contains("Cannot update missing field bad_column in test.alt_table"))
256279
}
257280
// Update column nullability in not existing table and namespace
258281
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
259-
intercept[AnalysisException] {
282+
val msg = intercept[AnalysisException] {
260283
sql(s"ALTER TABLE $table ALTER COLUMN ID DROP NOT NULL")
261-
}
284+
}.getMessage
285+
assert(msg.contains("Table not found"))
262286
}
263287
}
264288

265289
test("alter table ... update column comment not supported") {
266290
withTable("h2.test.alt_table") {
267291
sql("CREATE TABLE h2.test.alt_table (ID INTEGER) USING _")
268-
val thrown = intercept[java.sql.SQLFeatureNotSupportedException] {
292+
val msg1 = intercept[java.sql.SQLFeatureNotSupportedException] {
269293
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN ID COMMENT 'test'")
270-
}
271-
assert(thrown.getMessage.contains("Unsupported TableChange"))
294+
}.getMessage
295+
assert(msg1.contains("Unsupported TableChange"))
272296
// Update comment for not existing column
273-
intercept[AnalysisException] {
297+
val msg2 = intercept[AnalysisException] {
274298
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN bad_column COMMENT 'test'")
275-
}
299+
}.getMessage
300+
assert(msg2.contains("Cannot update missing field bad_column in test.alt_table"))
276301
}
277302
// Update column comments in not existing table and namespace
278303
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
279-
intercept[AnalysisException] {
304+
val msg = intercept[AnalysisException] {
280305
sql(s"ALTER TABLE $table ALTER COLUMN ID COMMENT 'test'")
281-
}
306+
}.getMessage
307+
assert(msg.contains("Table not found"))
282308
}
283309
}
284310
}

0 commit comments

Comments
 (0)