-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support numeric json serde for decimals (#3588)
- Loading branch information
Showing
29 changed files
with
586 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 0 additions & 94 deletions
94
ksql-functional-tests/src/main/java/io/confluent/ksql/test/serde/ValueSpec.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
...functional-tests/src/main/java/io/confluent/ksql/test/tools/ExpectedRecordComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"; you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.test.tools; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeType; | ||
import com.fasterxml.jackson.databind.node.NumericNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import com.google.common.collect.ImmutableMap; | ||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.function.BiPredicate; | ||
import java.util.function.Function; | ||
import org.apache.kafka.connect.data.Struct; | ||
|
||
public final class ExpectedRecordComparator { | ||
|
||
private static final Map<JsonNodeType, BiPredicate<Object, JsonNode>> COMPARATORS = | ||
ImmutableMap.<JsonNodeType, BiPredicate<Object, JsonNode>>builder() | ||
.put(JsonNodeType.OBJECT, ExpectedRecordComparator::compareStruct) | ||
.put(JsonNodeType.ARRAY, ExpectedRecordComparator::compareArray) | ||
.put(JsonNodeType.NUMBER, ExpectedRecordComparator::compareNumber) | ||
.put(JsonNodeType.STRING, ExpectedRecordComparator::compareText) | ||
.put(JsonNodeType.BOOLEAN, ExpectedRecordComparator::compareBoolean) | ||
.put(JsonNodeType.NULL, ExpectedRecordComparator::compareNull) | ||
.build(); | ||
|
||
private ExpectedRecordComparator() { | ||
} | ||
|
||
public static boolean matches(final Object actualValue, final JsonNode expectedValue) { | ||
return comparator(expectedValue).test(actualValue, expectedValue); | ||
} | ||
|
||
private static boolean compareStruct(final Object actualValue, final JsonNode expectedValue) { | ||
final ObjectNode expected = (ObjectNode) expectedValue; | ||
final Function<String, Object> getter; | ||
if (actualValue instanceof Struct) { | ||
getter = ((Struct) actualValue)::get; | ||
} else if (actualValue instanceof Map) { | ||
getter = ((Map<?, ?>) actualValue)::get; | ||
} else { | ||
return false; | ||
} | ||
|
||
final Iterator<Entry<String, JsonNode>> fields = expected.fields(); | ||
while (fields.hasNext()) { | ||
final Entry<String, JsonNode> field = fields.next(); | ||
if (!comparator(field.getValue()).test(getter.apply(field.getKey()), field.getValue())) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private static boolean compareArray(final Object actualValue, final JsonNode expectedValue) { | ||
final ArrayNode expected = (ArrayNode) expectedValue; | ||
if (actualValue instanceof List) { | ||
final List<?> actual = (List<?>) actualValue; | ||
final Iterator<JsonNode> elements = expected.elements(); | ||
|
||
int i = 0; | ||
while (elements.hasNext()) { | ||
final JsonNode el = elements.next(); | ||
if (!comparator(el).test(actual.get(i), el)) { | ||
return false; | ||
} | ||
i++; | ||
} | ||
|
||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean compareNumber(final Object actualValue, final JsonNode expectedValue) { | ||
final NumericNode expected = (NumericNode) expectedValue; | ||
if (actualValue instanceof Integer) { | ||
return expected.intValue() == (Integer) actualValue; | ||
} | ||
if (actualValue instanceof Long) { | ||
return expected.longValue() == (Long) actualValue; | ||
} | ||
if (actualValue instanceof Double) { | ||
return expected.doubleValue() == (Double) actualValue; | ||
} | ||
if (actualValue instanceof BigDecimal) { | ||
if (!expected.isBigDecimal()) { | ||
// we don't want to risk comparing a BigDecimal with something of | ||
// lower precision | ||
return false; | ||
} | ||
|
||
expected.isBigDecimal(); | ||
try { | ||
return expected.decimalValue() | ||
.setScale(((BigDecimal) actualValue).scale(), RoundingMode.UNNECESSARY) | ||
.equals(actualValue); | ||
} catch (final ArithmeticException e) { | ||
// the scale of the expected value cannot match the scale of the actual value | ||
// without rounding | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean compareText(final Object actualValue, final JsonNode expectedValue) { | ||
final TextNode expected = (TextNode) expectedValue; | ||
if (actualValue instanceof String) { | ||
return expected.asText().equals(actualValue); | ||
} | ||
if (actualValue instanceof BigDecimal) { | ||
return new BigDecimal(expected.asText()).equals(actualValue); | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean compareBoolean(final Object actualValue, final JsonNode expectedValue) { | ||
return expectedValue.asBoolean() == (Boolean) actualValue; | ||
} | ||
|
||
private static boolean compareNull(final Object actualValue, final JsonNode expectedValue) { | ||
return actualValue == null; | ||
} | ||
|
||
private static BiPredicate<Object, JsonNode> comparator(final JsonNode node) { | ||
final JsonNodeType type = node == null ? JsonNodeType.NULL : node.getNodeType(); | ||
final BiPredicate<Object, JsonNode> predicate = COMPARATORS.get(type); | ||
|
||
if (predicate == null) { | ||
throw new IllegalArgumentException( | ||
"KSQL Testing Tool cannot expect JSON node type: " + type); | ||
} | ||
return predicate; | ||
} | ||
|
||
} |
Oops, something went wrong.