Skip to content

Commit

Permalink
fix: zero decimal bug (#5531)
Browse files Browse the repository at this point in the history
* fix zero decimal bug

* fix zero decimal bug

* fix zero decimal bug

* fix zero decimal bug and add test

* fix zero decimal bug add tests

Co-authored-by: Natea Beshada <[email protected]>
  • Loading branch information
nae701 and Natea Beshada authored Jun 3, 2020
1 parent bc9ad2e commit 1b9094b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ private static void ensureMax(final BigDecimal value, final int precision, final
}

public static SqlType fromValue(final BigDecimal value) {
final BigDecimal bigDecimalZero = BigDecimal.ZERO;

/* When a BigDecimal has value 0, the built-in method precision() always returns 1. To account
for this edge case, we just take the scale and add one and use that for the precision instead.
*/
if (value.compareTo(bigDecimalZero) == 0) {
return SqlTypes.decimal(value.scale() + 1, value.scale());
}
return SqlTypes.decimal(value.precision(), value.scale());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ public void shouldExtractPrecisionFromDecimalSchema() {
assertThat(scale, is(2));
}

@Test
public void shouldExtractPrecisionFromZeroValue() {
// When:
final SqlType zeroDecimal = DecimalUtil.fromValue(BigDecimal.ZERO.setScale(2));

// Then:
assertThat(zeroDecimal, is(SqlTypes.decimal(3,2)));
}
@Test
public void shouldCastDecimal() {
// When:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,18 @@
"outputs": [
{"topic": "test_topic", "key": null, "value": {"ID": 1}}
]
},
{
"name": "should insert decimals that equal zero",
"statements": [
"CREATE STREAM S (ID INT KEY, V0 DECIMAL(12, 2)) WITH (kafka_topic='test_topic',partitions = 1, value_format='JSON');",
"INSERT INTO S (ID,V0) VALUES (1,0.00);"
],
"inputs": [
],
"outputs": [
{"topic": "test_topic", "key": 1, "value": {"V0": 0.00}}
]
}
]
}

0 comments on commit 1b9094b

Please sign in to comment.