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 b7c4cc06674 [opt](Nereids) let DecimalV2Literal overflow check same
with V3 (#25699)
b7c4cc06674 is described below
commit b7c4cc06674d372170e1cde74271886ae62f6262
Author: morrySnow <[email protected]>
AuthorDate: Tue Oct 24 17:40:32 2023 +0800
[opt](Nereids) let DecimalV2Literal overflow check same with V3 (#25699)
---
.../trees/expressions/literal/DecimalLiteral.java | 34 +++++++++++++++++++++-
.../expressions/literal/DecimalV3Literal.java | 25 +---------------
2 files changed, 34 insertions(+), 25 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 5cde2e155fd..472ea3a5dc7 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
@@ -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.DecimalV2Type;
+import com.google.common.base.Preconditions;
+
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
@@ -36,9 +39,15 @@ public class DecimalLiteral extends Literal {
this(DecimalV2Type.createDecimalV2Type(value), value);
}
+ /**
+ * Constructor for DecimalLiteral
+ */
public DecimalLiteral(DecimalV2Type dataType, BigDecimal value) {
super(dataType);
- BigDecimal adjustedValue = value.scale() < 0 ? value :
value.setScale(dataType.getScale(), RoundingMode.DOWN);
+ 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);
}
@@ -61,4 +70,27 @@ public class DecimalLiteral extends Literal {
public double getDouble() {
return value.doubleValue();
}
+
+ /**
+ * check precision and scale is enough for value.
+ */
+ public 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/trees/expressions/literal/DecimalV3Literal.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java
index bc36c75436d..3a96bc5b442 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,12 +18,9 @@
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;
@@ -46,7 +43,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");
- checkPrecisionAndScale(dataType.getPrecision(), dataType.getScale(),
value);
+ DecimalLiteral.checkPrecisionAndScale(dataType.getPrecision(),
dataType.getScale(), value);
BigDecimal adjustedValue = value.scale() < 0 ? value
: value.setScale(dataType.getScale(), RoundingMode.HALF_UP);
this.value = Objects.requireNonNull(adjustedValue);
@@ -83,24 +80,4 @@ public class DecimalV3Literal extends Literal {
.createDecimalV3Type(((DecimalV3Type)
dataType).getPrecision(), newScale),
value.setScale(newScale, RoundingMode.FLOOR));
}
-
- 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));
- }
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]