@@ -1445,4 +1445,54 @@ class CachedTableSuite extends QueryTest with SQLTestUtils
14451445 }
14461446 }
14471447 }
1448+
1449+ test(" SPARK-34546: ALTER VIEW AS should uncache if a temp view is cached" ) {
1450+ Seq (true , false ).foreach { storeAnalyzed =>
1451+ withSQLConf(SQLConf .STORE_ANALYZED_PLAN_FOR_VIEW .key -> storeAnalyzed.toString) {
1452+ withTempView(" tv" ) {
1453+ sql(" CREATE TEMPORARY VIEW tv AS SELECT 1" )
1454+ sql(" CACHE TABLE tv" )
1455+ assert(spark.catalog.isCached(" tv" ))
1456+ assert(spark.sharedState.cacheManager.lookupCachedData(sql(" SELECT 1" )).nonEmpty)
1457+
1458+ sql(" ALTER VIEW tv as SELECT 2" )
1459+ assert(! spark.catalog.isCached(" tv" ))
1460+ assert(spark.sharedState.cacheManager.lookupCachedData(sql(" SELECT 1" )).isEmpty)
1461+ }
1462+ }
1463+ }
1464+ }
1465+
1466+ test(" SPARK-34546: ALTER VIEW AS should uncache if a global temp view is cached" ) {
1467+ Seq (true , false ).foreach { storeAnalyzed =>
1468+ withSQLConf(SQLConf .STORE_ANALYZED_PLAN_FOR_VIEW .key -> storeAnalyzed.toString) {
1469+ withGlobalTempView(" global_tv" ) {
1470+ sql(" CREATE GLOBAL TEMPORARY VIEW global_tv AS SELECT 1" )
1471+
1472+ val db = spark.sharedState.globalTempViewManager.database
1473+ val gv = s " $db.global_tv "
1474+ sql(s " CACHE TABLE $gv" )
1475+ assert(spark.catalog.isCached(gv))
1476+ assert(spark.sharedState.cacheManager.lookupCachedData(sql(" SELECT 1" )).nonEmpty)
1477+
1478+ sql(s " ALTER VIEW $gv as SELECT 2 " )
1479+ assert(! spark.catalog.isCached(gv))
1480+ assert(spark.sharedState.cacheManager.lookupCachedData(sql(" SELECT 1" )).isEmpty)
1481+ }
1482+ }
1483+ }
1484+ }
1485+
1486+ test(" SPARK-34546: ALTER VIEW AS should uncache if a permanent temp view is cached" ) {
1487+ withView(" view" ) {
1488+ sql(" CREATE VIEW view AS SELECT 1" )
1489+ sql(" CACHE TABLE view" )
1490+ assert(spark.catalog.isCached(" view" ))
1491+ assert(spark.sharedState.cacheManager.lookupCachedData(sql(" SELECT 1" )).nonEmpty)
1492+
1493+ sql(" ALTER VIEW view as SELECT 2" )
1494+ assert(! spark.catalog.isCached(" view" ))
1495+ assert(spark.sharedState.cacheManager.lookupCachedData(sql(" SELECT 1" )).isEmpty)
1496+ }
1497+ }
14481498}
0 commit comments