dongjoon-hyun opened a new pull request, #60245:
URL: https://github.com/apache/spark/pull/60245

   ### What changes were proposed in this pull request?
   
   This PR aims to fix a correctness issue where `LogicalRDD` keeps a 
constraint of its source plan on the source plan's exprId instead of moving it 
onto its own output.
   
   `LogicalRDD.buildOutputAssocForRewrite` and 
`buildOutputAssocForRewriteWithNewOutput` now return an `AttributeMap`, which 
is keyed by exprId, instead of a `Map` keyed by full `AttributeReference` 
equality. The positional name and type checks and the size check stay 
unchanged, so the same outputs are accepted or rejected as before. Only the 
lookup in `rewriteStatistics` and `rewriteConstraints` changes.
   
   `rewriteStatsAndConstraints` uses these helpers in two places.
   - `LogicalRDD.fromDataset`, which builds `Dataset.checkpoint`, 
`Dataset.localCheckpoint` and the input of the `foreachBatch` sink.
   - `UnionLoopExec`, which builds the input of every recursive CTE iteration.
   
   ### Why are the changes needed?
   
   This is a correctness issue. The helpers pair the optimized output with the 
`LogicalRDD` output by position, but the lookup compares full attributes, 
including the qualifier and the nullability. When the optimizer changes the 
root exprIds, for example when `RemoveRedundantAliases` removes a same-name 
root alias, a constraint attribute that differs from the optimized output only 
in qualifier misses the map and keeps the exprId of the source plan.
   
   ```scala
   val df = spark.range(16).as("t").where($"t.id" > 5)
   val cp = df.select($"id".as("id")).localCheckpoint()
   // Optimized output: id#0, constraint: t.id#0 > 5, checkpoint output: id#1
   // `Map(id#0 -> id#1)` misses `t.id#0`, so `cp` keeps the constraint on the 
column of `df`.
   ```
   
   **Issue 1: A join of a checkpoint with its source returns wrong results.** 
`EliminateOuterJoin` reads the stale constraint as a null filter on the left 
side and turns the RIGHT OUTER JOIN into an INNER JOIN.
   
   ```scala
   // Issue 1: It should return 10, but returns 9 because the row (null, 15) is 
dropped.
   scala> df.join(cp, df("id") === cp("id") + 1, "right_outer").where(cp("id") 
> 0).count()
   res0: Long = 9
   ```
   
   **Issue 2: A recursive CTE fails.** The constraint `t.lvl < 5` of an 
iteration keeps the exprId of `lvl + 1 AS lvl`, so the next iteration sees it 
as a constraint of its own new `lvl` column, and `PruneFilters` removes the 
filter that stops the recursion. This is the symptom that SPARK-52353 fixed, in 
a case that its fix does not cover.
   
   ```sql
   -- Issue 2: It should return [1000, 4], but fails with 
RECURSION_LEVEL_LIMIT_EXCEEDED.
   WITH RECURSIVE r(k, lvl) AS (
     SELECT id AS k, 0 AS lvl FROM range(200)
     UNION ALL
     SELECT * FROM (SELECT k, lvl + 1 AS lvl FROM r LIMIT 100000) t WHERE t.lvl 
< 5)
   SELECT count(*), max(lvl) FROM r
   ```
   
   Column stats go through the same lookup, so they are keyed by exprId as well.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, this fixes wrong results and a query failure. With the default 
configurations, both issues above are reproduced on Apache Spark 4.0.4 (Issue 1 
only, because it has no recursive CTE), 4.1.3, 4.2.0 and 4.3.0. The checkpoint 
path has used this lookup since 
[v3.4.0](https://github.com/apache/spark/releases/tag/v3.4.0) (2023-04-07), and 
the recursive CTE path since 
[v4.1.0](https://github.com/apache/spark/releases/tag/v4.1.0) (2025-12-11).
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test cases.
   - `DataFrameSuite`: a join of a checkpoint with its source 
(`LogicalRDD.fromDataset`).
   - `cte-recursion.sql`: a recursive CTE whose filter on a subquery alias 
stays on top of the recursion (`UnionLoopExec`).
   
   Without the change in `buildOutputAssocForRewrite`, the `DataFrameSuite` 
test fails. Without the change in `buildOutputAssocForRewriteWithNewOutput`, 
the `cte-recursion.sql` test fails.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   


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