← Back to Pull requests

[fix](constant folding) Keep the sign of zero when folding ceil() and round() on DOUBLE #68731

Open
abhinav-phi opened this pull request with 180 lines diff across 6 files
View on GitHub
abhinav-phi abhinav-phi commented on Oct 04, 2026 22:44 UTC
Author

What problem does this PR solve?

Issue Number: close #68719

Related PR: None

Problem Summary:

ceil(DoubleLiteral) and round(DoubleLiteral) folded through DecimalV3Literal at scale 0:

DecimalV3Literal middleResult = DecimalV3Literal.createWithoutCheck256(
        new BigDecimal(Double.toString(first.getValue())));
return new DoubleLiteral(middleResult.roundCeiling(0).getDouble());

BigDecimal has no signed zero, so ceil(-0.5) folded to +0 and round(-0.4) folded to +0.
BE computes these with std::ceil / std::round (be/src/exprs/function/round.h,
roundWithMode<double>), which return -0. The reported symptom is exactly that:

SELECT signbit(ceil(CAST(-0.5 AS DOUBLE))) AS fe, signbit(ceil(d1)) AS be FROM t;
+------+------+
| fe   | be   |
|    0 |    1 |
+------+------+

The review found two more problems in the first version of the fix, and both are fixed here too:

1. Computing the rounding threshold as Math.abs(value) + 0.5 is inexact. 0.49999999999999994
(the representable double immediately below 0.5) becomes 0.9999999999999999 then 1.0 after
the floor, and 4503599627370497.0 (integral, and beyond the double ULP of 1) rounds to
4503599627370498.0. The threshold is now derived from the exact fractional part instead:
magnitude - Math.floor(magnitude) >= 0.5, which Sterbenz makes exact for every finite
magnitude.
2. Keeping -0.0 alive exposed that the shared literal comparison semantics treat -0.0 and +0.0
as different values: NumericLiteral.compareTo used Double.compare (which orders -0.0 below
+0.0) and Literal.equals/computeHashCode compared boxed Doubles (which compare bits). So
after this PR ceil(CAST(-0.5 AS DOUBLE)) = CAST(0 AS DOUBLE) folded to FALSE, and
... IN (CAST(0 AS DOUBLE), 1) folded to FALSE, while BE answers TRUE for both. The floating
literals now use SQL value equality, with computeHashCode mapping both signed zeros to the same
hash so the HashSet / retainAll paths agree with equals. Expression identity is untouched,
so signbit() still reports the sign of the zero.

The bankers variant is a separate BE function (round_bankers) and is not touched. NaN and both
infinities keep their existing behaviour, and integral, date and decimal targets are unchanged.

Release note

None

