spark-test commented on code in PR #60246:
URL: https://github.com/apache/spark/pull/60246#discussion_r4519438093
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExistingRDD.scala:
##########
@@ -205,11 +205,29 @@ object LogicalRDD extends Logging {
val (stats, constraints) = rewriteStatsAndConstraints(logicalPlan,
optimizedPlan)
+ // The executed plan can output other attributes than
`logicalPlan.output`, e.g. when
+ // `RemoveRedundantAliases` removes a same-name root alias. So map its
partitioning and ordering
+ // onto `logicalPlan.output` by position, and drop a partitioning that
still refers to other
+ // attributes, e.g. the input attributes of `SerializeFromObjectExec`.
+ val toOutput = AttributeMap(executedPlan.output.zip(logicalPlan.output))
+ def remap[E <: Expression](e: E): E =
+ e.transform { case a: Attribute => toOutput.getOrElse(a, a)
}.asInstanceOf[E]
+ val partitioning = firstLeafPartitioning(executedPlan.outputPartitioning)
match {
+ case p: Expression =>
+ val remapped = remap(p)
+ if (remapped.references.subsetOf(AttributeSet(logicalPlan.output))) {
+ remapped
+ } else {
+ UnknownPartitioning(p.numPartitions)
+ }
+ case p => p
+ }
+
LogicalRDD(
originDataset.logicalPlan.output,
rdd,
- firstLeafPartitioning(executedPlan.outputPartitioning),
- executedPlan.outputOrdering,
+ partitioning,
+ executedPlan.outputOrdering.map(remap),
Review Comment:
**Non-blocking (P2):** The partitioning above is withdrawn when it still
names an attribute outside `logicalPlan.output`, but the ordering here is only
remapped. `remap` leaves every attribute outside `executedPlan.output`
unchanged, so a sort order whose child or `sameOrderExpressions` names one
still reaches the checkpoint.
One root that reports such an ordering is a column-pruned scan of a cached
Dataset. `InMemoryScans` plans it as a bare `InMemoryTableScanExec` with no
`ProjectExec`, and the scan maps the cached plan's ordering onto the cached
relation's full output:
```scala
val r = spark.range(0, 16, 1, 1)
val c = r.select($"id", ($"id" % 2).as("p")).cache()
val cp = c.select($"p").localCheckpoint() // output [p], ordering [r.id ASC]
broadcast(r).join(cp, r("id") % 2 === cp("p"))
.select(r("id"), count(lit(1)).over(Window.partitionBy(r("id"))))
```
Traced through the code, not run: the checkpoint keeps `SinglePartition` and
the ordering `r.id ASC`, the BuildLeft broadcast hash join passes that ordering
up, and the window over `r("id")` gets neither an exchange nor a sort. It then
returns count 1 for each of the 128 rows instead of count 8, which is the
description's third example with a column-pruned cache scan in place of the
stripped alias. A `sortWithinPartitions` or a global `orderBy` on `r("id")`
over the same join loses its sort to `RemoveRedundantSorts` in the same way.
The new test's name says a checkpoint no longer uses the ordering over its
source's columns, which does not yet hold for these orderings.
**Recommended change:** In `LogicalRDD.fromDataset`, check the remapped
ordering against `logicalPlan.output` the way the partitioning already is: drop
each sort order's children, its child and its `sameOrderExpressions`, that
still reference an attribute outside the output, promote the first remaining
child to be the sort order's child, and keep only the leading sort orders that
still have a child, as `AliasAwareQueryOutputOrdering` does for an operator's
ordering. Update the comment above the remap to say the ordering is checked
too, and add Dataset checkpoint tests for a checkpoint of a column-pruned cache
scan whose ordering names the dropped column.
**Why this works:** The checkpoint's declared ordering then names only
attributes the relation outputs, so no later query can match it against a live
column of the source, whatever executed root produced it; keeping only the
leading valid sort orders preserves every claim the data still satisfies,
because a sort order that cannot be expressed invalidates the ones after it.
**Scope:** Check the remapped ordering in `LogicalRDD.fromDataset` the way
the PR already checks the partitioning, and cover it with Dataset checkpoint
tests.
**Compatibility:** Orderings that name only the checkpoint's output,
including remapped stripped-alias orderings, are reported exactly as before.
**Risks:** A checkpoint whose stale ordering happened to be valid, such as
one equi-joined back on the very column the ordering names, now gets the sort
it skipped before; that is the cost of correct results the PR already accepts,
and since the PR adds no flag, only reverting the change rolls it back.
**Constraints:** Keep the leading-prefix rule: once a sort order has no
child left over the output, drop it and every sort order after it. Apply the
check after the remap, so stripped-alias orderings stay on the checkpoint's own
columns and only references still outside `logicalPlan.output` are dropped.
**Exclusions:**
- The cache scan's own export of a cached plan's partitioning and ordering,
which the repair for cached Datasets covers.
**Success:** Every sort order of a `LogicalRDD` that `fromDataset` builds
references only its output, `sameOrderExpressions` included. A checkpoint of a
column-pruned cache scan, joined back with the Dataset its dropped column
belongs to, returns correct window counts and correctly ordered rows for a sort
on that column. A stripped-alias checkpoint keeps its ordering over its own
column.
**Verification:**
- **Regression:** A checkpoint of a column-pruned cache scan whose ordering
names the dropped column, streamed through a broadcast hash join with the
Dataset that column belongs to, returns the correct counts for a window
partitioned by that column and correctly ordered rows for a sort on it, and
fails on the current head.
- **Behavior:** A checkpoint's reported ordering keeps only the leading sort
orders that reference its output, with any `sameOrderExpressions` entry that
names an attribute outside the output removed.
- **Compatibility:** A stripped-alias checkpoint still reports its ordering
over its own column, and a checkpoint whose executed and analyzed outputs share
attributes still reports the executed plan's ordering.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExistingRDD.scala:
##########
@@ -205,11 +205,29 @@ object LogicalRDD extends Logging {
val (stats, constraints) = rewriteStatsAndConstraints(logicalPlan,
optimizedPlan)
+ // The executed plan can output other attributes than
`logicalPlan.output`, e.g. when
+ // `RemoveRedundantAliases` removes a same-name root alias. So map its
partitioning and ordering
+ // onto `logicalPlan.output` by position, and drop a partitioning that
still refers to other
+ // attributes, e.g. the input attributes of `SerializeFromObjectExec`.
+ val toOutput = AttributeMap(executedPlan.output.zip(logicalPlan.output))
+ def remap[E <: Expression](e: E): E =
+ e.transform { case a: Attribute => toOutput.getOrElse(a, a)
}.asInstanceOf[E]
+ val partitioning = firstLeafPartitioning(executedPlan.outputPartitioning)
match {
+ case p: Expression =>
+ val remapped = remap(p)
+ if (remapped.references.subsetOf(AttributeSet(logicalPlan.output))) {
Review Comment:
**Non-blocking (P2):** This check covers only the checkpoint form, because
cached data never goes through `fromDataset`.
`InMemoryTableScanExec.outputPartitioning` and `outputOrdering`, which this PR
leaves unchanged, rewrite the cached plan's properties through
`AttributeMap(cachedPlan.output.zip(relation.output))` and keep every other
attribute. So the new test's typed-map query still returns wrong results with
`cache()` in place of `localCheckpoint()`:
```scala
val src = spark.range(0, 16, 1, 4)
val c2 = src.as[Long].map(x => x).toDF().cache()
broadcast(src).join(c2, src("id") % 2 === c2("value") %
2).groupBy(src("id")).count()
```
Traced through the code, not run: the cached plan reports
`RangePartitioning(src.id ASC, 4)`, because `SerializeFromObjectExec`,
`MapElementsExec` and `DeserializeToObjectExec` forward their child's
partitioning. In the join, `DeduplicateRelations` keeps the left `src`'s `id`
and renews the copy of the range inside `c2`'s plan, and the cache lookup still
substitutes the cached relation. The scan therefore reports a partitioning over
the live `src.id`, the broadcast hash join passes it up, and the final
aggregate skips its shuffle. The query then returns 64 rows with count 2, the
numbers the description gives for the checkpoint form on the base branch,
instead of 16 rows with count 8. The same applies to a cached generator that
drops its child's columns, to `mapInPandas` and `mapInArrow`, and to a cache
whose root is a column-pruned scan of another cache. After this PR, the
checkpointed and cached forms of the same query return different answers.
**Recommended change:** In `InMemoryTableScanExec`, after mapping the cached
plan's partitioning and ordering onto `relation.output`, withdraw each claim
that still references an attribute outside `relation.output`. Flatten a
`PartitioningCollection` and keep every leaf partitioning whose references all
lie in `relation.output`, reporting the one remaining leaf or a collection of
the remaining leaves, and report `UnknownPartitioning` of the same partition
count only when no leaf remains, as `PartitioningPreservingUnaryExecNode` does.
For the ordering, drop each sort order's children that reference an attribute
outside `relation.output`, promote the first remaining child, and keep only the
leading sort orders that still have a child, as `AliasAwareQueryOutputOrdering`
does for an operator's ordering. Add Dataset cache tests for the new checkpoint
test's typed-map query run over `cache()`, for a cached plan whose ordering
names a column outside its output, and for a cached plan whos
e partitioning collection mixes a leaf over its own columns with a leaf over a
column it does not output.
**Why this works:** The scan's declared partitioning and ordering then name
only attributes of the cached relation in the current query, so an attribute
left over from when the cache was built can no longer match a live column of
the source. Claims over the relation's own columns keep their current behavior,
including claims over columns the scan prunes and the leaves of a partitioning
collection that name only the relation's columns, even when other leaves of the
same collection are withdrawn.
**Scope:** Withdraw, in the cache scan, each partitioning leaf and sort
order that still names attributes outside the cached relation's output, and
cover it with Dataset cache tests.
**Compatibility:** Cached plans whose partitioning and ordering name only
their own output, including aliased output, report them exactly as before, and
a partitioning collection keeps every leaf that names only the cached
relation's columns.
**Risks:** A cached Dataset whose stale partitioning or ordering happened to
be valid, such as an identity typed map equi-joined back with its source on
that column, now gets the exchange or sort it skipped before; that is the cost
of correct results, and since neither the PR nor this repair adds a flag, only
reverting the change rolls it back.
**Constraints:** Check against the cached relation's output in the current
query, `relation.output`, not the scan's pruned output, so claims over the
relation's own columns keep their current behavior. Report the cached plan's
partitioning and ordering unchanged when they name only the cached plan's
output, so cached data partitioned or sorted by its own columns still avoids
exchanges and sorts. Check a partitioning collection leaf by leaf, so a stale
leaf does not take a valid leaf over the relation's own columns with it.
**Exclusions:**
- The checkpoint boundary's ordering check, which the repair for checkpoint
orderings covers.
**Success:** The cache scan reports no partitioning or ordering that
references an attribute outside the cached relation's output. The new
checkpoint test's typed-map query, run over `cache()` instead of
`localCheckpoint()`, returns 16 rows with count 8. A cached Dataset partitioned
or sorted by its own columns keeps that partitioning and ordering, including a
leaf over its own columns in a partitioning collection whose other leaves are
withdrawn.
**Verification:**
- **Regression:** The new checkpoint test's typed-map query over a cached
Dataset instead of a checkpoint, with the cached side streamed through a
broadcast hash join with its source and aggregated by the source's column,
returns one row per source value with count 8, and fails on the current head.
- **Behavior:** A cached Dataset whose cached plan reports an ordering over
a column outside its output, such as a cache of a column-pruned scan of another
cache, no longer lets a window or sort over that column skip its sort when it
is joined with the Dataset that column belongs to.
- **Compatibility:** A cached Dataset repartitioned or sorted by its own
columns, including through an alias, still reports that partitioning and
ordering, and a cached generator that consumes one key of an inner sort-merge
join and outputs only the other keeps the partitioning over the key it outputs,
so queries over those columns plan no extra exchange or sort, with adaptive
execution on and off.
--
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]