Skip to content

opt: cast inlined constant to the variable's type in InlineConstVar#172083

Open
shafi-VM wants to merge 1 commit into
cockroachdb:masterfrom
shafi-VM:fix/170544-inlineconstvar-cast
Open

opt: cast inlined constant to the variable's type in InlineConstVar#172083
shafi-VM wants to merge 1 commit into
cockroachdb:masterfrom
shafi-VM:fix/170544-inlineconstvar-cast

Conversation

@shafi-VM

Copy link
Copy Markdown

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. This 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 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 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

@shafi-VM
shafi-VM requested a review from a team as a code owner June 29, 2026 07:32
@shafi-VM
shafi-VM requested review from mw5h and removed request for a team June 29, 2026 07:32
@blathers-crl

blathers-crl Bot commented Jun 29, 2026

Copy link
Copy Markdown

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.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Jun 29, 2026
@tuansydau tuansydau added the T-sql-queries SQL Queries Team label Jul 2, 2026

@mw5h mw5h left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really good. I have a couple of requested changes around testing, but this is a nice clean fix.

Comment thread pkg/sql/opt/norm/testdata/rules/inline
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.
@shafi-VM
shafi-VM force-pushed the fix/170544-inlineconstvar-cast branch from 09014df to 7b75585 Compare July 22, 2026 04:17
@blathers-crl

blathers-crl Bot commented Jul 22, 2026

Copy link
Copy Markdown

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.

@shafi-VM
shafi-VM requested a review from mw5h July 22, 2026 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community T-sql-queries SQL Queries Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opt: InlineConstVar should cast when replacing a variable with an equivalent-but-not-identical type

3 participants