This is an automated email from the ASF dual-hosted git repository. kxiao pushed a commit to branch branch-2.0 in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.0 by this push: new 95c8d3ed879 [feature](nereids)support decimalv2 #28726 (#28838) 95c8d3ed879 is described below commit 95c8d3ed87965be7360e763c844670204973dca7 Author: starocean999 <40539150+starocean...@users.noreply.github.com> AuthorDate: Mon Dec 25 15:43:05 2023 +0800 [feature](nereids)support decimalv2 #28726 (#28838) --- .../trees/expressions/literal/DecimalLiteral.java | 5 ++-- .../expressions/literal/DecimalV3Literal.java | 28 +++++++++++++++++++++- .../org/apache/doris/nereids/types/DataType.java | 12 ++++++++++ .../doris/nereids/parser/NereidsParserTest.java | 8 +++++++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalLiteral.java index 472ea3a5dc7..711673cc2ff 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalLiteral.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalLiteral.java @@ -74,13 +74,14 @@ public class DecimalLiteral extends Literal { /** * check precision and scale is enough for value. */ - public static void checkPrecisionAndScale(int precision, int scale, BigDecimal value) throws AnalysisException { + private static void checkPrecisionAndScale(int precision, int scale, BigDecimal value) throws AnalysisException { Preconditions.checkNotNull(value); int realPrecision = value.precision(); int realScale = value.scale(); boolean valid = true; if (precision != -1 && scale != -1) { - if (precision < realPrecision || scale < realScale) { + if (precision < realPrecision || scale < realScale + || realPrecision - realScale > DecimalV2Type.MAX_PRECISION - DecimalV2Type.MAX_SCALE) { valid = false; } } else { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java index 3a96bc5b442..a2b500cc08f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java @@ -18,9 +18,12 @@ package org.apache.doris.nereids.trees.expressions.literal; import org.apache.doris.analysis.LiteralExpr; +import org.apache.doris.nereids.exceptions.AnalysisException; import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; import org.apache.doris.nereids.types.DecimalV3Type; +import com.google.common.base.Preconditions; + import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Objects; @@ -43,7 +46,7 @@ public class DecimalV3Literal extends Literal { public DecimalV3Literal(DecimalV3Type dataType, BigDecimal value) { super(DecimalV3Type.createDecimalV3Type(dataType.getPrecision(), dataType.getScale())); Objects.requireNonNull(value, "value not be null"); - DecimalLiteral.checkPrecisionAndScale(dataType.getPrecision(), dataType.getScale(), value); + checkPrecisionAndScale(dataType.getPrecision(), dataType.getScale(), value); BigDecimal adjustedValue = value.scale() < 0 ? value : value.setScale(dataType.getScale(), RoundingMode.HALF_UP); this.value = Objects.requireNonNull(adjustedValue); @@ -80,4 +83,27 @@ public class DecimalV3Literal extends Literal { .createDecimalV3Type(((DecimalV3Type) dataType).getPrecision(), newScale), value.setScale(newScale, RoundingMode.FLOOR)); } + + /** + * check precision and scale is enough for value. + */ + private static void checkPrecisionAndScale(int precision, int scale, BigDecimal value) throws AnalysisException { + Preconditions.checkNotNull(value); + int realPrecision = value.precision(); + int realScale = value.scale(); + boolean valid = true; + if (precision != -1 && scale != -1) { + if (precision < realPrecision || scale < realScale) { + valid = false; + } + } else { + valid = false; + } + + if (!valid) { + throw new AnalysisException( + String.format("Invalid precision and scale - expect (%d, %d), but (%d, %d)", + precision, scale, realPrecision, realScale)); + } + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/types/DataType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/types/DataType.java index 7baa65d2647..ffe3c738a2d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/types/DataType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/types/DataType.java @@ -159,6 +159,18 @@ public abstract class DataType implements AbstractDataType { throw new AnalysisException("Nereids do not support type: " + type); } } + case "decimalv2": + switch (types.size()) { + case 1: + return DecimalV2Type.SYSTEM_DEFAULT; + case 2: + return DecimalV2Type.createDecimalV2Type(Integer.parseInt(types.get(1)), 0); + case 3: + return DecimalV2Type.createDecimalV2Type(Integer.parseInt(types.get(1)), + Integer.parseInt(types.get(2))); + default: + throw new AnalysisException("Nereids do not support type: " + type); + } case "decimalv3": switch (types.size()) { case 1: diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java index 128569af31b..31cf7710021 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java @@ -284,6 +284,14 @@ public class NereidsParserTest extends ParserTestBase { } + @Test + public void testDecimalv2() { + String decv2 = "SELECT CAST('1.234' AS decimalv2(10,5)) FROM t"; + NereidsParser nereidsParser = new NereidsParser(); + LogicalPlan logicalPlan = (LogicalPlan) nereidsParser.parseSingle(decv2).child(0); + Assertions.assertTrue(logicalPlan.getExpressions().get(0).getDataType().isDecimalV2Type()); + } + @Test public void parseSetOperation() { String union = "select * from t1 union select * from t2 union all select * from t3"; --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org