PageRenderTime 53ms CodeModel.GetById 25ms 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
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.commons.lang.StringUtils;
  20. import org.apache.hadoop.hive.ql.exec.Description;
  21. import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
  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.ObjectInspectorUtils;
  26. import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
  27. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
  28. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
  29. import org.apache.hadoop.io.IntWritable;
  30. /**
  31. * GenericUDFField.
  32. *
  33. */
  34. @Description(name = "field", value = "_FUNC_(str, str1, str2, ...) - "
  35. + "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)."
  36. + " If str is NULL, the return value is 0.")
  37. public class GenericUDFField extends GenericUDF {
  38. private ObjectInspector[] argumentOIs;
  39. @Override
  40. public ObjectInspector initialize(ObjectInspector[] arguments)
  41. throws UDFArgumentException {
  42. if (arguments.length < 2) {
  43. throw new UDFArgumentException(
  44. "The function FIELD(str, str1, str2, ...) needs at least two arguments.");
  45. }
  46. argumentOIs = arguments;
  47. for (int i = 0; i < arguments.length; i++) {
  48. Category category = arguments[i].getCategory();
  49. if (category != Category.PRIMITIVE) {
  50. throw new UDFArgumentTypeException(i, "The "
  51. + GenericUDFUtils.getOrdinal(i + 1)
  52. + " argument of function FIELD is expected to a "
  53. + Category.PRIMITIVE.toString().toLowerCase() + " type, but "
  54. + category.toString().toLowerCase() + " is found");
  55. }
  56. }
  57. return PrimitiveObjectInspectorFactory.writableIntObjectInspector;
  58. }
  59. private final IntWritable r = new IntWritable();
  60. @Override
  61. public Object evaluate(DeferredObject[] arguments) throws HiveException {
  62. if (arguments[0].get() == null) {
  63. r.set(0);
  64. return r;
  65. }
  66. for (int i = 1; i < arguments.length; i++) {
  67. if (arguments[i].get() == null) {
  68. continue;
  69. }
  70. if (ObjectInspectorUtils.compare(arguments[0].get(), argumentOIs[0],
  71. arguments[i].get(), argumentOIs[i]) == 0) {
  72. r.set(i);
  73. return r;
  74. }
  75. }
  76. r.set(0);
  77. return r;
  78. }
  79. @Override
  80. public String getDisplayString(String[] children) {
  81. assert (children.length >= 2);
  82. final StringBuilder sb = new StringBuilder();
  83. sb.append("field(");
  84. sb.append(StringUtils.join(children, ", "));
  85. sb.append(")");
  86. return sb.toString();
  87. }
  88. }