Skip to content

Commit 2b42acc

Browse files
committed
Initial commit
1 parent bba8bb8 commit 2b42acc

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,13 @@ object SQLConf {
21382138
.enumConf(classOf[Level])
21392139
.createWithDefault(Level.TRACE)
21402140

2141+
val DROP_TABLE_VIEW_ENABLED =
2142+
buildConf("spark.sql.dropTableOnView.enabled")
2143+
.doc("When true, DROP TABLE command will work on VIEW as well.")
2144+
.version("4.2.0")
2145+
.booleanConf
2146+
.createWithDefault(true)
2147+
21412148
val CROSS_JOINS_ENABLED = buildConf("spark.sql.crossJoin.enabled")
21422149
.internal()
21432150
.doc("When false, we will throw an error if a query contains a cartesian product without " +
@@ -7442,6 +7449,8 @@ class SQLConf extends Serializable with Logging with SqlApiConf {
74427449

74437450
def dataframeCacheLogLevel: Level = getConf(DATAFRAME_CACHE_LOG_LEVEL)
74447451

7452+
def dropTableOnView: Boolean = getConf(DROP_TABLE_VIEW_ENABLED)
7453+
74457454
def crossJoinEnabled: Boolean = getConf(SQLConf.CROSS_JOINS_ENABLED)
74467455

74477456
override def sessionLocalTimeZone: String = getConf(SQLConf.SESSION_LOCAL_TIMEZONE)

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2SessionCatalog.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ class V2SessionCatalog(catalog: SessionCatalog)
330330
private def dropTableInternal(ident: Identifier, purge: Boolean = false): Boolean = {
331331
try {
332332
loadTable(ident) match {
333-
case V1Table(v1Table) if v1Table.tableType == CatalogTableType.VIEW =>
333+
case V1Table(v1Table) if v1Table.tableType == CatalogTableType.VIEW &&
334+
!SQLConf.get.getConf(SQLConf.DROP_TABLE_VIEW_ENABLED) =>
334335
throw QueryCompilationErrors.wrongCommandForObjectTypeError(
335336
operation = "DROP TABLE",
336337
requiredType = s"${CatalogTableType.EXTERNAL.name} or" +

sql/core/src/test/scala/org/apache/spark/sql/execution/SQLViewSuite.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
5050
}
5151
}
5252

53+
test("view can be dropped using DROP TABLE") {
54+
val viewName = "v"
55+
withView(viewName) {
56+
sql(s"DROP VIEW IF EXISTS $viewName")
57+
// First, create a simple view.
58+
sql(s"CREATE VIEW $viewName AS SELECT 1 AS a")
59+
checkAnswer(sql(s"SELECT * FROM $viewName"), Row(1))
60+
// Then, drop the view using DROP TABLE.
61+
sql(s"DROP TABLE $viewName")
62+
// Finally, verify that the view is dropped.
63+
checkError(
64+
exception = intercept[AnalysisException] {
65+
sql(s"SELECT * FROM $viewName")
66+
},
67+
condition = "TABLE_OR_VIEW_NOT_FOUND",
68+
parameters = Map("relationName" -> s"`$viewName`"),
69+
ExpectedContext(s"$viewName", 14, 13 + viewName.length))
70+
}
71+
}
72+
5373
test("create a permanent view on a permanent view") {
5474
withView("jtv1", "jtv2") {
5575
sql("CREATE VIEW jtv1 AS SELECT * FROM jt WHERE id > 3")

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,17 +1155,16 @@ class HiveDDLSuite
11551155
spark.range(10).write.saveAsTable("tab1")
11561156
withView("view1") {
11571157
sql("CREATE VIEW view1 AS SELECT * FROM tab1")
1158-
assertAnalysisErrorCondition(
1159-
sqlText = "DROP TABLE view1",
1160-
condition = "WRONG_COMMAND_FOR_OBJECT_TYPE",
1161-
parameters = Map(
1162-
"alternative" -> "DROP VIEW",
1163-
"operation" -> "DROP TABLE",
1164-
"foundType" -> "VIEW",
1165-
"requiredType" -> "EXTERNAL or MANAGED",
1166-
"objectName" -> "spark_catalog.default.view1"
1167-
)
1168-
)
1158+
// Dropping a VIEW using DROP TABLE is allowed.
1159+
sql("DROP TABLE view1")
1160+
// Verify that the VIEW has been dropped.
1161+
checkError(
1162+
exception = intercept[AnalysisException] {
1163+
sql(s"SELECT * FROM view1")
1164+
},
1165+
condition = "TABLE_OR_VIEW_NOT_FOUND",
1166+
parameters = Map("relationName" -> s"`view1`"),
1167+
ExpectedContext("view1", 14, 18))
11691168
}
11701169
}
11711170
}

0 commit comments

Comments
 (0)