abhinav-phi opened a new pull request, #68731:
URL: https://github.com/apache/doris/pull/68731
### 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:
```java
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 fix drops the decimal detour: `Math.ceil(value)` for `ceil`, and for
`round` the half-away-from-zero
rule with the sign carried over (`Math.copySign(Math.floor(Math.abs(value) +
0.5), value)`), which is
what `std::round` does. The bankers variant is a separate BE function
(`round_bankers`) and is not
touched here.
`NaN` and both infinities keep their existing behaviour through this
formulation, 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 or steps below)
- [ ] No need to test or manual test. Explain why:
New `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`.
Red, on `master` with only the test added:
```
[ERROR] NumericArithmeticTest.testCeilAndRoundKeepSignedZero:93
expected: <true> but was: <false>
```
Green, focused run with the fix:
```
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0 - in
NumericArithmeticTest
[INFO] BUILD SUCCESS
```
Wider net (`NumericArithmeticTest, FoldConstantTest,
ConstantProjectionFoldingTest,
InferPredicateFromMonotonicFunctionTest, CastTest, DoubleLiteralTest`) on
the patched tree:
```
NumericArithmeticTest Tests run: 5, Failures: 0
DoubleLiteralTest Tests run: 2, Failures: 0
CastTest Tests run: 23, Failures: 0
ConstantProjectionFoldingTest Tests run: 1, Failures: 0
InferPredicateFromMonotonicFunctionTest Tests run: 37, Failures: 0
```
The wider run still exits non-zero because of two `FoldConstantTest`
failures
(`testDateConstructFunction:1552`, `testFoldString:581`). Those are
**pre-existing on this machine
and unrelated to this change** - they fail identically on pristine
`master` with all of my edits
stashed, and they are session-time-zone dependent timestamp assertions:
```
[ERROR] FoldConstantTest.testDateConstructFunction:1552 expected:
<'1977-06-03 17:57:24'> but was: <'1977-06-03 15:27:24'>
[ERROR] FoldConstantTest.testFoldString:581 expected: <'2025-10-27
14:58:08.100000'> but was: <'2025-10-27 12:28:08.100000'>
```
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.`
Scope note: `round(x, 0)` and `truncate(x)` reach the same decimal detour
for a scale-0 result and
therefore have the same sign-of-zero loss. I deliberately left them out of
this PR because the
report is about the 1-arg `ceil`/`round`; happy to fold them in here or in
a follow-up if a
maintainer prefers one change.
- Behavior changed:
- [ ] No.
- [x] Yes. <!-- Explain the behavior change -->
Folding `ceil(<double literal in (-1,0))>` and `round(<double literal
that rounds to zero>)`
now yields `-0.0` instead of `+0.0`, matching the BE result for the
same value, so `signbit()`
agrees between the constant path and the column path. Magnitudes are
unchanged.
- 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 test. 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 commit in this branch, and I take
responsibility for the
result.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]