Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: zero decimal bug #5531

Merged
merged 7 commits into from
Jun 3, 2020
Merged
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
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) {
agavra marked this conversation as resolved.
Show resolved Hide resolved
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",
agavra marked this conversation as resolved.
Show resolved Hide resolved
"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}}
]
}
]
}