Skip to content

Commit acdad90

Browse files
committed
Fix more unit tests
1 parent edfcf36 commit acdad90

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/conversion/DrillRexBuilder.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,24 @@ public RexNode makeCast(RelDataType type, RexNode exp, boolean matchNullability)
144144
return makeAbstractCast(type, exp);
145145
}
146146

147-
// for the case when BigDecimal literal has a scale or precision
148-
// that differs from the value from specified RelDataType, cast cannot be removed
149-
// TODO: remove this code when CALCITE-1468 is fixed
150-
if (type.getSqlTypeName() == SqlTypeName.DECIMAL && exp instanceof RexLiteral) {
147+
// Validate DECIMAL precision and scale for all DECIMAL casts
148+
// This catches user-specified invalid types before DrillTypeFactory auto-fixes them
149+
if (type.getSqlTypeName() == SqlTypeName.DECIMAL) {
151150
int precision = type.getPrecision();
152151
int scale = type.getScale();
153152
validatePrecisionAndScale(precision, scale);
154-
Comparable<?> value = ((RexLiteral) exp).getValueAs(Comparable.class);
155-
if (value instanceof BigDecimal) {
156-
BigDecimal bigDecimal = (BigDecimal) value;
157-
DecimalUtility.checkValueOverflow(bigDecimal, precision, scale);
158-
if (bigDecimal.precision() != precision || bigDecimal.scale() != scale) {
159-
return makeAbstractCast(type, exp);
153+
154+
// for the case when BigDecimal literal has a scale or precision
155+
// that differs from the value from specified RelDataType, cast cannot be removed
156+
// TODO: remove this code when CALCITE-1468 is fixed
157+
if (exp instanceof RexLiteral) {
158+
Comparable<?> value = ((RexLiteral) exp).getValueAs(Comparable.class);
159+
if (value instanceof BigDecimal) {
160+
BigDecimal bigDecimal = (BigDecimal) value;
161+
DecimalUtility.checkValueOverflow(bigDecimal, precision, scale);
162+
if (bigDecimal.precision() != precision || bigDecimal.scale() != scale) {
163+
return makeAbstractCast(type, exp);
164+
}
160165
}
161166
}
162167
}

exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/TestDecimal.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,10 @@ public void testSimpleDecimalArithmetic() throws Exception {
159159

160160
// NOTE: Calcite 1.38 changed DECIMAL arithmetic behavior affecting scale in results.
161161
// Multiplication results now include decimal scale in string representation.
162+
// Values calculated: row2: 11.1*11.1=123.21, row5: 987654321.1*123.1=121580246927.41
162163
String addOutput[] = {"123456888.0", "22.2", "0.2", "-0.2", "-987654444.2","-3.0"};
163164
String subtractOutput[] = {"123456690.0", "0.0", "0.0", "0.0", "-987654198.0", "-1.0"};
164-
String multiplyOutput[] = {"12222222111.00", "123.21", "0.01", "0.01", "121580246926.01", "2.03"};
165+
String multiplyOutput[] = {"12222222111.00", "123.21", "0.01", "0.01", "121580246927.41", "2.03"};
165166

166167
Iterator<VectorWrapper<?>> itr = batchLoader.iterator();
167168

@@ -213,7 +214,7 @@ public void testComplexDecimal() throws Exception {
213214
// NOTE: Calcite 1.38 changed DECIMAL arithmetic behavior affecting precision and scale in results.
214215
// Results may now include more decimal places in string representation.
215216
String addOutput[] = {"-99999998877.700000000", "11.423456789", "123456789.100000000", "-0.119998000", "100000000112.423456789", "-99999999879.907000000", "123456789123456801.300000000"};
216-
String subtractOutput[] = {"-100000001124.300000000", "10.823456789", "-123456788.899999999", "-0.120002000", "99999999889.823456789", "-100000000122.093000000", "123456789123456776.700000000"};
217+
String subtractOutput[] = {"-100000001124.300000000", "10.823456789", "-123456788.900000000", "-0.120002000", "99999999889.823456789", "-100000000122.093000000", "123456789123456776.700000000"};
217218

218219
Iterator<VectorWrapper<?>> itr = batchLoader.iterator();
219220

0 commit comments

Comments
 (0)