PageRenderTime 26ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayContains.java

#
Java | 137 lines | 87 code | 23 blank | 27 comment | 14 complexity | 9179f846b995eadad67d520798824a64 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.apache.hadoop.hive.ql.udf.generic;
 19
 20import org.apache.hadoop.hive.ql.exec.Description;
 21import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 22import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
 23import org.apache.hadoop.hive.ql.metadata.HiveException;
 24import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
 25import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 26import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
 27import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
 28import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 29import org.apache.hadoop.io.BooleanWritable;
 30
 31/**
 32 * GenericUDFArrayContains.
 33 *
 34 */
 35@Description(name = "array_contains",
 36    value="_FUNC_(array, value) - Returns TRUE if the array contains value.",
 37    extended="Example:\n"
 38      + "  > SELECT _FUNC_(array(1, 2, 3), 2) FROM src LIMIT 1;\n"
 39      + "  true")
 40public class GenericUDFArrayContains extends GenericUDF {
 41
 42  private static final int ARRAY_IDX = 0;
 43  private static final int VALUE_IDX = 1;
 44  private static final int ARG_COUNT = 2; // Number of arguments to this UDF
 45  private static final String FUNC_NAME = "ARRAY_CONTAINS"; // External Name
 46
 47  private ObjectInspector valueOI;
 48  private ListObjectInspector arrayOI;
 49  private ObjectInspector arrayElementOI;
 50  private BooleanWritable result;
 51
 52  @Override
 53  public ObjectInspector initialize(ObjectInspector[] arguments)
 54      throws UDFArgumentException {
 55
 56    // Check if two arguments were passed
 57    if (arguments.length != ARG_COUNT) {
 58      throw new UDFArgumentException(
 59          "The function " + FUNC_NAME + " accepts "
 60              + ARG_COUNT + " arguments.");
 61    }
 62
 63    // Check if ARRAY_IDX argument is of category LIST
 64    if (!arguments[ARRAY_IDX].getCategory().equals(Category.LIST)) {
 65      throw new UDFArgumentTypeException(ARRAY_IDX,
 66          "\"" + org.apache.hadoop.hive.serde.Constants.LIST_TYPE_NAME + "\" "
 67          + "expected at function ARRAY_CONTAINS, but "
 68          + "\"" + arguments[ARRAY_IDX].getTypeName() + "\" "
 69          + "is found");
 70    }
 71
 72    arrayOI = (ListObjectInspector) arguments[ARRAY_IDX];
 73    arrayElementOI = arrayOI.getListElementObjectInspector();
 74
 75    valueOI = arguments[VALUE_IDX];
 76
 77    // Check if list element and value are of same type
 78    if (!ObjectInspectorUtils.compareTypes(arrayElementOI, valueOI)) {
 79      throw new UDFArgumentTypeException(VALUE_IDX,
 80          "\"" + arrayElementOI.getTypeName() + "\""
 81          + " expected at function ARRAY_CONTAINS, but "
 82          + "\"" + valueOI.getTypeName() + "\""
 83          + " is found");
 84    }
 85
 86    // Check if the comparison is supported for this type
 87    if (!ObjectInspectorUtils.compareSupported(valueOI)) {
 88      throw new UDFArgumentException("The function " + FUNC_NAME
 89          + " does not support comparison for "
 90          + "\"" + valueOI.getTypeName() + "\""
 91          + " types");
 92    }
 93
 94    result = new BooleanWritable(false);
 95
 96    return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
 97  }
 98
 99
100
101  @Override
102  public Object evaluate(DeferredObject[] arguments) throws HiveException {
103
104    result.set(false);
105
106    Object array = arguments[ARRAY_IDX].get();
107    Object value = arguments[VALUE_IDX].get();
108
109    int arrayLength = arrayOI.getListLength(array);
110
111    // Check if array is null or empty or value is null
112    if (value == null || arrayLength <= 0) {
113      return result;
114    }
115
116    // Compare the value to each element of array until a match is found
117    for (int i=0; i<arrayLength; ++i) {
118      Object listElement = arrayOI.getListElement(array, i);
119      if (listElement != null) {
120        if (ObjectInspectorUtils.compare(value, valueOI,
121            listElement, arrayElementOI) == 0) {
122          result.set(true);
123          break;
124        }
125      }
126    }
127
128    return result;
129  }
130
131  @Override
132  public String getDisplayString(String[] children) {
133    assert (children.length == ARG_COUNT);
134    return "array_contains(" + children[ARRAY_IDX] + ", "
135              + children[VALUE_IDX] + ")";
136  }
137}