Skip to content

Commit

Permalink
Fix JavaScript Deciaml Values
Browse files Browse the repository at this point in the history
  • Loading branch information
psainics committed May 15, 2024
1 parent bf191b5 commit d5ae110
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -271,6 +273,22 @@ private InvalidEntry<StructuredRecord> getErrorObject(Map result, StructuredReco
private Object decode(Object object, Schema schema) {
Schema.Type type = schema.getType();

Schema.LogicalType logicalType = schema.getLogicalType();
if (logicalType != null) {
switch (logicalType) {
case DECIMAL:
BigDecimal bigDecimal = null;
if (object instanceof Double) {
bigDecimal = BigDecimal.valueOf((Double) object).setScale(schema.getScale(), RoundingMode.HALF_EVEN);
} else if (object instanceof Integer) {
bigDecimal = BigDecimal.valueOf((Integer) object).setScale(schema.getScale(), RoundingMode.HALF_EVEN);
}
if (bigDecimal != null) {
return bigDecimal.unscaledValue().toByteArray();
}
}
}

switch (type) {
case NULL:
case BOOLEAN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.junit.Assert;
import org.junit.Test;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -404,4 +405,30 @@ public void testComplex() throws Exception {
Assert.assertEquals(1, mockContext.getMockMetrics().getCount("script.transform.count"));
Assert.assertEquals(1, mockContext.getMockMetrics().getPipelineCount("transform.1.script.transform.count"));
}

@Test
public void testDecimalTransform() throws Exception {
Schema outputSchema = Schema.recordOf("test",
Schema.Field.of("pie", Schema.decimalOf(3, 2)),
Schema.Field.of("int_pie", Schema.decimalOf(1, 0))
);
Schema inputSchema = Schema.recordOf("test",
Schema.Field.of("pie", Schema.decimalOf(3, 2)),
Schema.Field.of("int_pie", Schema.decimalOf(1, 0))
);
StructuredRecord input = StructuredRecord.builder(inputSchema)
.setDecimal("pie", new BigDecimal("3.14"))
.setDecimal("int_pie", new BigDecimal("3")
).build();
JavaScriptTransform.Config config = new JavaScriptTransform.Config(
"function transform(input, emitter, context) { emitter.emit(input); }", outputSchema.toString(), null);
Transform<StructuredRecord, StructuredRecord> transform = new JavaScriptTransform(config);
transform.initialize(new MockTransformContext());
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(input, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Assert.assertEquals(outputSchema, output.getSchema());
Assert.assertEquals(input.getDecimal("pie"), output.getDecimal("pie"));
Assert.assertEquals(input.getDecimal("int_pie"), output.getDecimal("int_pie"));
}
}

0 comments on commit d5ae110

Please sign in to comment.