Skip to content

Commit f4b20ec

Browse files
committed
Fixed Unit Tests
1 parent cce2982 commit f4b20ec

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

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

+3-11
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,11 @@ public RexNode ensureType(
5959
* {@link RexLiteral} if {@code matchNullability} is false.
6060
*
6161
* @param type Type to cast to
62-
* @param exp Expression being cast
63-
* @param matchNullability Whether to ensure the result has the same
64-
* nullability as {@code type}
62+
* @param exp Expression being castnullability as {@code type}
6563
* @return Call to CAST operator
6664
*/
6765
@Override
68-
public RexNode makeCast(RelDataType type, RexNode exp, boolean matchNullability) {
69-
if (matchNullability) {
70-
return makeAbstractCast(type, exp);
71-
}
72-
// for the case when BigDecimal literal has a scale or precision
73-
// that differs from the value from specified RelDataType, cast cannot be removed
74-
// TODO: remove this code when CALCITE-1468 is fixed
66+
public RexNode makeCast(RelDataType type, RexNode exp) {
7567
if (type.getSqlTypeName() == SqlTypeName.DECIMAL && exp instanceof RexLiteral) {
7668
int precision = type.getPrecision();
7769
int scale = type.getScale();
@@ -85,7 +77,7 @@ public RexNode makeCast(RelDataType type, RexNode exp, boolean matchNullability)
8577
}
8678
}
8779
}
88-
return super.makeCast(type, exp, false);
80+
return super.makeCast(type, exp);
8981
}
9082

9183
private void validatePrecisionAndScale(int precision, int scale) {

exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestCastFunctions.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.collect.Maps;
2222
import org.apache.drill.categories.SqlFunctionTest;
2323
import org.apache.drill.categories.UnlikelyTest;
24+
import org.apache.drill.common.exceptions.UserException;
2425
import org.apache.drill.common.exceptions.UserRemoteException;
2526
import org.apache.drill.common.types.TypeProtos;
2627
import org.apache.drill.common.types.Types;
@@ -41,6 +42,7 @@
4142
import org.junit.rules.ExpectedException;
4243

4344
import java.math.BigDecimal;
45+
import java.math.RoundingMode;
4446
import java.time.LocalDate;
4547
import java.time.LocalDateTime;
4648
import java.time.LocalTime;
@@ -620,7 +622,7 @@ public void testCastDecimalLiteral() throws Exception {
620622
.sqlQuery(query)
621623
.ordered()
622624
.baselineColumns("c1")
623-
.baselineValues(new BigDecimal("100.00"))
625+
.baselineValues(new BigDecimal("100.00").setScale(2, RoundingMode.HALF_UP))
624626
.go();
625627
}
626628

@@ -638,8 +640,8 @@ public void testCastDecimalZeroPrecision() throws Exception {
638640
public void testCastDecimalGreaterScaleThanPrecision() throws Exception {
639641
String query = "select cast('123.0' as decimal(3, 5))";
640642

641-
thrown.expect(UserRemoteException.class);
642-
thrown.expectMessage(containsString("VALIDATION ERROR: Expected scale less than or equal to precision, but was scale 5 and precision 3."));
643+
thrown.expect(UserException.class);
644+
thrown.expectMessage(containsString("VALIDATION ERROR: Expected scale less than or equal to precision, but was precision 3 and scale 5."));
643645

644646
run(query);
645647
}

exec/vector/src/main/java/org/apache/drill/exec/util/DecimalUtility.java

+18
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,22 @@ public static void checkValueOverflow(BigDecimal value, int desiredPrecision, in
451451
.build(logger);
452452
}
453453
}
454+
455+
/**
456+
* Ensures that a BigDecimal value conforms to the specified precision and scale.
457+
*
458+
* @param value the BigDecimal to be adjusted
459+
* @param precision the desired precision
460+
* @param scale the desired scale
461+
* @return the adjusted BigDecimal value
462+
*/
463+
public static BigDecimal enforcePrecisionAndScale(BigDecimal value, int precision, int scale) {
464+
// Log values being checked
465+
logger.debug("Adjusting value: {}, desired precision: {}, desired scale: {}", value, precision, scale);
466+
if (value.precision() > precision) {
467+
throw new IllegalArgumentException("Precision overflow: value = " + value);
468+
}
469+
return value.setScale(scale, RoundingMode.HALF_UP);
470+
}
471+
454472
}

0 commit comments

Comments
 (0)