Skip to content

Commit

Permalink
[fix](Nereids) simplify comparison predicate do wrong cast (#44054)
Browse files Browse the repository at this point in the history
Related PR: #41151
Problem Summary:
lead to analysis error: Invalid precision and scale
  • Loading branch information
morrySnow authored and Your Name committed Nov 18, 2024
1 parent ea61206 commit 5e08674
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@ private static Expression processDecimalV3TypeCoercion(ComparisonPredicate compa
left = cast.child();
DecimalV3Literal literal = (DecimalV3Literal) right;
if (left.getDataType().isDecimalV3Type()) {
if (((DecimalV3Type) left.getDataType())
.getScale() < ((DecimalV3Type) literal.getDataType()).getScale()) {
DecimalV3Type leftType = (DecimalV3Type) left.getDataType();
DecimalV3Type literalType = (DecimalV3Type) literal.getDataType();
if (leftType.getScale() < literalType.getScale()) {
int toScale = ((DecimalV3Type) left.getDataType()).getScale();
if (comparisonPredicate instanceof EqualTo) {
try {
return TypeCoercionUtils.processComparisonPredicate((ComparisonPredicate)
comparisonPredicate.withChildren(left,
new DecimalV3Literal((DecimalV3Type) left.getDataType(),
literal.getValue().setScale(toScale, RoundingMode.UNNECESSARY))));
comparisonPredicate.withChildren(left, new DecimalV3Literal(
literal.getValue().setScale(toScale, RoundingMode.UNNECESSARY))));
} catch (ArithmeticException e) {
if (left.nullable()) {
// TODO: the ideal way is to return an If expr like:
Expand All @@ -255,9 +255,8 @@ private static Expression processDecimalV3TypeCoercion(ComparisonPredicate compa
} else if (comparisonPredicate instanceof NullSafeEqual) {
try {
return TypeCoercionUtils.processComparisonPredicate((ComparisonPredicate)
comparisonPredicate.withChildren(left,
new DecimalV3Literal((DecimalV3Type) left.getDataType(),
literal.getValue().setScale(toScale, RoundingMode.UNNECESSARY))));
comparisonPredicate.withChildren(left, new DecimalV3Literal(
literal.getValue().setScale(toScale, RoundingMode.UNNECESSARY))));
} catch (ArithmeticException e) {
return BooleanLiteral.of(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,17 @@ void testDecimalV3Literal() {
rewrittenExpression.child(0).getDataType());
Assertions.assertInstanceOf(DecimalV3Literal.class, rewrittenExpression.child(1));
Assertions.assertEquals(new BigDecimal("12.35"), ((DecimalV3Literal) rewrittenExpression.child(1)).getValue());

// left's child range smaller than right literal
leftChild = new DecimalV3Literal(new BigDecimal("1234.12"));
left = new Cast(leftChild, DecimalV3Type.createDecimalV3Type(10, 5));
right = new DecimalV3Literal(new BigDecimal("12345.12000"));
expression = new EqualTo(left, right);
rewrittenExpression = executor.rewrite(expression, context);
Assertions.assertInstanceOf(Cast.class, rewrittenExpression.child(0));
Assertions.assertEquals(DecimalV3Type.createDecimalV3Type(7, 2),
rewrittenExpression.child(0).getDataType());
Assertions.assertInstanceOf(DecimalV3Literal.class, rewrittenExpression.child(1));
Assertions.assertEquals(new BigDecimal("12345.12"), ((DecimalV3Literal) rewrittenExpression.child(1)).getValue());
}
}

0 comments on commit 5e08674

Please sign in to comment.