optbuilder: do not create invalid casts when building COALESCE and IF#77608
optbuilder: do not create invalid casts when building COALESCE and IF#77608craig[bot] merged 1 commit intocockroachdb:masterfrom
Conversation
a899814 to
e54d1f3
Compare
rharding6373
left a comment
There was a problem hiding this comment.
Happy to see sqlsmith finding useful issues! Thanks for the quick fix.
Reviewed 6 of 6 files at r1, all commit messages.
Reviewable status:complete! 1 of 0 LGTMs obtained (waiting on @mgartner and @msirek)
pkg/sql/sem/tree/normalize.go, line 138 at r1 (raw file):
case treebin.Plus: if v.isNumericZero(right) { final, _ = ReType(left, expectedType)
Why don't we care whether or not the ReType was successful here?
msirek
left a comment
There was a problem hiding this comment.
Nice observability improvement for bad casts!
Reviewable status:
complete! 1 of 0 LGTMs obtained (waiting on @mgartner and @rharding6373)
pkg/sql/opt/optbuilder/scalar.go, line 247 at r1 (raw file):
pgcode.DatatypeMismatch, "CASE types %s and %s cannot be matched", t.Whens[i].Val.(tree.TypedExpr).ResolvedType(), valType,
nit: Should we change the text to CASE WHEN types %s and %s cannot be matched and in the error below indicate CASE ELSE types... ?
pkg/sql/sem/tree/normalize.go, line 138 at r1 (raw file):
Previously, rharding6373 (Rachael Harding) wrote…
Why don't we care whether or not the ReType was successful here?
I had the same question.
e54d1f3 to
3becbbd
Compare
mgartner
left a comment
There was a problem hiding this comment.
Thanks for the reviews!
Reviewable status:
complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rharding6373)
pkg/sql/opt/optbuilder/scalar.go, line 247 at r1 (raw file):
Previously, msirek (Mark Sirek) wrote…
nit: Should we change the text to
CASE WHEN types %s and %s cannot be matchedand in the error below indicateCASE ELSE types...?
Done.
pkg/sql/sem/tree/normalize.go, line 138 at r1 (raw file):
Previously, msirek (Mark Sirek) wrote…
I had the same question.
ReType returns nil, false when it's unsuccessful, and there's a check below for if final == nil {} that handles this case by returning the original expression unchanged. I added a comment below to explain.
Also, this normalization code is a remnant from the pre-cost-based-optimizer era, and I've been trying to rip it out for a few weeks. Maybe something I can finish up during breather week.
rharding6373
left a comment
There was a problem hiding this comment.
Reviewable status:
complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rharding6373)
pkg/sql/sem/tree/normalize.go, line 138 at r1 (raw file):
Previously, mgartner (Marcus Gartner) wrote…
ReTypereturnsnil, falsewhen it's unsuccessful, and there's a check below forif final == nil {}that handles this case by returning the original expression unchanged. I added a comment below to explain.Also, this normalization code is a remnant from the pre-cost-based-optimizer era, and I've been trying to rip it out for a few weeks. Maybe something I can finish up during breather week.
Thanks for the explanation.
msirek
left a comment
There was a problem hiding this comment.
Reviewable status:
complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @rharding6373)
pkg/sql/sem/tree/normalize.go, line 138 at r1 (raw file):
Previously, rharding6373 (Rachael Harding) wrote…
Thanks for the explanation.
3becbbd to
96eecb7
Compare
|
I've had to add some logic to make casts from any tuple type to the special type |
rharding6373
left a comment
There was a problem hiding this comment.
Reviewable status:
complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @msirek and @rharding6373)
The optbuilder no longer creates invalid casts when building COALESCE and IF expressions that have children with different types. Expressions that previously caused internal errors now result in user-facing errors. Both UNION and CASE expressions had similar bugs that were recently fixed in cockroachdb#75219 and cockroachdb#76193. This commit also updates the `tree.ReType` function to return `ok=false` if there is no valid cast to re-type the expression to the given type. This forces callers to explicitly deal with situations where re-typing is not possible and it ensures that the function never creates invalid casts. This will make it easier to track down future related bugs because internal errors should originate from the call site of `tree.ReType` rather than from logic further along in the optimization process (in the case of cockroachdb#76807 the internal error originated from the logical props builder when it attempted to lookup the volatility of the invalid cast). This commit also adds special logic to make casts from any tuple type to `types.AnyTuple` valid immutable, implicit casts. Evaluation of these casts are no-ops. Users cannot construct these casts, but they are built by optbuilder in some cases. Fixes cockroachdb#76807 Release justification: This is a low-risk change that fixes a minor bug. Release note (bug fix): A bug has been fixed that caused internal errors when COALESCE and IF expressions had inner expressions with different types that could not be cast to a common type.
96eecb7 to
e55e6b6
Compare
msirek
left a comment
There was a problem hiding this comment.
Reviewable status:
complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @msirek and @rharding6373)
|
TFTRs! bors r+ |
|
Build succeeded: |
|
Encountered an error creating backports. Some common things that can go wrong:
You might need to create your backport manually using the backport tool. error creating backport branch refs/heads/blathers/backport-release-21.1-77608: POST https://api.github.com/repos/cockroachlabs/cockroach/git/refs: 403 Resource not accessible by integration [] Backport to branch 21.1.x failed. See errors above. error creating backport branch refs/heads/blathers/backport-release-21.2-77608: POST https://api.github.com/repos/cockroachlabs/cockroach/git/refs: 403 Resource not accessible by integration [] Backport to branch 21.2.x failed. See errors above. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan. |
The optbuilder no longer creates invalid casts when building COALESCE
and IF expressions that have children with different types. Expressions
that previously caused internal errors now result in user-facing errors.
Both UNION and CASE expressions had similar bugs that were recently
fixed in #75219 and #76193.
This commit also updates the
tree.ReTypefunction to returnok=falseif there is no valid cast to re-type the expression to the given type.
This forces callers to explicitly deal with situations where re-typing
is not possible and it ensures that the function never creates invalid
casts. This will make it easier to track down future related bugs
because internal errors should originate from the call site of
tree.ReTyperather than from logic further along in the optimizationprocess (in the case of #76807 the internal error originated from the
logical props builder when it attempted to lookup the volatility of the
invalid cast).
This commit also adds special logic to make casts from any tuple type to
types.AnyTuplevalid immutable, implicit casts. Evaluation of thesecasts are no-ops. Users cannot construct these casts, but they are built
by optbuilder in some cases.
Fixes #76807
Release justification: This is a low-risk change that fixes a minor bug.
Release note (bug fix): A bug has been fixed that caused internal errors
when COALESCE and IF expressions had inner expressions with different
types that could not be cast to a common type.