[fix](expr opt) Preserve cast boundaries during arithmetic simplification - #67733
Merged
morrySnow merged 2 commits intoSep 11, 2026
Merged
Conversation
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 |
morrySnow
force-pushed
the
fix/preserve-arithmetic-cast-semantics
branch
from
September 10, 2026 05:06
167e4e9 to
756a053
Compare
Contributor
Author
|
run buildall |
1 similar comment
Contributor
Author
|
run buildall |
Contributor
TPC-H: Total hot run time: 17016 ms |
Contributor
TPC-DS: Total hot run time: 83457 ms |
Contributor
ClickBench: Total hot run time: 15.01 s |
Contributor
FE UT Coverage ReportIncrement line coverage |
morrySnow
marked this pull request as draft
September 10, 2026 07:26
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
Keep the debug session-variable statement separate from a large block of disabled timestamp queries. This prevents the SQL file splitter from sending the comments and SET as one oversized dialect-conversion request, whose update count can become unstable under concurrent regression runs. The statement order and expected results remain unchanged. Issue Number: None Tests: - TestOperators with doris-sql-convertor 1.0.5
morrySnow
force-pushed
the
fix/preserve-arithmetic-cast-semantics
branch
from
September 10, 2026 08:41
e570bb8 to
dd88b8b
Compare
Contributor
Author
|
run buildall |
Contributor
FE UT Coverage ReportIncrement line coverage |
Contributor
TPC-H: Total hot run time: 16782 ms |
Contributor
TPC-DS: Total hot run time: 81747 ms |
Contributor
ClickBench: Total hot run time: 14.61 s |
morrySnow
marked this pull request as ready for review
September 11, 2026 05:31
starocean999
approved these changes
Sep 11, 2026
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.
The P0 run also exposed a regression-test fragility: a session
SETstatement followed 167 lines of disabled Presto timestamp queries, so the SQL splitter sent the comments and theSETtogether to the dialect-conversion service. Move theSETbefore that disabled block so it is converted and executed as a standalone statement. Query order and expected result tags remain unchanged.A separate chained-MTMV failure came from the task-wait helper temporarily falling back to the preceding task after it had already observed the new task. The branch is rebased onto current master, which contains the framework correction that keeps waiting for the observed task instead of latching onto the previous one.
The rebase also exposed a recently added IVM join-normalization test that still called a removed rewrite-context constructor. Build the test context through the supported CREATE-mode factory and set its full-key flag explicitly, matching the production setup path and restoring FE test-source compilation.
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.TestOperatorssuite withdoris-sql-convertor1.0.5: passed.SuiteMTMVTaskWaitTest: 4 tests passed.IvmNormalizeMTMVJoinTest: 44 tests passed.