PageRenderTime 48ms CodeModel.GetById 14ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

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

#
Java | 122 lines | 85 code | 14 blank | 23 comment | 31 complexity | 90382444556faf57b9b1fad11bfeca75 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 java.util.HashSet;
 22import java.util.Set;
 23
 24import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 25import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
 26import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
 27import org.apache.hadoop.hive.ql.metadata.HiveException;
 28import org.apache.hadoop.hive.serde.Constants;
 29import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 30import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 31import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
 32import org.apache.hadoop.io.Text;
 33
 34/**
 35 * Mimics oracle's function translate(str1, str2, str3).
 36 */
 37public class GenericUDFTestTranslate extends GenericUDF {
 38  ObjectInspector[] argumentOIs;
 39
 40  /**
 41   * Return a corresponding ordinal from an integer.
 42   */
 43  static String getOrdinal(int i) {
 44    int unit = i % 10;
 45    return (i <= 0) ? "" : (i != 11 && unit == 1) ? i + "st"
 46        : (i != 12 && unit == 2) ? i + "nd" : (i != 13 && unit == 3) ? i + "rd"
 47        : i + "th";
 48  }
 49
 50  @Override
 51  public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
 52    if (arguments.length != 3) {
 53      throw new UDFArgumentLengthException(
 54          "The function TRANSLATE(expr,from_string,to_string) accepts exactly 3 arguments, but "
 55          + arguments.length + " arguments is found.");
 56    }
 57
 58    for (int i = 0; i < 3; i++) {
 59      if (arguments[i].getTypeName() != Constants.STRING_TYPE_NAME
 60          && arguments[i].getTypeName() != Constants.VOID_TYPE_NAME) {
 61        throw new UDFArgumentTypeException(i, "The " + getOrdinal(i + 1)
 62            + " argument of function TRANSLATE is expected to \""
 63            + Constants.STRING_TYPE_NAME + "\", but \""
 64            + arguments[i].getTypeName() + "\" is found");
 65      }
 66    }
 67
 68    argumentOIs = arguments;
 69    return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
 70  }
 71
 72  private final Text resultText = new Text();
 73
 74  @Override
 75  public Object evaluate(DeferredObject[] arguments) throws HiveException {
 76    if (arguments[0].get() == null || arguments[1].get() == null
 77        || arguments[2].get() == null) {
 78      return null;
 79    }
 80    String exprString = ((StringObjectInspector) argumentOIs[0])
 81        .getPrimitiveJavaObject(arguments[0].get());
 82    String fromString = ((StringObjectInspector) argumentOIs[1])
 83        .getPrimitiveJavaObject(arguments[1].get());
 84    String toString = ((StringObjectInspector) argumentOIs[2])
 85        .getPrimitiveJavaObject(arguments[2].get());
 86
 87    char[] expr = exprString.toCharArray();
 88    char[] from = fromString.toCharArray();
 89    char[] to = toString.toCharArray();
 90    char[] result = new char[expr.length];
 91    System.arraycopy(expr, 0, result, 0, expr.length);
 92    Set<Character> seen = new HashSet<Character>();
 93
 94    for (int i = 0; i < from.length; i++) {
 95      if (seen.contains(from[i])) {
 96        continue;
 97      }
 98      seen.add(from[i]);
 99      for (int j = 0; j < expr.length; j++) {
100        if (expr[j] == from[i]) {
101          result[j] = (i < to.length) ? to[i] : 0;
102        }
103      }
104    }
105
106    int pos = 0;
107    for (int i = 0; i < result.length; i++) {
108      if (result[i] != 0) {
109        result[pos++] = result[i];
110      }
111    }
112    resultText.set(new String(result, 0, pos));
113    return resultText;
114  }
115
116  @Override
117  public String getDisplayString(String[] children) {
118    assert (children.length == 3);
119    return "translate(" + children[0] + "," + children[1] + "," + children[2]
120        + ")";
121  }
122}