PageRenderTime 17ms CodeModel.GetById 9ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFField.java

#
Java | 102 lines | 67 code | 14 blank | 21 comment | 11 complexity | d34d22b08992e0510a4a544cb5350c8e 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 */
 18
 19package org.apache.hadoop.hive.ql.udf.generic;
 20
 21import org.apache.commons.lang.StringUtils;
 22import org.apache.hadoop.hive.ql.exec.Description;
 23import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 24import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
 25import org.apache.hadoop.hive.ql.metadata.HiveException;
 26import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 27import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
 28import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
 29import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
 30import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 31import org.apache.hadoop.io.IntWritable;
 32
 33/**
 34 * GenericUDFField.
 35 * 
 36 */
 37@Description(name = "field", value = "_FUNC_(str, str1, str2, ...) - "
 38    + "returns the index of str in the str1,str2,... list or 0 if not found", extended = "All primitive types are supported, arguments are compared using str.equals(x)."
 39    + " If str is NULL, the return value is 0.")
 40public class GenericUDFField extends GenericUDF {
 41  private ObjectInspector[] argumentOIs;
 42
 43  @Override
 44  public ObjectInspector initialize(ObjectInspector[] arguments)
 45      throws UDFArgumentException {
 46    if (arguments.length < 2) {
 47      throw new UDFArgumentException(
 48          "The function FIELD(str, str1, str2, ...) needs at least two arguments.");
 49    }
 50
 51    argumentOIs = arguments;
 52
 53    for (int i = 0; i < arguments.length; i++) {
 54      Category category = arguments[i].getCategory();
 55      if (category != Category.PRIMITIVE) {
 56        throw new UDFArgumentTypeException(i, "The "
 57            + GenericUDFUtils.getOrdinal(i + 1)
 58            + " argument of function FIELD is expected to a "
 59            + Category.PRIMITIVE.toString().toLowerCase() + " type, but "
 60            + category.toString().toLowerCase() + " is found");
 61      }
 62    }
 63
 64    return PrimitiveObjectInspectorFactory.writableIntObjectInspector;
 65  }
 66
 67  private final IntWritable r = new IntWritable();
 68
 69  @Override
 70  public Object evaluate(DeferredObject[] arguments) throws HiveException {
 71    if (arguments[0].get() == null) {
 72      r.set(0);
 73      return r;
 74    }
 75
 76    for (int i = 1; i < arguments.length; i++) {
 77      if (arguments[i].get() == null) {
 78        continue;
 79      }
 80      if (ObjectInspectorUtils.compare(arguments[0].get(), argumentOIs[0],
 81          arguments[i].get(), argumentOIs[i]) == 0) {
 82        r.set(i);
 83        return r;
 84      }
 85    }
 86
 87    r.set(0);
 88    return r;
 89  }
 90
 91  @Override
 92  public String getDisplayString(String[] children) {
 93    assert (children.length >= 2);
 94
 95    final StringBuilder sb = new StringBuilder();
 96    sb.append("field(");
 97    sb.append(StringUtils.join(children, ", "));
 98    sb.append(")");
 99
100    return sb.toString();
101  }
102}