-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Conversation
There was a problem hiding this 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: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball)
TFTR! Let's create a separate issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #107848
Reviewable status: complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: 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;
?
There was a problem hiding this 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: 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.
7346f0a
to
f891522
Compare
There was a problem hiding this 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: 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.
There was a problem hiding this 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: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @msirek)
Build failed (retrying...): |
Build failed: |
There was a problem hiding this 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: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @msirek)
Build failed: |
Another flake: #108248 bors r+ |
Build failed: |
There was a problem hiding this 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: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @msirek)
Timed out. |
There was a problem hiding this 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: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @DrewKimball, @mgartner, and @msirek)
Build failed: |
Flake: #106960 bors r+ |
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.
f891522
to
9dcdbcd
Compare
This is ridiculous 😭 I just rebased in the hopes that some of the flakes are fixed. bors r+ |
Build succeeded: |
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 thatDISTINCT ON
columns must be the prefix of any ordering columns, we must account for the new ordering column when buildingDISTINCT ON
. A previous bug fix for #90763 caused the new column to be simply ignored when buildingDISTINCT ON
, but this was insufficient. We need to actually include the new column among theDISTINCT 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
andORDER BY ASC NULLS LAST
orORDER BY DESC NULLS FIRST
.