PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 115 lines | 75 code | 11 blank | 29 comment | 27 complexity | 62416e83df9a9cb61d48de404c8d48d9 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.hadoop.hive.ql.exec.Description;
 22import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 23import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
 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.ObjectInspectorConverters;
 28import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
 29import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 30import org.apache.hadoop.io.IntWritable;
 31import org.apache.hadoop.io.Text;
 32
 33/**
 34 * Generic UDF for string function <code>LOCATE(substr, str)</code>,
 35 * <code>LOCATE(substr, str, start)</code>. This mimcs the function from MySQL
 36 * http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_locate
 37 * 
 38 * <pre>
 39 * usage:
 40 * LOCATE(substr, str)
 41 * LOCATE(substr, str, start)
 42 * </pre>
 43 * <p>
 44 */
 45@Description(name = "locate",
 46    value = "_FUNC_(substr, str[, pos]) - Returns the position of the first "
 47    + "occurance of substr in str after position pos",
 48    extended = "Example:\n"
 49    + "  > SELECT _FUNC_('bar', 'foobarbar', 5) FROM src LIMIT 1;\n" + "  7")
 50public class GenericUDFLocate extends GenericUDF {
 51  private ObjectInspectorConverters.Converter[] converters;
 52
 53  @Override
 54  public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
 55    if (arguments.length != 2 && arguments.length != 3) {
 56      throw new UDFArgumentLengthException(
 57          "The function LOCATE accepts exactly 2 or 3 arguments.");
 58    }
 59
 60    for (int i = 0; i < arguments.length; i++) {
 61      Category category = arguments[i].getCategory();
 62      if (category != Category.PRIMITIVE) {
 63        throw new UDFArgumentTypeException(i, "The "
 64            + GenericUDFUtils.getOrdinal(i + 1)
 65            + " argument of function LOCATE is expected to a "
 66            + Category.PRIMITIVE.toString().toLowerCase() + " type, but "
 67            + category.toString().toLowerCase() + " is found");
 68      }
 69    }
 70
 71    converters = new ObjectInspectorConverters.Converter[arguments.length];
 72    for (int i = 0; i < arguments.length; i++) {
 73      if (i == 0 || i == 1) {
 74        converters[i] = ObjectInspectorConverters.getConverter(arguments[i],
 75            PrimitiveObjectInspectorFactory.writableStringObjectInspector);
 76      } else if (i == 2) {
 77        converters[i] = ObjectInspectorConverters.getConverter(arguments[i],
 78            PrimitiveObjectInspectorFactory.writableIntObjectInspector);
 79      }
 80    }
 81
 82    return PrimitiveObjectInspectorFactory.writableIntObjectInspector;
 83  }
 84
 85  private IntWritable intWritable = new IntWritable(0);
 86
 87  @Override
 88  public Object evaluate(DeferredObject[] arguments) throws HiveException {
 89    if (arguments[0].get() == null || arguments[1].get() == null) {
 90      return null;
 91    }
 92
 93    Text subtext = (Text) converters[0].convert(arguments[0].get());
 94    Text text = (Text) converters[1].convert(arguments[1].get());
 95    int start = 1;
 96    if (arguments.length == 3) {
 97      IntWritable startWritable = (IntWritable) converters[2]
 98          .convert(arguments[2].get());
 99      if (startWritable == null) {
100        intWritable.set(0);
101        return intWritable;
102      }
103      start = startWritable.get();
104    }
105    intWritable.set(GenericUDFUtils.findText(text, subtext, start - 1) + 1);
106    return intWritable;
107  }
108
109  @Override
110  public String getDisplayString(String[] children) {
111    assert (children.length == 2 || children.length == 3);
112    return "locate(" + children[0] + children[1]
113        + (children.length == 3 ? children[2] : "") + ")";
114  }
115}