Copilot commented on code in PR #17156:
URL: https://github.com/apache/pinot/pull/17156#discussion_r2497782721
##########
pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/custom/ArrayTest.java:
##########
@@ -557,6 +557,93 @@ public void testDoubleArrayLiteral(boolean
useMultiStageQueryEngine)
}
}
+ @Test(dataProvider = "useBothQueryEngines")
+ public void testArrayHasAnyWithLiterals(boolean useMultiStageQueryEngine)
+ throws Exception {
+ setUseMultiStageQueryEngine(useMultiStageQueryEngine);
+
+ // INT array literals
+ JsonNode result = postQuery("SELECT ARRAYHASANY(ARRAY[1,2],
ARRAY[3,2])").get("resultTable");
+ System.out.println("result = " + result);
+ assertTrue(result.get("rows").get(0).get(0).asBoolean());
+ result = postQuery("SELECT ARRAYHASANY(ARRAY[1,2],
ARRAY[3,4])").get("resultTable");
+ assertFalse(result.get("rows").get(0).get(0).asBoolean());
+
+ // LONG array literals (use large values to ensure LONG_ARRAY typing)
+ result = postQuery("SELECT ARRAYHASANY(ARRAY[2147483648,2147483649],
ARRAY[2147483650,2147483649])")
+ .get("resultTable");
+ assertTrue(result.get("rows").get(0).get(0).asBoolean());
+ result = postQuery("SELECT ARRAYHASANY(ARRAY[2147483648,2147483649],
ARRAY[2147483650,2147483651])")
+ .get("resultTable");
+ assertFalse(result.get("rows").get(0).get(0).asBoolean());
+
+ // DOUBLE array literals
+ result = postQuery(
+ "SELECT ARRAYHASANY(ARRAY[CAST(0.1 AS DOUBLE),CAST(0.2 AS DOUBLE)],
ARRAY[CAST(0.3 AS DOUBLE),CAST(0.2 AS "
+ + "DOUBLE)])")
+ .get("resultTable");
+ assertTrue(result.get("rows").get(0).get(0).asBoolean());
+ result = postQuery(
+ "SELECT ARRAYHASANY(ARRAY[CAST(0.1 AS DOUBLE),CAST(0.2 AS DOUBLE)],
ARRAY[CAST(0.3 AS DOUBLE),CAST(0.4 AS "
+ + "DOUBLE)])")
+ .get("resultTable");
+ assertFalse(result.get("rows").get(0).get(0).asBoolean());
+
+ // STRING array literals
+ result = postQuery("SELECT ARRAYHASANY(ARRAY['a','b'],
ARRAY['x','b'])").get("resultTable");
+ assertTrue(result.get("rows").get(0).get(0).asBoolean());
+ result = postQuery("SELECT ARRAYHASANY(ARRAY['a','b'],
ARRAY['x','y'])").get("resultTable");
+ assertFalse(result.get("rows").get(0).get(0).asBoolean());
+ }
+
+ @Test(dataProvider = "useBothQueryEngines")
+ public void testArrayHasAnyWithColumns(boolean useMultiStageQueryEngine)
+ throws Exception {
+ setUseMultiStageQueryEngine(useMultiStageQueryEngine);
+
+ // LONG array column always contains [0,1,2,3] in this dataset
+ String queryTrue = String.format(
+ "SELECT COUNT(*) FROM %s WHERE ARRAYHASANY(%s, ARRAY[CAST(2 AS
BIGINT), CAST(10 AS BIGINT)])",
+ getTableName(), LONG_ARRAY_COLUMN);
+ JsonNode jsonNode = postQuery(queryTrue);
+ System.out.println("jsonNode = " + jsonNode);
Review Comment:
Debug print statement should be removed. Use proper logging or assertion
messages instead of System.out for test output.
```suggestion
// Removed debug print statement as per coding standards.
```
##########
pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/custom/ArrayTest.java:
##########
@@ -557,6 +557,93 @@ public void testDoubleArrayLiteral(boolean
useMultiStageQueryEngine)
}
}
+ @Test(dataProvider = "useBothQueryEngines")
+ public void testArrayHasAnyWithLiterals(boolean useMultiStageQueryEngine)
+ throws Exception {
+ setUseMultiStageQueryEngine(useMultiStageQueryEngine);
+
+ // INT array literals
+ JsonNode result = postQuery("SELECT ARRAYHASANY(ARRAY[1,2],
ARRAY[3,2])").get("resultTable");
+ System.out.println("result = " + result);
Review Comment:
Debug print statement should be removed. Use proper logging or assertion
messages instead of System.out for test output.
```suggestion
```
##########
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/array/ArrayHasAnyScalarFunction.java:
##########
@@ -0,0 +1,229 @@
+/**
+ * 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.Collections;
+import java.util.EnumMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+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 = {"ARRAYHASANY"})
+public class ArrayHasAnyScalarFunction implements PinotScalarFunction {
+
+ private static final Map<DataSchema.ColumnDataType, FunctionInfo>
+ TYPE_FUNCTION_INFO_MAP = new EnumMap<>(DataSchema.ColumnDataType.class);
+
+ static {
+ try {
+ TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.INT_ARRAY,
+ new
FunctionInfo(ArrayHasAnyScalarFunction.class.getMethod("arrayHasAny",
int[].class, int[].class),
+ ArrayHasAnyScalarFunction.class, false));
+ TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.LONG_ARRAY,
+ new
FunctionInfo(ArrayHasAnyScalarFunction.class.getMethod("arrayHasAny",
long[].class, long[].class),
+ ArrayHasAnyScalarFunction.class, false));
+ TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.FLOAT_ARRAY,
+ new
FunctionInfo(ArrayHasAnyScalarFunction.class.getMethod("arrayHasAny",
float[].class, float[].class),
+ ArrayHasAnyScalarFunction.class, false));
+ TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.DOUBLE_ARRAY,
+ new
FunctionInfo(ArrayHasAnyScalarFunction.class.getMethod("arrayHasAny",
double[].class, double[].class),
+ ArrayHasAnyScalarFunction.class, false));
+ TYPE_FUNCTION_INFO_MAP.put(DataSchema.ColumnDataType.STRING_ARRAY,
+ new
FunctionInfo(ArrayHasAnyScalarFunction.class.getMethod("arrayHasAny",
String[].class, String[].class),
+ ArrayHasAnyScalarFunction.class, false));
+ } catch (NoSuchMethodException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public String getName() {
+ return "ARRAYHASANY";
+ }
+
+ @Override
+ public Set<String> getNames() {
+ return Set.of("ARRAYHASANY");
+ }
+
+ @Nullable
+ @Override
+ public PinotSqlFunction toPinotSqlFunction() {
+ // Should already be registered in PinotOperatorTable by the transform
function implementation
+ return null;
+ }
+
+ @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
+ });
+ }
+
+ public static boolean arrayHasAny(int[] array1, int[] array2) {
+ Set<Integer> elements;
+ if (array1.length <= array2.length) {
+ elements = new HashSet<>(array1.length);
+ for (int v : array1) {
+ elements.add(v);
+ }
+ for (int v : array2) {
+ if (elements.contains(v)) {
+ return true;
+ }
+ }
Review Comment:
This logic is duplicated across all five arrayHasAny overloads (int, long,
float, double, String). Consider extracting this pattern into a generic helper
method or using a functional approach to reduce duplication and improve
maintainability.
--
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]