-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-33849][SQL][TESTS] Unify v1 and v2 DROP TABLE tests
### What changes were proposed in this pull request? 1. Move the `DROP TABLE` parsing tests to `DropTableParserSuite` 2. Place the v1 tests for `DROP TABLE` from `DDLSuite` and v2 tests from `DataSourceV2SQLSuite` to the common trait `DropTableSuiteBase`, so, the tests will run for V1, Hive V1 and V2 DS. ### Why are the changes needed? - The unification will allow to run common `DROP TABLE` tests for both DSv1 and Hive DSv1, DSv2 - We can detect missing features and differences between DSv1 and DSv2 implementations. ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? By running new test suites: ``` $ build/sbt -Phive-2.3 -Phive-thriftserver "test:testOnly *DropTableParserSuite" $ build/sbt -Phive-2.3 -Phive-thriftserver "test:testOnly *DropTableSuite" ``` Closes #30854 from MaxGekk/unify-drop-table-tests. Authored-by: Max Gekk <max.gekk@gmail.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
- Loading branch information
Showing
9 changed files
with
312 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sql/core/src/test/scala/org/apache/spark/sql/execution/command/DropTableParserSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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, UnresolvedTableOrView} | ||
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan | ||
import org.apache.spark.sql.catalyst.plans.logical.{DropTable, LogicalPlan} | ||
import org.apache.spark.sql.test.SharedSparkSession | ||
|
||
class DropTableParserSuite extends AnalysisTest with SharedSparkSession { | ||
private def parseCompare(sql: String, expected: LogicalPlan): Unit = { | ||
comparePlans(parsePlan(sql), expected, checkAnalysis = false) | ||
} | ||
|
||
test("drop table") { | ||
parseCompare("DROP TABLE testcat.ns1.ns2.tbl", | ||
DropTable( | ||
UnresolvedTableOrView(Seq("testcat", "ns1", "ns2", "tbl"), "DROP TABLE"), | ||
ifExists = false, | ||
purge = false)) | ||
parseCompare(s"DROP TABLE db.tab", | ||
DropTable( | ||
UnresolvedTableOrView(Seq("db", "tab"), "DROP TABLE"), ifExists = false, purge = false)) | ||
parseCompare(s"DROP TABLE IF EXISTS db.tab", | ||
DropTable( | ||
UnresolvedTableOrView(Seq("db", "tab"), "DROP TABLE"), ifExists = true, purge = false)) | ||
parseCompare(s"DROP TABLE tab", | ||
DropTable( | ||
UnresolvedTableOrView(Seq("tab"), "DROP TABLE"), ifExists = false, purge = false)) | ||
parseCompare(s"DROP TABLE IF EXISTS tab", | ||
DropTable( | ||
UnresolvedTableOrView(Seq("tab"), "DROP TABLE"), ifExists = true, purge = false)) | ||
parseCompare(s"DROP TABLE tab PURGE", | ||
DropTable( | ||
UnresolvedTableOrView(Seq("tab"), "DROP TABLE"), ifExists = false, purge = true)) | ||
parseCompare(s"DROP TABLE IF EXISTS tab PURGE", | ||
DropTable( | ||
UnresolvedTableOrView(Seq("tab"), "DROP TABLE"), ifExists = true, purge = true)) | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
sql/core/src/test/scala/org/apache/spark/sql/execution/command/DropTableSuiteBase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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, Row} | ||
|
||
trait DropTableSuiteBase extends QueryTest with DDLCommandTestUtils { | ||
override val command = "DROP TABLE" | ||
|
||
protected def createTable(tableName: String): Unit = { | ||
sql(s"CREATE TABLE $tableName (c int) $defaultUsing") | ||
sql(s"INSERT INTO $tableName SELECT 0") | ||
} | ||
|
||
protected def checkTables(namespace: String, expectedTables: String*): Unit = { | ||
val tables = sql(s"SHOW TABLES IN $catalog.$namespace").select("tableName") | ||
val rows = expectedTables.map(Row(_)) | ||
checkAnswer(tables, rows) | ||
} | ||
|
||
test("basic") { | ||
withNamespace(s"$catalog.ns") { | ||
sql(s"CREATE NAMESPACE $catalog.ns") | ||
|
||
createTable(s"$catalog.ns.tbl") | ||
checkTables("ns", "tbl") | ||
|
||
sql(s"DROP TABLE $catalog.ns.tbl") | ||
checkTables("ns") // no tables | ||
} | ||
} | ||
|
||
test("try to drop a nonexistent table") { | ||
withNamespace(s"$catalog.ns") { | ||
sql(s"CREATE NAMESPACE $catalog.ns") | ||
checkTables("ns") // no tables | ||
|
||
val errMsg = intercept[AnalysisException] { | ||
sql(s"DROP TABLE $catalog.ns.tbl") | ||
}.getMessage | ||
assert(errMsg.contains("Table or view not found")) | ||
} | ||
} | ||
|
||
test("with IF EXISTS") { | ||
withNamespace(s"$catalog.ns") { | ||
sql(s"CREATE NAMESPACE $catalog.ns") | ||
|
||
createTable(s"$catalog.ns.tbl") | ||
checkTables("ns", "tbl") | ||
sql(s"DROP TABLE IF EXISTS $catalog.ns.tbl") | ||
checkTables("ns") | ||
|
||
// It must not throw any exceptions | ||
sql(s"DROP TABLE IF EXISTS $catalog.ns.tbl") | ||
checkTables("ns") | ||
} | ||
} | ||
|
||
test("SPARK-33174: DROP TABLE should resolve to a temporary view first") { | ||
withNamespaceAndTable("ns", "t") { t => | ||
withTempView("t") { | ||
sql(s"CREATE TABLE $t (id bigint) $defaultUsing") | ||
sql("CREATE TEMPORARY VIEW t AS SELECT 2") | ||
sql(s"USE $catalog.ns") | ||
try { | ||
// Check the temporary view 't' exists. | ||
checkAnswer( | ||
sql("SHOW TABLES FROM spark_catalog.default LIKE 't'") | ||
.select("tableName", "isTemporary"), | ||
Row("t", true)) | ||
sql("DROP TABLE t") | ||
// Verify that the temporary view 't' is resolved first and dropped. | ||
checkAnswer( | ||
sql("SHOW TABLES FROM spark_catalog.default LIKE 't'") | ||
.select("tableName", "isTemporary"), | ||
Seq.empty) | ||
} finally { | ||
sql(s"USE spark_catalog") | ||
} | ||
} | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/DropTableSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.spark.sql.execution.command | ||
|
||
trait DropTableSuiteBase extends command.DropTableSuiteBase { | ||
test("purge option") { | ||
withNamespace(s"$catalog.ns") { | ||
sql(s"CREATE NAMESPACE $catalog.ns") | ||
|
||
createTable(s"$catalog.ns.tbl") | ||
checkTables("ns", "tbl") | ||
|
||
sql(s"DROP TABLE $catalog.ns.tbl PURGE") | ||
checkTables("ns") // no tables | ||
} | ||
} | ||
} | ||
|
||
class DropTableSuite extends DropTableSuiteBase with CommandSuiteBase { | ||
// The test fails in Hive External catalog with: | ||
// org.apache.spark.sql.AnalysisException: | ||
// spark_catalog.ns.tbl is not a valid TableIdentifier as it has more than 2 name parts. | ||
test("SPARK-33305: DROP TABLE should also invalidate cache") { | ||
val t = s"$catalog.ns.tbl" | ||
val view = "view" | ||
withNamespace(s"$catalog.ns") { | ||
sql(s"CREATE NAMESPACE $catalog.ns") | ||
withTempView(view, "source") { | ||
val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") | ||
df.createOrReplaceTempView("source") | ||
sql(s"CREATE TABLE $t $defaultUsing AS SELECT id, data FROM source") | ||
sql(s"CACHE TABLE $view AS SELECT id FROM $t") | ||
checkAnswer(sql(s"SELECT * FROM $t"), spark.table("source").collect()) | ||
checkAnswer( | ||
sql(s"SELECT * FROM $view"), | ||
spark.table("source").select("id").collect()) | ||
|
||
assert(!spark.sharedState.cacheManager.lookupCachedData(spark.table(view)).isEmpty) | ||
sql(s"DROP TABLE $t") | ||
assert(spark.sharedState.cacheManager.lookupCachedData(spark.table(view)).isEmpty) | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.