Karman580 opened a new issue, #19274:
URL: https://github.com/apache/iotdb/issues/19274
### Search before asking
- [x] I searched in the issues and found nothing similar. (#14149 is about
FLOAT to FLOAT casts and goes through a different code path.)
### Version
master @ e78f7366134e7f3cbedb463c3f5e9ebb26ba021c. The behaviour has been
there since the cast function became built-in (#9226).
### Describe the bug and provide the minimal reproduce step
`CAST(<double> AS INT32)` returns the wrong integer for many in-range
values. `CastFunctionUtils.castDoubleToInt` narrows the value to FLOAT before
rounding:
```java
//
iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/util/CastFunctionUtils.java
public static int castDoubleToInt(double value) {
if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
throw new SemanticException(...);
}
return Math.round((float) value);
}
```
A FLOAT has a 24-bit significand, so integers above 2^24 can change before
rounding. Doubles that sit just below a `.5` tie can also be moved onto the tie
and rounded the wrong way (2.4999999999 becomes 3). `castDoubleToLong` already
does `Math.round(value)` on the double, and the conversion table in the user
guide documents DOUBLE to INT32 as "Out of the range of INT32: throw Exception,
otherwise Math.round()".
Every DOUBLE to INT32 path ends up in this method: the per-value and batch
column casts used by both the tree and table models, and table-model constant
folding (`IrExpressionInterpreter.visitCast` -> `CastFunctionUtils.cast`).
**Tree model**
```sql
CREATE DATABASE root.cast;
CREATE TIMESERIES root.cast.d1.s WITH DATATYPE=DOUBLE, ENCODING=PLAIN;
INSERT INTO root.cast.d1(time,s) VALUES(1,16777217.0);
INSERT INTO root.cast.d1(time,s) VALUES(2,123456789.0);
INSERT INTO root.cast.d1(time,s) VALUES(3,-123456789.0);
INSERT INTO root.cast.d1(time,s) VALUES(4,2147483000.0);
INSERT INTO root.cast.d1(time,s) VALUES(5,2.4999999999);
INSERT INTO root.cast.d1(time,s) VALUES(6,100000001.5);
SELECT s, CAST(s AS INT32), CAST(s AS INT64) FROM root.cast.d1;
```
```
+-----------------------------+--------------+-----------------------------+-----------------------------+
| Time|root.cast.d1.s|CAST(root.cast.d1.s AS
INT32)|CAST(root.cast.d1.s AS INT64)|
+-----------------------------+--------------+-----------------------------+-----------------------------+
|1970-01-01T05:30:00.001+05:30| 1.6777217E7| 16777216|
16777217|
|1970-01-01T05:30:00.002+05:30| 1.23456789E8| 123456792|
123456789|
|1970-01-01T05:30:00.003+05:30| -1.23456789E8| -123456792|
-123456789|
|1970-01-01T05:30:00.004+05:30| 2.147483E9| 2147483008|
2147483000|
|1970-01-01T05:30:00.005+05:30| 2.4999999999| 3|
2|
|1970-01-01T05:30:00.006+05:30| 1.000000015E8| 100000000|
100000002|
+-----------------------------+--------------+-----------------------------+-----------------------------+
```
**Table model**
```sql
CREATE DATABASE castdb;
CREATE TABLE castdb.t (id STRING TAG, d DOUBLE FIELD);
-- same six values inserted with id = 'a'
SELECT d, CAST(d AS INT32) AS d_int32, CAST(d AS INT64) AS d_int64 FROM
castdb.t ORDER BY time;
```
```
+-------------+----------+----------+
| d| d_int32| d_int64|
+-------------+----------+----------+
| 1.6777217E7| 16777216| 16777217|
| 1.23456789E8| 123456792| 123456789|
|-1.23456789E8|-123456792|-123456789|
| 2.147483E9|2147483008|2147483000|
| 2.4999999999| 3| 2|
|1.000000015E8| 100000000| 100000002|
+-------------+----------+----------+
```
Constant folding gives the same results:
```sql
SELECT CAST(123456789.0 AS INT32) AS c1, CAST(16777217.0 AS INT32) AS c2,
CAST(2.4999999999 AS INT32) AS c3;
```
```
+---------+--------+--+
| c1| c2|c3|
+---------+--------+--+
|123456792|16777216| 3|
+---------+--------+--+
```
**Unit level**
I added these assertions to `CastFunctionUtilsTest` (calc-commons) and ran
them against unmodified master:
```java
Assert.assertEquals(16777217, CastFunctionUtils.castDoubleToInt(16777217.0));
Assert.assertEquals(123456789,
CastFunctionUtils.castDoubleToInt(123456789.0));
// ...
```
```
./mvnw -pl iotdb-core/calc-commons test -Dtest=CastFunctionUtilsTest
[ERROR] Tests run: 23, Failures: 1, Errors: 0, Skipped: 0
[ERROR] CastFunctionUtilsTest.castDoubleToIntKeepsDoublePrecision:79
expected:<16777217> but was:<16777216>
```
Calling the method directly on the same build:
```
castDoubleToInt(1.6777217E7) = 16777216 Math.round = 16777217
castDoubleToInt(1.23456789E8) = 123456792 Math.round = 123456789
castDoubleToInt(-1.23456789E8) = -123456792 Math.round = -123456789
castDoubleToInt(2.147483E9) = 2147483008 Math.round = 2147483000
castDoubleToInt(2.4999999999) = 3 Math.round = 2
castDoubleToInt(1.000000015E8) = 100000000 Math.round = 100000002
```
**Environment**
- macOS 26 (Darwin 25.6.0, arm64)
- Unit tests: Temurin JDK 25.0.2, Maven wrapper 3.9.12
- SQL: standalone `2.0.11-SNAPSHOT` built locally from master.
`CastFunctionUtils` has not changed on master since 2026-11-02.
### What did you expect to see?
The value rounded with `Math.round` on the double: 16777217, 123456789,
-123456789, 2147483000, 2 and 100000002. These are the same numbers `CAST(...
AS INT64)` returns.
### What did you see instead?
16777216, 123456792, -123456792, 2147483008, 3 and 100000000.
### Anything else?
Values that already worked are unchanged. ±1.5 and ±2.5 round as before (2,
3, -1, -2), and 2147483648.0 / -2147483649.0 still throw the out-of-range error.
The fix should be `return (int) Math.round(value);`. The existing range
check limits the value to [Integer.MIN_VALUE, Integer.MAX_VALUE] before
rounding, so the narrowing from `long` cannot overflow.
### Are you willing to submit a PR?
- [x] I'm willing to submit a PR!
--
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]