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


##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/array/ArraysOverlapScalarFunction.java:
##########
@@ -0,0 +1,280 @@
+/**
+ * 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 java.util.function.BooleanSupplier;
+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 boolean hasOverlap(int lenA, int lenB, Runnable buildFromA, 
BooleanSupplier checkB,
+      Runnable buildFromB, BooleanSupplier checkA) {

Review Comment:
   [nitpick] The method signature has six parameters with paired behavior 
(buildFromA/checkB, buildFromB/checkA). Consider introducing a small helper 
class or record to encapsulate these paired operations, improving readability 
and making the intent clearer.



##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/array/ArraysOverlapScalarFunction.java:
##########
@@ -0,0 +1,280 @@
+/**
+ * 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 java.util.function.BooleanSupplier;
+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 boolean hasOverlap(int lenA, int lenB, Runnable buildFromA, 
BooleanSupplier checkB,
+      Runnable buildFromB, BooleanSupplier checkA) {
+    if (lenA <= lenB) {
+      buildFromA.run();
+      return checkB.getAsBoolean();
+    } else {
+      buildFromB.run();
+      return checkA.getAsBoolean();
+    }
+  }
+
+  private static int capacityForSize(int size) {
+    // Avoid HashSet rehashing under default load factor
+    return (int) ((size / HASH_SET_LOAD_FACTOR) + 1);
+  }
+
+  public static boolean arraysOverlap(int[] array1, int[] array2) {
+    Set<Integer> elements = new 
HashSet<>(capacityForSize(Math.min(array1.length, array2.length)));

Review Comment:
   All five `arraysOverlap` overloads follow identical logic with only type 
differences. Consider using a generic helper method to reduce code duplication, 
parameterized by a function that converts primitive arrays to streams or lists.



##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/array/ArraysOverlapScalarFunction.java:
##########
@@ -0,0 +1,280 @@
+/**
+ * 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 java.util.function.BooleanSupplier;
+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 boolean hasOverlap(int lenA, int lenB, Runnable buildFromA, 
BooleanSupplier checkB,
+      Runnable buildFromB, BooleanSupplier checkA) {
+    if (lenA <= lenB) {
+      buildFromA.run();
+      return checkB.getAsBoolean();
+    } else {
+      buildFromB.run();
+      return checkA.getAsBoolean();
+    }
+  }
+
+  private static int capacityForSize(int size) {
+    // Avoid HashSet rehashing under default load factor
+    return (int) ((size / HASH_SET_LOAD_FACTOR) + 1);

Review Comment:
   The comment states 'Avoid HashSet rehashing' but the implementation doesn't 
guarantee this. The formula should account for the load factor threshold 
correctly. Consider documenting that this is an approximation or update to 
`(int) Math.ceil(size / HASH_SET_LOAD_FACTOR)` for accuracy.
   ```suggestion
       // Avoid HashSet rehashing under default load factor.
       // This calculation ensures the capacity is sufficient for the expected 
number of elements.
       return (int) Math.ceil(size / HASH_SET_LOAD_FACTOR);
   ```



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