mkeskells commented on code in PR #3307:
URL: https://github.com/apache/avro/pull/3307#discussion_r1993719369
##########
lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java:
##########
@@ -1515,38 +1516,74 @@
}
- /*
+ /**
* Called to create new array instances. Subclasses may override to use a
- * different array implementation. By default, this returns a {@link
- * GenericData.Array}.
+ * 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();
- return old;
- } else if (old instanceof Collection) {
- ((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);
+ final var logicalType = schema.getElementType().getLogicalType();
+ final var conversion = getConversionFor(logicalType);
+ final var optimalValueType = 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;
}
- return new GenericData.Array<Object>(size, schema);
}
+ // we can't reuse the old array, so we create a new one
+ 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;
Review Comment:
removed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]