Skip to content
Closed
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
11 changes: 9 additions & 2 deletions core/src/main/java/org/apache/calcite/rex/RexBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -887,8 +887,15 @@ public RexLiteral makeExactLiteral(BigDecimal bd) {
}
} else {
int precision = bd.unscaledValue().abs().toString().length();
relType =
typeFactory.createSqlType(SqlTypeName.DECIMAL, precision, scale);
if (precision > scale) {
// bd is greater than or equal to 1
relType =
typeFactory.createSqlType(SqlTypeName.DECIMAL, precision, scale);
} else {
// bd is less than 1
relType =
typeFactory.createSqlType(SqlTypeName.DECIMAL, scale + 1, scale);
}
}
return makeExactLiteral(bd, relType);
}
Expand Down
13 changes: 11 additions & 2 deletions core/src/test/java/org/apache/calcite/test/RexTransformerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ private RexNode isTrue(RexNode node) {

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-814">[CALCITE-814]
* RexBuilder reverses precision and scale of DECIMAL literal</a>. */
* RexBuilder reverses precision and scale of DECIMAL literal</a>
* and
* <a href="https://issues.apache.org/jira/browse/CALCITE-1344">[CALCITE-1344]
* Incorrect inferred precision when BigDecimal value is less than 1</a>. */
@Test public void testExactLiteral() {
final RexLiteral literal =
rexBuilder.makeExactLiteral(new BigDecimal("-1234.56"));
Expand All @@ -336,8 +339,14 @@ private RexNode isTrue(RexNode node) {
final RexLiteral literal3 =
rexBuilder.makeExactLiteral(new BigDecimal("0.0123456"));
assertThat(literal3.getType().getFullTypeString(),
is("DECIMAL(6, 7) NOT NULL"));
is("DECIMAL(8, 7) NOT NULL"));
assertThat(literal3.getValue().toString(), is("0.0123456"));

final RexLiteral literal4 =
rexBuilder.makeExactLiteral(new BigDecimal("0.01234560"));
assertThat(literal4.getType().getFullTypeString(),
is("DECIMAL(9, 8) NOT NULL"));
assertThat(literal4.getValue().toString(), is("0.01234560"));
}

/** Test case for
Expand Down