opt: cast inlined constant to the variable's type in InlineConstVar#172083
Open
shafi-VM wants to merge 1 commit into
Open
opt: cast inlined constant to the variable's type in InlineConstVar#172083shafi-VM wants to merge 1 commit into
shafi-VM wants to merge 1 commit into
Conversation
InlineConstVar replaces a variable that a filter restricts to a constant
(e.g. x in `x = 4 AND x IN (1, 2, 3, 4)`) with that constant elsewhere in the
filters. The constant is matched against the variable using Equivalent rather
than Identical, so a constant whose type is equivalent but not identical to the
variable's type could be substituted directly -- for example a STRING constant
for a NAME column. That changes the result of type-sensitive expressions:
pg_typeof(n) became pg_typeof('hello'), returning 'text' instead of 'name', so
a filter like pg_typeof(n)::TEXT = 'name' folded to false and the row was
incorrectly dropped.
Cast the inlined constant to the variable's type when the two are not
identical, so type-sensitive expressions still observe the original type. The
common case where the types are identical is unaffected -- no cast is added.
Fixes cockroachdb#170544
Epic: none
Release note (bug fix): Fixed a bug that could cause a query to return
incorrect results when a column was compared to a constant of an equivalent
but different type (for example, a NAME column compared to a string literal)
and a type-sensitive expression such as pg_typeof was applied to the same
column. The optimizer could substitute the constant using the wrong type,
changing the result of the type-sensitive expression.
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
InlineConstVar replaces a variable that a filter restricts to a constant (e.g.
xinx = 4 AND x IN (1, 2, 3, 4)) with that constant elsewhere in the filters. The constant is matched against the variable usingEquivalentrather thanIdentical, so a constant whose type is equivalent but not identical to the variable's type could be substituted directly—for example, aSTRINGconstant for aNAMEcolumn. This changes the result of type-sensitive expressions:pg_typeof(n)becamepg_typeof('hello'), returningtextinstead ofname, so a filter likepg_typeof(n)::TEXT = 'name'folded tofalseand the row was incorrectly dropped.Cast the inlined constant to the variable's type when the two types are not identical, so type-sensitive expressions continue to observe the original type. The common case, where the types are identical, is unaffected—no cast is added.
Fixes #170544
Epic: none
Release note (bug fix): Fixed a bug that could cause a query to return incorrect results when a column was compared to a constant of an equivalent but different type (for example, a
NAMEcolumn compared to a string literal) and a type-sensitive expression such aspg_typeofwas applied to the same column. The optimizer could substitute the constant using the wrong type, changing the result of the type-sensitive expression