[SPARK-53738][SQL][3.5] Fix planned write when query output contains foldable orderings #52697
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 #52584 to branch-3.5
What changes were proposed in this pull request?
This is the second try of #52474, following the suggestion from cloud-fan
This PR fixes a bug in
plannedWrite, where thequeryhas foldable orderings in the partition columns.The evaluation of
FileFormatWriter.orderingMatchedfails becauseSortOrder(Literal)is eliminated byEliminateSorts.Why are the changes needed?
V1Writeswill override the custom sort order when the query output ordering does not satisfy the required ordering. Before SPARK-53707, when the query's output contains literals in partition columns, the judgment produces a false-negative result, thus causing the sort order not to take effect.SPARK-53707 partially fixes the issue on the logical plan by adding a
Projectof query inV1Writes.Before SPARK-53707
After SPARK-53707
Note, note the issue still exists because there is another place to check the ordering match again in
FileFormatWriter.This PR fixes the issue thoroughly, with new UTs added.
Does this PR introduce any user-facing change?
Yes, it's a bug fix.
How was this patch tested?
New UTs are added.
Was this patch authored or co-authored using generative AI tooling?
No.