Skip to content

Commit

Permalink
[Enhancement] add Type Check for decimal input (#53217)
Browse files Browse the repository at this point in the history
(cherry picked from commit af8df35)
  • Loading branch information
stdpain authored and mergify[bot] committed Nov 27, 2024
1 parent 746a0f2 commit e025afe
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.starrocks.sql.optimizer.validate;

import com.starrocks.catalog.PrimitiveType;
import com.starrocks.catalog.ScalarType;
import com.starrocks.catalog.Type;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.optimizer.OptExpression;
Expand Down Expand Up @@ -190,7 +192,25 @@ private void checkAggCall(Map<ColumnRefOperator, CallOperator> aggregations, Agg
}
}

private boolean checkDecimalType(Type decimalType) {
ScalarType type = (ScalarType) decimalType;
final int scale = type.getScalarScale();
final int precision = type.getScalarPrecision();
final PrimitiveType primitiveType = type.getPrimitiveType();
return scale >= 0 && scale <= precision && precision <= PrimitiveType.getMaxPrecisionOfDecimal(primitiveType);
}

private void checkColType(ScalarOperator arg, ScalarOperator expr, Type defined, Type actual) {
if (actual.isDecimalV3()) {
checkArgument(checkDecimalType(actual),
"expr '%s' invalid actual type: %s",
PREFIX, expr, actual);
}
if (defined.isDecimalV3()) {
checkArgument(checkDecimalType(actual),
"expr '%s' invalid defined type: %s",
PREFIX, expr, defined);
}
checkArgument(actual.matchesType(defined),
"%s the type of arg %s in expr '%s' is defined as %s, but the actual type is %s",
PREFIX, arg, expr, defined, actual);
Expand Down

0 comments on commit e025afe

Please sign in to comment.