PageRenderTime 36ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/exec/UnionOperator.java

#
Java | 147 lines | 90 code | 20 blank | 37 comment | 14 complexity | a12e031acc05670f8466974aa3c51b98 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.exec;
 20
 21import java.io.Serializable;
 22import java.util.ArrayList;
 23import java.util.List;
 24
 25import org.apache.hadoop.conf.Configuration;
 26import org.apache.hadoop.hive.ql.metadata.HiveException;
 27import org.apache.hadoop.hive.ql.plan.UnionDesc;
 28import org.apache.hadoop.hive.ql.plan.api.OperatorType;
 29import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver;
 30import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 31import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
 32import org.apache.hadoop.hive.serde2.objectinspector.StructField;
 33import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
 34
 35/**
 36 * Union Operator Just forwards. Doesn't do anything itself.
 37 **/
 38public class UnionOperator extends Operator<UnionDesc> implements Serializable {
 39  private static final long serialVersionUID = 1L;
 40
 41  StructObjectInspector[] parentObjInspectors;
 42  List<? extends StructField>[] parentFields;
 43
 44  ReturnObjectInspectorResolver[] columnTypeResolvers;
 45  boolean[] needsTransform;
 46
 47  ArrayList<Object> outputRow;
 48
 49  /**
 50   * UnionOperator will transform the input rows if the inputObjInspectors from
 51   * different parents are different. If one parent has exactly the same
 52   * ObjectInspector as the output ObjectInspector, then we don't need to do
 53   * transformation for that parent. This information is recorded in
 54   * needsTransform[].
 55   */
 56  @Override
 57  protected void initializeOp(Configuration hconf) throws HiveException {
 58
 59    int parents = parentOperators.size();
 60    parentObjInspectors = new StructObjectInspector[parents];
 61    parentFields = new List[parents];
 62    for (int p = 0; p < parents; p++) {
 63      parentObjInspectors[p] = (StructObjectInspector) inputObjInspectors[p];
 64      parentFields[p] = parentObjInspectors[p].getAllStructFieldRefs();
 65    }
 66
 67    // Get columnNames from the first parent
 68    int columns = parentFields[0].size();
 69    ArrayList<String> columnNames = new ArrayList<String>(columns);
 70    for (int c = 0; c < columns; c++) {
 71      columnNames.add(parentFields[0].get(c).getFieldName());
 72    }
 73
 74    // Get outputFieldOIs
 75    columnTypeResolvers = new ReturnObjectInspectorResolver[columns];
 76    for (int c = 0; c < columns; c++) {
 77      columnTypeResolvers[c] = new ReturnObjectInspectorResolver();
 78    }
 79
 80    for (int p = 0; p < parents; p++) {
 81      assert (parentFields[p].size() == columns);
 82      for (int c = 0; c < columns; c++) {
 83        columnTypeResolvers[c].update(parentFields[p].get(c)
 84            .getFieldObjectInspector());
 85      }
 86    }
 87
 88    ArrayList<ObjectInspector> outputFieldOIs = new ArrayList<ObjectInspector>(
 89        columns);
 90    for (int c = 0; c < columns; c++) {
 91      outputFieldOIs.add(columnTypeResolvers[c].get());
 92    }
 93
 94    // create output row ObjectInspector
 95    outputObjInspector = ObjectInspectorFactory
 96        .getStandardStructObjectInspector(columnNames, outputFieldOIs);
 97    outputRow = new ArrayList<Object>(columns);
 98    for (int c = 0; c < columns; c++) {
 99      outputRow.add(null);
100    }
101
102    // whether we need to do transformation for each parent
103    needsTransform = new boolean[parents];
104    for (int p = 0; p < parents; p++) {
105      // Testing using != is good enough, because we use ObjectInspectorFactory
106      // to
107      // create ObjectInspectors.
108      needsTransform[p] = (inputObjInspectors[p] != outputObjInspector);
109      if (needsTransform[p]) {
110        LOG.info("Union Operator needs to transform row from parent[" + p
111            + "] from " + inputObjInspectors[p] + " to " + outputObjInspector);
112      }
113    }
114    initializeChildren(hconf);
115  }
116
117  @Override
118  public synchronized void processOp(Object row, int tag) throws HiveException {
119
120    StructObjectInspector soi = parentObjInspectors[tag];
121    List<? extends StructField> fields = parentFields[tag];
122
123    if (needsTransform[tag]) {
124      for (int c = 0; c < fields.size(); c++) {
125        outputRow.set(c, columnTypeResolvers[c].convertIfNecessary(soi
126            .getStructFieldData(row, fields.get(c)), fields.get(c)
127            .getFieldObjectInspector()));
128      }
129      forward(outputRow, outputObjInspector);
130    } else {
131      forward(row, inputObjInspectors[tag]);
132    }
133  }
134
135  /**
136   * @return the name of the operator
137   */
138  @Override
139  public String getName() {
140    return new String("UNION");
141  }
142
143  @Override
144  public OperatorType getType() {
145    return OperatorType.UNION;
146  }
147}