okumin commented on code in PR #4653: URL: https://github.com/apache/hive/pull/4653#discussion_r1314898179
########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/AbstractGenericUDFArrayBase.java: ########## @@ -34,6 +35,7 @@ public abstract class AbstractGenericUDFArrayBase extends GenericUDF { static final int ARRAY_IDX = 0; + static final int ELEMENT_IDX = 1; Review Comment: I guess it is more flexible if `checkValueAndListElementTypes` accepts an index than we hardcode the index here. For example, `array_insert` of Spark SQL receives an element in the 3rd argument, not the 2nd. https://spark.apache.org/docs/latest/api/sql/index.html#array_insert Another option is we keep only `checkValueAndListElementTypes(arrayElementOI, valueOI)`, removing `checkValueAndListElementTypes(arguments, index?)`. ########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayPosition.java: ########## @@ -0,0 +1,61 @@ +/* + * 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.hadoop.hive.ql.udf.generic; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.io.IntWritable; + +import java.util.ArrayList; +import java.util.List; + +/** + * GenericUDFArrayPosition. + */ +@Description(name = "array_position", value = "_FUNC_(array, element) - Returns the position of the first occurrence of " + + "element in array. Array indexing starts at 1. If the element value is NULL, a NULL is returned.", extended = + "Example:\n" + " > SELECT _FUNC_(array(1, 2, 3,4,2), 2) FROM src;\n" + " 2") +public class GenericUDFArrayPosition extends AbstractGenericUDFArrayBase { + static final String FUNC_NAME = "ARRAY_POSITION"; + + public GenericUDFArrayPosition() { + super(FUNC_NAME, 2, 2, ObjectInspector.Category.PRIMITIVE); + } + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + super.initialize(arguments); + checkValueAndListElementTypes(arguments); + return PrimitiveObjectInspectorFactory.writableIntObjectInspector; + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + Object array = arguments[ARRAY_IDX].get(); + Object value = arguments[ELEMENT_IDX].get(); + if (arrayOI.getListLength(array) <= 0 || value == null) { Review Comment: I guess the first condition should be `arrayOI.getListLength(array) < 0`. ########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayPosition.java: ########## @@ -0,0 +1,61 @@ +/* + * 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.hadoop.hive.ql.udf.generic; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.io.IntWritable; + +import java.util.ArrayList; +import java.util.List; + +/** + * GenericUDFArrayPosition. + */ +@Description(name = "array_position", value = "_FUNC_(array, element) - Returns the position of the first occurrence of " + + "element in array. Array indexing starts at 1. If the element value is NULL, a NULL is returned.", extended = + "Example:\n" + " > SELECT _FUNC_(array(1, 2, 3,4,2), 2) FROM src;\n" + " 2") Review Comment: I originally thought arrays of Hive, Spark, MySQL, etc. were 0-based. But I also see `array_position` of Spark is 1-based... - https://spark.apache.org/docs/latest/api/sql/index.html#array_position ``` spark-sql (default)> SELECT array(1, 2, 3, 4, 5)[3]; 4 spark-sql (default)> SELECT array_position(array(1, 2, 3, 4, 5), 3); 3 ``` ########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayPosition.java: ########## @@ -0,0 +1,61 @@ +/* + * 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.hadoop.hive.ql.udf.generic; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.io.IntWritable; + +import java.util.ArrayList; +import java.util.List; + +/** + * GenericUDFArrayPosition. + */ +@Description(name = "array_position", value = "_FUNC_(array, element) - Returns the position of the first occurrence of " + + "element in array. Array indexing starts at 1. If the element value is NULL, a NULL is returned.", extended = + "Example:\n" + " > SELECT _FUNC_(array(1, 2, 3,4,2), 2) FROM src;\n" + " 2") +public class GenericUDFArrayPosition extends AbstractGenericUDFArrayBase { + static final String FUNC_NAME = "ARRAY_POSITION"; + + public GenericUDFArrayPosition() { + super(FUNC_NAME, 2, 2, ObjectInspector.Category.PRIMITIVE); + } + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + super.initialize(arguments); + checkValueAndListElementTypes(arguments); + return PrimitiveObjectInspectorFactory.writableIntObjectInspector; + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + Object array = arguments[ARRAY_IDX].get(); + Object value = arguments[ELEMENT_IDX].get(); + if (arrayOI.getListLength(array) <= 0 || value == null) { + return null; + } + List<?> resultArray = new ArrayList<>(((ListObjectInspector) argumentOIs[ARRAY_IDX]).getList(array)); Review Comment: We may not need to copy the original object here because we don't return the reference of `resultArray`. -- 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: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org