From 1ab63b5cad86ad343ec9d45f479eb86b43d6b17b Mon Sep 17 00:00:00 2001 From: stdpain <34912776+stdpain@users.noreply.github.com> Date: Wed, 27 Nov 2024 16:57:10 +0800 Subject: [PATCH] [Enhancement] add Type Check for decimal input (#53217) (cherry picked from commit af8df3513ffe6b01fdb0f50b6953ee8f2c729add) --- .../sql/optimizer/validate/TypeChecker.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/validate/TypeChecker.java b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/validate/TypeChecker.java index 7970afb87df98..985e1d83eb9e4 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/validate/TypeChecker.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/validate/TypeChecker.java @@ -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; @@ -190,7 +192,25 @@ private void checkAggCall(Map 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);