Check List (For Author)

  • Test <!-- At least one of them must be included. -->

  • - [ ] Regression test
    - [x] Unit Test
    - [ ] Manual test (add detailed scripts and steps below)
    - [ ] No need to test or manual test. Explain why:

    NumericArithmeticTest.testCeilAndRoundKeepSignedZero asserts the sign of the folded zero for
    ceil(-0.5) and round(-0.4), that round(0.4) stays positive zero, the half-away-from-zero ties
    (round(2.5) = 3, round(-2.5) = -3), ordinary values (ceil(1.2) = 2, ceil(-1.5) = -1),
    ceil(+Inf) = +Inf and round(NaN) = NaN, and now also the threshold cases
    (0.49999999999999994, -0.49999999999999994, +/-4503599627370497.0, 1e30).

    testScaleZeroOverloadsKeepSignedZero covers the two-argument forms: round(-0.4, 0) and
    ceil(-0.5, 0) keep the sign of zero, round(2.5, 0) = 3, round(-2.5, 0) = -3, while a non-zero
    scale still goes through the decimal path (round(-0.44, 1) = -0.4, ceil(-0.5, 1) = -0.5).

    testFoldedPredicateTreatsSignedZerosAsEqual (was @Disabled, now enabled) asserts the folded
    DoubleLiteral(-0.0) compares equal to DoubleLiteral(0.0), equals it, hashes the same and lands
    in one HashSet, while Math.signbit still differs. FoldConstantTest.testSignedZeroFoldsEqual
    asserts the planner result: ceil(CAST(-0.5 AS DOUBLE)) = CAST(0 AS DOUBLE) folds to true,
    round(CAST(-0.4 AS DOUBLE)) = CAST(0 AS DOUBLE) to true,
    ceil(CAST(-0.5 AS DOUBLE)) IN (CAST(0 AS DOUBLE), 1) to true and != to false.

    Red, on master with only the test added:

      [ERROR]   NumericArithmeticTest.testCeilAndRoundKeepSignedZero:93 expected: <true> but was: <false>
      

    For the threshold, a standalone JDK 17 program over the same expression space counted the values
    where Math.copySign(Math.floor(Math.abs(v) + 0.5), v) disagrees with the exact-fractional rule
    and with a Fraction-based reference model: 4 mismatches before, 0 after.

    Green, the focused set on the branch (NumericArithmeticTest, DoubleLiteralTest, FloatLiteralTest,
    CompareLiteralTest, LiteralEqualTest, FoldConstantTest, ConstantProjectionFoldingTest, CastTest,
    TryCastTest, ExpressionTest
    ):

      Tests run: 88, Failures: 2, Errors: 0, Skipped: 0
      [ERROR]   FoldConstantTest.testDateConstructFunction:1624 expected: <'1977-06-03 17:57:24'> but was: <'1977-06-03 15:27:24'>
      [ERROR]   FoldConstantTest.testFoldString:601 expected: <'2025-10-27 14:58:08.100000'> but was: <'2025-10-27 12:28:08.100000'>
      

    Those two are session-time-zone dependent timestamp assertions that fail on this machine
    independently of this change (the offset is the IST difference, and the same two appear on
    master). Everything else passes, including the previously erroring
    FoldConstantTest.testSignedZeroFoldsEqual.

    Command (the project's own flags from run-fe-ut.sh):

      mvn test -pl fe-core -am -Dcheckstyle.skip=true -DfailIfNoTests=false \
          -Dmaven.build.cache.enabled=false -Dtest=NumericArithmeticTest
      

    Environment: JDK 17.0.20 (Temurin), Maven 3.9.16, thrift 0.24.0 from the prebuilt thirdparty
    package, Ubuntu 22.04 under WSL2. Style gate mvn -pl fe-core checkstyle:check ->
    You have 0 Checkstyle violations.

  • Behavior changed:

  • - [ ] No.
    - [x] Yes.
    Two related changes. Folding ceil(<double literal in (-1,0)>) and round(<double literal that
    rounds to zero>)
    now yields -0.0 instead of +0.0, matching BE, so signbit() agrees
    between the constant path and the column path. And for the floating literals, -0.0 and +0.0
    now compare and hash as the same value, which is what BE and IEEE 754 both do, so folded =
    and IN predicates agree with the row-by-row answer. Magnitudes, ordering of non-zero values
    and NaN handling are unchanged; DoubleLiteral.toString() and the stored bits still show the
    sign.

  • Does this need documentation?

  • - [x] No.
    - [ ] Yes. <!-- Add document PR link here. -->

    Check List (For Reviewer who merge this PR)

  • Confirm the release note

  • Confirm test cases

  • Confirm document

  • Add branch pick label
  • AI assistance disclosure

    An AI coding agent was used to draft this change and the new tests. I read and reviewed every line
    myself, checked the BE semantics in be/src/exprs/function/round.h against what the FE now computes,
    ran the FE unit tests above on the exact commits in this branch, and I take responsibility for the
    result.

    Diff: 180 lines (+173 / -7) across 6 files Reviewer: morrySnow Review duration: 84841m
    All Vouch review confidence checks passed
    Residual risk 0.05 = Change Risk (0.48) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 180 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 5 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.311
    Real PR Conversation Timeline
    20 items
    hello-stephen hello-stephen commented on Oct 04, 2026 22:44 UTC
    Contributor

    Thank you for your contribution to Apache Doris.
    Don't know what should be done next? See How to process your PR.

    Please clearly describe your PR:
    1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
    2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
    3. What features were added. Why was this function added?
    4. Which code was refactored and why was this part of the code refactored?
    5. Which functions were optimized and what is the difference before and after the optimization?

    morrySnow morrySnow commented on Oct 09, 2026 03:47 UTC
    Contributor

    run buildall

    morrySnow morrySnow commented on Oct 09, 2026 03:48 UTC
    Contributor

    /review

    github-actions[bot] github-actions[bot] commented on code on Oct 09, 2026 04:05 UTC
    Code Review
    fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java:1
    @@ -364,9 +364,10 @@ public static Expression round(DecimalV3Literal first, IntegerLiteral second) {
          */
         @ExecFunction(name = "round")
         public static Expression round(DoubleLiteral first) {
    -        DecimalV3Literal middleResult = DecimalV3Literal.createWithoutCheck256(
    -                new BigDecimal(Double.toString(first.getValue())));
    -        return new DoubleLiteral(middleResult.round(0).getDouble());
    +        // BE uses std::round(): half away from zero, and a zero result keeps the sign of the
    +        // argument. Rounding through BigDecimal at scale 0 collapses -0.4 to positive zero.
    +        double value = first.getValue();
    +        return new DoubleLiteral(Math.copySign(Math.floor(Math.abs(value) + 0.5), value));

    [P1] Avoid rounding the input while computing the rounding threshold. For 0.49999999999999994 (the representable DOUBLE immediately below 0.5), Math.abs(value) + 0.5 becomes exactly 1.0, so FE folds round(CAST(0.49999999999999994 AS DOUBLE)) to 1 even though BE std::round returns 0; the negative input becomes -1 instead of -0. The same expression changes an already integral 4503599627370497.0 to 4503599627370498.0. Please use a half-away-from-zero algorithm that does not perform this inexact addition, and cover both boundaries in folding tests.

    github-actions[bot] github-actions[bot] commented on code on Oct 09, 2026 04:05 UTC
    Code Review
    fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmeticTest.java:1
    @@ -86,4 +86,34 @@ private void assertInterval(int expected, long compareValue, long... thresholds)
                     new BigIntLiteral(compareValue), thresholdLiterals);
             Assertions.assertEquals(expected, result.getValue());
         }
    +
    +    @Test
    +    public void testCeilAndRoundKeepSignedZero() {
    +        DoubleLiteral ceil = (DoubleLiteral) NumericArithmetic.ceil(new DoubleLiteral(-0.5));
    +        Assertions.assertTrue(isNegativeZero(ceil.getValue()));
    +
    +        DoubleLiteral round = (DoubleLiteral) NumericArithmetic.round(new DoubleLiteral(-0.4));

    [P2] Cover the explicit scale-zero DOUBLE overloads as well. The new test and implementation handle only unary calls, but round(CAST(-0.4 AS DOUBLE), 0) and ceil(CAST(-0.5 AS DOUBLE), 0) still use the BigDecimal overloads below and FE folds both to positive zero; BE's scale-zero dispatch calls std::round/std::ceil and returns negative zero. This leaves signbit dependent on whether the argument is constant or a column for the same public functions. Please extend the fix and a folded-expression test to these overloads (or explicitly arrange a tracked follow-up if this PR must remain unary-only).

    github-actions[bot] github-actions[bot] commented on code on Oct 09, 2026 04:05 UTC
    Code Review
    fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java:1
    @@ -401,9 +402,8 @@ public static Expression ceil(DecimalV3Literal first, IntegerLiteral second) {
          */
         @ExecFunction(name = "ceil")
         public static Expression ceil(DoubleLiteral first) {
    -        DecimalV3Literal middleResult = DecimalV3Literal.createWithoutCheck256(
    -                new BigDecimal(Double.toString(first.getValue())));
    -        return new DoubleLiteral(middleResult.roundCeiling(0).getDouble());
    +        // BE uses std::ceil(), which keeps the sign of a zero result.

    [P1] Align FE numeric comparison with the newly preserved negative zero. ceil(CAST(-0.5 AS DOUBLE)) now folds to DoubleLiteral(-0.0), but NumericLiteral.compareTo calls Double.compare(-0.0, +0.0) and returns -1. FE therefore folds ceil(CAST(-0.5 AS DOUBLE)) = CAST(0 AS DOUBLE) to FALSE, while BE treats the zeros as equal; RangeInference can also erase x >= +0 AND x <= ceil(-0.5) even though x = 0 qualifies. Unary round(-0.4) has the same path. Please correct the shared numeric comparator semantics and cover folded predicates and ranges.

    github-actions[bot] github-actions[bot] commented on code on Oct 09, 2026 04:05 UTC
    Code Review
    fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java:1
    @@ -401,9 +402,8 @@ public static Expression ceil(DecimalV3Literal first, IntegerLiteral second) {
          */
         @ExecFunction(name = "ceil")
         public static Expression ceil(DoubleLiteral first) {
    -        DecimalV3Literal middleResult = DecimalV3Literal.createWithoutCheck256(
    -                new BigDecimal(Double.toString(first.getValue())));
    -        return new DoubleLiteral(middleResult.roundCeiling(0).getDouble());
    +        // BE uses std::ceil(), which keeps the sign of a zero result.

    [P1] Use SQL value equality where the newly preserved negative zero enters sets. ceil(CAST(-0.5 AS DOUBLE)) IN (CAST(0 AS DOUBLE), CAST(1 AS DOUBLE)) reaches visitInPredicate, whose Literal.equals check treats -0.0 and +0.0 as different, so FE folds FALSE; BE HybridSet returns TRUE. RangeInference.intersect similarly uses retainAll and can erase x = ceil(-0.5) AND x = +0 even though x = 0 qualifies. Correct these semantic equality sites, while retaining sign-sensitive expression identity for signbit, and add composed IN and range tests.

    github-actions[bot] github-actions[bot] requested changes on Oct 09, 2026 04:05 UTC
    Changes Requested Reviewer

    Review verdict: request changes. The three permitted review rounds are complete, but convergence was not achieved: the final round found a new valuable signed-zero IN issue. This review is therefore incomplete under the review prompt's round cap. All four substantiated findings are reported inline. There are no existing inline threads or still-applicable existing P0/P1 comment IDs.

    Critical checkpoints:

  • Goal and proof: Unary ceil and ordinary unary round now preserve negative zero in the direct cases asserted by the new JUnit test; DoubleLiteral, legacy FloatLiteral, thrift transport, and FE result formatting preserve the bit. The goal is not fully met for observable query semantics because the new round formula changes other values (P1), explicit scale-zero overloads still differ from BE (P2), and FE predicate/range folding misinterprets the newly produced negative zero (two P1s).

  • Scope and parallel paths: The edit is small and uses existing function registration/signatures. Math.ceil matches BE for the inspected finite and exceptional values. The (DOUBLE, INT) round/ceil paths still use BigDecimal; the PR body acknowledges deferring round(x,0), and the inline P2 covers the remaining public overload discrepancy.

  • Concurrency and lifecycle: These are synchronous, stateless FE scalar methods. No shared state, locking, thread start, static initialization dependency, or special resource lifecycle was introduced.

  • Configuration, compatibility, and FE/BE transfer: No configuration, function symbol, stored format, protocol field, or transmitted variable changed. The existing raw-double transport preserves signed zero. FE/BE result compatibility is nevertheless broken in the four described paths; rolling-upgrade wire compatibility itself is unchanged.

  • Conditions and error handling: No new conditional or error-handling branch was added. NaN and both infinities were traced through the changed arithmetic and literal/result paths without another substantiated defect.

  • Tests and results: The new JUnit assertions have correct expectations for their direct inputs. They do not cover the predecessor of 0.5, large odd integral doubles, SQL folding, composed comparisons/IN/range predicates, or explicit scale zero. The author reports a focused passing unit run and checkstyle; this review did not run builds or Doris tests, as required by the review prompt. No regression result file changed.

  • Observability, persistence, writes, and memory: No new logging or metric is needed for these local deterministic functions. There is no transaction, EditLog, data write, version, delete-bitmap, or BE memory-tracking path in this diff. The primitive arithmetic reduces BigDecimal work, but the correctness regressions block that performance benefit.

  • User focus: No additional focus was supplied. The final sweep covered both changed files and all recorded candidates; none remain without a disposition.
  • Release note: The PR marks it None even though signed-zero behavior is user-visible; please describe the correction when updating the PR.

    <!-- code-review-completion:3bf9a16c-0b50-4733-928d-7d1ebef5e444:85317319f02e639f0df9de83162edd14bdaa1f2e3d5803ac8545f29261342faf -->

    hello-stephen hello-stephen commented on Oct 09, 2026 04:08 UTC
    Contributor

    <details>
    <summary>TPC-H: <b>Total hot run time: 29507 ms</b></summary>

    machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
    scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
    Tpch sf100 test result on commit 35eca0a4d023670a2d648db1a4ae93851511c21f, data reload: false
    
    ------ Round 1 ----------------------------------
    ============================================
    q1	17613	5792	5597	5597
    q2	2132	310	261	261
    q3	10218	1473	860	860
    q4	4681	504	361	361
    q5	7432	815	690	690
    q6	187	184	156	156
    q7	812	812	602	602
    q8	9253	1355	1133	1133
    q9	5830	4499	4482	4482
    q10	6826	1332	1020	1020
    q11	432	259	240	240
    q12	635	411	298	298
    q13	18034	3091	2388	2388
    q14	271	265	260	260
    q15	q16	743	736	670	670
    q17	1184	750	577	577
    q18	7169	6284	6186	6186
    q19	1246	958	648	648
    q20	395	347	222	222
    q21	6121	2537	2660	2537
    q22	406	362	319	319
    Total cold run time: 101620 ms
    Total hot run time: 29507 ms
    
    ----- Round 2, with runtime_filter_mode=off -----
    ============================================
    q1	6651	6713	6808	6713
    q2	795	634	599	599
    q3	5886	5740	5316	5316
    q4	2135	2250	1641	1641
    q5	5690	5738	5825	5738
    q6	246	190	148	148
    q7	2223	2036	1843	1843
    q8	3315	2828	2826	2826
    q9	8214	8083	8131	8083
    q10	3706	3666	3315	3315
    q11	608	422	384	384
    q12	670	702	512	512
    q13	2763	3107	2391	2391
    q14	282	280	266	266
    q15	q16	676	691	632	632
    q17	7892	7022	6905	6905
    q18	13210	12269	13177	12269
    q19	951	841	845	841
    q20	2200	2213	1934	1934
    q21	5968	4823	4886	4823
    q22	504	469	414	414
    Total cold run time: 74585 ms
    Total hot run time: 67593 ms
    

    </details>

    hello-stephen hello-stephen commented on Oct 09, 2026 04:18 UTC
    Contributor

    <details>
    <summary>TPC-DS: <b>Total hot run time: 152472 ms</b></summary>

    machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
    scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
    TPC-DS sf100 test result on commit 35eca0a4d023670a2d648db1a4ae93851511c21f, data reload: false
    
    query5	4310	588	456	456
    query6	415	205	188	188
    query7	4796	448	219	219
    query8	327	183	165	165
    query9	8755	4062	4027	4027
    query10	462	314	260	260
    query11	5908	3455	3210	3210
    query12	141	88	82	82
    query13	1249	447	308	308
    query14	6546	4822	4560	4560
    query14_1	4356	4329	4305	4305
    query15	210	206	181	181
    query16	939	446	412	412
    query17	870	653	535	535
    query18	2431	424	314	314
    query19	217	163	138	138
    query20	85	85	80	80
    query21	207	140	117	117
    query22	13145	13028	12809	12809
    query23	13163	12672	12071	12071
    query23_1	12206	12314	12218	12218
    query24	7092	870	477	477
    query24_1	469	482	465	465
    query25	545	441	330	330
    query26	1264	249	135	135
    query27	2779	446	265	265
    query28	4620	1914	1910	1910
    query29	1547	585	447	447
    query30	289	216	179	179
    query31	900	764	645	645
    query32	136	88	89	88
    query33	512	305	234	234
    query34	909	861	506	506
    query35	767	779	633	633
    query36	805	829	726	726
    query37	126	96	90	90
    query38	1796	1755	1724	1724
    query39	698	732	704	704
    query39_1	664	683	669	669
    query40	219	118	97	97
    query41	76	74	65	65
    query42	92	85	83	83
    query43	379	386	337	337
    query44	1370	689	687	687
    query45	183	180	167	167
    query46	860	974	576	576
    query47	2945	2969	2945	2945
    query48	318	320	214	214
    query49	581	390	297	297
    query50	703	276	221	221
    query51	10484	10523	10509	10509
    query52	87	89	79	79
    query53	191	210	152	152
    query54	235	190	196	190
    query55	69	71	64	64
    query56	220	223	217	217
    query57	1636	1616	1571	1571
    query58	278	280	247	247
    query59	2356	2399	2155	2155
    query60	286	236	221	221
    query61	147	145	151	145
    query62	387	344	295	295
    query63	211	172	174	172
    query64	2763	942	754	754
    query65	3453	3373	3366	3366
    query66	1781	407	312	312
    query67	20190	20171	20014	20014
    query68	3077	1021	605	605
    query69	389	294	244	244
    query70	921	847	849	847
    query71	302	230	216	216
    query72	2754	2767	2444	2444
    query73	549	553	301	301
    query74	4656	4508	4332	4332
    query75	2335	2298	1981	1981
    query76	2318	1054	641	641
    query77	379	416	309	309
    query78	8969	9023	8417	8417
    query79	971	928	528	528
    query80	801	454	344	344
    query81	562	321	280	280
    query82	567	141	110	110
    query83	273	195	179	179
    query84	316	120	96	96
    query85	965	413	368	368
    query86	359	256	231	231
    query87	2058	1952	1821	1821
    query88	3656	2686	2681	2681
    query89	343	300	260	260
    query90	1708	187	184	184
    query91	161	145	123	123
    query92	97	90	77	77
    query93	1062	1017	557	557
    query94	516	317	274	274
    query95	598	413	318	318
    query96	685	529	241	241
    query97	2480	2433	2290	2290
    query98	161	150	143	143
    query99	780	762	658	658
    Total cold run time: 234669 ms
    Total hot run time: 152472 ms
    

    </details>

    hello-stephen hello-stephen commented on Oct 09, 2026 04:23 UTC
    Contributor

    <details>
    <summary>ClickBench: <b>Total hot run time: 24.37 s</b></summary>

    machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
    scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
    ClickBench test result on commit 35eca0a4d023670a2d648db1a4ae93851511c21f, data reload: false
    
    query1	0.01	0.00	0.01
    query2	0.10	0.05	0.05
    query3	0.26	0.12	0.12
    query4	1.60	0.11	0.12
    query5	0.29	0.25	0.26
    query6	1.16	0.68	0.67
    query7	0.04	0.01	0.01
    query8	0.06	0.03	0.04
    query9	0.42	0.34	0.35
    query10	0.57	0.58	0.55
    query11	0.21	0.14	0.14
    query12	0.19	0.15	0.14
    query13	0.48	0.48	0.49
    query14	0.97	0.98	0.95
    query15	0.66	0.60	0.61
    query16	0.35	0.36	0.35
    query17	1.11	1.10	1.16
    query18	0.22	0.20	0.21
    query19	2.07	2.01	2.01
    query20	0.02	0.02	0.01
    query21	15.49	0.28	0.14
    query22	4.47	0.05	0.05
    query23	16.23	0.33	0.12
    query24	3.38	0.67	0.43
    query25	0.14	0.04	0.05
    query26	1.25	0.26	0.15
    query27	0.04	0.03	0.03
    query28	1.84	0.60	0.39
    query29	12.48	4.10	3.24
    query30	0.29	0.13	0.14
    query31	2.77	0.57	0.34
    query32	3.22	0.62	0.52
    query33	3.16	3.17	3.19
    query34	15.38	4.03	3.39
    query35	3.33	3.34	3.33
    query36	0.55	0.47	0.44
    query37	0.09	0.07	0.06
    query38	0.05	0.04	0.03
    query39	0.03	0.03	0.03
    query40	0.17	0.14	0.14
    query41	0.08	0.04	0.03
    query42	0.04	0.03	0.03
    query43	0.04	0.03	0.04
    Total cold run time: 95.31 s
    Total hot run time: 24.37 s
    

    </details>

    hello-stephen hello-stephen commented on Oct 09, 2026 06:39 UTC
    Contributor

    FE Regression Coverage Report


    Increment line coverage 100.00% (3/3) :tada:
    Increment coverage report
    Complete coverage report

    abhinav-phi abhinav-phi commented on code on Oct 16, 2026 16:35 UTC
    Code Review
    fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java:1
    @@ -364,9 +364,10 @@ public static Expression round(DecimalV3Literal first, IntegerLiteral second) {
          */
         @ExecFunction(name = "round")
         public static Expression round(DoubleLiteral first) {
    -        DecimalV3Literal middleResult = DecimalV3Literal.createWithoutCheck256(
    -                new BigDecimal(Double.toString(first.getValue())));
    -        return new DoubleLiteral(middleResult.round(0).getDouble());
    +        // BE uses std::round(): half away from zero, and a zero result keeps the sign of the
    +        // argument. Rounding through BigDecimal at scale 0 collapses -0.4 to positive zero.
    +        double value = first.getValue();
    +        return new DoubleLiteral(Math.copySign(Math.floor(Math.abs(value) + 0.5), value));

    fixed in 8da19ea9. the threshold no longer comes from adding a half to the input. round(DoubleLiteral) now takes the fractional part and compares that:

    double magnitude = Math.abs(value);
    double truncated = Math.floor(magnitude);
    if (magnitude - truncated >= 0.5) {
    truncated += 1.0;
    }
    return new DoubleLiteral(Math.copySign(truncated, value));

    magnitude - truncated is exact for every finite magnitude of 1 or above, and below 1 it is just the magnitude itself, so neither of the two cases you raised can reach a wrong branch. round(CAST(0.49999999999999994 AS DOUBLE)) folds to 0.0 and round(CAST(4503599627370497.0 AS DOUBLE)) comes back unchanged. both are asserted in NumericArithmeticTest now, along with the sign of a zero result.

    abhinav-phi abhinav-phi commented on code on Oct 16, 2026 16:35 UTC
    Code Review
    fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmeticTest.java:1
    @@ -86,4 +86,34 @@ private void assertInterval(int expected, long compareValue, long... thresholds)
                     new BigIntLiteral(compareValue), thresholdLiterals);
             Assertions.assertEquals(expected, result.getValue());
         }
    +
    +    @Test
    +    public void testCeilAndRoundKeepSignedZero() {
    +        DoubleLiteral ceil = (DoubleLiteral) NumericArithmetic.ceil(new DoubleLiteral(-0.5));
    +        Assertions.assertTrue(isNegativeZero(ceil.getValue()));
    +
    +        DoubleLiteral round = (DoubleLiteral) NumericArithmetic.round(new DoubleLiteral(-0.4));

    fixed in 8da19ea9. both scale zero DOUBLE overloads now hand off to the unary binary path instead of going through BigDecimal:

    public static Expression round(DoubleLiteral first, IntegerLiteral second) {
    if (second.getValue() == 0) {
    return round(first);
    }

    ceil(DoubleLiteral, IntegerLiteral) does the same against ceil(first), which matches the dispatch the BE makes for scale 0. round(CAST(-0.4 AS DOUBLE), 0) and ceil(CAST(-0.5 AS DOUBLE), 0) are both in the parameterized set now with the sign of the zero result asserted, so the two overloads can not drift back apart.

    abhinav-phi abhinav-phi commented on code on Oct 16, 2026 16:35 UTC
    Code Review
    fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java:1
    @@ -401,9 +402,8 @@ public static Expression ceil(DecimalV3Literal first, IntegerLiteral second) {
          */
         @ExecFunction(name = "ceil")
         public static Expression ceil(DoubleLiteral first) {
    -        DecimalV3Literal middleResult = DecimalV3Literal.createWithoutCheck256(
    -                new BigDecimal(Double.toString(first.getValue())));
    -        return new DoubleLiteral(middleResult.roundCeiling(0).getDouble());
    +        // BE uses std::ceil(), which keeps the sign of a zero result.

    fixed in 8da19ea9, in NumericLiteral.compareTo rather than at the call site you anchored on:

    double self = this.getDouble();
    double otherValue = ((Literal) other).getDouble();
    if (self == otherValue) {
    return 0;
    }
    return Double.compare(self, otherValue);

    the == settles the two signed zeros as one SQL value before Double.compare can order -0.0 below +0.0. every other ordering is untouched, because for all non zero doubles == and Double.compare agree, and the NaN case still falls through to Double.compare so the total order over NaN is preserved. ceil(CAST(-0.5 AS DOUBLE)) = CAST(0 AS DOUBLE) now folds to TRUE.

    abhinav-phi abhinav-phi commented on code on Oct 16, 2026 16:35 UTC
    Code Review
    fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java:1
    @@ -401,9 +402,8 @@ public static Expression ceil(DecimalV3Literal first, IntegerLiteral second) {
          */
         @ExecFunction(name = "ceil")
         public static Expression ceil(DoubleLiteral first) {
    -        DecimalV3Literal middleResult = DecimalV3Literal.createWithoutCheck256(
    -                new BigDecimal(Double.toString(first.getValue())));
    -        return new DoubleLiteral(middleResult.roundCeiling(0).getDouble());
    +        // BE uses std::ceil(), which keeps the sign of a zero result.

    fixed in 8da19ea9, but on DoubleLiteral rather than on Literal.equals. changing Literal.equals would move the equality contract for every literal type at once, so I overrode it where the value is actually a double:

    public boolean equals(Object o) {
    if (!(o instanceof DoubleLiteral)) {
    return false;
    }
    double other = ((DoubleLiteral) o).getValue();
    return value == other || (Double.isNaN(value) && Double.isNaN(other));
    }

    with a matching computeHashCode over Double.hashCode(value == 0.0 ? 0.0 : value), so a folded -0.0 and a +0.0 also land in the same bucket. java dispatches to the override, so visitInPredicate and the set dedup both see one value, which is the BE behaviour. ceil(CAST(-0.5 AS DOUBLE)) IN (CAST(0 AS DOUBLE), CAST(1 AS DOUBLE)) folds to TRUE and is asserted.

    one thing i want to flag rather than bury: FloatLiteral has the identical bit comparison today. i did not change it here because that widening is the subject of sibling pr #68730 and i did not want two branches editing the same equality question. if you would prefer this rule on Literal so both float and double get it in one place, say so and i will move it.

    abhinav-phi abhinav-phi commented on Oct 16, 2026 16:37 UTC
    Author

    @morrySnow all four of the review findings are now answered in the thread, with the code at 8da19ea9.

    two things I cannot do myself on this branch:

    the push that carried those fixes dismissed your earlier approval, so the pr needs a fresh +1 from a committer.

    and the GitHub Actions runs on this head are still sitting at action_required, 16 of them, created 2026-10-14T20:08Z, none with a single job. as a first time contributor I cannot approve them. could someone approve and run them, and if it is easier run buildall on 8da19ea9 so the FE UT and regression suites report against this head rather than the old one. nothing is red right now, it is that nothing has ever executed on this head.

    the one open question I am deliberately leaving to you is on the second signed zero finding: I put the value equality override on DoubleLiteral rather than on Literal so this branch does not move equality for every literal type. FloatLiteral still compares bits today. if you would rather that rule live on Literal, say so and I will move it in this branch.

    abhinav-phi abhinav-phi commented on Nov 08, 2026 04:16 UTC
    Author

    @morrySnow could you take a final look at 8da19ea9? your approval from 9 oct was dismissed by the push that carried the fixes, so this has been sitting without a verdict since.

    the four bot findings from 9 oct were raised against the old head 35eca0a4 and are addressed in 8da19ea9: the rounding threshold no longer adds 0.5, and signed zero now agrees with the backend through equals, compareTo and computeHashCode on DoubleLiteral, FloatLiteral and NumericLiteral. the bot review still points at the pre-fix head, so it reads as unresolved even though the code changed.

    if something still blocks it, tell me what and i will fix it in one pass rather than another round of single items. if it is fine, a fresh +1 is all it needs.

    abhinav-phi abhinav-phi commented on Nov 19, 2026 15:33 UTC
    Author

    hi @morrySnow, gentle ping on this ceil and round sign-of-zero fix for issue #68719.. unit tests included and it is still waiting on a first review.. thanks

    Reviewers
    Assignees
    abhinav-phi abhinav-phi
    Labels
    risk: low confidence: 89.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.4% (MEDIUM) · Higher = larger blast radius
    48.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.3% · Lower = rushed or shallow review
    89.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 5.2% · Safe to merge
    5.2%
    Repository