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: floating point comparison was inexact #4372

Merged
merged 3 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -449,11 +449,14 @@ private String visitBytesComparisonExpression(
}

private String toDecimal(final SqlType schema, final int index) {
if (schema.baseType() == SqlBaseType.DECIMAL) {
return "%" + index + "$s";
switch (schema.baseType()) {
case DECIMAL:
return "%" + index + "$s";
case DOUBLE:
return "new BigDecimal(Double.toString(%" + index + "$s))";
default:
return "new BigDecimal(%" + index + "$s)";
}

return "new BigDecimal(%" + index + "$s)";
}

private String visitBooleanComparisonExpression(final ComparisonExpression.Type type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ public void shouldGenerateCorrectCodeForDecimalDoubleEQ() {
final String java = sqlToJavaVisitor.process(compExp);

// Then:
assertThat(java, containsString("(TEST1_COL8.compareTo(new BigDecimal(TEST1_COL3)) == 0))"));
assertThat(java, containsString("(TEST1_COL8.compareTo(new BigDecimal(Double.toString(TEST1_COL3))) == 0))"));
}

@Test
Expand All @@ -672,7 +672,7 @@ public void shouldGenerateCorrectCodeForDoubleDecimalEQ() {
final String java = sqlToJavaVisitor.process(compExp);

// Then:
assertThat(java, containsString("(new BigDecimal(TEST1_COL3).compareTo(TEST1_COL8) == 0))"));
assertThat(java, containsString("(new BigDecimal(Double.toString(TEST1_COL3)).compareTo(TEST1_COL8) == 0))"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"comments": [
"Tests covering working with floating point numbers"
],
"tests": [
{
"name": "filter by DOUBLE",
"statements": [
"CREATE STREAM INPUT (ROWKEY DOUBLE KEY, ID STRING) WITH (kafka_topic='test_topic', value_format='JSON');",
"CREATE STREAM OUTPUT AS SELECT * FROM INPUT WHERE ROWKEY > 0.1;"
],
"inputs": [
{"topic": "test_topic", "key": 0.0, "value": {"ID": 0}},
{"topic": "test_topic", "key": 0.099, "value": {"ID": 1}},
{"topic": "test_topic", "key": 0.1, "value": {"ID": 2}},
{"topic": "test_topic", "key": 0.10001, "value": {"ID": 3}},
{"topic": "test_topic", "key": 0.2, "value": {"ID": 4}},
{"topic": "test_topic", "key": null, "value": {"ID": 5}}
],
"outputs": [
{"topic": "OUTPUT", "key": 0.10001, "value": {"ID": 3}},
{"topic": "OUTPUT", "key": 0.2, "value": {"ID": 4}}
]
}
]
}