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: fix error with DISTINCT ON and ORDER BY ASC NULLS LAST #107842

Merged
merged 1 commit into from
Aug 7, 2023

Conversation

rytaft
Copy link
Collaborator

@rytaft rytaft commented Jul 29, 2023

This commit fixes an error that could occur in the optbuilder when planning a query with DISTINCT ON and a non-standard nulls ordering.

The optimizer supports queries with a non-standard nulls ordering by projecting a column with the expression (col IS NULL) and adding it to the ordering. Since we require that DISTINCT ON columns must be the prefix of any ordering columns, we must account for the new ordering column when building DISTINCT ON. A previous bug fix for #90763 caused the new column to be simply ignored when building DISTINCT ON, but this was insufficient. We need to actually include the new column among the DISTINCT ON columns. This commit makes that change.

Fixes #107839

Release note (bug fix): Fixed a spurious error "no data source matches prefix" that could occur during planning for a query with DISTINCT ON and ORDER BY ASC NULLS LAST or ORDER BY DESC NULLS FIRST.

@rytaft rytaft requested review from msirek and DrewKimball July 29, 2023 00:18
@rytaft rytaft requested a review from a team as a code owner July 29, 2023 00:18
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rytaft rytaft added backport-22.2.x backport-23.1.x Flags PRs that need to be backported to 23.1 labels Jul 29, 2023
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! Should we try to handle ordering by expressions also in this PR, or open a separate issue for it? For example:

SELECT  
DISTINCT ON (COALESCE(t2.id, t1.id))  
t1.id,  
t1.str  
FROM t1 JOIN t2 ON t1.id = t2.id  
ORDER BY COALESCE(t2.id, t1.id) DESC NULLS FIRST;

error (42P10): SELECT DISTINCT ON expressions must match initial ORDER BY expressions

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

@rytaft
Copy link
Collaborator Author

rytaft commented Jul 29, 2023

TFTR! Let's create a separate issue.

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.

Created #107848
:LGTM:

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

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm:

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


pkg/sql/opt/optbuilder/distinct.go line 95 at r1 (raw file):

			scopeCol := inScope.getColumn(col.ID())
			if scopeCol != nil {
				if isExpr, ok := scopeCol.scalar.(*memo.IsExpr); ok {

Does this bit of logic allow an query like SELECT DISTINCT ON (x) * FROM xy ORDER BY x IS NULL, x;?

Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

Any ideas why we didn't catch this with sqlsmith with b10363d ?

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


pkg/sql/opt/optbuilder/distinct.go line 95 at r1 (raw file):

Previously, DrewKimball (Drew Kimball) wrote…

Does this bit of logic allow an query like SELECT DISTINCT ON (x) * FROM xy ORDER BY x IS NULL, x;?

+1 that'd be a good test to add.

@rytaft rytaft force-pushed the distinct-order-by-bug branch from 7346f0a to f891522 Compare August 3, 2023 16:57
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.

TFTRs!

Any ideas why we didn't catch this with sqlsmith with b10363d ?

It doesn't return an internal error, so I don't think we'd catch it in a normal sqlsmith test. We wouldn't catch it in one of the query comparison tests either since we can't really catch optbuilder bugs with those tests.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @msirek)


pkg/sql/opt/optbuilder/distinct.go line 95 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

+1 that'd be a good test to add.

Yes it does (that also worked before this most recent change). I added a test.

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.

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @msirek)

@craig
Copy link
Contributor

craig bot commented Aug 5, 2023

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 5, 2023

Build failed:

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.

Flake: #108246

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @msirek)

@craig
Copy link
Contributor

craig bot commented Aug 5, 2023

Build failed:

@rytaft
Copy link
Collaborator Author

rytaft commented Aug 5, 2023

Another flake: #108248

bors r+

@craig
Copy link
Contributor

craig bot commented Aug 5, 2023

Build failed:

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.

Another: #108221

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @msirek)

@craig
Copy link
Contributor

craig bot commented Aug 5, 2023

Timed out.

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.

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @msirek)

@craig
Copy link
Contributor

craig bot commented Aug 5, 2023

Build failed:

@rytaft
Copy link
Collaborator Author

rytaft commented Aug 6, 2023

Flake: #106960

bors r+

@craig
Copy link
Contributor

craig bot commented Aug 6, 2023

Build failed:

This commit fixes an error that could occur in the optbuilder when planning a
query with DISTINCT ON and a non-standard nulls ordering.

The optimizer supports queries with a non-standard nulls ordering by projecting
a column with the expression (col IS NULL) and adding it to the ordering. Since
we require that DISTINCT ON columns must be the prefix of any ordering columns,
we must account for the new ordering column when building DISTINCT ON. A
previous bug fix for cockroachdb#90763 caused the new column to be simply ignored when
building DISTINCT ON, but this was insufficient. We need to actually include
the new column among the DISTINCT ON columns. This commit makes that change.

Fixes cockroachdb#107839

Release note (bug fix): Fixed a spurious error "no data source matches prefix"
that could occur during planning for a query with DISTINCT ON and ORDER BY ASC
NULLS LAST or ORDER BY DESC NULLS FIRST.
@rytaft rytaft force-pushed the distinct-order-by-bug branch from f891522 to 9dcdbcd Compare August 7, 2023 16:10
@rytaft
Copy link
Collaborator Author

rytaft commented Aug 7, 2023

This is ridiculous 😭 I just rebased in the hopes that some of the flakes are fixed.

bors r+

@craig
Copy link
Contributor

craig bot commented Aug 7, 2023

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

opt: query returns error when DISTINCT ON used with ASC NULLS LAST or DESC NULLS FIRST
5 participants