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

   ### What changes were proposed in this pull request?
   
   `ReplaceCTERefWithRepartition` materializes a non-inlined CTE through a 
round-robin shuffle that all references reuse, but skips that shuffle when the 
definition already ends in a `RepartitionOperation`. A coalesce (`Repartition` 
with `shuffle = false`) is planned as `CoalesceExec`, not as an exchange, so 
there is nothing to reuse and every reference evaluates the CTE body again.
   
   For a coalesce root this PR puts the round-robin shuffle below the coalesce 
and keeps the coalesce on top, so the CTE still has at most that many 
partitions (one for `COALESCE(1)`). `canSkipExtraRepartition` now checks 
`shuffle` explicitly. Other roots are unchanged.
   
   A shuffling `Repartition(1)` on top would not work: its single-partition 
exchange can be rewritten differently in each reference by 
`EnsureRequirements`, which loses reuse in the same way.
   
   ### Why are the changes needed?
   
   A non-deterministic CTE referenced more than once, or a CTE marked `AS 
MATERIALIZED`, is supposed to be evaluated once. With a coalesce at the end of 
the body it is evaluated once per reference, so the references see different 
rows:
   
   ```python
   spark.udf.register("random_uuid", udf(lambda: 
str(uuid.uuid4())).asNondeterministic())
   spark.sql("""
     WITH t AS (SELECT /*+ COALESCE(1) */ id, random_uuid() AS u FROM range(0, 
1000, 1, 4))
     SELECT count(*) FROM t a JOIN t b ON a.u = b.u
   """).show()
   # 0 instead of 1000 (AQE on and off; 4.0.1, 4.1.4, 4.3.0, master)
   ```
   
   Seeded built-ins (`rand()`, `uuid()`, `shuffle()`) produce the same values 
on re-evaluation, so the problem shows with non-deterministic UDFs and other 
unseeded expressions.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, a correctness fix: such CTEs are now evaluated once, as without the 
coalesce. Plan change: a non-inlined CTE whose body ends in a coalesce now 
materializes through a round-robin shuffle below the coalesce (before, the plan 
above the body could be shuffle-free); `COALESCE(n)` over `REPARTITION(m)` gets 
a second shuffle when the coalesce survives optimization (m > n). The CTE keeps 
at most the coalesce's number of partitions.
   
   ### How was this patch tested?
   
   New test in `CTEInlineSuite` (runs with AQE on and off): `COALESCE(1)` and 
`COALESCE(2)` bodies in a self-join, with one reference joined to another table 
first (sort-merge join), and with `AS MATERIALIZED`; it checks the answers and 
that each reference reads a round-robin shuffle below the coalesce. A 
deterministic `AS MATERIALIZED` body takes the same shape. A positive control 
checks that a `REPARTITION(3)` root still gets no extra shuffle. The test fails 
without the change.
   
   Also ran `InlineCTESuite`, `CTEInlineSuiteAEOff/AEOn`, `CTEHintSuite` 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