github-advanced-security[bot] commented on code in PR #3307:
URL: https://github.com/apache/avro/pull/3307#discussion_r1993417012


##########
lang/java/avro/src/main/java/org/apache/avro/generic/PrimitivesArrays.java:
##########
@@ -17,23 +17,60 @@
  */
 package org.apache.avro.generic;
 
-import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.Schema;
 
 import java.util.Arrays;
 import java.util.Collection;
 
 public class PrimitivesArrays {
 
-  public static class IntArray extends GenericData.AbstractArray<Integer> {
+  /**
+   * 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) {

Review Comment:
   ## Missing enum case in switch
   
   Switch statement does not have a case for [ARRAY](1), [BYTES](2), [ENUM](3), 
or 6 more.
   
   [Show more 
details](https://github.com/apache/avro/security/code-scanning/3306)



##########
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:
   ## Unread local variable
   
   Variable 'Type convertedType' is never read.
   
   [Show more 
details](https://github.com/apache/avro/security/code-scanning/3303)



##########
lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java:
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.Conversion;
+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.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.*;
+
+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);
+  }
+
+  static Map<Schema.Type, GenericData.AbstractArray<?>> validMappings = new 
EnumMap<>(Schema.Type.class);
+  static {
+    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;
+      }
+    }
+  }
+
+  public static Stream<Arguments> testNewArrayData() {
+
+    List<Arguments> data = new ArrayList<>();
+
+    validMappings.forEach((validKey, optimalValue) -> {
+      Class<?> optimalValueType = optimalValue.getClass();
+      // cant reuse null, or a string
+      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<Object>(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),
+            Collections.emptyList(), null, GenericData.Array.class));
+        data.add(Arguments.of("String (with logical type) input, " + 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<Object>(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<Object>(0, 
arraySchema), GenericData.Array.class));
+          }
+        });
+
+      }
+
+      validMappings.forEach((suppliedValueType, suppliedValue) -> {
+        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), 
Collections.emptyList(), suppliedValue,
+              GenericData.Array.class));
+      });
+    });
+    return data.stream();
+  }
+
+  private static <T> List<Conversion<?>> singleConversion(Schema.Type 
targetKey) {
+    return Collections.singletonList(new Conversion<T>() {
+
+      public Class<T> getConvertedType() {
+        switch (targetKey) {
+        case INT:
+          return (Class<T>) Integer.TYPE;
+        case LONG:
+          return (Class<T>) Long.TYPE;
+        case DOUBLE:
+          return (Class<T>) Double.TYPE;
+        case FLOAT:
+          return (Class<T>) Float.TYPE;
+        case BOOLEAN:
+          return (Class<T>) Boolean.TYPE;
+        default:
+          return (Class<T>) Object.class;
+        }
+
+      }
+
+      public String getLogicalTypeName() {

Review Comment:
   ## Missing Override annotation
   
   This method overrides [Conversion<T>.getLogicalTypeName](1); it is advisable 
to add an Override annotation.
   
   [Show more 
details](https://github.com/apache/avro/security/code-scanning/3304)



##########
lang/java/avro/src/test/java/org/apache/avro/generic/GenericDataTest.java:
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.Conversion;
+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.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.*;
+
+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);
+  }
+
+  static Map<Schema.Type, GenericData.AbstractArray<?>> validMappings = new 
EnumMap<>(Schema.Type.class);
+  static {
+    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;
+      }
+    }
+  }
+
+  public static Stream<Arguments> testNewArrayData() {
+
+    List<Arguments> data = new ArrayList<>();
+
+    validMappings.forEach((validKey, optimalValue) -> {
+      Class<?> optimalValueType = optimalValue.getClass();
+      // cant reuse null, or a string
+      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<Object>(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),
+            Collections.emptyList(), null, GenericData.Array.class));
+        data.add(Arguments.of("String (with logical type) input, " + 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<Object>(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<Object>(0, 
arraySchema), GenericData.Array.class));
+          }
+        });
+
+      }
+
+      validMappings.forEach((suppliedValueType, suppliedValue) -> {
+        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), 
Collections.emptyList(), suppliedValue,
+              GenericData.Array.class));
+      });
+    });
+    return data.stream();
+  }
+
+  private static <T> List<Conversion<?>> singleConversion(Schema.Type 
targetKey) {
+    return Collections.singletonList(new Conversion<T>() {
+
+      public Class<T> getConvertedType() {

Review Comment:
   ## Missing Override annotation
   
   This method overrides [Conversion<T>.getConvertedType](1); it is advisable 
to add an Override annotation.
   
   [Show more 
details](https://github.com/apache/avro/security/code-scanning/3305)



-- 
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]

Reply via email to