akupchinskiy opened a new pull request, #60241:
URL: https://github.com/apache/spark/pull/60241

   ### What changes were proposed in this pull request?
   
   When a reference's output attributes differ from the definition's (e.g. the 
second reference of a CTE), `ReplaceCTERefWithRepartition` gives a copy of the 
non-inlined CTE body new attributes with `DeduplicateRelations`. 
`DeduplicateRelations` also renews the plans of subquery expressions. For a 
`DynamicPruningSubquery` that renews the build-side plan but not `buildKeys`, 
which still point at the old plan's output, so the plan becomes unresolved.
   
   This PR makes `DeduplicateRelations` skip the plan of a 
`DynamicPruningSubquery`: its build plan, build keys and expression ID stay as 
they are, and its pruning key is still remapped with the rest of the copy. The 
build side is self-contained, so the copies can share it, as they already do 
when a filter is pushed through a `Union`.
   
   ### Why are the changes needed?
   
   A non-inlined CTE (non-deterministic and referenced more than once, or `AS 
MATERIALIZED`) whose body gets a dynamic partition pruning filter can fail with 
default settings (this repro on 4.3.0; on master the new tests below fail the 
same way):
   
   ```python
   spark.range(0, 1000, 1, 4).selectExpr("id % 7 AS g", "id AS 
u").write.partitionBy("g").parquet("/tmp/fact")
   spark.range(0, 1000, 1, 4).selectExpr("id % 7 AS g", "id % 5 AS 
x").write.parquet("/tmp/dim")
   spark.read.parquet("/tmp/fact").createOrReplaceTempView("fact")
   spark.read.parquet("/tmp/dim").createOrReplaceTempView("dim")
   spark.sql("""
     WITH t AS (
       SELECT f.g, f.u, rand() AS r
       FROM fact f JOIN (SELECT DISTINCT g FROM dim WHERE x = 1 AND g < 3) d ON 
f.g = d.g)
     SELECT count(*) FROM t a JOIN t b ON a.u = b.u
   """).show()
   # [PLAN_VALIDATION_FAILED_RULE_IN_BATCH] Rule 
...ReplaceCTERefWithRepartition ... generated an invalid plan:
   # The plan was previously resolved and now became unresolved.
   ```
   
   Older versions failed later with `Couldn't find ... in [...]` in 
`PlanAdaptiveDynamicPruningFilters` (see the JIRA).
   
   The earlier attempt, #50490, removed the deduplication from 
`ReplaceCTERefWithRepartition`. Emulated on master, that breaks 
`CTEInlineSuite` "MATERIALIZED CTE with an inner CTE correlated to its own 
relations" ("An alias reuses the same expression ID").
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, such queries now run instead of failing.
   
   ### How was this patch tested?
   
   Two new tests in `DynamicPartitionPruningSuiteBase`: a `MATERIALIZED` CTE 
over a fact/dimension join referenced twice in a `UNION ALL`, and the same CTE 
joined with itself. They check the answers and that a pruning subquery is 
planned. Both fail without the change in the V1 suites, AQE on and off. In the 
V2 suites they pass either way: the build-side scan attributes in these queries 
are not renewed by the copy.
   
   Also ran the other `DynamicPartitionPruning*` suites, `CTEInlineSuite`, 
`CTEHintSuite`, `InlineCTESuite`, `AnalysisSuite` and the TPC-DS plan stability 
suites (golden files unchanged).
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5), reviewed with OpenAI Codex 
(gpt-6-astra)
   


-- 
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]

Reply via email to