Skip to content

Commit

Permalink
[CALCITE-6714] Cast literal to interval gives the wrong result if lit…
Browse files Browse the repository at this point in the history
…eral is casted
  • Loading branch information
alex-plekhanov committed Dec 4, 2024
1 parent 0a96bee commit 1172002
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions core/src/main/java/org/apache/calcite/rex/RexBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1172002

Please sign in to comment.