maropu commented on a change in pull request #31550:
URL: https://github.com/apache/spark/pull/31550#discussion_r578141199
##########
File path: sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
##########
@@ -3943,6 +3943,43 @@ class SQLQuerySuite extends QueryTest with
SharedSparkSession with AdaptiveSpark
}
}
}
+
+ test("SPARK-34421: Resolve temporary functions and views in views with
CTEs") {
+ val tempViewName = "temp_view"
+
+ withTempView(tempViewName) {
+ spark.udf.register("temp_func", identity[Int](_))
+
+ sql(
+ s"""
+ |CREATE TEMPORARY VIEW $tempViewName AS
+ |WITH cte AS (
+ | SELECT temp_func(0)
+ |)
+ |SELECT * FROM cte
+ |""".stripMargin)
+ checkAnswer(sql(s"SELECT * FROM $tempViewName"), Row(0))
+ }
+
+ withTempView(tempViewName) {
+ sql(s"CREATE TEMPORARY VIEW $tempViewName AS SELECT 0")
+
+ val viewName = "view_on_temp_view"
+
+ val e = intercept[AnalysisException] {
+ sql(
+ s"""
+ |CREATE VIEW $viewName AS
+ |WITH cte AS (
+ | SELECT * FROM $tempViewName
+ |)
+ |SELECT * FROM cte
+ |""".stripMargin)
+ }
+ assert(e.message.contains(s"Not allowed to create a permanent view
`default`.`$viewName` " +
+ s"by referencing a temporary view $tempViewName."))
Review comment:
hm, this message looks correct, so `v3.0.2` has the same issue?
```
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/___/ .__/\_,_/_/ /_/\_\ version 3.0.2
/_/
Using Scala version 2.12.10 (OpenJDK 64-Bit Server VM, Java 11.0.2)
Type in expressions to have them evaluated.
Type :help for more information.
scala> val tempViewName = "temp_view"
scala> val viewName = "view_on_temp_view"
scala> sql(s"CREATE TEMPORARY VIEW $tempViewName AS SELECT 0")
scala> sql(
| s"""
| |CREATE VIEW $viewName AS
| |WITH cte AS (
| | SELECT * FROM $tempViewName
| |)
| |SELECT * FROM cte
| |""".stripMargin)
scala> spark.table(viewName).show()
+---+
| 0|
+---+
| 0|
+---+
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]