[fix](expr opt) Preserve cast boundaries during arithmetic simplification - #67733
Open
morrySnow wants to merge 1 commit into
Open
[fix](expr opt) Preserve cast boundaries during arithmetic simplification#67733morrySnow wants to merge 1 commit into
morrySnow wants to merge 1 commit into
Conversation
Arithmetic flattening distributed a cast over its child operands when a constant was present. This can change the evaluation type and move rounding, overflow, error, or null behavior before the original operation. Treat explicit cast expressions as atomic operands during flattening. Arithmetic inside and outside each cast can still be simplified without crossing the conversion boundary. Issue Number: None Tests: - SimplifyArithmeticRuleTest - sandbox SQL reproduction near the BIGINT upper bound
morrySnow
requested review from
924060929,
englefly and
starocean999
as code owners
September 9, 2026 10:19
Contributor
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
Contributor
Author
|
run buildall |
Contributor
FE UT Coverage ReportIncrement line coverage |
Contributor
TPC-H: Total hot run time: 16926 ms |
Contributor
TPC-DS: Total hot run time: 82068 ms |
Contributor
ClickBench: Total hot run time: 14.81 s |
Contributor
FE Regression Coverage ReportIncrement line coverage |
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
Arithmetic simplification can move a
CASTorTRY_CASTfrom around an arithmetic expression to each operand. That changes where type conversion happens. With large exact integers converted toDOUBLE, the rewritten expression loses precision before subtraction and returns the wrong value.For example, subtracting
9223372036854775800from9223372036854775807inBIGINTproduces the exact value7. Converting that result toDOUBLEmust still produce7, but converting both operands first rounds them to the same floating-point value and produces0.Root cause
The arithmetic flattener recognized a cast containing addition, subtraction, multiplication, or division and recursively propagated the cast target type to every operand. This rewrite assumes that conversion distributes over arithmetic, which is not generally true. Besides floating-point rounding, the transformation can alter overflow, conversion-error, and
TRY_CASTnull behavior.Reproduction
Before this change,
exact_deltais7/6, while both converted expressions incorrectly return0/0.Fix
Treat explicit cast expressions as semantic boundaries and atomic operands during arithmetic flattening. The simplifier can continue optimizing arithmetic below and above a cast, but it no longer distributes the conversion into the cast's operands.
This deliberately chooses correctness over the optimization enabled by cross-cast flattening; no target-type whitelist is used because conversion distribution is not safe across all values and operation types.
Tests
CASTandTRY_CASTaround a large-integer subtraction.SimplifyArithmeticRuleTest: 5 tests passed.7/6for both converted expressions, and the physical expression keeps theBIGINTsubtraction inside the cast.