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 4eb443fa3e9..aab7a244cef 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.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,14 @@ protected RexLiteral makeLiteral( 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) { + o = ((BigDecimal) o).multiply( + ((IntervalSqlType) type).getIntervalQualifier().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 9c0cd589c1a..f04c02c61a9 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,10 @@ void testCastToInterval(CastType castType, SqlOperatorFixture f) { "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"); } @ParameterizedTest