You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2023/06/27 09:25:11 UTC
[doris] branch master updated: [fix](nereids)return original expr if cast to decimal literal overflow (#21189)
This is an automated email from the ASF dual-hosted git repository.
morrysnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new c52c73c1c6 [fix](nereids)return original expr if cast to decimal literal overflow (#21189)
c52c73c1c6 is described below
commit c52c73c1c64be3d3b067364717f5e4e4f6c4e52d
Author: starocean999 <40...@users.noreply.github.com>
AuthorDate: Tue Jun 27 17:25:04 2023 +0800
[fix](nereids)return original expr if cast to decimal literal overflow (#21189)
---
.../java/org/apache/doris/analysis/CastExpr.java | 5 +-
.../rules/expression/rules/SimplifyCastRule.java | 81 ++++++++++++----------
.../expressions/literal/DecimalV3Literal.java | 27 ++++++++
.../suites/correctness_p0/test_cast_decimal.groovy | 37 ++++++++++
4 files changed, 114 insertions(+), 36 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
index be93975efb..6ceb8e3d84 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
@@ -446,8 +446,11 @@ public class CastExpr extends Expr {
return new LargeIntLiteral(value.getStringValue());
} else if (type.isDecimalV2() || type.isDecimalV3()) {
if (targetTypeDef != null) {
- return new DecimalLiteral(value.getStringValue(),
+ DecimalLiteral literal = new DecimalLiteral(value.getStringValue(),
((ScalarType) targetTypeDef.getType()).getScalarScale());
+ literal.checkPrecisionAndScale(targetTypeDef.getType().getPrecision(),
+ ((ScalarType) targetTypeDef.getType()).getScalarScale());
+ return literal;
} else {
return new DecimalLiteral(value.getStringValue());
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyCastRule.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyCastRule.java
index dad18d8aaf..d88489084d 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyCastRule.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyCastRule.java
@@ -65,42 +65,53 @@ public class SimplifyCastRule extends AbstractExpressionRewriteRule {
}
if (child instanceof Literal) {
- DataType castType = cast.getDataType();
- if (castType instanceof StringType) {
- if (child instanceof VarcharLiteral) {
- return new StringLiteral(((VarcharLiteral) child).getValue());
- } else if (child instanceof CharLiteral) {
- return new StringLiteral(((CharLiteral) child).getValue());
- }
- } else if (castType instanceof VarcharType) {
- if (child instanceof VarcharLiteral) {
- return new VarcharLiteral(((VarcharLiteral) child).getValue(), ((VarcharType) castType).getLen());
- } else if (child instanceof CharLiteral) {
- return new VarcharLiteral(((CharLiteral) child).getValue(), ((VarcharType) castType).getLen());
- }
- } else if (castType instanceof DecimalV2Type) {
- if (child instanceof TinyIntLiteral) {
- return new DecimalLiteral(new BigDecimal(((TinyIntLiteral) child).getValue()));
- } else if (child instanceof SmallIntLiteral) {
- return new DecimalLiteral(new BigDecimal(((SmallIntLiteral) child).getValue()));
- } else if (child instanceof IntegerLiteral) {
- return new DecimalLiteral(new BigDecimal(((IntegerLiteral) child).getValue()));
- } else if (child instanceof BigIntLiteral) {
- return new DecimalLiteral(new BigDecimal(((BigIntLiteral) child).getValue()));
- }
- } else if (castType instanceof DecimalV3Type) {
- DecimalV3Type decimalV3Type = (DecimalV3Type) castType;
- if (child instanceof TinyIntLiteral) {
- return new DecimalV3Literal(decimalV3Type, new BigDecimal(((TinyIntLiteral) child).getValue()));
- } else if (child instanceof SmallIntLiteral) {
- return new DecimalV3Literal(decimalV3Type, new BigDecimal(((SmallIntLiteral) child).getValue()));
- } else if (child instanceof IntegerLiteral) {
- return new DecimalV3Literal(decimalV3Type, new BigDecimal(((IntegerLiteral) child).getValue()));
- } else if (child instanceof BigIntLiteral) {
- return new DecimalV3Literal(decimalV3Type, new BigDecimal(((BigIntLiteral) child).getValue()));
- } else if (child instanceof DecimalV3Literal) {
- return new DecimalV3Literal(decimalV3Type, ((DecimalV3Literal) child).getValue());
+ try {
+ DataType castType = cast.getDataType();
+ if (castType instanceof StringType) {
+ if (child instanceof VarcharLiteral) {
+ return new StringLiteral(((VarcharLiteral) child).getValue());
+ } else if (child instanceof CharLiteral) {
+ return new StringLiteral(((CharLiteral) child).getValue());
+ }
+ } else if (castType instanceof VarcharType) {
+ if (child instanceof VarcharLiteral) {
+ return new VarcharLiteral(((VarcharLiteral) child).getValue(),
+ ((VarcharType) castType).getLen());
+ } else if (child instanceof CharLiteral) {
+ return new VarcharLiteral(((CharLiteral) child).getValue(),
+ ((VarcharType) castType).getLen());
+ }
+ } else if (castType instanceof DecimalV2Type) {
+ if (child instanceof TinyIntLiteral) {
+ return new DecimalLiteral(new BigDecimal(((TinyIntLiteral) child).getValue()));
+ } else if (child instanceof SmallIntLiteral) {
+ return new DecimalLiteral(new BigDecimal(((SmallIntLiteral) child).getValue()));
+ } else if (child instanceof IntegerLiteral) {
+ return new DecimalLiteral(new BigDecimal(((IntegerLiteral) child).getValue()));
+ } else if (child instanceof BigIntLiteral) {
+ return new DecimalLiteral(new BigDecimal(((BigIntLiteral) child).getValue()));
+ }
+ } else if (castType instanceof DecimalV3Type) {
+ DecimalV3Type decimalV3Type = (DecimalV3Type) castType;
+ if (child instanceof TinyIntLiteral) {
+ return new DecimalV3Literal(decimalV3Type,
+ new BigDecimal(((TinyIntLiteral) child).getValue()));
+ } else if (child instanceof SmallIntLiteral) {
+ return new DecimalV3Literal(decimalV3Type,
+ new BigDecimal(((SmallIntLiteral) child).getValue()));
+ } else if (child instanceof IntegerLiteral) {
+ return new DecimalV3Literal(decimalV3Type,
+ new BigDecimal(((IntegerLiteral) child).getValue()));
+ } else if (child instanceof BigIntLiteral) {
+ return new DecimalV3Literal(decimalV3Type,
+ new BigDecimal(((BigIntLiteral) child).getValue()));
+ } else if (child instanceof DecimalV3Literal) {
+ return new DecimalV3Literal(decimalV3Type,
+ ((DecimalV3Literal) child).getValue());
+ }
}
+ } catch (Throwable t) {
+ return cast;
}
}
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 748c44cd09..fe6040ca85 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;
@@ -37,9 +40,13 @@ public class DecimalV3Literal extends Literal {
this.value = Objects.requireNonNull(value);
}
+ /**
+ * Constructor for DecimalV3Literal
+ */
public DecimalV3Literal(DecimalV3Type dataType, BigDecimal value) {
super(DecimalV3Type.createDecimalV3Type(dataType.getPrecision(), dataType.getScale()));
Objects.requireNonNull(value, "value not be null");
+ checkPrecisionAndScale(dataType.getPrecision(), dataType.getScale(), value);
BigDecimal adjustedValue = value.scale() < 0 ? value
: value.setScale(dataType.getScale(), RoundingMode.HALF_UP);
this.value = Objects.requireNonNull(adjustedValue);
@@ -64,4 +71,24 @@ public class DecimalV3Literal extends Literal {
public double getDouble() {
return value.doubleValue();
}
+
+ private 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/regression-test/suites/correctness_p0/test_cast_decimal.groovy b/regression-test/suites/correctness_p0/test_cast_decimal.groovy
new file mode 100644
index 0000000000..88859ea1d5
--- /dev/null
+++ b/regression-test/suites/correctness_p0/test_cast_decimal.groovy
@@ -0,0 +1,37 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("test_cast_decimal") {
+ sql """
+ set enable_nereids_planner=true;
+ """
+
+ explain {
+ sql """select cast(32123.34212456734 as decimal(3,2));"""
+ contains "cast(32123.34212456734 as DECIMALV3(3, 2))"
+ }
+
+
+ sql """
+ set enable_nereids_planner=false;
+ """
+
+ explain {
+ sql """select cast(32123.34212456734 as decimal(3,2));"""
+ contains "CAST(32123.34212456734 AS DECIMALV3(3, 2))"
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org