[fix](expr opt) Preserve try-cast nullability in comparison simplification - #67730
Open
morrySnow wants to merge 1 commit into
Open
[fix](expr opt) Preserve try-cast nullability in comparison simplification#67730morrySnow wants to merge 1 commit into
morrySnow wants to merge 1 commit into
Conversation
The type-range comparison simplifier used a cast's child both to refine the value range and to decide whether a constant result could be null. That is incorrect for try-cast because conversion failures can produce null even when its child is non-nullable. Keep the original try-cast expression for null-aware constant results, while retaining the existing child-based behavior for regular casts. Issue Number: None Tests: - SimplifyComparisonPredicateTest - sandbox SQL reproduction with an overflowing BIGINT-to-TINYINT try-cast
morrySnow
requested review from
924060929,
englefly and
starocean999
as code owners
September 9, 2026 09:50
Contributor
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
Contributor
Author
|
run buildall |
Contributor
TPC-H: Total hot run time: 16764 ms |
Contributor
TPC-DS: Total hot run time: 82116 ms |
Contributor
ClickBench: Total hot run time: 14.55 s |
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.
Problem
A comparison whose left side is an overflowing
TRY_CASTcan be simplified to a non-null boolean constant even though the cast evaluates toNULL. For example, with a non-nullBIGINTvalue of128, this predicate must produceNULL:The incorrect simplification also makes
(... > 127) IS NULLfalse and can filter out rows that should match.Root cause
The type-range comparison simplifier unwraps a cast to inspect the source type and tighten the possible numeric range. It then reuses that unwrapped child to decide whether an always-true or always-false result can be null. A non-nullable child does not capture
TRY_CASTsemantics: a failed conversion can introduceNULLindependently of child nullability.Reproduction
Before this change, the comparison for
id = 1is simplified toFALSE. The correct result isNULL, and the finalIS NULLexpression isTRUE.Fix
Separate the expression used for numeric range inference from the expression used for nullability. The cast child remains the range source, but an original
TRY_CASTis retained when producing null-aware boolean constants. RegularCASTexpressions keep their existing child-based normalization.Tests
SimplifyComparisonPredicateTestwith out-of-range comparisons over a non-nullable source wrapped byTRY_CAST, covering both true-or-null and false-or-null simplifications.NULLfor the overflow comparison and returns the row through anIS NULLfilter.