You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2020/05/26 13:53:44 UTC
[avro] branch master updated: AVRO-2836 Generated java includes
logical type conversions (#883)
This is an automated email from the ASF dual-hosted git repository.
rskraba pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new ab83dcd AVRO-2836 Generated java includes logical type conversions (#883)
ab83dcd is described below
commit ab83dcd6fee848cb0a82a9da328e114cc9e3568b
Author: Matthew McMahon <ma...@gmail.com>
AuthorDate: Tue May 26 23:53:36 2020 +1000
AVRO-2836 Generated java includes logical type conversions (#883)
* AVRO-2836 Generated java includes logical type conversions
If the logical type is used for a Fixed type (potentially also Enum)
then it should check if there are any conversions used, and
include in the generated java source
* AVRO-2836 Generated java includes logical type conversions
Move enum and fixed to use the javatype method, as if there is a
logical type conversion it will be picked up and remove a
code path
---
.../avro/compiler/specific/SpecificCompiler.java | 6 +++---
.../avro/codegentest/TestNestedLogicalTypes.java | 23 ++++++++++++++++++---
.../avro/nested_logical_types_union_fixed.avsc | 21 +++++++++++++++++++
.../org/apache/avro/codegentest/CustomDecimal.java | 4 ++++
.../avro/codegentest/CustomDecimalConversion.java | 24 ++++++++++++++++++++++
5 files changed, 72 insertions(+), 6 deletions(-)
diff --git a/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java b/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
index 317098f..9258a99 100644
--- a/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
+++ b/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
@@ -326,10 +326,10 @@ public class SpecificCompiler {
for (Schema s : schema.getTypes())
getClassNamesOfPrimitiveFields(s, result, seenSchemas);
break;
- case ENUM:
- case FIXED:
case NULL:
break;
+ case ENUM:
+ case FIXED:
case STRING:
case BYTES:
case INT:
@@ -337,7 +337,7 @@ public class SpecificCompiler {
case FLOAT:
case DOUBLE:
case BOOLEAN:
- result.add(javaType(schema));
+ result.add(javaType(schema, true));
break;
default:
throw new RuntimeException("Unknown type: " + schema);
diff --git a/lang/java/integration-test/codegen-test/src/test/java/org/apache/avro/codegentest/TestNestedLogicalTypes.java b/lang/java/integration-test/codegen-test/src/test/java/org/apache/avro/codegentest/TestNestedLogicalTypes.java
index f21572b..d452ccb 100644
--- a/lang/java/integration-test/codegen-test/src/test/java/org/apache/avro/codegentest/TestNestedLogicalTypes.java
+++ b/lang/java/integration-test/codegen-test/src/test/java/org/apache/avro/codegentest/TestNestedLogicalTypes.java
@@ -17,11 +17,20 @@
*/
package org.apache.avro.codegentest;
-import java.time.LocalDate;
-
-import org.apache.avro.codegentest.testdata.*;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesArray;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesMap;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesRecord;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesUnion;
+import org.apache.avro.codegentest.testdata.NestedLogicalTypesUnionFixedDecimal;
+import org.apache.avro.codegentest.testdata.NestedRecord;
+import org.apache.avro.codegentest.testdata.NullableLogicalTypesArray;
+import org.apache.avro.codegentest.testdata.RecordInArray;
+import org.apache.avro.codegentest.testdata.RecordInMap;
+import org.apache.avro.codegentest.testdata.RecordInUnion;
import org.junit.Test;
+import java.math.BigInteger;
+import java.time.LocalDate;
import java.util.Collections;
public class TestNestedLogicalTypes extends AbstractSpecificRecordTest {
@@ -64,4 +73,12 @@ public class TestNestedLogicalTypes extends AbstractSpecificRecordTest {
.build();
verifySerDeAndStandardMethods(nestedLogicalTypesMap);
}
+
+ @Test
+ public void testNullableLogicalTypeInRecordInFixedDecimal() {
+ final NestedLogicalTypesUnionFixedDecimal nestedLogicalTypesUnionFixedDecimal = NestedLogicalTypesUnionFixedDecimal
+ .newBuilder().setUnionOfFixedDecimal(new CustomDecimal(BigInteger.TEN, 15)).build();
+ verifySerDeAndStandardMethods(nestedLogicalTypesUnionFixedDecimal);
+ }
+
}
diff --git a/lang/java/integration-test/codegen-test/src/test/resources/avro/nested_logical_types_union_fixed.avsc b/lang/java/integration-test/codegen-test/src/test/resources/avro/nested_logical_types_union_fixed.avsc
new file mode 100644
index 0000000..d2dbb4b
--- /dev/null
+++ b/lang/java/integration-test/codegen-test/src/test/resources/avro/nested_logical_types_union_fixed.avsc
@@ -0,0 +1,21 @@
+{"namespace": "org.apache.avro.codegentest.testdata",
+ "type": "record",
+ "name": "NestedLogicalTypesUnionFixedDecimal",
+ "doc" : "Test nested types with logical types in generated Java classes",
+ "fields": [
+ {
+ "name": "unionOfFixedDecimal",
+ "type": ["null", {
+ "namespace": "org.apache.avro.codegentest.testdata",
+ "name": "FixedInUnion",
+ "type": "fixed",
+ "size": 12,
+ "logicalType": "decimal",
+ "precision": 28,
+ "scale": 15
+ }]
+ }]
+}
+
+
+
diff --git a/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimal.java b/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimal.java
index 991d58a..5b7514c 100644
--- a/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimal.java
+++ b/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimal.java
@@ -45,6 +45,10 @@ public class CustomDecimal implements Comparable<CustomDecimal> {
}
+ int signum() {
+ return internalValue.signum();
+ }
+
@Override
public boolean equals(Object o) {
if (this == o)
diff --git a/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimalConversion.java b/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimalConversion.java
index 845772b..1857ea2 100644
--- a/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimalConversion.java
+++ b/lang/java/integration-test/test-custom-conversions/src/main/java/org/apache/avro/codegentest/CustomDecimalConversion.java
@@ -22,9 +22,12 @@ import org.apache.avro.Conversion;
import org.apache.avro.LogicalType;
import org.apache.avro.LogicalTypes;
import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericFixed;
import java.math.BigInteger;
import java.nio.ByteBuffer;
+import java.util.Arrays;
public class CustomDecimalConversion extends Conversion<CustomDecimal> {
@Override
@@ -49,4 +52,25 @@ public class CustomDecimalConversion extends Conversion<CustomDecimal> {
int scale = ((LogicalTypes.Decimal) type).getScale();
return ByteBuffer.wrap(value.toByteArray(scale));
}
+
+ @Override
+ public CustomDecimal fromFixed(GenericFixed value, Schema schema, LogicalType type) {
+ int scale = ((LogicalTypes.Decimal) type).getScale();
+ return new CustomDecimal(new BigInteger(value.bytes()), scale);
+ }
+
+ @Override
+ public GenericFixed toFixed(CustomDecimal value, Schema schema, LogicalType type) {
+ int scale = ((LogicalTypes.Decimal) type).getScale();
+ byte fillByte = (byte) (value.signum() < 0 ? 0xFF : 0x00);
+ byte[] unscaled = value.toByteArray(scale);
+ byte[] bytes = new byte[schema.getFixedSize()];
+ int offset = bytes.length - unscaled.length;
+
+ // Fill the front of the array and copy remaining with unscaled values
+ Arrays.fill(bytes, 0, offset, fillByte);
+ System.arraycopy(unscaled, 0, bytes, offset, bytes.length - offset);
+
+ return new GenericData.Fixed(schema, bytes);
+ }
}