From fdfc2ef1ab01a9f61e54b78c7e2688bffe55c492 Mon Sep 17 00:00:00 2001 From: Mike Skells Date: Thu, 6 Feb 2025 11:20:37 +0000 Subject: [PATCH 1/7] AVRO-4039 fix GenericData.newArray only return an appropriate array --- .../org/apache/avro/generic/GenericData.java | 49 +++--- .../apache/avro/generic/PrimitivesArrays.java | 49 +++--- .../apache/avro/generic/GenericDataTest.java | 162 ++++++++++++++++++ 3 files changed, 215 insertions(+), 45 deletions(-) create mode 100644 lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java index 362ebdc9cfc..3d24c34d045 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java @@ -1515,34 +1515,43 @@ else if (value instanceof Utf8) { } - /* + /** * Called to create new array instances. Subclasses may override to use a * different array implementation. By default, this returns a {@link * GenericData.Array}. + * @param old the old array instance to reuse, if possible. + * If the old array is an appropriate type, it may be cleared and returned. + * @param size the size of the array to create. + * @param schema the schema of the array elements. */ public Object newArray(Object old, int size, Schema schema) { - if (old instanceof GenericArray) { - ((GenericArray) old).reset(); + if (old instanceof GenericData.AbstractArray && ((GenericData.AbstractArray) old).getSchema() == schema) { + ((GenericData.AbstractArray) old).reset(); return old; - } else if (old instanceof Collection) { + } + if (old instanceof Collection && (!(old instanceof GenericContainer) || ((GenericContainer) old).getSchema() == schema)) { ((Collection) old).clear(); return old; - } else { - if (schema.getElementType().getType() == Type.INT) { - return new PrimitivesArrays.IntArray(size, schema); - } - if (schema.getElementType().getType() == Type.BOOLEAN) { - return new PrimitivesArrays.BooleanArray(size, schema); - } - if (schema.getElementType().getType() == Type.LONG) { - return new PrimitivesArrays.LongArray(size, schema); - } - if (schema.getElementType().getType() == Type.FLOAT) { - return new PrimitivesArrays.FloatArray(size, schema); - } - if (schema.getElementType().getType() == Type.DOUBLE) { - return new PrimitivesArrays.DoubleArray(size, schema); - } + } + + //we can't reuse the old array, so we create a new one + + if (schema.getElementType().getLogicalType() != null) { + return new GenericData.Array(size, schema); + } + + switch (schema.getElementType().getType()) { + case INT: + return new PrimitivesArrays.IntArray(size, schema); + case BOOLEAN: + return new PrimitivesArrays.BooleanArray(size, schema); + case LONG: + return new PrimitivesArrays.LongArray(size, schema); + case FLOAT: + return new PrimitivesArrays.FloatArray(size, schema); + case DOUBLE: + return new PrimitivesArrays.DoubleArray(size, schema); + default: return new GenericData.Array(size, schema); } } diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java b/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java index 91540005a9b..01df0df07eb 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java @@ -24,22 +24,29 @@ import java.util.Collection; public class PrimitivesArrays { + public abstract static class PrimitiveArray extends GenericData.AbstractArray { + PrimitiveArray(Schema schema, Schema.Type underlyingType) { + super(schema); + if (!underlyingType.equals(schema.getElementType().getType())) + throw new AvroRuntimeException("Not a " + underlyingType + " array schema: " + schema); + if (schema.getElementType().getLogicalType() != null) + throw new AvroRuntimeException("Logical types cant use primitive arrays - array schema: " + schema); + } + } - public static class IntArray extends GenericData.AbstractArray { + public static class IntArray extends PrimitiveArray { private static final int[] EMPTY = new int[0]; private int[] elements = EMPTY; public IntArray(int capacity, Schema schema) { - super(schema); - if (!Schema.Type.INT.equals(schema.getElementType().getType())) - throw new AvroRuntimeException("Not a int array schema: " + schema); + super(schema, Schema.Type.INT); if (capacity != 0) elements = new int[capacity]; } public IntArray(Schema schema, Collection c) { - super(schema); + super(schema, Schema.Type.INT); if (c != null) { elements = new int[c.size()]; addAll(c); @@ -129,21 +136,19 @@ protected void swap(final int index1, final int index2) { } } - public static class LongArray extends GenericData.AbstractArray { + public static class LongArray extends PrimitiveArray { private static final long[] EMPTY = new long[0]; private long[] elements = EMPTY; public LongArray(int capacity, Schema schema) { - super(schema); - if (!Schema.Type.LONG.equals(schema.getElementType().getType())) - throw new AvroRuntimeException("Not a long array schema: " + schema); + super(schema, Schema.Type.LONG); if (capacity != 0) elements = new long[capacity]; } public LongArray(Schema schema, Collection c) { - super(schema); + super(schema, Schema.Type.LONG); if (c != null) { elements = new long[c.size()]; addAll(c); @@ -233,21 +238,19 @@ protected void swap(final int index1, final int index2) { } } - public static class BooleanArray extends GenericData.AbstractArray { + public static class BooleanArray extends PrimitiveArray { private static final byte[] EMPTY = new byte[0]; private byte[] elements = EMPTY; public BooleanArray(int capacity, Schema schema) { - super(schema); - if (!Schema.Type.BOOLEAN.equals(schema.getElementType().getType())) - throw new AvroRuntimeException("Not a boolean array schema: " + schema); + super(schema, Schema.Type.BOOLEAN); if (capacity != 0) elements = new byte[1 + (capacity / Byte.SIZE)]; } public BooleanArray(Schema schema, Collection c) { - super(schema); + super(schema, Schema.Type.BOOLEAN); if (c != null) { elements = new byte[1 + (c.size() / 8)]; @@ -398,21 +401,19 @@ protected void swap(final int index1, final int index2) { } } - public static class FloatArray extends GenericData.AbstractArray { + public static class FloatArray extends PrimitiveArray { private static final float[] EMPTY = new float[0]; private float[] elements = EMPTY; public FloatArray(int capacity, Schema schema) { - super(schema); - if (!Schema.Type.FLOAT.equals(schema.getElementType().getType())) - throw new AvroRuntimeException("Not a float array schema: " + schema); + super(schema, Schema.Type.FLOAT); if (capacity != 0) elements = new float[capacity]; } public FloatArray(Schema schema, Collection c) { - super(schema); + super(schema, Schema.Type.FLOAT); if (c != null) { elements = new float[c.size()]; addAll(c); @@ -502,21 +503,19 @@ protected void swap(final int index1, final int index2) { } } - public static class DoubleArray extends GenericData.AbstractArray { + public static class DoubleArray extends PrimitiveArray { private static final double[] EMPTY = new double[0]; private double[] elements = EMPTY; public DoubleArray(int capacity, Schema schema) { - super(schema); - if (!Schema.Type.DOUBLE.equals(schema.getElementType().getType())) - throw new AvroRuntimeException("Not a double array schema: " + schema); + super(schema, Schema.Type.DOUBLE); if (capacity != 0) elements = new double[capacity]; } public DoubleArray(Schema schema, Collection c) { - super(schema); + super(schema, Schema.Type.DOUBLE); if (c != null) { elements = new double[c.size()]; addAll(c); diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java new file mode 100644 index 00000000000..d1c5ec4e09f --- /dev/null +++ b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java @@ -0,0 +1,162 @@ +package org.apache.avro.generic; + +import org.apache.avro.LogicalType; +import org.apache.avro.Schema; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.*; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +class GenericDataTest { + + static Schema createSchema(Schema.Type type) { + switch (type) { + case FIXED: + return Schema.createFixed("foo", null, null, 4); + case UNION: + return Schema.createUnion(Schema.create(Schema.Type.FLOAT), Schema.create(Schema.Type.STRING)); + case MAP: + return Schema.createMap(Schema.create(Schema.Type.FLOAT)); + case ARRAY: + return Schema.createArray(Schema.create(Schema.Type.STRING)); + case RECORD: + return Schema.createRecord("record", null, null, false); + case ENUM: + return Schema.createEnum("myEnum", null, null, Collections.emptyList()); + default: + return Schema.create(type); + } + } + static Object sampleValue(Schema schema) { + if (schema.getLogicalType() != null) { + return new Object(); + } + switch (schema.getElementType().getType()) { + case BOOLEAN: + return true; + case INT: + return Integer.MAX_VALUE; + case LONG: + return Long.MAX_VALUE; + case FLOAT: + return Float.MAX_VALUE; + case DOUBLE: + return Double.MAX_VALUE; + default: + return "foo"; + } + } + static Schema createArraySchema(Schema.Type type) { + return Schema.createArray(createSchema(type)); + } + static Schema createArraySchemaWithLogicalType(Schema.Type type) { + final LogicalType logicalType = new LogicalType("Mike"); + Schema schema = logicalType.addToSchema(createSchema(type)); + return Schema.createArray(schema); + } + public static Stream testNewArrayData() { + Map> validMappings = new EnumMap<>(Schema.Type.class); + + for (Schema.Type type : Schema.Type.values()) { + switch (type) { + case INT: + validMappings.put(type, new PrimitivesArrays.IntArray(0, createArraySchema(type))); + break; + case LONG: + validMappings.put(type, new PrimitivesArrays.LongArray(0, createArraySchema(type))); + break; + case DOUBLE: + validMappings.put(type, new PrimitivesArrays.DoubleArray(0, createArraySchema(type))); + break; + case FLOAT: + validMappings.put(type, new PrimitivesArrays.FloatArray(0, createArraySchema(type))); + break; + case BOOLEAN: + validMappings.put(type, new PrimitivesArrays.BooleanArray(0, createArraySchema(type))); + break; + default: + validMappings.put(type, new GenericData.Array<>(0, createArraySchema(type))); + break; + } + } + + List data = new ArrayList<>(); + + validMappings.forEach((validKey, optimalValue) -> { + Class optimalValueType = optimalValue.getClass(); + //cant reuse null, or a string + data.add(Arguments.of("null input, "+ validKey, createArraySchema(validKey), null, optimalValueType)); + data.add(Arguments.of("String input, "+ validKey, createArraySchema(validKey), "foo", optimalValueType)); + //should reuse arraylist + data.add(Arguments.of("ArrayList input, "+ validKey, createArraySchema(validKey), new ArrayList<>(), ArrayList.class)); + if (validKey != Schema.Type.UNION) { + data.add(Arguments.of("null (with logical type) input, " + validKey, createArraySchemaWithLogicalType(validKey), null, GenericData.Array.class)); + data.add(Arguments.of("String (with logical type) input, " + validKey, createArraySchemaWithLogicalType(validKey), "foo", GenericData.Array.class)); + data.add(Arguments.of("ArrayList (with logical type) input, " + validKey, createArraySchema(validKey), new ArrayList<>(), ArrayList.class)); + } + + validMappings.forEach((suppliedValueType, suppliedValue) -> { + data.add(Arguments.of(suppliedValueType +" input "+ validKey, createArraySchema(validKey), suppliedValue, optimalValueType)); + if (validKey != Schema.Type.UNION) + data.add(Arguments.of(suppliedValueType +" (with logical type) input "+ validKey, createArraySchemaWithLogicalType(validKey), suppliedValue, GenericData.Array.class)); + }); + }); + return data.stream(); + } + + @ParameterizedTest + @MethodSource("testNewArrayData") + void testNewArray(String description, Schema schema, Object initial, Class> expectedType) { + GenericData underTest = new GenericData(); + Object result = underTest.newArray(initial, 10, schema); + //never null + assertNotNull(result, description); + //should always be the best fit type, or a generic array + assertTrue(expectedType.isInstance(result) || result instanceof GenericData.Array, result.getClass() + " when expected generic or "+expectedType.getName()+ " - " +description); + + //must be a collection from the above list + Collection resultCollection = (Collection) result; + + //the result should be empty + assertEquals(0, resultCollection.size(), "not empty - " + description); + + //is the supplied type matched the return type, then we should not have allocated a new object + if (initial != null && initial.getClass() == result.getClass() && + (!(initial instanceof GenericContainer) || ((GenericContainer) initial).getSchema().getElementType() == schema.getElementType())) { + //if the result type is the same as the initial type, it should be reused, so we should not have allocated a new object + assertSame(initial, result, "not reused - " + description); + } + //is the supplied type matched the return type, then we should not have allocated a new object + if (initial == null) { + //if we did allocate a not object, we should have allocated the optimal type + assertSame(expectedType, result.getClass(), "not optimal - " + description); + } + //check the schema was set correctly + if (result instanceof GenericContainer) { + GenericContainer resultArray = (GenericContainer) result; + assertEquals(schema.getElementType(), resultArray.getSchema().getElementType(), "wrong element type - " + description); + } + + //for primitive arrays, we should not have a logical type, and the underlying array should be the correct type + if (result instanceof PrimitivesArrays.PrimitiveArray) { + assertSame(expectedType, resultCollection.getClass(), "wrong type for primitive - " + description); + assertNull(schema.getElementType().getLogicalType(), "Primitive array for logical type - " + description); + } + + final Object sample = sampleValue(schema); + resultCollection.add(sample); + assertEquals(1, resultCollection.size(), "not added - " + description); + assertEquals(sample, resultCollection.iterator().next(), "wrong value - " + description); + assertEquals(1, resultCollection.size(), "disappeared - " + description); + + Object result2 = underTest.newArray(resultCollection, 10, schema); + assertSame(result, result2, "not reused - " + description); + + assertEquals(0, resultCollection.size(), "not reset - " + description); + } + +} From d36b403f952278e9f96ca7a1e7c86ddc7e54a259 Mon Sep 17 00:00:00 2001 From: Mike Skells Date: Fri, 7 Feb 2025 15:46:08 +0000 Subject: [PATCH 2/7] AVRO-4039 fix GenericData.newArray only return an appropriate array --- .../apache/avro/generic/GenericDataTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java index d1c5ec4e09f..79389e81c4a 100644 --- a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java +++ b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.avro.generic; import org.apache.avro.LogicalType; From bd32ecbc753606c72c272132a731e9d7a4b57af6 Mon Sep 17 00:00:00 2001 From: Mike Skells Date: Fri, 7 Feb 2025 17:37:25 +0000 Subject: [PATCH 3/7] AVRO-4039 fix GenericData.newArray spotless --- .../org/apache/avro/generic/GenericData.java | 16 ++-- .../apache/avro/generic/GenericDataTest.java | 91 +++++++++++-------- 2 files changed, 62 insertions(+), 45 deletions(-) diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java index 3d24c34d045..684ff735169 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java @@ -1517,11 +1517,12 @@ else if (value instanceof Utf8) { /** * Called to create new array instances. Subclasses may override to use a - * different array implementation. By default, this returns a {@link - * GenericData.Array}. - * @param old the old array instance to reuse, if possible. - * If the old array is an appropriate type, it may be cleared and returned. - * @param size the size of the array to create. + * different array implementation. By default, this returns a + * {@link GenericData.Array}. + * + * @param old the old array instance to reuse, if possible. If the old array + * is an appropriate type, it may be cleared and returned. + * @param size the size of the array to create. * @param schema the schema of the array elements. */ public Object newArray(Object old, int size, Schema schema) { @@ -1529,12 +1530,13 @@ public Object newArray(Object old, int size, Schema schema) { ((GenericData.AbstractArray) old).reset(); return old; } - if (old instanceof Collection && (!(old instanceof GenericContainer) || ((GenericContainer) old).getSchema() == schema)) { + if (old instanceof Collection + && (!(old instanceof GenericContainer) || ((GenericContainer) old).getSchema() == schema)) { ((Collection) old).clear(); return old; } - //we can't reuse the old array, so we create a new one + // we can't reuse the old array, so we create a new one if (schema.getElementType().getLogicalType() != null) { return new GenericData.Array(size, schema); diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java index 79389e81c4a..ffb24b2c454 100644 --- a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java +++ b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java @@ -23,7 +23,6 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import java.util.*; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; @@ -48,6 +47,7 @@ static Schema createSchema(Schema.Type type) { return Schema.create(type); } } + static Object sampleValue(Schema schema) { if (schema.getLogicalType() != null) { return new Object(); @@ -67,31 +67,34 @@ static Object sampleValue(Schema schema) { return "foo"; } } + static Schema createArraySchema(Schema.Type type) { return Schema.createArray(createSchema(type)); } + static Schema createArraySchemaWithLogicalType(Schema.Type type) { final LogicalType logicalType = new LogicalType("Mike"); Schema schema = logicalType.addToSchema(createSchema(type)); return Schema.createArray(schema); } + public static Stream testNewArrayData() { Map> validMappings = new EnumMap<>(Schema.Type.class); for (Schema.Type type : Schema.Type.values()) { switch (type) { - case INT: - validMappings.put(type, new PrimitivesArrays.IntArray(0, createArraySchema(type))); - break; - case LONG: - validMappings.put(type, new PrimitivesArrays.LongArray(0, createArraySchema(type))); - break; - case DOUBLE: - validMappings.put(type, new PrimitivesArrays.DoubleArray(0, createArraySchema(type))); - break; - case FLOAT: - validMappings.put(type, new PrimitivesArrays.FloatArray(0, createArraySchema(type))); - break; + case INT: + validMappings.put(type, new PrimitivesArrays.IntArray(0, createArraySchema(type))); + break; + case LONG: + validMappings.put(type, new PrimitivesArrays.LongArray(0, createArraySchema(type))); + break; + case DOUBLE: + validMappings.put(type, new PrimitivesArrays.DoubleArray(0, createArraySchema(type))); + break; + case FLOAT: + validMappings.put(type, new PrimitivesArrays.FloatArray(0, createArraySchema(type))); + break; case BOOLEAN: validMappings.put(type, new PrimitivesArrays.BooleanArray(0, createArraySchema(type))); break; @@ -105,21 +108,27 @@ public static Stream testNewArrayData() { validMappings.forEach((validKey, optimalValue) -> { Class optimalValueType = optimalValue.getClass(); - //cant reuse null, or a string - data.add(Arguments.of("null input, "+ validKey, createArraySchema(validKey), null, optimalValueType)); - data.add(Arguments.of("String input, "+ validKey, createArraySchema(validKey), "foo", optimalValueType)); - //should reuse arraylist - data.add(Arguments.of("ArrayList input, "+ validKey, createArraySchema(validKey), new ArrayList<>(), ArrayList.class)); + // cant reuse null, or a string + data.add(Arguments.of("null input, " + validKey, createArraySchema(validKey), null, optimalValueType)); + data.add(Arguments.of("String input, " + validKey, createArraySchema(validKey), "foo", optimalValueType)); + // should reuse arraylist + data.add(Arguments.of("ArrayList input, " + validKey, createArraySchema(validKey), new ArrayList<>(), + ArrayList.class)); if (validKey != Schema.Type.UNION) { - data.add(Arguments.of("null (with logical type) input, " + validKey, createArraySchemaWithLogicalType(validKey), null, GenericData.Array.class)); - data.add(Arguments.of("String (with logical type) input, " + validKey, createArraySchemaWithLogicalType(validKey), "foo", GenericData.Array.class)); - data.add(Arguments.of("ArrayList (with logical type) input, " + validKey, createArraySchema(validKey), new ArrayList<>(), ArrayList.class)); + data.add(Arguments.of("null (with logical type) input, " + validKey, createArraySchemaWithLogicalType(validKey), + null, GenericData.Array.class)); + data.add(Arguments.of("String (with logical type) input, " + validKey, + createArraySchemaWithLogicalType(validKey), "foo", GenericData.Array.class)); + data.add(Arguments.of("ArrayList (with logical type) input, " + validKey, createArraySchema(validKey), + new ArrayList<>(), ArrayList.class)); } validMappings.forEach((suppliedValueType, suppliedValue) -> { - data.add(Arguments.of(suppliedValueType +" input "+ validKey, createArraySchema(validKey), suppliedValue, optimalValueType)); + data.add(Arguments.of(suppliedValueType + " input " + validKey, createArraySchema(validKey), suppliedValue, + optimalValueType)); if (validKey != Schema.Type.UNION) - data.add(Arguments.of(suppliedValueType +" (with logical type) input "+ validKey, createArraySchemaWithLogicalType(validKey), suppliedValue, GenericData.Array.class)); + data.add(Arguments.of(suppliedValueType + " (with logical type) input " + validKey, + createArraySchemaWithLogicalType(validKey), suppliedValue, GenericData.Array.class)); }); }); return data.stream(); @@ -130,35 +139,41 @@ public static Stream testNewArrayData() { void testNewArray(String description, Schema schema, Object initial, Class> expectedType) { GenericData underTest = new GenericData(); Object result = underTest.newArray(initial, 10, schema); - //never null + // never null assertNotNull(result, description); - //should always be the best fit type, or a generic array - assertTrue(expectedType.isInstance(result) || result instanceof GenericData.Array, result.getClass() + " when expected generic or "+expectedType.getName()+ " - " +description); + // should always be the best fit type, or a generic array + assertTrue(expectedType.isInstance(result) || result instanceof GenericData.Array, + result.getClass() + " when expected generic or " + expectedType.getName() + " - " + description); - //must be a collection from the above list - Collection resultCollection = (Collection) result; + // must be a collection from the above list + Collection resultCollection = (Collection) result; - //the result should be empty + // the result should be empty assertEquals(0, resultCollection.size(), "not empty - " + description); - //is the supplied type matched the return type, then we should not have allocated a new object - if (initial != null && initial.getClass() == result.getClass() && - (!(initial instanceof GenericContainer) || ((GenericContainer) initial).getSchema().getElementType() == schema.getElementType())) { - //if the result type is the same as the initial type, it should be reused, so we should not have allocated a new object + // is the supplied type matched the return type, then we should not have + // allocated a new object + if (initial != null && initial.getClass() == result.getClass() && (!(initial instanceof GenericContainer) + || ((GenericContainer) initial).getSchema().getElementType() == schema.getElementType())) { + // if the result type is the same as the initial type, it should be reused, so + // we should not have allocated a new object assertSame(initial, result, "not reused - " + description); } - //is the supplied type matched the return type, then we should not have allocated a new object + // is the supplied type matched the return type, then we should not have + // allocated a new object if (initial == null) { - //if we did allocate a not object, we should have allocated the optimal type + // if we did allocate a not object, we should have allocated the optimal type assertSame(expectedType, result.getClass(), "not optimal - " + description); } - //check the schema was set correctly + // check the schema was set correctly if (result instanceof GenericContainer) { GenericContainer resultArray = (GenericContainer) result; - assertEquals(schema.getElementType(), resultArray.getSchema().getElementType(), "wrong element type - " + description); + assertEquals(schema.getElementType(), resultArray.getSchema().getElementType(), + "wrong element type - " + description); } - //for primitive arrays, we should not have a logical type, and the underlying array should be the correct type + // for primitive arrays, we should not have a logical type, and the underlying + // array should be the correct type if (result instanceof PrimitivesArrays.PrimitiveArray) { assertSame(expectedType, resultCollection.getClass(), "wrong type for primitive - " + description); assertNull(schema.getElementType().getLogicalType(), "Primitive array for logical type - " + description); From 29e72786ba8166db73e8d46fe6115cfe33318a25 Mon Sep 17 00:00:00 2001 From: Mike Skells Date: Sat, 8 Feb 2025 07:17:14 +0000 Subject: [PATCH 4/7] AVRO-4039 fix import that spotless removed --- .../test/java/org/apache/avro/generic/GenericDataTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java index ffb24b2c454..499a48169a1 100644 --- a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java +++ b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java @@ -23,6 +23,13 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; + import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; From 55f23daa314aca1d371be30eb915bbdb8f0cd2d8 Mon Sep 17 00:00:00 2001 From: Mike Skells Date: Wed, 5 Mar 2025 16:46:24 +0000 Subject: [PATCH 5/7] AVRO-4039 review feedback remove schema check on returned value Check convertors with logical types --- .../org/apache/avro/generic/GenericData.java | 51 ++++---- .../apache/avro/generic/PrimitivesArrays.java | 121 +++++++++++++++--- .../apache/avro/generic/GenericDataTest.java | 95 +++++++++++--- 3 files changed, 204 insertions(+), 63 deletions(-) diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java index 684ff735169..9a86779c55e 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java @@ -57,6 +57,7 @@ import org.apache.avro.io.FastReaderBuilder; import org.apache.avro.util.Utf8; import org.apache.avro.util.internal.Accessor; +import org.apache.avro.generic.PrimitivesArrays.PrimitiveArray; import com.fasterxml.jackson.databind.JsonNode; import org.apache.avro.util.springframework.ConcurrentReferenceHashMap; @@ -1519,43 +1520,35 @@ else if (value instanceof Utf8) { * Called to create new array instances. Subclasses may override to use a * different array implementation. By default, this returns a * {@link GenericData.Array}. - * + * * @param old the old array instance to reuse, if possible. If the old array * is an appropriate type, it may be cleared and returned. * @param size the size of the array to create. * @param schema the schema of the array elements. */ public Object newArray(Object old, int size, Schema schema) { - if (old instanceof GenericData.AbstractArray && ((GenericData.AbstractArray) old).getSchema() == schema) { - ((GenericData.AbstractArray) old).reset(); - return old; - } - if (old instanceof Collection - && (!(old instanceof GenericContainer) || ((GenericContainer) old).getSchema() == schema)) { - ((Collection) old).clear(); - return old; + final var logicalType = schema.getElementType().getLogicalType(); + final var conversion = getConversionFor(logicalType); + final var optimalValueType = PrimitivesArrays.optimalValueType(schema, logicalType, + conversion == null ? null : conversion.getConvertedType()); + + if (old != null) { + if (old instanceof GenericData.Array) { + ((GenericData.Array) old).reset(); + return old; + } else if (old instanceof PrimitiveArray) { + var primitiveOld = (PrimitiveArray) old; + if (primitiveOld.valueType() == optimalValueType) { + primitiveOld.reset(); + return old; + } + } else if (old instanceof Collection) { + ((Collection) old).clear(); + return old; + } } - // we can't reuse the old array, so we create a new one - - if (schema.getElementType().getLogicalType() != null) { - return new GenericData.Array(size, schema); - } - - switch (schema.getElementType().getType()) { - case INT: - return new PrimitivesArrays.IntArray(size, schema); - case BOOLEAN: - return new PrimitivesArrays.BooleanArray(size, schema); - case LONG: - return new PrimitivesArrays.LongArray(size, schema); - case FLOAT: - return new PrimitivesArrays.FloatArray(size, schema); - case DOUBLE: - return new PrimitivesArrays.DoubleArray(size, schema); - default: - return new GenericData.Array(size, schema); - } + return PrimitivesArrays.createOptimizedArray(size, schema, optimalValueType); } /** diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java b/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java index 01df0df07eb..44ec93da8f4 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java @@ -17,21 +17,85 @@ */ package org.apache.avro.generic; -import org.apache.avro.AvroRuntimeException; +import org.apache.avro.LogicalType; import org.apache.avro.Schema; import java.util.Arrays; import java.util.Collection; +import java.util.Map; public class PrimitivesArrays { + /** + * Determine the optimal value type for an array. The value type is determined + * form the convertedElementType if supplied, otherwise the underlying type from + * the schema + * + * @param schema the schema of the array + * @param convertedElementType the converted elements value type. This may not + * be the same and the schema if for instance there + * is a logical type, and a convertor is use + * @return an indicator for the type of the array, useful for + * {@link #createOptimizedArray(int, Schema, Schema.Type)}. May be null + * if the type is not optimised + */ + public static Schema.Type optimalValueType(Schema schema, LogicalType logicalType, Class convertedElementType) { + final Schema.Type convertedType; + if (logicalType == null) + // if there are no logical types- use the schema type + return schema.getElementType().getType(); + else if (convertedElementType == null) + // if there is no convertor + return null; + else + // use the converted type + return primitiveTypesWithSpecialisedArrays.get(convertedElementType); + } + + /** + * Create a primitive array if the value type is has an associated optimised + * implementation, otherwise a generic array is returned. The value type is + * determined form the convertedElementType if supplied, otherwise the + * underlying type from the schema + * + * @param size the size of the array to create + * @param schema the schema of the array + * @param valueType the converted elements value type. This may not be the same + * and the schema if for instance there is a logical type, and + * a convertor is use + * @return an instance of a primitive array or a Generic array if the value type + * is does not have an associated optimised implementation. + */ + public static GenericData.AbstractArray createOptimizedArray(int size, Schema schema, Schema.Type valueType) { + + if (valueType != null) + switch (valueType) { + case INT: + return new PrimitivesArrays.IntArray(size, schema); + case BOOLEAN: + return new PrimitivesArrays.BooleanArray(size, schema); + case LONG: + return new PrimitivesArrays.LongArray(size, schema); + case FLOAT: + return new PrimitivesArrays.FloatArray(size, schema); + case DOUBLE: + return new PrimitivesArrays.DoubleArray(size, schema); + } + return new GenericData.Array<>(size, schema); + } + + private final static Map, Schema.Type> primitiveTypesWithSpecialisedArrays = Map.of(// + Long.TYPE, Schema.Type.LONG, // + Integer.TYPE, Schema.Type.INT, // + Float.TYPE, Schema.Type.FLOAT, // + Double.TYPE, Schema.Type.DOUBLE, // + Boolean.TYPE, Schema.Type.BOOLEAN); + public abstract static class PrimitiveArray extends GenericData.AbstractArray { - PrimitiveArray(Schema schema, Schema.Type underlyingType) { + PrimitiveArray(Schema schema) { super(schema); - if (!underlyingType.equals(schema.getElementType().getType())) - throw new AvroRuntimeException("Not a " + underlyingType + " array schema: " + schema); - if (schema.getElementType().getLogicalType() != null) - throw new AvroRuntimeException("Logical types cant use primitive arrays - array schema: " + schema); } + + public abstract Schema.Type valueType(); } public static class IntArray extends PrimitiveArray { @@ -40,13 +104,13 @@ public static class IntArray extends PrimitiveArray { private int[] elements = EMPTY; public IntArray(int capacity, Schema schema) { - super(schema, Schema.Type.INT); + super(schema); if (capacity != 0) elements = new int[capacity]; } public IntArray(Schema schema, Collection c) { - super(schema, Schema.Type.INT); + super(schema); if (c != null) { elements = new int[c.size()]; addAll(c); @@ -134,6 +198,11 @@ protected void swap(final int index1, final int index2) { elements[index1] = elements[index2]; elements[index2] = tmp; } + + @Override + public Schema.Type valueType() { + return Schema.Type.INT; + } } public static class LongArray extends PrimitiveArray { @@ -142,13 +211,13 @@ public static class LongArray extends PrimitiveArray { private long[] elements = EMPTY; public LongArray(int capacity, Schema schema) { - super(schema, Schema.Type.LONG); + super(schema); if (capacity != 0) elements = new long[capacity]; } public LongArray(Schema schema, Collection c) { - super(schema, Schema.Type.LONG); + super(schema); if (c != null) { elements = new long[c.size()]; addAll(c); @@ -236,6 +305,11 @@ protected void swap(final int index1, final int index2) { elements[index1] = elements[index2]; elements[index2] = tmp; } + + @Override + public Schema.Type valueType() { + return Schema.Type.LONG; + } } public static class BooleanArray extends PrimitiveArray { @@ -244,13 +318,13 @@ public static class BooleanArray extends PrimitiveArray { private byte[] elements = EMPTY; public BooleanArray(int capacity, Schema schema) { - super(schema, Schema.Type.BOOLEAN); + super(schema); if (capacity != 0) elements = new byte[1 + (capacity / Byte.SIZE)]; } public BooleanArray(Schema schema, Collection c) { - super(schema, Schema.Type.BOOLEAN); + super(schema); if (c != null) { elements = new byte[1 + (c.size() / 8)]; @@ -399,6 +473,11 @@ protected void swap(final int index1, final int index2) { this.set(index1, this.get(index2)); this.set(index2, tmp); } + + @Override + public Schema.Type valueType() { + return Schema.Type.BOOLEAN; + } } public static class FloatArray extends PrimitiveArray { @@ -407,13 +486,13 @@ public static class FloatArray extends PrimitiveArray { private float[] elements = EMPTY; public FloatArray(int capacity, Schema schema) { - super(schema, Schema.Type.FLOAT); + super(schema); if (capacity != 0) elements = new float[capacity]; } public FloatArray(Schema schema, Collection c) { - super(schema, Schema.Type.FLOAT); + super(schema); if (c != null) { elements = new float[c.size()]; addAll(c); @@ -501,6 +580,11 @@ protected void swap(final int index1, final int index2) { this.set(index1, this.get(index2)); this.set(index2, tmp); } + + @Override + public Schema.Type valueType() { + return Schema.Type.FLOAT; + } } public static class DoubleArray extends PrimitiveArray { @@ -509,13 +593,13 @@ public static class DoubleArray extends PrimitiveArray { private double[] elements = EMPTY; public DoubleArray(int capacity, Schema schema) { - super(schema, Schema.Type.DOUBLE); + super(schema); if (capacity != 0) elements = new double[capacity]; } public DoubleArray(Schema schema, Collection c) { - super(schema, Schema.Type.DOUBLE); + super(schema); if (c != null) { elements = new double[c.size()]; addAll(c); @@ -603,6 +687,11 @@ protected void swap(final int index1, final int index2) { this.set(index1, this.get(index2)); this.set(index2, tmp); } + + @Override + public Schema.Type valueType() { + return Schema.Type.DOUBLE; + } } } diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java index 499a48169a1..39047cd98a9 100644 --- a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java +++ b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java @@ -17,6 +17,7 @@ */ package org.apache.avro.generic; +import org.apache.avro.Conversion; import org.apache.avro.LogicalType; import org.apache.avro.Schema; import org.junit.jupiter.params.ParameterizedTest; @@ -85,9 +86,8 @@ static Schema createArraySchemaWithLogicalType(Schema.Type type) { return Schema.createArray(schema); } - public static Stream testNewArrayData() { - Map> validMappings = new EnumMap<>(Schema.Type.class); - + static Map> validMappings = new EnumMap<>(Schema.Type.class); + static { for (Schema.Type type : Schema.Type.values()) { switch (type) { case INT: @@ -110,41 +110,101 @@ public static Stream testNewArrayData() { break; } } + } + + public static Stream testNewArrayData() { List data = new ArrayList<>(); validMappings.forEach((validKey, optimalValue) -> { Class optimalValueType = optimalValue.getClass(); // cant reuse null, or a string - data.add(Arguments.of("null input, " + validKey, createArraySchema(validKey), null, optimalValueType)); - data.add(Arguments.of("String input, " + validKey, createArraySchema(validKey), "foo", optimalValueType)); - // should reuse arraylist - data.add(Arguments.of("ArrayList input, " + validKey, createArraySchema(validKey), new ArrayList<>(), + final Schema arraySchema = createArraySchema(validKey); + + data.add(Arguments.of("null input, " + validKey, arraySchema, Collections.emptyList(), null, optimalValueType)); + data.add( + Arguments.of("String input, " + validKey, arraySchema, Collections.emptyList(), "foo", optimalValueType)); + // should reuse arraylist & generic array + data.add(Arguments.of("ArrayList input, " + validKey, arraySchema, Collections.emptyList(), new ArrayList<>(), ArrayList.class)); + data.add(Arguments.of("Generic input, " + validKey, arraySchema, Collections.emptyList(), + new GenericData.Array(0, arraySchema), GenericData.Array.class)); + // with logical type if (validKey != Schema.Type.UNION) { data.add(Arguments.of("null (with logical type) input, " + validKey, createArraySchemaWithLogicalType(validKey), - null, GenericData.Array.class)); + Collections.emptyList(), null, GenericData.Array.class)); data.add(Arguments.of("String (with logical type) input, " + validKey, - createArraySchemaWithLogicalType(validKey), "foo", GenericData.Array.class)); - data.add(Arguments.of("ArrayList (with logical type) input, " + validKey, createArraySchema(validKey), + createArraySchemaWithLogicalType(validKey), Collections.emptyList(), "foo", GenericData.Array.class)); + data.add(Arguments.of("ArrayList (with logical type) input, " + validKey, arraySchema, Collections.emptyList(), new ArrayList<>(), ArrayList.class)); + data.add(Arguments.of("Generic (with logical type) input, " + validKey, arraySchema, Collections.emptyList(), + new GenericData.Array(0, arraySchema), GenericData.Array.class)); +// with logical type and conversion + + validMappings.forEach((targetKey, targetType) -> { + if (targetKey != Schema.Type.UNION) { + data.add(Arguments.of("null (with logical type) input, " + validKey + " convert to " + targetType, + createArraySchemaWithLogicalType(targetKey), singleConversion(targetKey), null, targetType.getClass())); + data.add(Arguments.of("String (with logical type) input, " + validKey + " convert to " + targetType, + createArraySchemaWithLogicalType(targetKey), singleConversion(targetKey), "foo", + targetType.getClass())); + data.add(Arguments.of("ArrayList (with logical type) input, " + validKey + " convert to " + targetType, + createArraySchemaWithLogicalType(targetKey), singleConversion(targetKey), new ArrayList<>(), + ArrayList.class)); + data.add(Arguments.of("Generic (with logical type) input, " + validKey, arraySchema, + Collections.emptyList(), new GenericData.Array(0, arraySchema), GenericData.Array.class)); + } + }); + } validMappings.forEach((suppliedValueType, suppliedValue) -> { - data.add(Arguments.of(suppliedValueType + " input " + validKey, createArraySchema(validKey), suppliedValue, - optimalValueType)); + data.add(Arguments.of(suppliedValueType + " input " + validKey, arraySchema, Collections.emptyList(), + suppliedValue, optimalValueType)); if (validKey != Schema.Type.UNION) data.add(Arguments.of(suppliedValueType + " (with logical type) input " + validKey, - createArraySchemaWithLogicalType(validKey), suppliedValue, GenericData.Array.class)); + createArraySchemaWithLogicalType(validKey), Collections.emptyList(), suppliedValue, + GenericData.Array.class)); }); }); return data.stream(); } + private static List> singleConversion(Schema.Type targetKey) { + return Collections.singletonList(new Conversion() { + + public Class getConvertedType() { + switch (targetKey) { + case INT: + return (Class) Integer.TYPE; + case LONG: + return (Class) Long.TYPE; + case DOUBLE: + return (Class) Double.TYPE; + case FLOAT: + return (Class) Float.TYPE; + case BOOLEAN: + return (Class) Boolean.TYPE; + default: + return (Class) Object.class; + } + + } + + public String getLogicalTypeName() { + return "Mike"; + } + + }); + } + @ParameterizedTest @MethodSource("testNewArrayData") - void testNewArray(String description, Schema schema, Object initial, Class> expectedType) { + void testNewArray(String description, Schema schema, List> convertions, Object initial, + Class> expectedType) { GenericData underTest = new GenericData(); + convertions.forEach(underTest::addLogicalTypeConversion); + Object result = underTest.newArray(initial, 10, schema); // never null assertNotNull(result, description); @@ -160,12 +220,12 @@ void testNewArray(String description, Schema schema, Object initial, Class Date: Fri, 7 Mar 2025 08:52:53 +0000 Subject: [PATCH 6/7] AVRO-4039 review feedback --- .../org/apache/avro/generic/GenericData.java | 35 ++++++++++++++++++- .../apache/avro/generic/PrimitivesArrays.java | 34 ------------------ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java index 9a86779c55e..b5810e592e9 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java @@ -1529,7 +1529,7 @@ else if (value instanceof Utf8) { public Object newArray(Object old, int size, Schema schema) { final var logicalType = schema.getElementType().getLogicalType(); final var conversion = getConversionFor(logicalType); - final var optimalValueType = PrimitivesArrays.optimalValueType(schema, logicalType, + final var optimalValueType = optimalValueType(schema, logicalType, conversion == null ? null : conversion.getConvertedType()); if (old != null) { @@ -1551,6 +1551,39 @@ public Object newArray(Object old, int size, Schema schema) { return PrimitivesArrays.createOptimizedArray(size, schema, optimalValueType); } + /** + * Determine the optimal value type for an array. The value type is determined + * form the convertedElementType if supplied, otherwise the underlying type from + * the schema + * + * @param schema the schema of the array + * @param convertedElementType the converted elements value type. This may not + * be the same and the schema if for instance there + * is a logical type, and a convertor is use + * @return an indicator for the type of the array, useful for + * {@link PrimitivesArrays#createOptimizedArray(int, Schema, Schema.Type)}. + * May be null if the type is not optimised + */ + public static Schema.Type optimalValueType(Schema schema, LogicalType logicalType, Class convertedElementType) { + final Schema.Type convertedType; + if (logicalType == null) + // if there are no logical types- use the schema type + return schema.getElementType().getType(); + else if (convertedElementType == null) + // if there is no convertor + return null; + else + // use the converted type + return PRIMITIVE_TYPES_WITH_SPECIALISED_ARRAYS.get(convertedElementType); + } + + private final static Map, Schema.Type> PRIMITIVE_TYPES_WITH_SPECIALISED_ARRAYS = Map.of(// + Long.TYPE, Schema.Type.LONG, // + Integer.TYPE, Schema.Type.INT, // + Float.TYPE, Schema.Type.FLOAT, // + Double.TYPE, Schema.Type.DOUBLE, // + Boolean.TYPE, Schema.Type.BOOLEAN); + /** * Called to create new array instances. Subclasses may override to use a * different map implementation. By default, this returns a {@link HashMap}. diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java b/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java index 44ec93da8f4..0854eceae3b 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java @@ -17,39 +17,12 @@ */ package org.apache.avro.generic; -import org.apache.avro.LogicalType; import org.apache.avro.Schema; import java.util.Arrays; import java.util.Collection; -import java.util.Map; public class PrimitivesArrays { - /** - * Determine the optimal value type for an array. The value type is determined - * form the convertedElementType if supplied, otherwise the underlying type from - * the schema - * - * @param schema the schema of the array - * @param convertedElementType the converted elements value type. This may not - * be the same and the schema if for instance there - * is a logical type, and a convertor is use - * @return an indicator for the type of the array, useful for - * {@link #createOptimizedArray(int, Schema, Schema.Type)}. May be null - * if the type is not optimised - */ - public static Schema.Type optimalValueType(Schema schema, LogicalType logicalType, Class convertedElementType) { - final Schema.Type convertedType; - if (logicalType == null) - // if there are no logical types- use the schema type - return schema.getElementType().getType(); - else if (convertedElementType == null) - // if there is no convertor - return null; - else - // use the converted type - return primitiveTypesWithSpecialisedArrays.get(convertedElementType); - } /** * Create a primitive array if the value type is has an associated optimised @@ -83,13 +56,6 @@ public static GenericData.AbstractArray createOptimizedArray(int size, Schema return new GenericData.Array<>(size, schema); } - private final static Map, Schema.Type> primitiveTypesWithSpecialisedArrays = Map.of(// - Long.TYPE, Schema.Type.LONG, // - Integer.TYPE, Schema.Type.INT, // - Float.TYPE, Schema.Type.FLOAT, // - Double.TYPE, Schema.Type.DOUBLE, // - Boolean.TYPE, Schema.Type.BOOLEAN); - public abstract static class PrimitiveArray extends GenericData.AbstractArray { PrimitiveArray(Schema schema) { super(schema); From 781ab894cf883a143e4eb2869b4eeb7b3a5a1235 Mon Sep 17 00:00:00 2001 From: Mike Skells Date: Fri, 14 Mar 2025 13:49:48 +0000 Subject: [PATCH 7/7] AVRO-4039 review feedback --- .../avro/src/main/java/org/apache/avro/generic/GenericData.java | 1 - .../src/main/java/org/apache/avro/generic/PrimitivesArrays.java | 2 ++ .../src/test/java/org/apache/avro/generic/GenericDataTest.java | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java index b5810e592e9..77ae76007e2 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java @@ -1565,7 +1565,6 @@ public Object newArray(Object old, int size, Schema schema) { * May be null if the type is not optimised */ public static Schema.Type optimalValueType(Schema schema, LogicalType logicalType, Class convertedElementType) { - final Schema.Type convertedType; if (logicalType == null) // if there are no logical types- use the schema type return schema.getElementType().getType(); diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java b/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java index 0854eceae3b..34b69acdd03 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java +++ b/lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java @@ -52,6 +52,8 @@ public static GenericData.AbstractArray createOptimizedArray(int size, Schema return new PrimitivesArrays.FloatArray(size, schema); case DOUBLE: return new PrimitivesArrays.DoubleArray(size, schema); + default: + break; } return new GenericData.Array<>(size, schema); } diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java index 39047cd98a9..040a71e2ea0 100644 --- a/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java +++ b/lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java @@ -173,6 +173,7 @@ public static Stream testNewArrayData() { private static List> singleConversion(Schema.Type targetKey) { return Collections.singletonList(new Conversion() { + @Override public Class getConvertedType() { switch (targetKey) { case INT: @@ -191,6 +192,7 @@ public Class getConvertedType() { } + @Override public String getLogicalTypeName() { return "Mike"; }