Skip to content

Commit

Permalink
[CALCITE-6322] Casts to DECIMAL types are ignored
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu committed Aug 1, 2024
1 parent e664ae0 commit 91cd2b1
Show file tree
Hide file tree
Showing 26 changed files with 522 additions and 426 deletions.
8 changes: 8 additions & 0 deletions babel/src/test/resources/sql/big-query.iq
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ SELECT SAFE_ADD(CAST(1.7e308 as DOUBLE), CAST(1.7e308 as DOUBLE)) as double_over

!ok

!if (fixed.calcite6328) {
SELECT SAFE_ADD(9, cast(9.999999999999999999e75 as DECIMAL(38, 19))) as decimal_overflow;
+------------------+
| decimal_overflow |
Expand All @@ -669,6 +670,7 @@ SELECT SAFE_ADD(9, cast(9.999999999999999999e75 as DECIMAL(38, 19))) as decimal_
(1 row)

!ok
!}

# NaN arguments should return NaN
SELECT SAFE_ADD(CAST('NaN' AS DOUBLE), CAST(3 as BIGINT)) as NaN_result;
Expand Down Expand Up @@ -720,6 +722,7 @@ SELECT SAFE_DIVIDE(CAST(1.7e308 as DOUBLE),

!ok

!if (fixed.calcite6328) {
SELECT SAFE_DIVIDE(CAST(-3.5e75 AS DECIMAL(76, 0)),
CAST(3.5e-75 AS DECIMAL(76, 0))) as decimal_overflow;
+------------------+
Expand All @@ -730,6 +733,7 @@ SELECT SAFE_DIVIDE(CAST(-3.5e75 AS DECIMAL(76, 0)),
(1 row)

!ok
!}

# NaN arguments should return NaN
SELECT SAFE_DIVIDE(CAST('NaN' AS DOUBLE), CAST(3 as BIGINT)) as NaN_result;
Expand Down Expand Up @@ -801,6 +805,7 @@ SELECT SAFE_MULTIPLY(CAST(1.7e308 as DOUBLE), CAST(3 as BIGINT)) as double_overf

!ok

!if (fixed.calcite6328) {
SELECT SAFE_MULTIPLY(CAST(-3.5e75 AS DECIMAL(76, 0)), CAST(10 AS BIGINT)) as decimal_overflow;
+------------------+
| decimal_overflow |
Expand All @@ -810,6 +815,7 @@ SELECT SAFE_MULTIPLY(CAST(-3.5e75 AS DECIMAL(76, 0)), CAST(10 AS BIGINT)) as dec
(1 row)

!ok
!}

# NaN arguments should return NaN
SELECT SAFE_MULTIPLY(CAST('NaN' AS DOUBLE), CAST(3 as BIGINT)) as NaN_result;
Expand Down Expand Up @@ -916,6 +922,7 @@ SELECT SAFE_SUBTRACT(CAST(1.7e308 as DOUBLE), CAST(-1.7e308 as DOUBLE)) as doubl

!ok

!if (fixed.calcite6328) {
SELECT SAFE_SUBTRACT(9, cast(-9.999999999999999999e75 as DECIMAL(38, 19))) as decimal_overflow;
+------------------+
| decimal_overflow |
Expand All @@ -925,6 +932,7 @@ SELECT SAFE_SUBTRACT(9, cast(-9.999999999999999999e75 as DECIMAL(38, 19))) as de
(1 row)

!ok
!}

# NaN arguments should return NaN
SELECT SAFE_SUBTRACT(CAST('NaN' AS DOUBLE), CAST(3 as BIGINT)) as NaN_result;
Expand Down
100 changes: 50 additions & 50 deletions babel/src/test/resources/sql/redshift.iq
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ select approximate percentile_disc(0.5) within group (order by sal) from emp gro
# AVG
select avg(sal) from emp;
EXPR$0
2073.214285714286
2073.21
!ok

# COUNT
Expand Down Expand Up @@ -288,12 +288,12 @@ select percentile_disc(0.6) within group (order by sal) from emp group by deptno
# STDDEV_SAMP and STDDEV_POP
select stddev_samp(sal) from emp;
EXPR$0
1182.503223516271873450023122131824493408203125
1182.50
!ok

select stddev_pop(sal) from emp;
EXPR$0
1139.488618295281639802851714193820953369140625
1139.48
!ok

# SUM
Expand All @@ -308,24 +308,24 @@ EXPR$0
!ok

# VAR_SAMP and VAR_POP
select var_samp(sal) from emp;
select var_samp(CAST(sal AS DECIMAL(11, 4))) from emp;
EXPR$0
1398313.873626374
1398313.8736
!ok

select var_samp(distinct sal) from emp;
select var_samp(distinct CAST(sal AS DECIMAL(11, 4))) from emp;
EXPR$0
1512779.356060606
1512779.3560
!ok

select var_samp(all sal) from emp;
select var_samp(all CAST(sal AS DECIMAL(11, 4))) from emp;
EXPR$0
1398313.873626374
1398313.8736
!ok

select var_pop(sal) from emp;
select var_pop(CAST(sal AS DECIMAL(11, 4))) from emp;
EXPR$0
1298434.31122449
1298434.3112
!ok

# 4 Bit-Wise Aggregate Functions
Expand Down Expand Up @@ -378,10 +378,10 @@ select empno, avg(sal) over (order by empno rows unbounded preceding) from emp w
EMPNO, EXPR$1
7499, 1600.00
7521, 1425.00
7654, 1366.666666666667
7654, 1366.66
7698, 1737.50
7844, 1690.00
7900, 1566.666666666667
7900, 1566.66
!ok

# COUNT
Expand Down Expand Up @@ -525,33 +525,33 @@ select deptno, ratio_to_report(sal) over (partition by deptno) from emp;
# STDDEV_POP
select empno, stddev_pop(comm) over (order by empno rows unbounded preceding) from emp where deptno = 30 order by 1;
EMPNO, EXPR$1
7499, 0
7521, 100
7654, 478.42333648024424519462627358734607696533203125
7698, 478.42333648024424519462627358734607696533203125
7844, 522.0153254455275373402400873601436614990234375
7900, 522.0153254455275373402400873601436614990234375
7499, 0.00
7521, 100.00
7654, 478.42
7698, 478.42
7844, 522.01
7900, 522.01
!ok

# STDDEV_SAMP (synonym for STDDEV)
select empno, stddev_samp(comm) over (order by empno rows unbounded preceding) from emp where deptno = 30 order by 1;
EMPNO, EXPR$1
7499, null
7521, 141.421356237309510106570087373256683349609375
7654, 585.9465277082316561063635163009166717529296875
7698, 585.9465277082316561063635163009166717529296875
7844, 602.7713773341707792496890760958194732666015625
7900, 602.7713773341707792496890760958194732666015625
7521, 141.42
7654, 585.94
7698, 585.94
7844, 602.77
7900, 602.77
!ok

select empno, stddev(comm) over (order by empno rows unbounded preceding) from emp where deptno = 30 order by 1;
EMPNO, EXPR$1
7499, null
7521, 141.421356237309510106570087373256683349609375
7654, 585.9465277082316561063635163009166717529296875
7698, 585.9465277082316561063635163009166717529296875
7844, 602.7713773341707792496890760958194732666015625
7900, 602.7713773341707792496890760958194732666015625
7521, 141.42
7654, 585.94
7698, 585.94
7844, 602.77
7900, 602.77
!ok

# SUM
Expand All @@ -566,35 +566,35 @@ EMPNO, EXPR$1
!ok

# VAR_POP
select empno, var_pop(comm) over (order by empno rows unbounded preceding) from emp where deptno = 30 order by 1;
select empno, var_pop(CAST(comm AS DECIMAL(10, 2))) over (order by empno rows unbounded preceding) from emp where deptno = 30 order by 1;
EMPNO, EXPR$1
7499, 0.0000
7521, 10000.0000
7654, 228888.888888889
7698, 228888.888888889
7844, 272500.0000
7900, 272500.0000
7499, 0.00
7521, 10000.00
7654, 228888.88
7698, 228888.88
7844, 272500.00
7900, 272500.00
!ok

# VAR_SAMP (synonym for VARIANCE)
select empno, var_samp(comm) over (order by empno rows unbounded preceding) from emp where deptno = 30 order by 1;
select empno, var_samp(CAST(comm AS DECIMAL(10, 2))) over (order by empno rows unbounded preceding) from emp where deptno = 30 order by 1;
EMPNO, EXPR$1
7499, null
7521, 20000.0000
7654, 343333.3333333335
7698, 343333.3333333335
7844, 363333.3333333333
7900, 363333.3333333333
7521, 20000.00
7654, 343333.33
7698, 343333.33
7844, 363333.33
7900, 363333.33
!ok

select empno, variance(comm) over (order by empno rows unbounded preceding) from emp where deptno = 30 order by 1;
select empno, variance(CAST(comm AS DECIMAL(10, 2))) over (order by empno rows unbounded preceding) from emp where deptno = 30 order by 1;
EMPNO, EXPR$1
7499, null
7521, 20000.0000
7654, 343333.3333333335
7698, 343333.3333333335
7844, 363333.3333333333
7900, 363333.3333333333
7521, 20000.00
7654, 343333.33
7698, 343333.33
7844, 363333.33
7900, 363333.33
!ok

# 5.2 Ranking functions
Expand Down Expand Up @@ -2030,12 +2030,12 @@ SELECT "JSON_EXTRACT_PATH_TEXT"('{"f2":{"f3":1},"f4":{"f5":99,"f6":"star"}}', 'f
# CAST and CONVERT
select cast(stddev_samp(sal) as dec(14, 2)) from emp;
EXPR$0
1182.503223516271873450023122131824493408203125
1182.50
!ok

select 123.456::decimal(8,4);
EXPR$0
123.456
123.4560
!ok

!if (position) {
Expand Down
2 changes: 1 addition & 1 deletion babel/src/test/resources/sql/spark.iq
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ EXPR$0

SELECT REVERSE(array(CAST(2.1 as decimal(17)), 2.1111111111111119));
EXPR$0
[2.1111111111111119, 2.1]
[2.11, 2.00]
!ok

SELECT REVERSE(array(CAST(2.1 as double), 2.1111111111111119));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,27 @@ private Expression getConvertExpression(
Expressions.constant(precision),
Expressions.constant(scale),
Expressions.constant(sourceType.getSqlTypeName().getEndUnit().multiplier));
} else if (sourceType.getSqlTypeName() == SqlTypeName.DECIMAL) {
// Cast from DECIMAL to DECIMAL, may adjust scale and precision.
return Expressions.call(
BuiltInMethod.DECIMAL_DECIMAL_CAST.method,
operand,
Expressions.constant(precision),
Expressions.constant(scale));
} else if (SqlTypeName.INT_TYPES.contains(sourceType.getSqlTypeName())) {
// Cast from INTEGER to DECIMAL, check for overflow
return Expressions.call(
BuiltInMethod.INTEGER_DECIMAL_CAST.method,
operand,
Expressions.constant(precision),
Expressions.constant(scale));
} else if (SqlTypeName.APPROX_TYPES.contains(sourceType.getSqlTypeName())) {
// Cast from FLOAT/DOUBLE to DECIMAL
return Expressions.call(
BuiltInMethod.FP_DECIMAL_CAST.method,
operand,
Expressions.constant(precision),
Expressions.constant(scale));
}
}
return defaultExpression.get();
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/calcite/rex/RexBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,12 @@ protected RexLiteral makeLiteral(
}
o = ((TimestampWithTimeZoneString) o).round(p);
break;
case DECIMAL:
if (o != null && type.getScale() != RelDataType.SCALE_NOT_SPECIFIED) {
assert o instanceof BigDecimal;
o = ((BigDecimal) o).setScale(type.getScale(), RoundingMode.DOWN);
}
break;
default:
break;
}
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/org/apache/calcite/util/Bug.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,22 @@ public abstract class Bug {
* [CALCITE-6294] Support IN filter in Arrow adapter</a> is fixed. */
public static final boolean CALCITE_6294_FIXED = false;

/** Whether
* <a href="https://issues.apache.org/jira/browse/CALCITE/issues/CALCITE-6295">
* [CALCITE-6295] Support IS NOT NULL in Arrow adapter</a> is fixed. */
public static final boolean CALCITE_6295_FIXED = false;

/** Whether
* <a href="https://issues.apache.org/jira/browse/CALCITE/issues/CALCITE-6296">
* [CALCITE-6296] Support IS NULL in Arrow adapter</a> is fixed. */
public static final boolean CALCITE_6296_FIXED = false;

/* Whether
* <a href="https://issues.apache.org/jira/browse/CALCITE-6328">[CALCITE-6328]
* The BigQuery functions SAFE_* do not match the BigQuery specification</a>
* is fixed. */
public static final boolean CALCITE_6328_FIXED = false;

/**
* Use this to flag temporary code.
*/
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ public enum BuiltInMethod {
ENUMERABLE_TO_LIST(ExtendedEnumerable.class, "toList"),
ENUMERABLE_TO_MAP(ExtendedEnumerable.class, "toMap", Function1.class, Function1.class),
AS_LIST(Primitive.class, "asList", Object.class),
DECIMAL_DECIMAL_CAST(Primitive.class, "decimalDecimalCast",
BigDecimal.class, int.class, int.class),
INTEGER_DECIMAL_CAST(Primitive.class, "integerDecimalCast",
Number.class, int.class, int.class),
FP_DECIMAL_CAST(Primitive.class, "fpDecimalCast",
Number.class, int.class, int.class),
INTEGER_CAST(Primitive.class, "integerCast", Primitive.class, Object.class),
MEMORY_GET0(MemoryFactory.Memory.class, "get"),
MEMORY_GET1(MemoryFactory.Memory.class, "get", int.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5393,7 +5393,8 @@ private void checkLiteral2(String expression, String expected) {
* is greater than maximum numeric scale</a>. */
@Test void testNumericScaleMod() {
final String sql = "SELECT MOD(CAST(2 AS DECIMAL(39, 20)), 2)";
final String expected = "SELECT MOD(2, 2)\nFROM (VALUES (0)) AS \"t\" (\"ZERO\")";
final String expected =
"SELECT MOD(2.00000000000000000000, 2)\nFROM (VALUES (0)) AS \"t\" (\"ZERO\")";
sql(sql).withPostgresqlModifiedDecimalTypeSystem()
.ok(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3851,7 +3851,7 @@ private SqlSpecialOperatorWithPolicy(String name, SqlKind kind, int prec, boolea
checkSimplify(div(a, one), "?0.notNullInt1");
checkSimplify(div(a, nullInt), "null:INTEGER");

checkSimplifyUnchanged(add(b, half));
checkSimplify(add(b, half), "?0.notNullDecimal2");

checkSimplify(add(zero, sub(nullInt, nullInt)), "null:INTEGER");
}
Expand All @@ -3863,5 +3863,4 @@ private SqlSpecialOperatorWithPolicy(String name, SqlKind kind, int prec, boolea
RexNode cast = rexBuilder.makeCast(nullableDateType, dateStr);
checkSimplify(cast, "2020-10-30");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ private static void assertRows(Interpreter interpreter,
final String sql = "select x, min(y), max(y), sum(y), avg(y)\n"
+ "from (values ('a', -1.2), ('a', 2.3), ('a', 15)) as t(x, y)\n"
+ "group by x";
sql(sql).returnsRows("[a, -1.2, 15.0, 16.1, 5.366666666666667]");
sql(sql).returnsRows("[a, -1.2, 15.0, 16.1, 5.3]");
}

@Test void testInterpretUnnest() {
Expand Down
Loading

0 comments on commit 91cd2b1

Please sign in to comment.