Copilot commented on code in PR #17156: URL: https://github.com/apache/pinot/pull/17156#discussion_r2498130434
########## pinot-common/src/main/java/org/apache/pinot/common/function/scalar/array/ArrayHasAnyScalarFunction.java: ########## @@ -0,0 +1,267 @@ +/** + * 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 java.util.function.BooleanSupplier; +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 + }); + } + + 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 0.75 load factor + return (int) ((size / 0.75f) + 1); Review Comment: The magic number 0.75f is hard-coded. Consider extracting this as a named constant (e.g., 'HASH_SET_LOAD_FACTOR') to improve readability and make it clear this matches HashSet's default 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]
