[SPARK-58894][SQL] Detect cyclic view references in nested subqueries - #58146
[SPARK-58894][SQL] Detect cyclic view references in nested subqueries#58146manuzhang wants to merge 1 commit into
Conversation
Generated-by: Codex (GPT-5)
c5705b1 to
415ccec
Compare
|
@szehon-ho @aokolnychyi This is why we can't use Spark's |
|
Nice catch and elegant fix @manuzhang! Review Summary:
LGTM +1! |
|
Thank you @manuzhang and @zahed1994! |
There was a problem hiding this comment.
Nice find. I think my only concern, this is a behave change. The old view probably will not run. But this prevent bad view from being created and it could today (and later fixed before running). but leave up to @uros-b @cloud-fan @dongjoon-hyun and others
cloud-fan
left a comment
There was a problem hiding this comment.
0 blocking, 0 non-blocking, 0 nits.
The patch is narrowly scoped, and its traversal semantics, shared entry paths, error behavior, and regression coverage are consistent.
Verification
Source-level verification traced Expression.foreach to its recursive child walk, confirmed that SubqueryExpression.plan is reached only by the explicit validator recursion, and checked the v1 and v2 callers of the shared helper. The regression assertion exercises the previously missed equality-wrapped scalar subquery and verifies RECURSIVE_VIEW. I did not rerun the test suite locally.
### What changes were proposed in this pull request? Update `ViewHelper.checkCyclicViewReference` to traverse complete expression trees when looking for subquery expressions. Add a regression case where a scalar subquery that references the altered view is nested below an equality predicate. ### Why are the changes needed? The current check only inspects top-level plan expressions. It detects an `EXISTS` subquery when the subquery is the expression root, but misses scalar and other subqueries nested inside larger expressions. A recursive view definition can therefore be accepted and fail later during view resolution with the maximum nested-view-depth error. ### Does this PR introduce _any_ user-facing change? Yes. `CREATE OR REPLACE VIEW` and `ALTER VIEW` now reject recursive references from subqueries nested inside larger expressions with `RECURSIVE_VIEW`, matching root-level subquery behavior. ### How was this patch tested? Added a regression assertion to `SimpleSQLViewSuite` and ran: ``` SPARK_LOCAL_IP=127.0.0.1 build/sbt \ 'sql/testOnly org.apache.spark.sql.execution.SimpleSQLViewSuite -- -z "correctly handle a cyclic view reference"' build/sbt 'sql/scalastyle' 'sql/Test/scalastyle' ``` ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Codex (GPT-5) Closes #58146 from manuzhang/codex/fix-nested-view-cycle. Authored-by: Manu Zhang <mauzhang@apache.org> Signed-off-by: Wenchen Fan <wenchen@databricks.com> (cherry picked from commit 02c21a6) Signed-off-by: Wenchen Fan <wenchen@databricks.com>
### What changes were proposed in this pull request? Update `ViewHelper.checkCyclicViewReference` to traverse complete expression trees when looking for subquery expressions. Add a regression case where a scalar subquery that references the altered view is nested below an equality predicate. ### Why are the changes needed? The current check only inspects top-level plan expressions. It detects an `EXISTS` subquery when the subquery is the expression root, but misses scalar and other subqueries nested inside larger expressions. A recursive view definition can therefore be accepted and fail later during view resolution with the maximum nested-view-depth error. ### Does this PR introduce _any_ user-facing change? Yes. `CREATE OR REPLACE VIEW` and `ALTER VIEW` now reject recursive references from subqueries nested inside larger expressions with `RECURSIVE_VIEW`, matching root-level subquery behavior. ### How was this patch tested? Added a regression assertion to `SimpleSQLViewSuite` and ran: ``` SPARK_LOCAL_IP=127.0.0.1 build/sbt \ 'sql/testOnly org.apache.spark.sql.execution.SimpleSQLViewSuite -- -z "correctly handle a cyclic view reference"' build/sbt 'sql/scalastyle' 'sql/Test/scalastyle' ``` ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Codex (GPT-5) Closes #58146 from manuzhang/codex/fix-nested-view-cycle. Authored-by: Manu Zhang <mauzhang@apache.org> Signed-off-by: Wenchen Fan <wenchen@databricks.com> (cherry picked from commit 02c21a6) Signed-off-by: Wenchen Fan <wenchen@databricks.com>
What changes were proposed in this pull request?
Update
ViewHelper.checkCyclicViewReferenceto traverse complete expression trees whenlooking for subquery expressions. Add a regression case where a scalar subquery that
references the altered view is nested below an equality predicate.
Why are the changes needed?
The current check only inspects top-level plan expressions. It detects an
EXISTSsubquery when the subquery is the expression root, but misses scalar and other subqueries
nested inside larger expressions. A recursive view definition can therefore be accepted
and fail later during view resolution with the maximum nested-view-depth error.
Does this PR introduce any user-facing change?
Yes.
CREATE OR REPLACE VIEWandALTER VIEWnow reject recursive references fromsubqueries nested inside larger expressions with
RECURSIVE_VIEW, matching root-levelsubquery behavior.
How was this patch tested?
Added a regression assertion to
SimpleSQLViewSuiteand ran:Was this patch authored or co-authored using generative AI tooling?
Generated-by: Codex (GPT-5)