Skip to content
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

opt: add support for NULLS LAST with DISTINCT ON #93567

Merged
merged 1 commit into from
Dec 14, 2022

Conversation

rytaft
Copy link
Collaborator

@rytaft rytaft commented Dec 14, 2022

This commit adds an exception for the requirement that the ORDER BY clause of a DISTINCT ON query must contain only columns in the ON clause. Specifically, it allows expressions of the form col IS NULL, where col is one of the ON columns. This is needed to support NULLS LAST, since we implement NULLS LAST by synthesizing a col IS NULL ordering column.

The approach feels a bit hacky, but it seems like the smallest, lowest risk option available.

Fixes #90763

Release note (bug fix): Fixed an issue where DISTINCT ON queries would fail with the error "SELECT DISTINCT ON expressions must match initial ORDER BY expressions" when the query included an ORDER BY clause containing ASC NULLS LAST or DESC NULLS FIRST.

This commit adds an exception for the requirement that the ORDER
BY clause of a DISTINCT ON query must contain only columns in the
ON clause. Specifically, it allows expressions of the form
`col IS NULL`, where col is one of the ON columns. This is needed
to support NULLS LAST, since we implement NULLS LAST by synthesizing
a `col IS NULL` ordering column.

The approach feels a bit hacky, but it seems like the smallest,
lowest risk option available.

Fixes cockroachdb#90763

Release note (bug fix): Fixed an issue where DISTINCT ON queries
would fail with the error "SELECT DISTINCT ON expressions must
match initial ORDER BY expressions" when the query included an
ORDER BY clause containing ASC NULLS LAST or DESC NULLS FIRST.
@rytaft rytaft requested review from msirek and michae2 December 14, 2022 02:50
@rytaft rytaft requested a review from a team as a code owner December 14, 2022 02:50
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@msirek msirek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix!
:lgtm:

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2)

Copy link
Collaborator Author

@rytaft rytaft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TFTR!

bors r+

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2)

@craig
Copy link
Contributor

craig bot commented Dec 14, 2022

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DISTINCT ON query fails when specifying nulls order
3 participants