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

mbudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 9cb71c0dca [CALCITE-6714] Cast literal to interval gives the wrong 
result if literal is casted
9cb71c0dca is described below

commit 9cb71c0dcab70255b070a52d40799855c96e0507
Author: Aleksey Plekhanov <[email protected]>
AuthorDate: Wed Dec 4 12:20:18 2024 +0300

    [CALCITE-6714] Cast literal to interval gives the wrong result if literal 
is casted
---
 .../src/main/java/org/apache/calcite/rex/RexBuilder.java | 12 ++++++++++--
 .../java/org/apache/calcite/test/SqlOperatorTest.java    | 16 ++++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/rex/RexBuilder.java 
b/core/src/main/java/org/apache/calcite/rex/RexBuilder.java
index 4eb443fa3e..b1291af74c 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexBuilder.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexBuilder.java
@@ -39,6 +39,7 @@ import org.apache.calcite.sql.fun.SqlLibraryOperators;
 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
 import org.apache.calcite.sql.parser.SqlParserPos;
 import org.apache.calcite.sql.type.ArraySqlType;
+import org.apache.calcite.sql.type.IntervalSqlType;
 import org.apache.calcite.sql.type.MapSqlType;
 import org.apache.calcite.sql.type.MultisetSqlType;
 import org.apache.calcite.sql.type.SqlTypeFamily;
@@ -1336,8 +1337,15 @@ public class RexBuilder {
       o = ((TimestampWithTimeZoneString) o).round(p);
       break;
     case DECIMAL:
-      if (o != null && type.getScale() != RelDataType.SCALE_NOT_SPECIFIED) {
-        assert o instanceof BigDecimal;
+      if (o == null) {
+        break;
+      }
+      assert o instanceof BigDecimal;
+      if (type instanceof IntervalSqlType) {
+        SqlIntervalQualifier qualifier = ((IntervalSqlType) 
type).getIntervalQualifier();
+        o = ((BigDecimal) o).multiply(qualifier.getUnit().multiplier);
+        typeName = type.getSqlTypeName();
+      } else if (type.getScale() != RelDataType.SCALE_NOT_SPECIFIED) {
         o = ((BigDecimal) o).setScale(type.getScale(), 
typeFactory.getTypeSystem().roundingMode());
         if (type.getScale() < 0) {
           o = new BigDecimal(((BigDecimal) o).toPlainString());
diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java 
b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
index edaabb8de0..cea089a2ee 100644
--- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
+++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
@@ -1000,6 +1000,22 @@ public class SqlOperatorTest {
         "cast(-5723 as interval minute(4))",
         "-5723",
         "INTERVAL MINUTE(4) NOT NULL");
+    f.checkScalar(
+        "cast(cast(1 as integer) as interval minute)",
+        "+1",
+        "INTERVAL MINUTE NOT NULL");
+    f.checkScalar(
+        "cast(cast(1 as decimal) as interval second)",
+        "+1.000000",
+        "INTERVAL SECOND NOT NULL");
+    f.checkScalar(
+        "cast(cast(5 as bigint) as interval month)",
+        "+5",
+        "INTERVAL MONTH NOT NULL");
+    f.checkScalar(
+        "cast(cast(5 as smallint) as interval hour)",
+        "+5",
+        "INTERVAL HOUR NOT NULL");
   }
 
   @ParameterizedTest

Reply via email to