Skip to content

Commit

Permalink
[CALCITE-6190] Incorrect precision derivation for negative numeric types
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
  • Loading branch information
mihaibudiu committed Jan 10, 2024
1 parent a86bca8 commit 135e963
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ public static SqlNumericLiteral createExactNumeric(
int prec;
int scale;

// We expect that s is already trimmed
int i = s.indexOf('.');
if ((i >= 0) && ((s.length() - 1) != i)) {
value = SqlParserUtil.parseDecimal(s);
Expand All @@ -957,6 +958,10 @@ public static SqlNumericLiteral createExactNumeric(
scale = 0;
prec = s.length();
}
if (value.compareTo(BigDecimal.ZERO) < 0) {
// The '-' sign should not be counted
prec--;
}
return new SqlNumericLiteral(
value,
prec,
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ static SqlOperatorTable operatorTableFor(SqlLibrary library) {
sql("select 1 as c1,2 as c2 from (values(true))").ok();
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6190">
* Incorrect precision derivation for negative numeric types</a>. */
@Test void testTypeOfDecimal() {
sql("select DECIMAL '100.01' as c1 from (values (true))")
.columnType("DECIMAL(5, 2) NOT NULL");
sql("select DECIMAL '-100.01' as c1 from (values (true))")
.columnType("DECIMAL(5, 2) NOT NULL");
sql("select DECIMAL ' 100.01 ' as c1 from (values (true))")
.columnType("DECIMAL(5, 2) NOT NULL");
sql("select DECIMAL ' -100.01 ' as c1 from (values (true))")
.columnType("DECIMAL(5, 2) NOT NULL");
}

@Test void testTypeOfAs() {
sql("select 1 as c1 from (values (true))")
.columnType("INTEGER NOT NULL");
Expand Down

0 comments on commit 135e963

Please sign in to comment.