PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFBridge.java

#
Java | 193 lines | 129 code | 33 blank | 31 comment | 15 complexity | 995d5102640b898956c43dac390687e1 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.io.Serializable;
  20. import java.lang.reflect.Method;
  21. import java.lang.reflect.Type;
  22. import java.util.Arrays;
  23. import org.apache.hadoop.hive.ql.exec.FunctionRegistry;
  24. import org.apache.hadoop.hive.ql.exec.UDAF;
  25. import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
  26. import org.apache.hadoop.hive.ql.metadata.HiveException;
  27. import org.apache.hadoop.hive.ql.parse.SemanticException;
  28. import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ConversionHelper;
  29. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  30. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
  31. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.ObjectInspectorOptions;
  32. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  33. import org.apache.hadoop.util.ReflectionUtils;
  34. /**
  35. * This class is a bridge between GenericUDAF and UDAF. Old UDAF can be used
  36. * with the GenericUDAF infrastructure through this bridge.
  37. */
  38. public class GenericUDAFBridge extends AbstractGenericUDAFResolver {
  39. UDAF udaf;
  40. public GenericUDAFBridge(UDAF udaf) {
  41. this.udaf = udaf;
  42. }
  43. public Class<? extends UDAF> getUDAFClass() {
  44. return udaf.getClass();
  45. }
  46. @Override
  47. public GenericUDAFEvaluator getEvaluator(TypeInfo[] parameters) throws SemanticException {
  48. Class<? extends UDAFEvaluator> udafEvaluatorClass = udaf.getResolver()
  49. .getEvaluatorClass(Arrays.asList(parameters));
  50. return new GenericUDAFBridgeEvaluator(udafEvaluatorClass);
  51. }
  52. /**
  53. * GenericUDAFBridgeEvaluator.
  54. *
  55. */
  56. public static class GenericUDAFBridgeEvaluator extends GenericUDAFEvaluator
  57. implements Serializable {
  58. private static final long serialVersionUID = 1L;
  59. // Used by serialization only
  60. public GenericUDAFBridgeEvaluator() {
  61. }
  62. public Class<? extends UDAFEvaluator> getUdafEvaluator() {
  63. return udafEvaluator;
  64. }
  65. public void setUdafEvaluator(Class<? extends UDAFEvaluator> udafEvaluator) {
  66. this.udafEvaluator = udafEvaluator;
  67. }
  68. public GenericUDAFBridgeEvaluator(
  69. Class<? extends UDAFEvaluator> udafEvaluator) {
  70. this.udafEvaluator = udafEvaluator;
  71. }
  72. Class<? extends UDAFEvaluator> udafEvaluator;
  73. transient ObjectInspector[] parameterOIs;
  74. transient Object result;
  75. transient Method iterateMethod;
  76. transient Method mergeMethod;
  77. transient Method terminatePartialMethod;
  78. transient Method terminateMethod;
  79. transient ConversionHelper conversionHelper;
  80. @Override
  81. public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException {
  82. super.init(m, parameters);
  83. parameterOIs = parameters;
  84. // Get the reflection methods from ue
  85. for (Method method : udafEvaluator.getMethods()) {
  86. method.setAccessible(true);
  87. if (method.getName().equals("iterate")) {
  88. iterateMethod = method;
  89. }
  90. if (method.getName().equals("merge")) {
  91. mergeMethod = method;
  92. }
  93. if (method.getName().equals("terminatePartial")) {
  94. terminatePartialMethod = method;
  95. }
  96. if (method.getName().equals("terminate")) {
  97. terminateMethod = method;
  98. }
  99. }
  100. // Input: do Java/Writable conversion if needed
  101. Method aggregateMethod = null;
  102. if (mode == Mode.PARTIAL1 || mode == Mode.COMPLETE) {
  103. aggregateMethod = iterateMethod;
  104. } else {
  105. aggregateMethod = mergeMethod;
  106. }
  107. conversionHelper = new ConversionHelper(aggregateMethod, parameters);
  108. // Output: get the evaluate method
  109. Method evaluateMethod = null;
  110. if (mode == Mode.PARTIAL1 || mode == Mode.PARTIAL2) {
  111. evaluateMethod = terminatePartialMethod;
  112. } else {
  113. evaluateMethod = terminateMethod;
  114. }
  115. // Get the output ObjectInspector from the return type.
  116. Type returnType = evaluateMethod.getGenericReturnType();
  117. try {
  118. return ObjectInspectorFactory.getReflectionObjectInspector(returnType,
  119. ObjectInspectorOptions.JAVA);
  120. } catch (RuntimeException e) {
  121. throw new HiveException("Cannot recognize return type " + returnType
  122. + " from " + evaluateMethod, e);
  123. }
  124. }
  125. /** class for storing UDAFEvaluator value. */
  126. static class UDAFAgg implements AggregationBuffer {
  127. UDAFEvaluator ueObject;
  128. UDAFAgg(UDAFEvaluator ueObject) {
  129. this.ueObject = ueObject;
  130. }
  131. }
  132. @Override
  133. public AggregationBuffer getNewAggregationBuffer() {
  134. return new UDAFAgg((UDAFEvaluator)ReflectionUtils.newInstance(udafEvaluator, null));
  135. }
  136. @Override
  137. public void reset(AggregationBuffer agg) throws HiveException {
  138. ((UDAFAgg) agg).ueObject.init();
  139. }
  140. @Override
  141. public void iterate(AggregationBuffer agg, Object[] parameters) throws HiveException {
  142. FunctionRegistry.invoke(iterateMethod, ((UDAFAgg) agg).ueObject,
  143. conversionHelper.convertIfNecessary(parameters));
  144. }
  145. @Override
  146. public void merge(AggregationBuffer agg, Object partial) throws HiveException {
  147. FunctionRegistry.invoke(mergeMethod, ((UDAFAgg) agg).ueObject,
  148. conversionHelper.convertIfNecessary(partial));
  149. }
  150. @Override
  151. public Object terminate(AggregationBuffer agg) throws HiveException {
  152. return FunctionRegistry.invoke(terminateMethod, ((UDAFAgg) agg).ueObject);
  153. }
  154. @Override
  155. public Object terminatePartial(AggregationBuffer agg) throws HiveException {
  156. return FunctionRegistry.invoke(terminatePartialMethod,
  157. ((UDAFAgg) agg).ueObject);
  158. }
  159. }
  160. }