PageRenderTime 25ms CodeModel.GetById 10ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ppr/OpProcFactory.java

#
Java | 143 lines | 76 code | 21 blank | 46 comment | 9 complexity | 4cf9738a30aa8109adadfe9d5ac83a24 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.optimizer.ppr;
 20
 21import java.util.Map;
 22import java.util.Stack;
 23
 24import org.apache.hadoop.hive.ql.exec.FilterOperator;
 25import org.apache.hadoop.hive.ql.exec.TableScanOperator;
 26import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 27import org.apache.hadoop.hive.ql.lib.Node;
 28import org.apache.hadoop.hive.ql.lib.NodeProcessor;
 29import org.apache.hadoop.hive.ql.lib.NodeProcessorCtx;
 30import org.apache.hadoop.hive.ql.parse.SemanticException;
 31import org.apache.hadoop.hive.ql.parse.TypeCheckProcFactory;
 32import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
 33
 34/**
 35 * Operator factory for partition pruning processing of operator graph We find
 36 * all the filter operators that appear just beneath the table scan operators.
 37 * We then pass the filter to the partition pruner to construct a pruner for
 38 * that table alias and store a mapping from the table scan operator to that
 39 * pruner. We call that pruner later during plan generation.
 40 */
 41public final class OpProcFactory {
 42
 43  /**
 44   * Determines the partition pruner for the filter. This is called only when
 45   * the filter follows a table scan operator.
 46   */
 47  public static class FilterPPR implements NodeProcessor {
 48
 49    @Override
 50    public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
 51        Object... nodeOutputs) throws SemanticException {
 52      OpWalkerCtx owc = (OpWalkerCtx) procCtx;
 53      FilterOperator fop = (FilterOperator) nd;
 54      FilterOperator fop2 = null;
 55
 56      // The stack contains either ... TS, Filter or
 57      // ... TS, Filter, Filter with the head of the stack being the rightmost
 58      // symbol. So we just pop out the two elements from the top and if the
 59      // second one of them is not a table scan then the operator on the top of
 60      // the stack is the Table scan operator.
 61      Node tmp = stack.pop();
 62      Node tmp2 = stack.pop();
 63      TableScanOperator top = null;
 64      if (tmp2 instanceof TableScanOperator) {
 65        top = (TableScanOperator) tmp2;
 66      } else {
 67        top = (TableScanOperator) stack.peek();
 68        fop2 = (FilterOperator) tmp2;
 69      }
 70      stack.push(tmp2);
 71      stack.push(tmp);
 72
 73      // If fop2 exists (i.e this is not the top level filter and fop2 is not
 74      // a sampling filter then we ignore the current filter
 75      if (fop2 != null && !fop2.getConf().getIsSamplingPred()) {
 76        return null;
 77      }
 78
 79      // ignore the predicate in case it is not a sampling predicate
 80      if (fop.getConf().getIsSamplingPred()) {
 81        return null;
 82      }
 83
 84      // Otherwise this is not a sampling predicate and we need to
 85      ExprNodeDesc predicate = fop.getConf().getPredicate();
 86      String alias = top.getConf().getAlias();
 87
 88      // Generate the partition pruning predicate
 89      boolean hasNonPartCols = false;
 90      ExprNodeDesc ppr_pred = ExprProcFactory.genPruner(alias, predicate,
 91          hasNonPartCols);
 92      owc.addHasNonPartCols(hasNonPartCols);
 93
 94      // Add the pruning predicate to the table scan operator
 95      addPruningPred(owc.getOpToPartPruner(), top, ppr_pred);
 96
 97      return null;
 98    }
 99
100    private void addPruningPred(Map<TableScanOperator, ExprNodeDesc> opToPPR,
101        TableScanOperator top, ExprNodeDesc new_ppr_pred) throws UDFArgumentException {
102      ExprNodeDesc old_ppr_pred = opToPPR.get(top);
103      ExprNodeDesc ppr_pred = null;
104      if (old_ppr_pred != null) {
105        // or the old_ppr_pred and the new_ppr_pred
106        ppr_pred = TypeCheckProcFactory.DefaultExprProcessor
107            .getFuncExprNodeDesc("OR", old_ppr_pred, new_ppr_pred);
108      } else {
109        ppr_pred = new_ppr_pred;
110      }
111
112      // Put the mapping from table scan operator to ppr_pred
113      opToPPR.put(top, ppr_pred);
114
115      return;
116    }
117  }
118
119  /**
120   * Default processor which just merges its children.
121   */
122  public static class DefaultPPR implements NodeProcessor {
123
124    @Override
125    public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
126        Object... nodeOutputs) throws SemanticException {
127      // Nothing needs to be done.
128      return null;
129    }
130  }
131
132  public static NodeProcessor getFilterProc() {
133    return new FilterPPR();
134  }
135
136  public static NodeProcessor getDefaultProc() {
137    return new DefaultPPR();
138  }
139
140  private OpProcFactory() {
141    // prevent instantiation
142  }
143}