Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ public double getDouble() {
/**
* 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,7 +46,7 @@ public DecimalV3Literal(BigDecimal value) {
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);
Expand Down Expand Up @@ -80,4 +83,27 @@ public DecimalV3Literal roundFloor(int newScale) {
.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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ public static DataType convertPrimitiveFromStrings(List<String> types, boolean t
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ public void testDatetimev1() {

}

@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";
Expand Down