Skip to content

Commit 9870cf9

Browse files
MaxGekkHyukjinKwon
andcommitted
[SPARK-33067][SQL][TESTS] Add negative checks to JDBC v2 Table Catalog tests
### What changes were proposed in this pull request? Add checks for the cases when JDBC v2 Table Catalog commands fail. ### Why are the changes needed? To improve test coverage. ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? By running `JDBCTableCatalogSuite`. Closes #29945 from MaxGekk/jdbcv2-negative-tests. Lead-authored-by: Max Gekk <[email protected]> Co-authored-by: Hyukjin Kwon <[email protected]> Signed-off-by: HyukjinKwon <[email protected]>
1 parent a0aa8f3 commit 9870cf9

File tree

1 file changed

+111
-3
lines changed

1 file changed

+111
-3
lines changed

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

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import java.sql.{Connection, DriverManager}
2020
import java.util.Properties
2121

2222
import org.apache.spark.SparkConf
23-
import org.apache.spark.sql.{QueryTest, Row}
23+
import org.apache.spark.sql.{AnalysisException, QueryTest, Row}
24+
import org.apache.spark.sql.catalyst.analysis.NoSuchTableException
2425
import org.apache.spark.sql.test.SharedSparkSession
2526
import org.apache.spark.sql.types._
2627
import org.apache.spark.util.Utils
@@ -63,6 +64,8 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
6364

6465
test("show tables") {
6566
checkAnswer(sql("SHOW TABLES IN h2.test"), Seq(Row("test", "people")))
67+
// Check not existing namespace
68+
checkAnswer(sql("SHOW TABLES IN h2.bad_test"), Seq())
6669
}
6770

6871
test("drop a table and test whether the table exists") {
@@ -72,6 +75,11 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
7275
checkAnswer(sql("SHOW TABLES IN h2.test"), Seq(Row("test", "to_drop"), Row("test", "people")))
7376
sql("DROP TABLE h2.test.to_drop")
7477
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] {
80+
sql(s"DROP TABLE $table")
81+
}
82+
}
7583
}
7684

7785
test("rename a table") {
@@ -87,6 +95,26 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
8795
sql("SHOW TABLES IN h2.test"),
8896
Seq(Row("test", "dst_table"), Row("test", "people")))
8997
}
98+
// 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] {
101+
sql(s"ALTER TABLE $table RENAME TO test.dst_table")
102+
}
103+
}
104+
// Rename to an existing table
105+
withTable("h2.test.dst_table") {
106+
withConnection { conn =>
107+
conn.prepareStatement("""CREATE TABLE "test"."dst_table" (id INTEGER)""").executeUpdate()
108+
}
109+
withTable("h2.test.src_table") {
110+
withConnection { conn =>
111+
conn.prepareStatement("""CREATE TABLE "test"."src_table" (id INTEGER)""").executeUpdate()
112+
}
113+
intercept[org.h2.jdbc.JdbcSQLException] {
114+
sql("ALTER TABLE h2.test.src_table RENAME TO h2.test.dst_table")
115+
}
116+
}
117+
}
90118
}
91119

92120
test("load a table") {
@@ -95,6 +123,11 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
95123
.add("NAME", StringType)
96124
.add("ID", IntegerType)
97125
assert(t.schema === expectedSchema)
126+
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+
}
130+
}
98131
}
99132

100133
test("create a table") {
@@ -105,6 +138,15 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
105138
sql("SHOW TABLES IN h2.test"),
106139
Seq(Row("test", "people"), Row("test", "new_table")))
107140
}
141+
withTable("h2.test.new_table") {
142+
sql("CREATE TABLE h2.test.new_table(i INT, j STRING) USING _")
143+
intercept[AnalysisException] {
144+
sql("CREATE TABLE h2.test.new_table(i INT, j STRING) USING _")
145+
}
146+
}
147+
intercept[org.h2.jdbc.JdbcSQLException] {
148+
sql("CREATE TABLE h2.bad_test.new_table(i INT, j STRING) USING _")
149+
}
108150
}
109151

