Skip to content

[SPARK-52565] [SQL] Enforce ordinal resolution before other sort order expressions #51268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mihailoale-db
Copy link
Contributor

@mihailoale-db mihailoale-db commented Jun 24, 2025

What changes were proposed in this pull request?

The following query passes, but should fail:

SELECT col2 FROM values(1,2) ORDER BY 2,col1

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 the ResolveReferencesInSort rule. It turns out to be a missing input and thus we add it to the Project below and add another Project on top of sort to retain original output. At that point, plan looks like:

Project[col1#x]
+- Sort['UnresolvedOrdinal(2), col1#x]
     +- Project[col2#x, col1#x]
          ...

Then we go to ResolveOrdinalInOrderByAndGroupBy and successfully resolve the UnresolvedOrdinal because there are two elements in the Project 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.

@github-actions github-actions bot added the SQL label Jun 24, 2025
@mihailoale-db mihailoale-db marked this pull request as draft June 25, 2025 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant