Skip to content

Commit

Permalink
DRILL-6401: Precision for decimal data types may be lost for the case…
Browse files Browse the repository at this point in the history
… when cast with literal is used

close #1254
  • Loading branch information
vvysotskyi authored and Aman Sinha committed May 11, 2018
1 parent 11036c5 commit 16659ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Expand Up @@ -550,7 +550,9 @@ public LogicalExpression visitLiteral(RexLiteral literal) {
literal.getType().getScale(),
literal.getType().getPrecision()));
}
return ValueExpressions.getVarDecimal((BigDecimal) literal.getValue());
return ValueExpressions.getVarDecimal((BigDecimal) literal.getValue(),
literal.getType().getPrecision(),
literal.getType().getScale());
}
double dbl = ((BigDecimal) literal.getValue()).doubleValue();
logger.warn("Converting exact decimal into approximate decimal.\n" +
Expand Down
Expand Up @@ -181,7 +181,9 @@ private LogicalExpression getValueExpressionFromConst(ValueHolder holder, TypePr
VarDecimalHolder decimalHolder = (VarDecimalHolder) holder;
return ValueExpressions.getVarDecimal(
DecimalUtility.getBigDecimalFromDrillBuf(decimalHolder.buffer,
decimalHolder.start, decimalHolder.end - decimalHolder.start, decimalHolder.scale));
decimalHolder.start, decimalHolder.end - decimalHolder.start, decimalHolder.scale),
decimalHolder.precision,
decimalHolder.scale);
case DATE:
return ValueExpressions.getDate(((DateHolder) holder).value);
case TIMESTAMP:
Expand Down
Expand Up @@ -110,8 +110,8 @@ public static LogicalExpression getDecimal38(BigDecimal i) {
return new Decimal38Expression(i, ExpressionPosition.UNKNOWN);
}

public static LogicalExpression getVarDecimal(BigDecimal i) {
return new VarDecimalExpression(i, ExpressionPosition.UNKNOWN);
public static LogicalExpression getVarDecimal(BigDecimal input, int precision, int scale) {
return new VarDecimalExpression(input, precision, scale, ExpressionPosition.UNKNOWN);
}

public static LogicalExpression getNumericExpression(String sign, String s, ExpressionPosition ep) {
Expand Down Expand Up @@ -403,11 +403,15 @@ public Iterator<LogicalExpression> iterator() {

public static class VarDecimalExpression extends LogicalExpressionBase {

private BigDecimal bigDecimal;
private final BigDecimal bigDecimal;
private final int precision;
private final int scale;

public VarDecimalExpression(BigDecimal input, ExpressionPosition pos) {
public VarDecimalExpression(BigDecimal input, int precision, int scale, ExpressionPosition pos) {
super(pos);
this.bigDecimal = input;
this.precision = precision;
this.scale = scale;
}

public BigDecimal getBigDecimal() {
Expand All @@ -419,8 +423,8 @@ public MajorType getMajorType() {
return MajorType
.newBuilder()
.setMinorType(MinorType.VARDECIMAL)
.setScale(bigDecimal.scale())
.setPrecision(bigDecimal.precision())
.setScale(scale)
.setPrecision(precision)
.setMode(DataMode.REQUIRED)
.build();
}
Expand Down

0 comments on commit 16659ab

Please sign in to comment.