PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayContains.java

#
Java | 137 lines | 87 code | 23 blank | 27 comment | 14 complexity | 9179f846b995eadad67d520798824a64 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.UDFArgumentTypeException;
  22. import org.apache.hadoop.hive.ql.metadata.HiveException;
  23. import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
  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.ObjectInspector.Category;
  27. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
  28. import org.apache.hadoop.io.BooleanWritable;
  29. /**
  30. * GenericUDFArrayContains.
  31. *
  32. */
  33. @Description(name = "array_contains",
  34. value="_FUNC_(array, value) - Returns TRUE if the array contains value.",
  35. extended="Example:\n"
  36. + " > SELECT _FUNC_(array(1, 2, 3), 2) FROM src LIMIT 1;\n"
  37. + " true")
  38. public class GenericUDFArrayContains extends GenericUDF {
  39. private static final int ARRAY_IDX = 0;
  40. private static final int VALUE_IDX = 1;
  41. private static final int ARG_COUNT = 2; // Number of arguments to this UDF
  42. private static final String FUNC_NAME = "ARRAY_CONTAINS"; // External Name
  43. private ObjectInspector valueOI;
  44. private ListObjectInspector arrayOI;
  45. private ObjectInspector arrayElementOI;
  46. private BooleanWritable result;
  47. @Override
  48. public ObjectInspector initialize(ObjectInspector[] arguments)
  49. throws UDFArgumentException {
  50. // Check if two arguments were passed
  51. if (arguments.length != ARG_COUNT) {
  52. throw new UDFArgumentException(
  53. "The function " + FUNC_NAME + " accepts "
  54. + ARG_COUNT + " arguments.");
  55. }
  56. // Check if ARRAY_IDX argument is of category LIST
  57. if (!arguments[ARRAY_IDX].getCategory().equals(Category.LIST)) {
  58. throw new UDFArgumentTypeException(ARRAY_IDX,
  59. "\"" + org.apache.hadoop.hive.serde.Constants.LIST_TYPE_NAME + "\" "
  60. + "expected at function ARRAY_CONTAINS, but "
  61. + "\"" + arguments[ARRAY_IDX].getTypeName() + "\" "
  62. + "is found");
  63. }
  64. arrayOI = (ListObjectInspector) arguments[ARRAY_IDX];
  65. arrayElementOI = arrayOI.getListElementObjectInspector();
  66. valueOI = arguments[VALUE_IDX];
  67. // Check if list element and value are of same type
  68. if (!ObjectInspectorUtils.compareTypes(arrayElementOI, valueOI)) {
  69. throw new UDFArgumentTypeException(VALUE_IDX,
  70. "\"" + arrayElementOI.getTypeName() + "\""
  71. + " expected at function ARRAY_CONTAINS, but "
  72. + "\"" + valueOI.getTypeName() + "\""
  73. + " is found");
  74. }
  75. // Check if the comparison is supported for this type
  76. if (!ObjectInspectorUtils.compareSupported(valueOI)) {
  77. throw new UDFArgumentException("The function " + FUNC_NAME
  78. + " does not support comparison for "
  79. + "\"" + valueOI.getTypeName() + "\""
  80. + " types");
  81. }
  82. result = new BooleanWritable(false);
  83. return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
  84. }
  85. @Override
  86. public Object evaluate(DeferredObject[] arguments) throws HiveException {
  87. result.set(false);
  88. Object array = arguments[ARRAY_IDX].get();
  89. Object value = arguments[VALUE_IDX].get();
  90. int arrayLength = arrayOI.getListLength(array);
  91. // Check if array is null or empty or value is null
  92. if (value == null || arrayLength <= 0) {
  93. return result;
  94. }
  95. // Compare the value to each element of array until a match is found
  96. for (int i=0; i<arrayLength; ++i) {
  97. Object listElement = arrayOI.getListElement(array, i);
  98. if (listElement != null) {
  99. if (ObjectInspectorUtils.compare(value, valueOI,
  100. listElement, arrayElementOI) == 0) {
  101. result.set(true);
  102. break;
  103. }
  104. }
  105. }
  106. return result;
  107. }
  108. @Override
  109. public String getDisplayString(String[] children) {
  110. assert (children.length == ARG_COUNT);
  111. return "array_contains(" + children[ARRAY_IDX] + ", "
  112. + children[VALUE_IDX] + ")";
  113. }
  114. }