PageRenderTime 75ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 155 lines | 115 code | 17 blank | 23 comment | 22 complexity | 895e4f3b670bb2a79d0659fb3021b8f3 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.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver;
  23. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  24. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
  25. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
  26. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter;
  27. import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector;
  28. import org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector;
  29. import org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector;
  30. import org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector;
  31. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
  32. import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
  33. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  34. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
  35. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
  36. import org.apache.hadoop.io.BooleanWritable;
  37. /**
  38. * GenericUDF Base Class for operations.
  39. */
  40. @Description(name = "op", value = "a op b - Returns the result of operation")
  41. public abstract class GenericUDFBaseCompare extends GenericUDF {
  42. public enum CompareType {
  43. // Now only string, text, int, long, byte and boolean comparisons are
  44. // treated as special cases.
  45. // For other types, we reuse ObjectInspectorUtils.compare()
  46. COMPARE_STRING, COMPARE_TEXT, COMPARE_INT, COMPARE_LONG, COMPARE_BYTE,
  47. COMPARE_BOOL, SAME_TYPE, NEED_CONVERT
  48. }
  49. protected String opName;
  50. protected String opDisplayName;
  51. protected ObjectInspector[] argumentOIs;
  52. protected ReturnObjectInspectorResolver conversionHelper = null;
  53. protected ObjectInspector compareOI;
  54. protected CompareType compareType;
  55. protected Converter converter0, converter1;
  56. protected StringObjectInspector soi0, soi1;
  57. protected IntObjectInspector ioi0, ioi1;
  58. protected LongObjectInspector loi0, loi1;
  59. protected ByteObjectInspector byoi0, byoi1;
  60. protected BooleanObjectInspector boi0,boi1;
  61. protected final BooleanWritable result = new BooleanWritable();
  62. @Override
  63. public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
  64. if (arguments.length != 2) {
  65. throw new UDFArgumentException(
  66. opName + " requires two arguments.");
  67. }
  68. argumentOIs = arguments;
  69. for (int i = 0; i < arguments.length; i++) {
  70. Category category = arguments[i].getCategory();
  71. if (category != Category.PRIMITIVE) {
  72. throw new UDFArgumentTypeException(i, "The "
  73. + GenericUDFUtils.getOrdinal(i + 1)
  74. + " argument of " + opName + " is expected to a "
  75. + Category.PRIMITIVE.toString().toLowerCase() + " type, but "
  76. + category.toString().toLowerCase() + " is found");
  77. }
  78. }
  79. if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
  80. TypeInfoFactory.stringTypeInfo) &&
  81. TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
  82. TypeInfoFactory.stringTypeInfo)) {
  83. soi0 = (StringObjectInspector) arguments[0];
  84. soi1 = (StringObjectInspector) arguments[1];
  85. if (soi0.preferWritable() || soi1.preferWritable()) {
  86. compareType = CompareType.COMPARE_TEXT;
  87. } else {
  88. compareType = CompareType.COMPARE_STRING;
  89. }
  90. } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
  91. TypeInfoFactory.intTypeInfo) &&
  92. TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
  93. TypeInfoFactory.intTypeInfo)) {
  94. compareType = CompareType.COMPARE_INT;
  95. ioi0 = (IntObjectInspector) arguments[0];
  96. ioi1 = (IntObjectInspector) arguments[1];
  97. } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
  98. TypeInfoFactory.longTypeInfo) &&
  99. TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
  100. TypeInfoFactory.longTypeInfo)) {
  101. compareType = CompareType.COMPARE_LONG;
  102. loi0 = (LongObjectInspector) arguments[0];
  103. loi1 = (LongObjectInspector) arguments[1];
  104. } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
  105. TypeInfoFactory.byteTypeInfo) &&
  106. TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
  107. TypeInfoFactory.byteTypeInfo)) {
  108. compareType = CompareType.COMPARE_BYTE;
  109. byoi0 = (ByteObjectInspector) arguments[0];
  110. byoi1 = (ByteObjectInspector) arguments[1];
  111. } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
  112. TypeInfoFactory.booleanTypeInfo) &&
  113. TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
  114. TypeInfoFactory.booleanTypeInfo)) {
  115. compareType = CompareType.COMPARE_BOOL;
  116. boi0 = (BooleanObjectInspector) arguments[0];
  117. boi1 = (BooleanObjectInspector) arguments[1];
  118. } else {
  119. TypeInfo oiTypeInfo0 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]);
  120. TypeInfo oiTypeInfo1 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]);
  121. if (oiTypeInfo0 != oiTypeInfo1) {
  122. compareType = CompareType.NEED_CONVERT;
  123. compareOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(
  124. TypeInfoFactory.doubleTypeInfo);
  125. converter0 = ObjectInspectorConverters.getConverter(arguments[0], compareOI);
  126. converter1 = ObjectInspectorConverters.getConverter(arguments[1], compareOI);
  127. } else {
  128. compareType = CompareType.SAME_TYPE;
  129. }
  130. }
  131. return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
  132. }
  133. @Override
  134. public String getDisplayString(String[] children) {
  135. assert (children.length == 2);
  136. return "(" + children[0] + " " + opDisplayName + " " + children[1] + ")";
  137. }
  138. }