-
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
colexec: add native support for COALESCE, IF, NULLIF #77658
Conversation
Previously, in a test helper method we allowed the fallback to the row-by-row engine. At some point this was needed, but right now all callers don't utilize that ability, so we can easily remove it. Release note: None Release justification: testing only change.
c2a8d89
to
c4273c7
Compare
Given that this PR doesn't introduce any new operators, I think it's safe to merge during the stability, so I'd really like to get this in. The insight of converting these expressions to the equivalent CASE expressions came to me after I already implemented them explicitly here :) The explicit implementation might be a bit faster, but I don't think it's worth it. |
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.
An alternative is to implement these transformations as normalization rules in the optimizer. The rules for IF and NULLIF would be fairly simple.
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.
Code , but agree with Marcus. If it's always better to model these as case statements, it would be better to apply them earlier in the pipeline as normalization rules so that we have more visibility into the operators that are actually used and can apply optimizations to them.
Reviewed 10 of 10 files at r1, 1 of 1 files at r2, 6 of 7 files at r3, all commit messages.
Reviewable status: complete! 1 of 0 LGTMs obtained (waiting on @michae2, @rharding6373, and @yuzefovich)
pkg/sql/colexec/proj_utils_test.go, line 1 at r3 (raw file):
// Copyright 2022 The Cockroach Authors.
nit on the file name: I think something like test_utils.go would be more fitting, since this isn't defining actual tests.
Release note: None Release justification: low risk cleanup.
This commit adds the native vectorized support for `CoalesceExpr`, `IfExpr`, and `NullIfExpr` by planning the equivalent CASE expressions. Namely, for `CoalesceExpr` we do ``` CASE WHEN CoalesceExpr.Exprs[0] IS DISTINCT FROM NULL THEN CoalesceExpr.Exprs[0] WHEN CoalesceExpr.Exprs[1] IS DISTINCT FROM NULL THEN CoalesceExpr.Exprs[1] ... END ``` for `IfExpr` we do ``` CASE WHEN IfExpr.Cond THEN IfExpr.True ELSE IfExpr.Else END ``` and for `NullIfExpr` we do ``` CASE WHEN Expr1 == Expr2 THEN NULL ELSE Expr1 END ``` This commit additionally introduces some unit tests for these newly supported expressions while extracting out some testing facilities from the caseOp tests. Release note: None Release justification: low risk, high benefit change to existing functionality.
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.
It's a good point, and I think we should it. We already have a precedent in the vectorized planning of doing this conversion, so I think it's ok to introduce some more conversions there; thus, I filed #77799 to track this idea.
I'd really like to get the Coalesce support in for 22.1, so unless someone volunteers to implement this before the branch is cut (which I believe is tomorrow), I'll go ahead and merge as is :)
Reviewable status: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @michae2 and @rharding6373)
pkg/sql/colexec/proj_utils_test.go, line 1 at r3 (raw file):
Previously, rharding6373 (Rachael Harding) wrote…
nit on the file name: I think something like test_utils.go would be more fitting, since this isn't defining actual tests.
There are some subtle differences:
.*_test.go
is test-only file that is not included into the compiled binary whereas.*_test_utils.go
might be included into the binary..*_test_utils.go
will be visible from the non-test code;
so I think it's a good hygiene to put test-only code exclusively (when possible) in .*_test.go
files.
I can try to write the rules, but probably won't make the branch cut. If the normalization rules are added later, would we keep this conversion code you're adding or remove it? |
The conversion code in |
I'll merge this, and we'll remove the vectorized planning code whenever the normalization rules are added. TFTR! bors r+ |
Build succeeded: |
Are you sure the changes for IF and NULLIF were required? We already build those expressions directly into CASE expressions in optbuilder. cockroach/pkg/sql/opt/optbuilder/scalar.go Lines 370 to 382 in e55e6b6
cockroach/pkg/sql/opt/optbuilder/scalar.go Lines 324 to 341 in e55e6b6
|
Interesting - no, I didn't check any end-to-end queries. The reason I wanted to add the support for |
colexec: remove no longer used testing knob
Previously, in a test helper method we allowed the fallback to the
row-by-row engine. At some point this was needed, but right now all
callers don't utilize that ability, so we can easily remove it.
Release note: None
Release justification: testing only change.
colbuilder: order expressions lexicographically
Release note: None
Release justification: low risk cleanup.
colexec: add native support for COALESCE, IF, NULLIF
This commit adds the native vectorized support for
CoalesceExpr
,IfExpr
, andNullIfExpr
by planning the equivalent CASE expressions.Namely, for
CoalesceExpr
we dofor
IfExpr
we doand for
NullIfExpr
we doThis commit additionally introduces some unit tests for these newly
supported expressions while extracting out some testing facilities from
the caseOp tests.
Fixes: #66015.
Release note: None
Release justification: low risk, high benefit change to existing
functionality.