xumingming opened a new pull request, #60671: URL: https://github.com/apache/spark/pull/60671
### What changes were proposed in this pull request? - Track CTE definition precedence using traversal order across all scopes instead of the definition's index within its local `WithCTE`. - Prevent predicates that reference CTE definitions in nested or later scopes from being pushed into a definition where those references are invalid. - Add a regression test with nested `MATERIALIZED` CTEs and scalar subqueries. ### Why are the changes needed? Local `WithCTE` indices do not establish visibility across nested scopes. A nested definition can receive a lower precedence than an outer definition even though the outer definition cannot reference it. The forward-reference guard then allows a cross-scope predicate to be pushed into the outer CTE definition, and a subsequent optimizer pass fails with a key-not-found error. ### Does this PR introduce _any_ user-facing change? Yes. Queries whose CTE-reference predicates contain scalar subqueries with nested `MATERIALIZED` CTEs now optimize and return results instead of failing during predicate pushdown. ### How was this patch tested? Added the regression test `SPARK-61957: predicate referencing a nested MATERIALIZED CTE stays in its scope` to `CTEInlineSuite`. The test checks the query results and verifies that no CTE definition absorbs a predicate containing a scalar subquery from another CTE scope. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Pi -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
