okumin commented on code in PR #4653: URL: https://github.com/apache/hive/pull/4653#discussion_r1317316622
########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/AbstractGenericUDFArrayBase.java: ########## @@ -123,4 +124,15 @@ ObjectInspector initListOI(ObjectInspector[] arguments) { return ObjectInspectorFactory.getStandardListObjectInspector(initOI(arguments)); } + void checkValueAndListElementTypes(ObjectInspector arrayElementOI, ObjectInspector valueOI, int elementIndex) + throws UDFArgumentTypeException { + // Check if list element and value are of same type + if (!ObjectInspectorUtils.compareTypes(arrayElementOI, valueOI)) { + throw new UDFArgumentTypeException(elementIndex, + String.format("%s type element is expected at function array_position(array<%s>,%s), but %s is found", Review Comment: Sorry, I overlooked that this also contains `array_position(array<%s>, %s)`. Maybe, that part, at least the function name, has to be a placeholder. ########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayPosition.java: ########## @@ -0,0 +1,73 @@ +/* + * 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.serde.serdeConstants; +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"; + private static final int ELEMENT_IDX = 1; + + public GenericUDFArrayPosition() { + super(FUNC_NAME, 2, 2, ObjectInspector.Category.PRIMITIVE); + } + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + super.initialize(arguments); + checkValueAndListElementTypes(arrayOI.getListElementObjectInspector(),arguments[ELEMENT_IDX],ELEMENT_IDX); + 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 = ((ListObjectInspector) argumentOIs[ARRAY_IDX]).getList(array); + // Handling Varchar type this way as Object comparison between string and varchar will not work + if ((argumentOIs[ELEMENT_IDX].getTypeName().contains(serdeConstants.VARCHAR_TYPE_NAME) + || ((ListObjectInspector) argumentOIs[ARRAY_IDX]).getListElementObjectInspector().getTypeName() + .contains(serdeConstants.VARCHAR_TYPE_NAME))) { + for (int index = 0; index < resultArray.size(); index++) { + if (resultArray.get(index).toString().equals(value.toString())) { + return new IntWritable(index + 1); + } + } + } + return new IntWritable(resultArray.indexOf(value) + 1); Review Comment: We might have assumed the returned value of `ListObjectInspector#getList` can be directly used. But I started feeling it is still an abstract value, and then we need to parse them via the object inspector of the element. https://github.com/apache/hive/blob/rel/release-4.0.0-beta-1/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayContains.java#L121-L122 This is the example where I compared arrays in a table and arrays or an element as a literal. ``` 0: jdbc:hive2://hive-hiveserver2:10000/defaul> create table test as select array('a', 'b', 'c', 'b') as a union all select array('a', 'c', 'd') as a; ... 0: jdbc:hive2://hive-hiveserver2:10000/defaul> select a, array_contains(a, 'b') from test; +--------------------+--------+ | a | _c1 | +--------------------+--------+ | ["a","b","c","b"] | true | | ["a","c","d"] | false | +--------------------+--------+ ... 0: jdbc:hive2://hive-hiveserver2:10000/defaul> select a, array_position(a, 'b') from test; +--------------------+------+ | a | _c1 | +--------------------+------+ | ["a","b","c","b"] | 0 | | ["a","c","d"] | 0 | +--------------------+------+ 0: jdbc:hive2://hive-hiveserver2:10000/defaul> select a, array_except(a, array('a', 'd')) from test; ... +--------------------+----------------+ | a | _c1 | +--------------------+----------------+ | ["a","b","c","b"] | ["a","b","c"] | | ["a","c","d"] | ["a","c","d"] | +--------------------+----------------+ ``` ########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayPosition.java: ########## @@ -0,0 +1,73 @@ +/* + * 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.serde.serdeConstants; +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"; + private static final int ELEMENT_IDX = 1; + + public GenericUDFArrayPosition() { + super(FUNC_NAME, 2, 2, ObjectInspector.Category.PRIMITIVE); + } + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + super.initialize(arguments); + checkValueAndListElementTypes(arrayOI.getListElementObjectInspector(),arguments[ELEMENT_IDX],ELEMENT_IDX); + 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 = ((ListObjectInspector) argumentOIs[ARRAY_IDX]).getList(array); + // Handling Varchar type this way as Object comparison between string and varchar will not work + if ((argumentOIs[ELEMENT_IDX].getTypeName().contains(serdeConstants.VARCHAR_TYPE_NAME) Review Comment: What is examples when we need this one? -- 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