Copilot commented on code in PR #17156:
URL: https://github.com/apache/pinot/pull/17156#discussion_r2502127040


##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/array/ArraysOverlapScalarFunction.java:
##########
@@ -0,0 +1,199 @@
+/**
+ * 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
+ *
+ *   http://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.pinot.common.function.scalar.array;
+
+import java.util.EnumMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.pinot.common.function.FunctionInfo;
+import org.apache.pinot.common.function.PinotScalarFunction;
+import org.apache.pinot.common.function.sql.PinotSqlFunction;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+@ScalarFunction(names = {"ARRAYS_OVERLAP", "ARRAYSOVERLAP"})
+public class ArraysOverlapScalarFunction implements PinotScalarFunction {
+
+  private static final Map<DataSchema.ColumnDataType, FunctionInfo>
+      TYPE_FUNCTION_INFO_MAP = new EnumMap<>(DataSchema.ColumnDataType.class);
+
+  private static final float HASH_SET_LOAD_FACTOR = 0.75f;
+
+  static {
+    try {
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.INT_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
int[].class, int[].class),
+              ArraysOverlapScalarFunction.class, false));
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.LONG_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
long[].class, long[].class),
+              ArraysOverlapScalarFunction.class, false));
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.FLOAT_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
float[].class, float[].class),
+              ArraysOverlapScalarFunction.class, false));
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.DOUBLE_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
double[].class, double[].class),
+              ArraysOverlapScalarFunction.class, false));
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.STRING_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
String[].class, String[].class),
+              ArraysOverlapScalarFunction.class, false));
+    } catch (NoSuchMethodException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
+  public String getName() {
+    return "ARRAYS_OVERLAP";
+  }
+
+  @Override
+  public Set<String> getNames() {
+    return Set.of("ARRAYS_OVERLAP", "ARRAYSOVERLAP");
+  }
+
+  @Nullable
+  @Override
+  public PinotSqlFunction toPinotSqlFunction() {
+    return new PinotSqlFunction("ARRAYS_OVERLAP", ReturnTypes.BOOLEAN,
+        OperandTypes.family(List.of(SqlTypeFamily.ARRAY, 
SqlTypeFamily.ARRAY)));
+  }
+
+  @Nullable
+  @Override
+  public FunctionInfo getFunctionInfo(DataSchema.ColumnDataType[] 
argumentTypes) {
+    if (argumentTypes.length != 2) {
+      return null;
+    }
+    if (argumentTypes[0] != argumentTypes[1]) {
+      return null;
+    }
+    return TYPE_FUNCTION_INFO_MAP.get(argumentTypes[0]);
+  }
+
+  @Nullable
+  @Override
+  public FunctionInfo getFunctionInfo(int numArguments) {
+    if (numArguments != 2) {
+      return null;
+    }
+    // Fall back to string
+    return getFunctionInfo(new DataSchema.ColumnDataType[]{
+        DataSchema.ColumnDataType.STRING_ARRAY,
+        DataSchema.ColumnDataType.STRING_ARRAY
+    });
+  }
+
+  private static int capacityForSize(int size) {
+    // Avoid HashSet rehashing under default load factor.
+    // Use ceiling to ensure sufficient capacity for expected elements.
+    return (int) Math.ceil(size / HASH_SET_LOAD_FACTOR);
+  }
+
+  private static boolean overlapInts(int[] small, int[] large) {
+    Set<Integer> elements = new HashSet<>(capacityForSize(small.length));
+    for (int v : small) {
+      elements.add(v);
+    }
+    for (int v : large) {
+      if (elements.contains(v)) {
+        return true;
+      }
+    }
+    return false;
+  }

Review Comment:
   The overlap detection logic is duplicated across five nearly identical 
methods (`overlapInts`, `overlapLongs`, `overlapFloats`, `overlapDoubles`, 
`overlapStrings`). Consider extracting a generic helper method that accepts 
boxed arrays or using a template pattern to reduce code duplication and improve 
maintainability.



##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/array/ArraysOverlapScalarFunction.java:
##########
@@ -0,0 +1,199 @@
+/**
+ * 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
+ *
+ *   http://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.pinot.common.function.scalar.array;
+
+import java.util.EnumMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.pinot.common.function.FunctionInfo;
+import org.apache.pinot.common.function.PinotScalarFunction;
+import org.apache.pinot.common.function.sql.PinotSqlFunction;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+@ScalarFunction(names = {"ARRAYS_OVERLAP", "ARRAYSOVERLAP"})
+public class ArraysOverlapScalarFunction implements PinotScalarFunction {
+
+  private static final Map<DataSchema.ColumnDataType, FunctionInfo>
+      TYPE_FUNCTION_INFO_MAP = new EnumMap<>(DataSchema.ColumnDataType.class);
+
+  private static final float HASH_SET_LOAD_FACTOR = 0.75f;
+
+  static {
+    try {
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.INT_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
int[].class, int[].class),
+              ArraysOverlapScalarFunction.class, false));
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.LONG_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
long[].class, long[].class),
+              ArraysOverlapScalarFunction.class, false));
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.FLOAT_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
float[].class, float[].class),
+              ArraysOverlapScalarFunction.class, false));
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.DOUBLE_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
double[].class, double[].class),
+              ArraysOverlapScalarFunction.class, false));
+      TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.STRING_ARRAY,
+          new 
FunctionInfo(ArraysOverlapScalarFunction.class.getMethod("arraysOverlap", 
String[].class, String[].class),
+              ArraysOverlapScalarFunction.class, false));
+    } catch (NoSuchMethodException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
+  public String getName() {
+    return "ARRAYS_OVERLAP";
+  }
+
+  @Override
+  public Set<String> getNames() {
+    return Set.of("ARRAYS_OVERLAP", "ARRAYSOVERLAP");
+  }
+
+  @Nullable
+  @Override
+  public PinotSqlFunction toPinotSqlFunction() {
+    return new PinotSqlFunction("ARRAYS_OVERLAP", ReturnTypes.BOOLEAN,
+        OperandTypes.family(List.of(SqlTypeFamily.ARRAY, 
SqlTypeFamily.ARRAY)));
+  }
+
+  @Nullable
+  @Override
+  public FunctionInfo getFunctionInfo(DataSchema.ColumnDataType[] 
argumentTypes) {
+    if (argumentTypes.length != 2) {
+      return null;
+    }
+    if (argumentTypes[0] != argumentTypes[1]) {
+      return null;
+    }
+    return TYPE_FUNCTION_INFO_MAP.get(argumentTypes[0]);
+  }
+
+  @Nullable
+  @Override
+  public FunctionInfo getFunctionInfo(int numArguments) {
+    if (numArguments != 2) {
+      return null;
+    }
+    // Fall back to string
+    return getFunctionInfo(new DataSchema.ColumnDataType[]{
+        DataSchema.ColumnDataType.STRING_ARRAY,
+        DataSchema.ColumnDataType.STRING_ARRAY
+    });
+  }
+
+  private static int capacityForSize(int size) {
+    // Avoid HashSet rehashing under default load factor.
+    // Use ceiling to ensure sufficient capacity for expected elements.
+    return (int) Math.ceil(size / HASH_SET_LOAD_FACTOR);

Review Comment:
   Integer division is performed before `Math.ceil`, which defeats the ceiling 
operation. The expression `size / HASH_SET_LOAD_FACTOR` evaluates as integer 
division (0.75 becomes 0), then casts to double for ceiling. Use `(int) 
Math.ceil(size / (double) HASH_SET_LOAD_FACTOR)` to ensure correct capacity 
calculation.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to