110152
test("alter table ... add column") {
@@ -121,16 +163,38 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
121163
t = spark.table("h2.test.alt_table")
122164
expectedSchema = expectedSchema.add("C3", DoubleType)
123165
assert(t.schema === expectedSchema)
166+
// Add already existing column
167+
intercept[AnalysisException] {
168+
sql("ALTER TABLE h2.test.alt_table ADD COLUMNS (C3 DOUBLE)")
169+
}
170+
}
171+
// Add a column to not existing table and namespace
172+
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
173+
intercept[AnalysisException] {
174+
sql(s"ALTER TABLE $table ADD COLUMNS (C4 STRING)")
175+
}
124176
}
125177
}
126178

127179
test("alter table ... rename column") {
128180
withTable("h2.test.alt_table") {
129-
sql("CREATE TABLE h2.test.alt_table (ID INTEGER) USING _")
181+
sql("CREATE TABLE h2.test.alt_table (ID INTEGER, C0 INTEGER) USING _")
130182
sql("ALTER TABLE h2.test.alt_table RENAME COLUMN ID TO C")
131183
val t = spark.table("h2.test.alt_table")
132-
val expectedSchema = new StructType().add("C", IntegerType)
184+
val expectedSchema = new StructType()
185+
.add("C", IntegerType)
186+
.add("C0", IntegerType)
133187
assert(t.schema === expectedSchema)
188+
// Rename to already existing column
189+
intercept[AnalysisException] {
190+
sql("ALTER TABLE h2.test.alt_table RENAME COLUMN C TO C0")
191+
}
192+
}
193+
// Rename a column in not existing table and namespace
194+
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
195+
intercept[AnalysisException] {
196+
sql(s"ALTER TABLE $table RENAME COLUMN ID TO C")
197+
}
134198
}
135199
}
136200

@@ -141,6 +205,16 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
141205
val t = spark.table("h2.test.alt_table")
142206
val expectedSchema = new StructType().add("C2", IntegerType)
143207
assert(t.schema === expectedSchema)
208+
// Drop not existing column
209+
intercept[AnalysisException] {
210+
sql("ALTER TABLE h2.test.alt_table DROP COLUMN bad_column")
211+
}
212+
}
213+
// Drop a column to not existing table and namespace
214+
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
215+
intercept[AnalysisException] {
216+
sql(s"ALTER TABLE $table DROP COLUMN C1")
217+
}
144218
}
145219
}
146220

@@ -151,6 +225,20 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
151225
val t = spark.table("h2.test.alt_table")
152226
val expectedSchema = new StructType().add("ID", DoubleType)
153227
assert(t.schema === expectedSchema)
228+
// Update not existing column
229+
intercept[AnalysisException] {
230+
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN bad_column TYPE DOUBLE")
231+
}
232+
// Update column to wrong type
233+
intercept[AnalysisException] {
234+
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN id TYPE bad_type")
235+
}
236+
}
237+
// Update column type in not existing table and namespace
238+
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
239+
intercept[AnalysisException] {
240+
sql(s"ALTER TABLE $table ALTER COLUMN id TYPE DOUBLE")
241+
}
154242
}
155243
}
156244

@@ -161,6 +249,16 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
161249
val t = spark.table("h2.test.alt_table")
162250
val expectedSchema = new StructType().add("ID", IntegerType, nullable = true)
163251
assert(t.schema === expectedSchema)
252+
// Update nullability of not existing column
253+
intercept[AnalysisException] {
254+
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN bad_column DROP NOT NULL")
255+
}
256+
}
257+
// Update column nullability in not existing table and namespace
258+
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
259+
intercept[AnalysisException] {
260+
sql(s"ALTER TABLE $table ALTER COLUMN ID DROP NOT NULL")
261+
}
164262
}
165263
}
166264

@@ -171,6 +269,16 @@ class JDBCTableCatalogSuite extends QueryTest with SharedSparkSession {
171269
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN ID COMMENT 'test'")
172270
}
173271
assert(thrown.getMessage.contains("Unsupported TableChange"))
272+
// Update comment for not existing column
273+
intercept[AnalysisException] {
274+
sql("ALTER TABLE h2.test.alt_table ALTER COLUMN bad_column COMMENT 'test'")
275+
}
276+
}
277+
// Update column comments in not existing table and namespace
278+
Seq("h2.test.not_existing_table", "h2.bad_test.not_existing_table").foreach { table =>
279+
intercept[AnalysisException] {
280+
sql(s"ALTER TABLE $table ALTER COLUMN ID COMMENT 'test'")
281+
}
174282
}
175283
}
176284
}

0 commit comments

Comments
 (0)