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

   ### What changes were proposed in this pull request?
   
   This PR fixes a correctness issue: a checkpointed Dataset can report a 
partitioning and an ordering over the columns of its source Dataset, so a query 
that joins it with the source can return wrong results.
   
   `LogicalRDD.fromDataset`, which `Dataset.checkpoint`, 
`Dataset.localCheckpoint` and `ForeachBatchSink` use, takes `output` from the 
analyzed plan, but copies `outputPartitioning` and `outputOrdering` from the 
executed plan without mapping them onto that output. They can refer to other 
attributes:
   
   1. `RemoveRedundantAliases` removes a same-name root alias, e.g., 
`select($"id".as("id"))` or `toDF("id")` over a column `id`. The executed plan 
then outputs the source column, while `output` is the alias.
   2. Some physical operators report the partitioning of their child, although 
their output does not contain its columns, e.g., `SerializeFromObjectExec` of a 
typed `map`, `GenerateExec` and `MapInBatchExec`.
   
   This PR maps the partitioning and the ordering of the executed plan onto 
`output` by position with an `AttributeMap`, like `rewriteStatsAndConstraints` 
maps the optimized plan for the stats and the constraints. If the partitioning 
still refers to other attributes, this PR replaces it with 
`UnknownPartitioning`, like `PartitioningPreservingUnaryExecNode` drops such a 
partitioning.
   
   ### Why are the changes needed?
   
   The stale attributes are the columns of the source Dataset. When the 
checkpoint is joined with its source, e.g., as the streamed side of a broadcast 
hash join, the join passes the partitioning and the ordering of the checkpoint 
up. Then, an aggregate or a window over the source column skips its shuffle or 
its sort.
   
   ```scala
   // Issue 1: It should return 16 rows with count 8, but returns 128 rows with 
count 1.
   val src = spark.range(0, 16, 1, 4)
   val cp1 = src.select($"id".as("id")).localCheckpoint()
   broadcast(src).join(cp1, src("id") % 2 === cp1("id") % 
2).groupBy(src("id")).count().collect()
   
   // Issue 2: It should return 16 rows with count 8, but returns 64 rows with 
count 2.
   val cp2 = src.as[Long].map(x => x).toDF().localCheckpoint()
   broadcast(src).join(cp2, src("id") % 2 === cp2("value") % 
2).groupBy(src("id")).count().collect()
   
   // Issue 3: It should return count 8 for all 128 rows, but returns count 1 
for all of them.
   import org.apache.spark.sql.expressions.Window
   val src3 = spark.range(0, 16, 1, 1)
   val cp3 = src3.select($"id".as("id")).localCheckpoint()
   broadcast(src3).join(cp3, src3("id") % 2 === cp3("id") % 2)
     .select(src3("id"), 
count(lit(1)).over(Window.partitionBy(src3("id")))).collect()
   ```
   
   These examples use the default configurations, because 
`AdaptiveSparkPlanExec` does not wrap a `range` without an exchange. When the 
source has an exchange, e.g., `repartition`, the same issue happens if AQE is 
disabled when checkpointing, because `AdaptiveSparkPlanExec` reports 
`UnknownPartitioning`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, this fixes wrong results. This issue exists since Apache Spark 2.1.0 
([v2.1.0](https://github.com/apache/spark/releases/tag/v2.1.0), 2016-12-15, 
SPARK-17972), which made a checkpoint keep the partitioning and the ordering of 
the executed plan.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case, which fails without this fix.
   
   ### 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