opt: drop provably-non-null COALESCE operands#170858
Conversation
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
|
Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
|
Detected infrastructure failure (matched: self-hosted runner lost communication with the server). Automatically rerunning failed jobs. (run link) |
|
Thank you for updating your pull request. Before a member of our team reviews your PR, I have some potential action items for you:
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
96f43bf to
164d43d
Compare
|
Thank you for updating your pull request. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
mw5h
left a comment
There was a problem hiding this comment.
@mw5h reviewed all commit messages and made 7 comments.
Reviewable status:complete! 0 of 0 LGTMs obtained (waiting on lohitkolluri).
pkg/sql/opt/norm/scalar_funcs.go line 78 at r1 (raw file):
func (c *CustomFuncs) SimplifyCoalesce(args memo.ScalarListExpr) opt.ScalarExpr { for i := 0; i < len(args)-1; i++ { item := args[i]
I don't see why we're getting rid of this. It leads to a lot of code churn for no reason.
pkg/sql/opt/norm/scalar_funcs.go line 82 at r1 (raw file):
// SimplifyCoalesceWithNotNullCols is like SimplifyCoalesce, but also discards // leading operands that are guaranteed to be non-null according to notNullCols. func (c *CustomFuncs) SimplifyCoalesceWithNotNullCols(
This is dead code.
pkg/sql/opt/norm/scalar_funcs.go line 91 at r1 (raw file):
args memo.ScalarListExpr, notNullCols opt.ColSet, ) opt.ScalarExpr { for i := 0; i < len(args); i++ {
Why are we now examining all the args vs. all but the last we did previously?
pkg/sql/opt/norm/scalar_funcs.go line 114 at r1 (raw file):
// Coalesce expression. func (c *CustomFuncs) CanSimplifyCoalesce(args memo.ScalarListExpr, notNullCols opt.ColSet) bool { simplified := c.simplifyCoalesce(args, notNullCols)
Kind of an expensive way to do this.
pkg/sql/opt/norm/scalar_funcs.go line 128 at r1 (raw file):
var replace ReplaceFunc replace = func(e opt.Expr) opt.Expr { if co, ok := e.(*memo.CoalesceExpr); ok {
I don't think this will handle nested Coalesce expressions, so COALESCE(a, COALESCE(b, c)) where b is non-null but a isn't, the outer match guard reports "nothing to simplify" and the inner Coalesce is missed. Either recurse into Coalesce args during the walk, or document the limitation. Worth a test case.
pkg/sql/opt/norm/select_funcs.go line 452 at r1 (raw file):
} func (c *CustomFuncs) scalarContainsSimplifiableCoalesce(
This belongs in scalar_funcs.go
pkg/sql/opt/norm/testdata/rules/scalar line 192 at r1 (raw file):
# -------------------------------------------------- # SimplifyCoalesceProject # --------------------------------------------------
Feels like both this and the select tests could benefit from more test cases. Negative tests (doesn't fire unexpectedly), middle argument non-null, maybe interactions with other expressions (IF, CASE, etc)?
|
Adding @DrewKimball because optimizer rules are tricky and he knows this code well. |
|
Thank you for updating your pull request. Before a member of our team reviews your PR, I have some potential action items for you:
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
51200f1 to
eff22df
Compare
|
@DrewKimball @mw5h — when you have a moment, could you please take another look? |
|
@DrewKimball @mw5h Hey just following up on this. |
| newProjections := make(memo.ProjectionsExpr, len(projections)) | ||
| for i := range projections { | ||
| p := &projections[i] | ||
| newProjections[i] = c.f.ConstructProjectionsItem( |
There was a problem hiding this comment.
Ideally, we should only re-build projections (and filters) that actually need this transformation. You could check if the result of SimplifyCoalesceInScalar is pointer-equal to the original projection, and reuse the original ProjectionsItem if it didn't change.
| (Project | ||
| $input:* | ||
| $projections:* & | ||
| (CanSimplifyCoalesceInProjections $projections $input) |
There was a problem hiding this comment.
nit: You can check for at least one simplifiable item in optgen like this:
$projections:[... (ProjectionsItem $scalar) ...] & (CanSimplifyCoalesceInScalar $scalar (NotNullCols $input))
A similar pattern is possible for the Select rule as well.
| func (c *CustomFuncs) CanSimplifyCoalesceInProjections( | ||
| projections memo.ProjectionsExpr, input memo.RelExpr, | ||
| ) bool { | ||
| notNullCols := c.NotNullCols(input) |
There was a problem hiding this comment.
nit: It would be a bit cleaner to derive the not-null cols once in the optgen rule(s), and then pass them into these helpers.
|
Thanks for the review. I'll put up a follow-up commit shortly addressing the feedback. Apologies if my initial approach introduced some unnecessary churn here. This is one of my first contributions to a codebase of this size, so I'm still learning the optimizer patterns and project conventions. I appreciate the detailed review and guidance. |
When a leading COALESCE argument is known non-null from the input's NotNullCols, the remaining arguments are unreachable. Trims leading COALESCE operands in Project projections and Select filters so the simplified plan no longer carries the dropped arguments. Example: SELECT COALESCE(i, j) FROM ij with i NOT NULL no longer keeps the COALESCE in the plan. Release note: None
eff22df to
abe252e
Compare
|
Your pull request contains more than 1000 changes. It is strongly encouraged to split big PRs into smaller chunks. Thank you for updating your pull request. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
|
@DrewKimball @mw5h This PR was replaced by #171887 (it had to be closed due to a shallow clone mistake that caused file bloat). Sorry for the trouble please refer to the updated PR i made sure to impliment the suggested changes. |
This PR was closed in favor of PR #171887. Please refer to that PR for the latest implementation and discussion.