PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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