File tree Expand file tree Collapse file tree 4 files changed +42
-12
lines changed
catalyst/src/main/scala/org/apache/spark/sql/internal
main/scala/org/apache/spark/sql/execution/datasources/v2
test/scala/org/apache/spark/sql/execution
hive/src/test/scala/org/apache/spark/sql/hive/execution Expand file tree Collapse file tree 4 files changed +42
-12
lines changed Original file line number Diff line number Diff line change @@ -2138,6 +2138,14 @@ 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+ .owner(" eng-sql-team-beg" )
2146+ .booleanConf
2147+ .createWithDefault(true )
2148+
21412149 val CROSS_JOINS_ENABLED = buildConf(" spark.sql.crossJoin.enabled" )
21422150 .internal()
21432151 .doc(" When false, we will throw an error if a query contains a cartesian product without " +
@@ -7442,6 +7450,8 @@ class SQLConf extends Serializable with Logging with SqlApiConf {
74427450
74437451 def dataframeCacheLogLevel : Level = getConf(DATAFRAME_CACHE_LOG_LEVEL )
74447452
7453+ def dropTableOnView : Boolean = getConf(DROP_TABLE_VIEW_ENABLED )
7454+
74457455 def crossJoinEnabled : Boolean = getConf(SQLConf .CROSS_JOINS_ENABLED )
74467456
74477457 override def sessionLocalTimeZone : String = getConf(SQLConf .SESSION_LOCAL_TIMEZONE )
Original file line number Diff line number Diff 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 " +
Original file line number Diff line number Diff 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" )
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments