This is an automated email from the ASF dual-hosted git repository.

JackieTien97 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 9b2238027be Fix CAST(DOUBLE AS INT32) precision loss from narrowing to 
FLOAT
9b2238027be is described below

commit 9b2238027be2a700afb30eadea37a32b0bbd3ebe
Author: KARMAN SINGH TALWAR <[email protected]>
AuthorDate: Mon Nov 16 08:40:15 2026 +0530

    Fix CAST(DOUBLE AS INT32) precision loss from narrowing to FLOAT
---
 .../transformation/dag/util/CastFunctionUtils.java |  2 +-
 .../dag/util/CastFunctionUtilsTest.java            | 31 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git 
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/util/CastFunctionUtils.java
 
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/util/CastFunctionUtils.java
index df5d41356b0..dff2abe6c0e 100644
--- 
a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/util/CastFunctionUtils.java
+++ 
b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/dag/util/CastFunctionUtils.java
@@ -67,7 +67,7 @@ public class CastFunctionUtils {
           String.format(
               
CalcMessages.EXCEPTION_DOUBLE_VALUE_ARG_OUT_RANGE_INTEGER_VALUE_BAB52E11, 
value));
     }
-    return Math.round((float) value);
+    return (int) Math.round(value);
   }
 
   public static long castDoubleToLong(double value) {
diff --git 
a/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/transformation/dag/util/CastFunctionUtilsTest.java
 
b/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/transformation/dag/util/CastFunctionUtilsTest.java
index 9bfb0e6177f..96005f20c23 100644
--- 
a/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/transformation/dag/util/CastFunctionUtilsTest.java
+++ 
b/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/transformation/dag/util/CastFunctionUtilsTest.java
@@ -74,11 +74,42 @@ public class CastFunctionUtilsTest {
     Assert.assertEquals(2, CastFunctionUtils.castDoubleToInt(1.5));
   }
 
+  @Test
+  public void castDoubleToIntKeepsDoublePrecision() {
+    Assert.assertEquals(16777217, 
CastFunctionUtils.castDoubleToInt(16777217.0));
+    Assert.assertEquals(123456789, 
CastFunctionUtils.castDoubleToInt(123456789.0));
+    Assert.assertEquals(-123456789, 
CastFunctionUtils.castDoubleToInt(-123456789.0));
+    Assert.assertEquals(2147483000, 
CastFunctionUtils.castDoubleToInt(2147483000.0));
+    Assert.assertEquals(2147483646, 
CastFunctionUtils.castDoubleToInt(2147483646.0));
+    Assert.assertEquals(-2147483647, 
CastFunctionUtils.castDoubleToInt(-2147483647.0));
+    Assert.assertEquals(Integer.MAX_VALUE, 
CastFunctionUtils.castDoubleToInt(2147483647.0));
+    Assert.assertEquals(Integer.MIN_VALUE, 
CastFunctionUtils.castDoubleToInt(-2147483648.0));
+    Assert.assertEquals(2, CastFunctionUtils.castDoubleToInt(2.4999999999));
+    Assert.assertEquals(100000002, 
CastFunctionUtils.castDoubleToInt(100000001.5));
+  }
+
+  @Test
+  public void castDoubleToIntRoundsHalfUp() {
+    Assert.assertEquals(3, CastFunctionUtils.castDoubleToInt(2.5));
+    Assert.assertEquals(-1, CastFunctionUtils.castDoubleToInt(-1.5));
+    Assert.assertEquals(-2, CastFunctionUtils.castDoubleToInt(-2.5));
+  }
+
   @Test(expected = SemanticException.class)
   public void castDoubleToIntOverflow() {
     CastFunctionUtils.castDoubleToInt(Double.MAX_VALUE);
   }
 
+  @Test(expected = SemanticException.class)
+  public void castDoubleToIntJustAboveMax() {
+    CastFunctionUtils.castDoubleToInt(2147483648.0);
+  }
+
+  @Test(expected = SemanticException.class)
+  public void castDoubleToIntJustBelowMin() {
+    CastFunctionUtils.castDoubleToInt(-2147483649.0);
+  }
+
   // ---- castDoubleToLong ----
   @Test
   public void castDoubleToLongInRange() {

Reply via email to