PageRenderTime 49ms CodeModel.GetById 13ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 1ms

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