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