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

WIP - Decimal support #294

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

simplesteph
Copy link
Contributor

A big feature that was missing from the Rest proxy is the ability to send decimal fields. As it stands it requires the user to figure out a string to send, which is super risky and quite a complicated process.

Instead, my wish is for the rest proxy to take {"foo": 123.45 } as an input, and do the conversion to avro correctly on its own. On read of decimal fields, it should do the opposite operation and return {"foo": 123.45 }

I have added test cases that demonstrate all of this.

I look forward to feedback.
The one thing I'm not happy with is the use of "ISO-8859-1". If you see a simpler solution please do let me know.

The accuracy of this has been tested on a wide range of decimal numbers, and the deserialisation code from byte to BigDecimal has been directly sourced from the master version of avro at:
https://github.com/apache/avro/blob/33d495840c896b693b7f37b5ec786ac1acacd3b4/lang/java/avro/src/main/java/org/apache/avro/Conversions.java#L79

Needless to say, an update to the avro library would greatly simplify this PR

@ConfluentJenkins
Copy link
Contributor

Can one of the admins verify this patch?

@@ -67,10 +68,16 @@
public static Object toAvro(JsonNode value, Schema schema) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
value = JsonNodeConverters.transformJsonNode(value, schema, new JsonNodeConverter() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont recall exactly how the JVM handles this (maybe it does optimally), but it might be more efficient to have your implementation of JsonNodeConverter defined somewhere rather than repeatedly created

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto for toJson.

};

static JsonNode decimalToText(JsonNode jsonNode, Schema schema) {
if (jsonNode instanceof DoubleNode) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a suggestion, I would make these methods more conditional in their names. For example, if the method was isDecimal (which encapsulated your instanceOf logic), then as a result of that condition being true, you'd do your conversion. False would lead to the function being an identity on the input jsonNode. The benefit of this is in AvroConverter, the code isn't read as "I am definitely turning this into text from a decimal", but more so "I will convert this into text iff it's a decimal". The same can be said for textToDecimal.

Copy link

@don41382 don41382 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, jsut a quick review


static public String toEncodedString(BigDecimal bigDecimal, int scale, int precision) {
return new String(toByteArray(bigDecimal,scale), Charset.forName("ISO-8859-1"));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you use ISO-8859-1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the only charset that I found that could encode the bytes the way I wanted for a String, so that my tests passed. Unsure why this one works and not others, but hoping for Confluent to let me know

} else if (schema.getType() == Schema.Type.ARRAY) {
Schema subSchema = schema.getElementType();
int i = 0;
for (Iterator<JsonNode> it = jsonNode.elements(); it.hasNext(); ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use instead

for (JsonNode elem: jsonNode.elements()) {
  //
}

ByteBuffer byteBuffer1 = (ByteBuffer) ((GenericData.Array) result).get(1);
assertEquals(new BigDecimal("555.55"), BigDecimalDecoder.fromBytes(byteBuffer1,2));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add more test for different scales? You really only testing scale of 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants