abhinav-phi opened a new pull request, #68729:
URL: https://github.com/apache/doris/pull/68729
### What problem does this PR solve?
Issue Number: close #68714
Related PR: None
Problem Summary:
`FractionalLiteral.uncheckedCastTo(BooleanType)` tested the value with
`((Number) value).floatValue() == 0`. For a `DOUBLE` literal, narrowing to
`float` first
flushes every magnitude below the float32 rounding limit (about `7e-46`,
i.e. half the
smallest positive float32 subnormal) to `0.0f`, so a nonzero double folded
to `FALSE`:
```sql
SELECT CAST(5e-324 AS BOOLEAN); -- FE fold: FALSE
SELECT IF(1e-300, 't', 'f'); -- FE fold: f
```
The same value read from a column goes through BE, where
`be/src/exprs/function/cast/cast_to_boolean.h` compares the original
`Float64` against zero
and returns `TRUE`. So a constant expression and a column expression
disagreed, and a
constant `WHERE` predicate built from such a literal could drop every row.
The fix compares in the value's own precision, `doubleValue() == 0`.
Widening `float` to
`double` is exact, so the `FloatLiteral` path keeps its previous behaviour;
only the `DOUBLE`
path changes, which is the one that was wrong. `NaN` and both infinities
still fold to
`TRUE` as before, which is what the existing assertions in
`DoubleLiteralTest` require.
### 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:
Five assertions were added to the existing boolean block of
`fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DoubleLiteralTest.java`
covering `Double.MIN_VALUE`, `-Double.MIN_VALUE`, `1e-300`, `1e-46` and
the exact literal
from the report, `5e-324`.
Red, on `master` (`7ac0eb2f`) with only the source line reverted:
```
[ERROR] DoubleLiteralTest.testUncheckedCastTo:61 expected: <true> but
was: <false>
[ERROR] Tests run: 7, Failures: 1, Errors: 0, Skipped: 0
[INFO] BUILD FAILURE
```
Green, with this change applied:
```
[INFO] Tests run: 2, Failures: 0 - DoubleLiteralTest
[INFO] Tests run: 3, Failures: 0 - FloatLiteralTest
[INFO] Tests run: 2, Failures: 0 -
org.apache.doris.analysis.FloatLiteralTest
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
```
Command used (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=DoubleLiteralTest,FloatLiteralTest
```
Environment: JDK 17.0.20 (Temurin), Maven 3.9.16, thrift compiler 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. <!-- Explain the behavior change -->
Constant folding of a nonzero `DOUBLE` whose magnitude is below the
float32 rounding
limit now yields `TRUE` when cast to `BOOLEAN` (and in predicates such
as `IF`, `CASE`,
`AND`, `WHERE`), instead of `FALSE`. This matches the BE result for
the same value and
matches the column path. No other cast target is touched.
- 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 added assertions. I
read and
reviewed every line myself, ran the FE unit tests above on the exact commit
in this branch,
and I take responsibility for the result. The change is one line of
production code.
--
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]