Skip to content

sql/opt: prevent NormalizeCmpTimeZoneFunction from masking bounds errors#169733

Open
SAY-5 wants to merge 1 commit intocockroachdb:masterfrom
SAY-5:fix-169202-normalize-cmp-timezone-bounds
Open

sql/opt: prevent NormalizeCmpTimeZoneFunction from masking bounds errors#169733
SAY-5 wants to merge 1 commit intocockroachdb:masterfrom
SAY-5:fix-169202-normalize-cmp-timezone-bounds

Conversation

@SAY-5
Copy link
Copy Markdown
Contributor

@SAY-5 SAY-5 commented May 5, 2026

Fixes #169202.

NormalizeCmpTimeZoneFunction and NormalizeCmpTimeZoneFunctionTZ rewrite predicates of the form timezone(zone, ts) op const into ts op timezone(zone, const), hoisting the per-row timezone() call out of the comparison. When a column value is at or near the supported timestamp bounds, the per-row call would return an exceeds supported timestamp bounds error, but the rewritten predicate evaluates the constant comparison directly and silently returns a result the original expression could not have produced.

This change adds a CanTimeZoneFunctionOverflow guard that requires both MinSupportedTime and MaxSupportedTime to round-trip through the chosen timezone shift without erroring, which is a sufficient condition that no value in the column domain can trigger the bounds error. The guard also declines when the zone is non-constant.

Same class of bug as #125751 / #88199.

Release note (bug fix): Fixed a bug where comparisons of the form timezone(zone, ts) op const could silently return results that the original expression could not have produced when the per-row timezone() evaluation would have errored with "exceeds supported timestamp bounds".

NormalizeCmpTimeZoneFunction and NormalizeCmpTimeZoneFunctionTZ rewrite
predicates of the form `timezone(zone, ts) op const` into
`ts op timezone(zone, const)`, hoisting the per-row timezone() call out of
the comparison. When a column value is at or near the supported timestamp
bounds (294276-12-31 23:59:59.999999 / 4714-11-24 BC), the per-row
timezone() call would return an "exceeds supported timestamp bounds"
error, but the rewritten predicate evaluates the constant comparison
directly and silently returns a result the original expression could not
have produced.

Add a CanTimeZoneFunctionOverflow guard that requires both
MinSupportedTime and MaxSupportedTime to round-trip through the
chosen timezone shift without erroring. This is a sufficient condition
that no value in the column domain can trigger the bounds error,
preserving the original expression's error semantics. The guard also
declines to fire when the zone is not a constant string, since we cannot
prove safety statically in that case.

Includes a logictest regression that fails on master and passes with
this change. Same class of bug as cockroachdb#125751 / cockroachdb#88199 (predicate rewrites
that ignore runtime evaluation errors), for a different rule and a
different error class.

Fixes cockroachdb#169202

Release note (bug fix): Fixed a bug where comparisons of the form
`timezone(zone, ts) op const` could silently return results that the
original expression could not have produced when the per-row
timezone() evaluation would have errored with "exceeds supported
timestamp bounds".

Signed-off-by: SAY-5 <[email protected]>
@SAY-5 SAY-5 requested a review from a team as a code owner May 5, 2026 09:45
@SAY-5 SAY-5 requested review from ZhouXing19 and removed request for a team May 5, 2026 09:45
@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented May 5, 2026

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

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

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented May 5, 2026

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 May 5, 2026
@cockroachlabs-cla-agent
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


SAY-5 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql/opt: NormalizeCmpTimeZoneFunction silently masks "exceeds supported timestamp bounds" errors

2 participants