Karman580 opened a new pull request, #19275:
URL: https://github.com/apache/iotdb/pull/19275

   ## Description
   
   Closes #19274
   
   ### Root cause
   
   `CastFunctionUtils.castDoubleToInt` rounded `(float) value`. A FLOAT keeps 
only 24 significand bits, so in-range doubles above 2^24 can change before they 
are rounded, and doubles just below a `.5` tie can be moved onto the tie. For 
example, `123456789.0` came out as `123456792` and `2.4999999999` as `3`. Every 
DOUBLE to INT32 cast goes through this method: the per-value and batch column 
casts in the tree and table models, and table-model constant folding.
   
   ### Change
   
   ```java
   - return Math.round((float) value);
   + return (int) Math.round(value);
   ```
   
   This matches `castDoubleToLong` and the documented rule for DOUBLE to INT32 
("Math.round()"). The range check above it is unchanged. It limits the value to 
[Integer.MIN_VALUE, Integer.MAX_VALUE], so `Math.round(value)` stays within int 
range and the cast from `long` cannot overflow. Half values keep their existing 
behaviour (2.5 -> 3, -2.5 -> -2), and out-of-range values still throw.
   
   ### Tests
   
   New cases in `CastFunctionUtilsTest`:
   - `castDoubleToIntKeepsDoublePrecision`: 16777217, ±123456789, 2147483000, 
2147483646, -2147483647, Integer.MAX_VALUE, Integer.MIN_VALUE, 2.4999999999 and 
100000001.5
   - `castDoubleToIntRoundsHalfUp`: 2.5, -1.5 and -2.5
   - `castDoubleToIntJustAboveMax` / `castDoubleToIntJustBelowMin`: 
2147483648.0 and -2147483649.0 throw `SemanticException`
   
   Results with `./mvnw -pl iotdb-core/calc-commons test 
-Dtest=CastFunctionUtilsTest` (Temurin JDK 25.0.2, macOS arm64):
   
   Before the fix:
   ```
   [ERROR] Tests run: 23, Failures: 1, Errors: 0, Skipped: 0
   [ERROR]   CastFunctionUtilsTest.castDoubleToIntKeepsDoublePrecision:79 
expected:<16777217> but was:<16777216>
   ```
   
   After the fix:
   ```
   [INFO] Tests run: 23, Failures: 0, Errors: 0, Skipped: 0
   [INFO] BUILD SUCCESS
   ```
   
   `./mvnw -pl iotdb-core/calc-commons test` (whole module) also passes: `Tests 
run: 91, Failures: 0, Errors: 0, Skipped: 0`. Spotless check passes.
   
   I also ran the SQL from #19274 on a standalone `2.0.11-SNAPSHOT` server with 
the patched class. Tree, table and constant-folded casts now return the same 
values as `CAST(... AS INT64)`:
   
   ```
   |  1.6777217E7|  16777217|  16777217|
   | 1.23456789E8| 123456789| 123456789|
   |-1.23456789E8|-123456789|-123456789|
   |   2.147483E9|2147483000|2147483000|
   | 2.4999999999|         2|         2|
   |1.000000015E8| 100000002| 100000002|
   ```
   
   `SELECT CAST(2147483648.0 AS INT32)` still fails with `701: Double value 
2147483648.000000 is out of range of integer value.`, and 
`TRY_CAST(-2147483649.0 AS INT32)` returns null.
   
   Not run locally: the integration-test suites (`IoTDBCastFunctionIT`, 
`IoTDBCastFunctionTableIT`) and the datanode module tests. I checked that no 
existing IT asserts the old FLOAT-rounded values.
   
   <hr>
   
   This PR has:
   - [x] been self-reviewed.
   - [x] added unit tests or modified existing tests to cover new code paths, 
ensuring the threshold
     for code coverage.
   
   <hr>
   
   ##### Key changed/added classes (or packages if there are too many classes) 
in this PR
   
   - `CastFunctionUtils` (calc-commons)
   - `CastFunctionUtilsTest`
   


-- 
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]

Reply via email to