Skip to content

Commit 176b055

Browse files
RoryQicloud-fan
authored andcommitted
[SPARK-36011][SQL] Disallow altering permanent views based on temporary views or UDFs
### What changes were proposed in this pull request? PR #15764 disabled creating permanent views based on temporary views or UDFs. But AlterViewCommand didn't block temporary objects. ### Why are the changes needed? More robust view canonicalization. ### Does this PR introduce _any_ user-facing change? Yes, now if you alter a permanent view based on temporary views or UDFs, the operation will fail. ### How was this patch tested? Add new unit tests. Closes #33204 from jerqi/alter_view. Authored-by: RoryQi <[email protected]> Signed-off-by: Wenchen Fan <[email protected]> (cherry picked from commit e0c6b2e) Signed-off-by: Wenchen Fan <[email protected]>
1 parent e09feda commit 176b055

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,10 @@ case class AlterViewAsCommand(
259259
def markAsAnalyzed(): LogicalPlan = copy(isAnalyzed = true)
260260

261261
override def run(session: SparkSession): Seq[Row] = {
262-
if (session.sessionState.catalog.isTempView(name)) {
262+
val isTemporary = session.sessionState.catalog.isTempView(name)
263+
verifyTemporaryObjectsNotExists(session.sessionState.catalog, isTemporary, name, query)
264+
verifyAutoGeneratedAliasesNotExists(query, isTemporary, name)
265+
if (isTemporary) {
263266
alterTemporaryView(session, query)
264267
} else {
265268
alterPermanentView(session, query)
@@ -286,7 +289,6 @@ case class AlterViewAsCommand(
286289
}
287290

288291
private def alterPermanentView(session: SparkSession, analyzedPlan: LogicalPlan): Unit = {
289-
verifyAutoGeneratedAliasesNotExists(analyzedPlan, isTemporary = false, name)
290292
val viewMeta = session.sessionState.catalog.getTableMetadata(name)
291293

292294
// Detect cyclic view reference on ALTER VIEW.

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,32 @@ class PersistedViewTestSuite extends SQLViewTestSuite with SharedSparkSession {
465465
}
466466
}
467467
}
468+
469+
test("SPARK-36011: Disallow altering permanent views based on temporary views or UDFs") {
470+
import testImplicits._
471+
withTable("t") {
472+
(1 to 10).toDF("id").write.saveAsTable("t")
473+
withView("v1") {
474+
withTempView("v2") {
475+
sql("CREATE VIEW v1 AS SELECT * FROM t")
476+
sql("CREATE TEMPORARY VIEW v2 AS SELECT * FROM t")
477+
var e = intercept[AnalysisException] {
478+
sql("ALTER VIEW v1 AS SELECT * FROM v2")
479+
}.getMessage
480+
assert(e.contains("Not allowed to create a permanent view `default`.`v1` by " +
481+
"referencing a temporary view v2"))
482+
val tempFunctionName = "temp_udf"
483+
val functionClass = "test.org.apache.spark.sql.MyDoubleAvg"
484+
withUserDefinedFunction(tempFunctionName -> true) {
485+
sql(s"CREATE TEMPORARY FUNCTION $tempFunctionName AS '$functionClass'")
486+
e = intercept[AnalysisException] {
487+
sql(s"ALTER VIEW v1 AS SELECT $tempFunctionName(id) from t")
488+
}.getMessage
489+
assert(e.contains("Not allowed to create a permanent view `default`.`v1` by " +
490+
s"referencing a temporary function `$tempFunctionName`"))
491+
}
492+
}
493+
}
494+
}
495+
}
468496
}

0 commit comments

Comments
 (0)