Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.hadoop.hive.ql.ppd;
 
 import java.util.List;
 import java.util.Map;
 
Expression factory for predicate pushdown processing. Each processor determines whether the expression is a possible candidate for predicate pushdown optimization for the given operator
 
 public final class ExprWalkerProcFactory {

  
ColumnExprProcessor.
 
   public static class ColumnExprProcessor implements NodeProcessor {

    
Converts the reference from child row resolver to current row resolver.
 
     @Override
     public Object process(Node ndStack<NodestackNodeProcessorCtx procCtx,
         Object... nodeOutputsthrows SemanticException {
       ExprWalkerInfo ctx = (ExprWalkerInfoprocCtx;
       ExprNodeColumnDesc colref = (ExprNodeColumnDescnd;
       RowResolver toRR = ctx.getToRR();
       Operator<? extends Serializableop = ctx.getOp();
       String[] colAlias = toRR.reverseLookup(colref.getColumn());
 
       if (op.getColumnExprMap() != null) {
         // replace the output expression with the input expression so that
         // parent op can understand this expression
         ExprNodeDesc exp = op.getColumnExprMap().get(colref.getColumn());
         if (exp == null) {
           // means that expression can't be pushed either because it is value in
           // group by
           ctx.setIsCandidate(colreffalse);
           return false;
         }
         ctx.addConvertedNode(colrefexp);
         ctx.setIsCandidate(exptrue);
         ctx.addAlias(expcolAlias[0]);
       } else {
         if (colAlias == null) {
           assert false;
         }
         ctx.addAlias(colrefcolAlias[0]);
       }
       ctx.setIsCandidate(colreftrue);
       return true;
     }
 
   }

  
FieldExprProcessor.
 
   public static class FieldExprProcessor implements NodeProcessor {
    @Override
    public Object process(Node ndStack<NodestackNodeProcessorCtx procCtx,
        Object... nodeOutputsthrows SemanticException {
      ExprWalkerInfo ctx = (ExprWalkerInfoprocCtx;
      String alias = null;
      ExprNodeFieldDesc expr = (ExprNodeFieldDescnd;
      boolean isCandidate = true;
      assert (nd.getChildren().size() == 1);
      ExprNodeDesc ch = (ExprNodeDescnd.getChildren().get(0);
      ExprNodeDesc newCh = ctx.getConvertedNode(ch);
      if (newCh != null) {
        expr.setDesc(newCh);
        ch = newCh;
      }
      String chAlias = ctx.getAlias(ch);
      isCandidate = isCandidate && ctx.isCandidate(ch);
      // need to iterate through all children even if one is found to be not a
      // candidate
      // in case if the other children could be individually pushed up
      if (isCandidate && chAlias != null) {
        if (alias == null) {
          alias = chAlias;
        } else if (!chAlias.equalsIgnoreCase(alias)) {
          isCandidate = false;
        }
      }
      ctx.addAlias(expralias);
      ctx.setIsCandidate(exprisCandidate);
      return isCandidate;
    }
  }

  
If all children are candidates and refer only to one table alias then this expr is a candidate else it is not a candidate but its children could be final candidates.
  public static class GenericFuncExprProcessor implements NodeProcessor {
    @Override
    public Object process(Node ndStack<NodestackNodeProcessorCtx procCtx,
        Object... nodeOutputsthrows SemanticException {
      ExprWalkerInfo ctx = (ExprWalkerInfoprocCtx;
      String alias = null;
      if (!FunctionRegistry.isDeterministic(expr.getGenericUDF())) {
        // this GenericUDF can't be pushed down
        ctx.setIsCandidate(exprfalse);
        ctx.setDeterministic(false);
        return false;
      }
      boolean isCandidate = true;
      for (int i = 0; i < nd.getChildren().size(); i++) {
        ExprNodeDesc ch = (ExprNodeDescnd.getChildren().get(i);
        ExprNodeDesc newCh = ctx.getConvertedNode(ch);
        if (newCh != null) {
          expr.getChildExprs().set(inewCh);
          ch = newCh;
        }
        String chAlias = ctx.getAlias(ch);
        isCandidate = isCandidate && ctx.isCandidate(ch);
        // need to iterate through all children even if one is found to be not a
        // candidate
        // in case if the other children could be individually pushed up
        if (isCandidate && chAlias != null) {
          if (alias == null) {
            alias = chAlias;
          } else if (!chAlias.equalsIgnoreCase(alias)) {
            isCandidate = false;
          }
        }
        if (!isCandidate) {
          break;
        }
      }
      ctx.addAlias(expralias);
      ctx.setIsCandidate(exprisCandidate);
      return isCandidate;
    }
  }

  
For constants and null expressions.
  public static class DefaultExprProcessor implements NodeProcessor {
    @Override
    public Object process(Node ndStack<NodestackNodeProcessorCtx procCtx,
        Object... nodeOutputsthrows SemanticException {
      ExprWalkerInfo ctx = (ExprWalkerInfoprocCtx;
      ctx.setIsCandidate((ExprNodeDescndtrue);
      return true;
    }
  }
  public static NodeProcessor getDefaultExprProcessor() {
    return new DefaultExprProcessor();
  }
  public static NodeProcessor getGenericFuncProcessor() {
    return new GenericFuncExprProcessor();
  }
  public static NodeProcessor getColumnProcessor() {
    return new ColumnExprProcessor();
  }
  private static NodeProcessor getFieldProcessor() {
    return new FieldExprProcessor();
  }
  public static ExprWalkerInfo extractPushdownPreds(OpWalkerInfo opContext,
      Operator<? extends SerializableopExprNodeDesc pred)
      throws SemanticException {
    List<ExprNodeDescpreds = new ArrayList<ExprNodeDesc>();
    preds.add(pred);
    return extractPushdownPreds(opContextoppreds);
  }

  
Extracts pushdown predicates from the given list of predicate expression.

Parameters:
opContext operator context used for resolving column references
op operator of the predicates being processed
preds
Returns:
The expression walker information
Throws:
org.apache.hadoop.hive.ql.parse.SemanticException
  public static ExprWalkerInfo extractPushdownPreds(OpWalkerInfo opContext,
      Operator<? extends SerializableopList<ExprNodeDescpreds)
      throws SemanticException {
    // Create the walker, the rules dispatcher and the context.
    ExprWalkerInfo exprContext = new ExprWalkerInfo(opopContext
        .getRowResolver(op));
    // create a walker which walks the tree in a DFS manner while maintaining
    // the operator stack. The dispatcher
    // generates the plan from the operator tree
    Map<RuleNodeProcessorexprRules = new LinkedHashMap<RuleNodeProcessor>();
    exprRules.put(
        new RuleRegExp("R1"ExprNodeColumnDesc.class.getName() + "%"),
        getColumnProcessor());
    exprRules.put(
        new RuleRegExp("R2"ExprNodeFieldDesc.class.getName() + "%"),
        getFieldProcessor());
    exprRules.put(new RuleRegExp("R3"ExprNodeGenericFuncDesc.class.getName()
        + "%"), getGenericFuncProcessor());
    // The dispatcher fires the processor corresponding to the closest matching
    // rule and passes the context along
        exprRulesexprContext);
    GraphWalker egw = new DefaultGraphWalker(disp);
    List<NodestartNodes = new ArrayList<Node>();
    List<ExprNodeDescclonedPreds = new ArrayList<ExprNodeDesc>();
    for (ExprNodeDesc node : preds) {
      clonedPreds.add(node.clone());
    }
    startNodes.addAll(clonedPreds);
    egw.startWalking(startNodesnull);
    // check the root expression for final candidates
    for (ExprNodeDesc pred : clonedPreds) {
      extractFinalCandidates(predexprContext);
    }
    return exprContext;
  }

  
Walks through the top AND nodes and determine which of them are final candidates.
  private static void extractFinalCandidates(ExprNodeDesc expr,
      ExprWalkerInfo ctx) {
    if (ctx.isCandidate(expr)) {
      ctx.addFinalCandidate(expr);
      return;
    }
    if (FunctionRegistry.isOpAnd(expr)) {
      // If the operator is AND, we need to determine if any of the children are
      // final candidates.
      for (Node ch : expr.getChildren()) {
        extractFinalCandidates((ExprNodeDescchctx);
      }
    }
  }
  private ExprWalkerProcFactory() {
    // prevent instantiation
  }
New to GrepCode? Check out our FAQ X