PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeGenericFuncDesc.java

#
Java | 192 lines | 131 code | 27 blank | 34 comment | 21 complexity | 686dceb2e2f95ff87ffb519b87b504b1 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.plan;
  19. import java.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.apache.hadoop.hive.ql.exec.FunctionRegistry;
  23. import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
  24. import org.apache.hadoop.hive.ql.exec.Utilities;
  25. import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
  26. import org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge;
  27. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  28. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  29. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
  30. /**
  31. * Describes a GenericFunc node.
  32. */
  33. public class ExprNodeGenericFuncDesc extends ExprNodeDesc implements
  34. Serializable {
  35. private static final long serialVersionUID = 1L;
  36. /**
  37. * In case genericUDF is Serializable, we will serialize the object.
  38. *
  39. * In case genericUDF does not implement Serializable, Java will remember the
  40. * class of genericUDF and creates a new instance when deserialized. This is
  41. * exactly what we want.
  42. */
  43. private GenericUDF genericUDF;
  44. private List<ExprNodeDesc> childExprs;
  45. public ExprNodeGenericFuncDesc() {
  46. }
  47. public ExprNodeGenericFuncDesc(TypeInfo typeInfo, GenericUDF genericUDF,
  48. List<ExprNodeDesc> children) {
  49. super(typeInfo);
  50. assert (genericUDF != null);
  51. this.genericUDF = genericUDF;
  52. childExprs = children;
  53. }
  54. public GenericUDF getGenericUDF() {
  55. return genericUDF;
  56. }
  57. public void setGenericUDF(GenericUDF genericUDF) {
  58. this.genericUDF = genericUDF;
  59. }
  60. public List<ExprNodeDesc> getChildExprs() {
  61. return childExprs;
  62. }
  63. public void setChildExprs(List<ExprNodeDesc> children) {
  64. childExprs = children;
  65. }
  66. @Override
  67. public List<ExprNodeDesc> getChildren() {
  68. return childExprs;
  69. }
  70. @Override
  71. public String toString() {
  72. StringBuilder sb = new StringBuilder();
  73. sb.append(genericUDF.getClass().toString());
  74. sb.append("(");
  75. for (int i = 0; i < childExprs.size(); i++) {
  76. if (i > 0) {
  77. sb.append(", ");
  78. }
  79. sb.append(childExprs.get(i).toString());
  80. }
  81. sb.append("(");
  82. sb.append(")");
  83. return sb.toString();
  84. }
  85. @Explain(displayName = "expr")
  86. @Override
  87. public String getExprString() {
  88. // Get the children expr strings
  89. String[] childrenExprStrings = new String[childExprs.size()];
  90. for (int i = 0; i < childrenExprStrings.length; i++) {
  91. childrenExprStrings[i] = childExprs.get(i).getExprString();
  92. }
  93. return genericUDF.getDisplayString(childrenExprStrings);
  94. }
  95. @Override
  96. public List<String> getCols() {
  97. List<String> colList = new ArrayList<String>();
  98. if (childExprs != null) {
  99. int pos = 0;
  100. while (pos < childExprs.size()) {
  101. List<String> colCh = childExprs.get(pos).getCols();
  102. colList = Utilities.mergeUniqElems(colList, colCh);
  103. pos++;
  104. }
  105. }
  106. return colList;
  107. }
  108. @Override
  109. public ExprNodeDesc clone() {
  110. List<ExprNodeDesc> cloneCh = new ArrayList<ExprNodeDesc>(childExprs.size());
  111. for (ExprNodeDesc ch : childExprs) {
  112. cloneCh.add(ch.clone());
  113. }
  114. ExprNodeGenericFuncDesc clone = new ExprNodeGenericFuncDesc(typeInfo,
  115. FunctionRegistry.cloneGenericUDF(genericUDF), cloneCh);
  116. return clone;
  117. }
  118. /**
  119. * Create a exprNodeGenericFuncDesc based on the genericUDFClass and the
  120. * children parameters.
  121. *
  122. * @throws UDFArgumentException
  123. */
  124. public static ExprNodeGenericFuncDesc newInstance(GenericUDF genericUDF,
  125. List<ExprNodeDesc> children) throws UDFArgumentException {
  126. ObjectInspector[] childrenOIs = new ObjectInspector[children.size()];
  127. for (int i = 0; i < childrenOIs.length; i++) {
  128. childrenOIs[i] = TypeInfoUtils
  129. .getStandardWritableObjectInspectorFromTypeInfo(children.get(i)
  130. .getTypeInfo());
  131. }
  132. ObjectInspector oi = genericUDF.initialize(childrenOIs);
  133. return new ExprNodeGenericFuncDesc(TypeInfoUtils
  134. .getTypeInfoFromObjectInspector(oi), genericUDF, children);
  135. }
  136. @Override
  137. public boolean isSame(Object o) {
  138. if (!(o instanceof ExprNodeGenericFuncDesc)) {
  139. return false;
  140. }
  141. ExprNodeGenericFuncDesc dest = (ExprNodeGenericFuncDesc) o;
  142. if (!typeInfo.equals(dest.getTypeInfo())
  143. || !genericUDF.getClass().equals(dest.getGenericUDF().getClass())) {
  144. return false;
  145. }
  146. if (genericUDF instanceof GenericUDFBridge) {
  147. GenericUDFBridge bridge = (GenericUDFBridge) genericUDF;
  148. GenericUDFBridge bridge2 = (GenericUDFBridge) dest.getGenericUDF();
  149. if (!bridge.getUdfClass().equals(bridge2.getUdfClass())
  150. || !bridge.getUdfName().equals(bridge2.getUdfName())
  151. || bridge.isOperator() != bridge2.isOperator()) {
  152. return false;
  153. }
  154. }
  155. if (childExprs.size() != dest.getChildExprs().size()) {
  156. return false;
  157. }
  158. for (int pos = 0; pos < childExprs.size(); pos++) {
  159. if (!childExprs.get(pos).isSame(dest.getChildExprs().get(pos))) {
  160. return false;
  161. }
  162. }
  163. return true;
  164. }
  165. }