tarak271 commented on code in PR #3774: URL: https://github.com/apache/hive/pull/3774#discussion_r1028349448
########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/AbstractGenericUDFArrayBase.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.UDFArgumentException; +import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; +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.ObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; + +import java.util.ArrayList; +import java.util.List; + +/** + * Abstract GenericUDF for array functions + */ + +public abstract class AbstractGenericUDFArrayBase extends GenericUDF { + + static final int ARRAY_IDX = 0; + static final int ARRAY2_IDX = 1; + static final int START_IDX = 1; + static final int LENGTH_IDX = 2; + static final int SEPARATOR_IDX = 1; + static final int REPLACE_NULL_IDX = 2; + + private final int minArgCount; + private final int maxArgCount; + private final ObjectInspector.Category outputCategory; + + private final FUNC_NAMES funcName; + + transient ListObjectInspector arrayOI; + transient ObjectInspector[] argumentOIs; + + transient Converter converter; + + public AbstractGenericUDFArrayBase(FUNC_NAMES funcName, int minArgCount, int maxArgCount, ObjectInspector.Category outputCategory) { + this.funcName = funcName; + this.minArgCount = minArgCount; + this.maxArgCount = maxArgCount; + this.outputCategory = outputCategory; + } + + enum FUNC_NAMES { + ARRAY_MAX, ARRAY_MIN, ARRAY_DISTINCT, ARRAY_SLICE, ARRAY_JOIN, ARRAY_EXCEPT, ARRAY_INTERSECT + } + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) + throws UDFArgumentException { + + // Check if wrong number of arguments were passed + checkArgsSize(arguments, minArgCount, maxArgCount); + + // Check if the argument is of category LIST or not + checkArgCategory(arguments, ARRAY_IDX, ObjectInspector.Category.LIST, funcName, + org.apache.hadoop.hive.serde.serdeConstants.LIST_TYPE_NAME); + + //return ObjectInspectors based on expected output type + arrayOI = (ListObjectInspector) arguments[ARRAY_IDX]; + argumentOIs = arguments; + if (outputCategory == ObjectInspector.Category.LIST) { + return initListOI(arguments); + } else { + return initOI(arguments); + } + } + + @Override + public String getDisplayString(String[] children) { + assert (children.length == minArgCount); + return funcName.toString().toLowerCase() + "(" + children[ARRAY_IDX] + ")"; + } + + List<Object> convertArray(List objects) { Review Comment: Removed unused function ########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/AbstractGenericUDFArrayBase.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.UDFArgumentException; +import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; +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.ObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; + +import java.util.ArrayList; +import java.util.List; + +/** + * Abstract GenericUDF for array functions + */ + +public abstract class AbstractGenericUDFArrayBase extends GenericUDF { + + static final int ARRAY_IDX = 0; + static final int ARRAY2_IDX = 1; + static final int START_IDX = 1; + static final int LENGTH_IDX = 2; + static final int SEPARATOR_IDX = 1; + static final int REPLACE_NULL_IDX = 2; + + private final int minArgCount; + private final int maxArgCount; + private final ObjectInspector.Category outputCategory; + + private final FUNC_NAMES funcName; + + transient ListObjectInspector arrayOI; + transient ObjectInspector[] argumentOIs; + + transient Converter converter; + + public AbstractGenericUDFArrayBase(FUNC_NAMES funcName, int minArgCount, int maxArgCount, ObjectInspector.Category outputCategory) { + this.funcName = funcName; + this.minArgCount = minArgCount; + this.maxArgCount = maxArgCount; + this.outputCategory = outputCategory; + } + + enum FUNC_NAMES { + ARRAY_MAX, ARRAY_MIN, ARRAY_DISTINCT, ARRAY_SLICE, ARRAY_JOIN, ARRAY_EXCEPT, ARRAY_INTERSECT + } + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) + throws UDFArgumentException { + + // Check if wrong number of arguments were passed + checkArgsSize(arguments, minArgCount, maxArgCount); + + // Check if the argument is of category LIST or not + checkArgCategory(arguments, ARRAY_IDX, ObjectInspector.Category.LIST, funcName, + org.apache.hadoop.hive.serde.serdeConstants.LIST_TYPE_NAME); + + //return ObjectInspectors based on expected output type + arrayOI = (ListObjectInspector) arguments[ARRAY_IDX]; + argumentOIs = arguments; + if (outputCategory == ObjectInspector.Category.LIST) { + return initListOI(arguments); + } else { + return initOI(arguments); + } + } + + @Override + public String getDisplayString(String[] children) { + assert (children.length == minArgCount); + return funcName.toString().toLowerCase() + "(" + children[ARRAY_IDX] + ")"; + } + + List<Object> convertArray(List objects) { + List<Object> ret = new ArrayList<>(); + for (Object o : objects) { + ret.add(converter.convert(o)); + } + return ret; + } + + void checkArgCategory(ObjectInspector[] arguments, int idx, Enum category, + FUNC_NAMES functionName, String typeName) throws UDFArgumentTypeException { + + if (!arguments[idx].getCategory().equals(category)) { + throw new UDFArgumentTypeException(idx, + "\"" + typeName + "\" " + + "expected at function " + functionName + ", but " + + "\"" + arguments[idx].getTypeName() + "\" " + + "is found"); + } + } + + void checkArgIntPrimitiveCategory(PrimitiveObjectInspector objectInspector, Review Comment: Removed unused function -- 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]
