spark-test commented on code in PR #60245:
URL: https://github.com/apache/spark/pull/60245#discussion_r4519383562


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExistingRDD.scala:
##########
@@ -229,7 +229,9 @@ object LogicalRDD extends Logging {
     }.toMap
 
     if (rewrite.size == source.size) {
-      Some(rewrite)
+      // Keyed by exprId, because the attributes in the stats and the 
constraints can differ from
+      // `source` in qualifier or nullability.
+      Some(AttributeMap(rewrite))

Review Comment:
   **Non-blocking (P2):** This moves a checkpoint's inherited constraints and 
column statistics onto its own output, but `fromDataset` still copies 
`executedPlan.outputPartitioning` and `executedPlan.outputOrdering` unmapped 
(lines 211-212), while its output comes from the analyzed plan (line 209). When 
the optimizer changes the root exprIds, as `RemoveRedundantAliases` does for 
the new DataFrameSuite fixture, those two properties keep naming the source 
Dataset's columns. `newInstance` already remaps all four properties through one 
exprId-keyed map.
   
   So joining such a checkpoint with its source can still return wrong results, 
through partitioning or ordering instead of constraints. Reading the code with 
the new test's `df` and `cp`:
   
   ```scala
   cp.join(broadcast(df), cp("id") % 2 === df("id") % 
2).groupBy(df("id")).count()
   ```
   
   `cp` inherits `RangePartitioning(id#0)` from `RangeExec`, although its own 
column is `id#1`. The broadcast hash join forwards that partitioning from the 
streamed `cp` side, and it satisfies the aggregate's clustering on `df`'s 
`id#0`, so no shuffle is planned. In a two-core session, `cp`'s partitions hold 
{6, 7} and {8..15}, both with rows of both parities, so the query returns 20 
groups instead of 10. Likewise, `sortWithinPartitions(df("id"))` over the same 
join loses its Sort, because `RemoveRedundantSorts` trusts the inherited 
ordering. This was traced through the source, not run. The base branch behaves 
the same way, so this is a path the fix leaves uncovered rather than a 
regression. The new test passes because its right outer join has no grouping or 
sorting requirement that this metadata could satisfy.
   
   The affected inputs are `Dataset.checkpoint` and `localCheckpoint` results 
whose executed root reports partitioning or ordering over the changed columns, 
such as a `RangeExec` root or, with AQE off, a root over a shuffle. The 
`foreachBatch` input goes through the same code, but its function only receives 
the LogicalRDD-backed Dataset, and recursive CTE steps carry partitioning and 
ordering that name no columns, so this review found no way for either to hit it.
   
   **Recommended change:** In LogicalRDD.fromDataset, map the executed plan's 
partitioning and ordering onto the LogicalRDD's own output through the same 
exprId-keyed output association that now re-keys its constraints and 
statistics, as LogicalRDD.newInstance already does for all four properties, and 
keep a partitioning, or an ordering prefix, only when every attribute it names 
belongs to that output, falling back to unknown partitioning otherwise. Add 
DataFrame checkpoint tests next to the new constraint test that join a 
checkpoint with its source, group by or sort within partitions by the source's 
column, and check both the rows and that the checkpoint's partitioning and 
ordering name only its own columns.
   
   **Why this works:** The association pairs each output column of the source's 
optimized plan, which the executed plan shares, with the LogicalRDD's output 
column at the same position and looks it up by exprId, so rewriting the 
partitioning and ordering through it replaces the source plan's root exprIds 
with the LogicalRDD's own. Physical planning then sees the checkpoint's real 
layout on its own columns, and no requirement on the source Dataset's columns 
can be satisfied by it, so EnsureRequirements adds the shuffle and 
RemoveRedundantSorts keeps the Sort that a join with the source needs. The 
reference check keeps that guarantee when the association is unavailable or 
misses an attribute.
   
   **Scope:** Re-key the partitioning and ordering that LogicalRDD.fromDataset 
copies from the executed plan onto the LogicalRDD's own output, drop any part 
that still names a column outside that output, and add checkpoint join tests.
   
   **Compatibility:** Checkpoints, local checkpoints and foreachBatch inputs 
whose source plan keeps its root exprIds, and whose executed partitioning and 
ordering name only the source's output columns, report the same partitioning 
and ordering as on the base branch, so their plans do not change, and 
constraints and statistics keep the mapping this PR introduces.
   
   **Risks:** Plans change for queries over a checkpoint, local checkpoint or 
foreachBatch input whose source had a same-name root alias removed by the 
optimizer: where the input's real partitioning or ordering satisfies a 
requirement on its own columns, a shuffle or sort is no longer added, just as 
for inputs whose exprIds the optimizer kept. No flag rolls this back, and it 
relies on the same partitioning claim the base branch already trusts for those 
inputs. Where an attribute of the executed partitioning or ordering has no 
counterpart in the output, the fallback reports unknown partitioning or a 
shorter ordering. That also happens when the source keeps its root exprIds but 
its root forwards its child's partitioning past a column it drops, such as a 
typed map or a generator. It only removes a claim about a column the LogicalRDD 
does not output, which never satisfied a requirement on its own columns.
   
   **Constraints:** Keep reading the partitioning and ordering from the 
executed plan that fromDataset reads today and only re-point their attributes, 
so the claim describes the checkpointed rows exactly as accurately as the base 
branch's claim does. Use the same positional, exprId-keyed association as the 
constraints and statistics, so all four inherited properties of one LogicalRDD 
name the same output columns.
   
   **Exclusions:**
   - UnionLoopExec's per-step copy keeps the partitioning and ordering of a 
step it has repartitioned or of a one-row projection, and neither names an 
attribute, so it needs no change for this defect.
   
   **Success:** Every LogicalRDD built by LogicalRDD.fromDataset reports a 
partitioning and an ordering whose attributes all belong to its own output. 
When the optimizer changed the source plan's root exprIds, the LogicalRDD's 
partitioning and ordering name its own output columns at the positions the 
executed plan's partitioning and ordering named. Joining a checkpoint with its 
source Dataset and grouping or sorting by the source's columns returns correct 
results, for example one row per group for the new checkpoint test's Dataset. A 
LogicalRDD whose source kept its root exprIds, and whose executed partitioning 
and ordering name only the source's output columns, reports the same 
partitioning and ordering as on the base branch.
   
   **Verification:**
   - **Regression:** Locally checkpoint a Dataset whose same-name root alias 
the optimizer removes, join it with its source through a broadcast hash join on 
a non-key expression, and group by the source's column: the query returns 
exactly one row per group, with adaptive query execution both on and off. Over 
the same join, sortWithinPartitions by the source's column keeps its Sort and 
returns each partition's rows sorted by that column.
   - **Behavior:** For that checkpoint, the logical node's partitioning and 
ordering reference only attributes of its own output, and they name its own 
column where the source's range partitioning and ordering named the source's 
column.
   - **Compatibility:** For a checkpoint whose source keeps its root exprIds 
and whose executed partitioning and ordering name only the source's output 
columns, the reported partitioning and ordering are unchanged, and the existing 
checkpoint self-join, checkpoint join with its source, and recursive CTE cases 
still return their expected rows.



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