[fix](planner)strip trailing zeros for decimal literal if the precision larger than max decimal precision in doris (#29737)

This commit is contained in:
starocean999
2024-01-10 09:55:57 +08:00
committed by yiguolei
parent a94f2564e3
commit 883d6dfc73
2 changed files with 21 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.Config;
import org.apache.doris.common.NotImplementedException;
import org.apache.doris.common.io.Text;
import org.apache.doris.qe.SessionVariable;
import org.apache.doris.thrift.TDecimalLiteral;
import org.apache.doris.thrift.TExprNode;
import org.apache.doris.thrift.TExprNodeType;
@ -126,6 +127,19 @@ public class DecimalLiteral extends LiteralExpr {
this.value = value;
int precision = getBigDecimalPrecision(this.value);
int scale = getBigDecimalScale(this.value);
int maxPrecision =
SessionVariable.getEnableDecimal256() ? ScalarType.MAX_DECIMAL256_PRECISION
: ScalarType.MAX_DECIMAL128_PRECISION;
int integerPart = precision - scale;
if (precision > maxPrecision) {
BigDecimal stripedValue = value.stripTrailingZeros();
int stripedPrecision = getBigDecimalPrecision(stripedValue);
if (stripedPrecision <= maxPrecision) {
this.value = stripedValue.setScale(maxPrecision - integerPart);
precision = getBigDecimalPrecision(this.value);
scale = getBigDecimalScale(this.value);
}
}
if (enforceV3) {
type = ScalarType.createDecimalV3Type(precision, scale);
} else {

View File

@ -90,5 +90,12 @@ public class DecimalLiteralTest {
scale = ((ScalarType) literal.getType()).getScalarScale();
Assert.assertEquals(3, precision);
Assert.assertEquals(3, scale);
decimal = new BigDecimal("197323961.520000000000000000000000000000");
literal = new DecimalLiteral(decimal);
precision = ((ScalarType) literal.getType()).getScalarPrecision();
scale = ((ScalarType) literal.getType()).getScalarScale();
Assert.assertEquals(38, precision);
Assert.assertEquals(29, scale);
}
}