[SPARK-55535][SQL][FOLLOW-UP] Fix OrderedDistribution handling and minor improvements to EnsureRequirements#54727
Conversation
…other improvements to `EnsureRequirements`
| .map(_.asInstanceOf[Attribute]) | ||
| val keyRowOrdering = RowOrdering.create(o.ordering, attrs) | ||
| val keyOrdering = keyRowOrdering.on((t: InternalRowComparableWrapper) => t.row) | ||
| val sorted = satisfyingKeyedPartitioning.partitionKeys.sorted(keyOrdering) |
There was a problem hiding this comment.
The bug is that sorted should be distict as well (as it was before the refactor), but after the refactor we can do better:
- We can avoid adding a grouping operator entirelly when the non-grouped
satisfyingKeyedPartitioning.partitionKeyssatisfies the required sort order. - Or if it doesn't, then we need to add a
GroupPartitionsExecoperator, but we can avoid coalescing partitions in the operator with settingapplyPartialClustering.
| val dfWithDuplicate = sql(s"SELECT id FROM testcat.ns.$items i ORDER BY id") | ||
|
|
||
| val expectedWithDuplicate = Seq(1, 2, 2, 3).map(Row(_)) | ||
| checkAnswer(dfWithDuplicate, expectedWithDuplicate) |
There was a problem hiding this comment.
This is the bug test as it returned Seq(1, 2, 2, 2, 2, 3) before the fix.
| df -> Seq.empty, | ||
| reverseDf -> Seq(3), | ||
| dfWithDuplicate -> Seq.empty, | ||
| reverseDfWithDuplicate -> Seq(4) |
There was a problem hiding this comment.
This is a minor improvement compared to pre-refactor. Although we need to add GroupPartitions to reorder the 4 partitions by their key in descending order, we don't need to coalesce them into 3.
|
@cloud-fan, @dongjoon-hyun, @viirya, @szehon-ho, @chirag-s-db this is a follow-up PR to #54330. |
OrderedDistribution handling and other improvements to EnsureRequirementsOrderedDistribution handling and minor improvements to EnsureRequirements
| // shuffles or group partitions | ||
| Seq(Row(null, 3), Row(10.0, 2), Row(15.5, null), | ||
| Row(15.5, 3), Row(40.0, 1), Row(41.0, 1))) | ||
| Row(15.5, 3), Row(40.0, 1), Row(41.0, 1)), 0) |
There was a problem hiding this comment.
This is a minor improvement compared to pre-refactor.
dongjoon-hyun
left a comment
There was a problem hiding this comment.
+1, LGTM (Pending CIs). Thank you, @peter-toth .
viirya
left a comment
There was a problem hiding this comment.
One Minor Concern
applyPartialClustering=true is semantically overloaded. This flag was designed for the partial clustering join optimization, but here it's being used purely as a way to say "distribute one input split per output partition." The actual partial clustering join logic (deciding which side to replicate based on stats, join type checks, etc.) is completely irrelevant here — the flag just happens to switch alignToExpectedKeys into the "one split per task" branch instead of "all splits into one task."
This is correct but confusing. Someone reading GroupPartitionsExec(..., applyPartialClustering=true) in an ORDER BY context would reasonably wonder what partial clustering has to do with sorting. A cleaner fix might be a dedicated boolean like distributeInputPartitions, but that's a bigger change and the current approach works correctly.
A comment at the call site explaining why applyPartialClustering=true is used here would at minimum help, even if renaming the parameter is too big a change.
viirya
left a comment
There was a problem hiding this comment.
Small, well-targeted fix. The correctness bug was real and the fix is correct. The main review note is the semantic overloading of applyPartialClustering.
Yeah, during the refactor I too was thinking about whether keeping the 2 flags ( |
The suggestion of a single distributePartitions flag is cleaner and more honest about what's actually happening: distributePartitions=false → put all splits for a key into every expected output task (group when numSplits=1, replicate when numSplits>1) This also resolves the naming confusion — distributePartitions describes the mechanical behavior of alignToExpectedKeys without implying anything about joins or skew handling. It would read naturally in both the partial clustering context and the OrderedDistribution sorting context. |
fad74ff does the rename. I added 2 more commits: |
|
Thank you @dongjoon-hyun and @viirya for the review. Merged to |
What changes were proposed in this pull request?
This is a follow-up PR to #54330 to fix
OrderedDistributionhandling inEnsureRequirementsso as to avoid a correctness bug. The PR contains minor improvements toEnsureRequirementsand configuration docs updates as well.Why are the changes needed?
To fix a correctness bug introduced with the refactor.
Does this PR introduce any user-facing change?
Yes, but the refactor (#54330) hasn't been released.
How was this patch tested?
Added new UT.
Was this patch authored or co-authored using generative AI tooling?
No.