[SPARK-52565] [SQL] Enforce ordinal resolution before other sort order expressions #51268
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.
What changes were proposed in this pull request?
The following query passes, but should fail:
As it can be seen in the query, we try to order by 2 which is an ordinal in this case and it should fail with
ORDER_BY_POS_OUT_OF_RANGE
. It doesn't because of Analyzer rule ordering issue.We first resolve the sort order
col1
using theResolveReferencesInSort
rule. It turns out to be a missing input and thus we add it to theProject
below and add anotherProject
on top of sort to retain original output. At that point, plan looks like:Then we go to
ResolveOrdinalInOrderByAndGroupBy
and successfully resolve theUnresolvedOrdinal
because there are two elements in theProject
below. This shouldn't happen and we should fail.In this issue I propose that we fix it by enforcing ordinal resolution before other sort order expressions.
Why are the changes needed?
To correct behavior of explained query patterns.
Does this PR introduce any user-facing change?
Yes, query that passed will fail (as it should).
How was this patch tested?
Added tests.
Was this patch authored or co-authored using generative AI tooling?
No.