[SPARK-46485][SQL][3.5] V1Write should not add Sort when not needed #52692
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport #44458 to branch-3.5.
Justification: it fixes a hidden bug (until exposed by #44429) that has existed since 3.4.
What changes were proposed in this pull request?
In
V1Writes, we try to avoid adding Sort if the output ordering always satisfies. However, the code is completely broken with two issues:SortOrderas the child of anotherSortOrderand compare, which always returns false.empty2null, we change the query output attribute id and the sort order never matches.It's not a big issue as we still have QO rules to eliminate useless sorts, but #44429 exposes this problem because the way we optimize sort is a bit different. For
V1Writes, we should always avoid adding sort even if the number of ordering key is less, to not change the user query.Why are the changes needed?
fix code mistakes.
Does this PR introduce any user-facing change?
no
How was this patch tested?
updated test
Was this patch authored or co-authored using generative AI tooling?
no