diff --git ql/src/java/org/apache/hadoop/hive/ql/lib/DefaultGraphWalker.java ql/src/java/org/apache/hadoop/hive/ql/lib/DefaultGraphWalker.java index 583c113..9b4eb85 100644 --- ql/src/java/org/apache/hadoop/hive/ql/lib/DefaultGraphWalker.java +++ ql/src/java/org/apache/hadoop/hive/ql/lib/DefaultGraphWalker.java @@ -22,7 +22,9 @@ import java.util.Collection; import java.util.HashMap; import java.util.IdentityHashMap; +import java.util.LinkedList; import java.util.List; +import java.util.Queue; import java.util.Set; import java.util.Stack; @@ -36,7 +38,8 @@ */ public class DefaultGraphWalker implements GraphWalker { - protected Stack opStack; + protected final Stack opStack; + protected final Queue opQueue; protected final List toWalk = new ArrayList(); protected final IdentityHashMap retMap = new IdentityHashMap(); protected final Dispatcher dispatcher; @@ -50,13 +53,7 @@ public DefaultGraphWalker(Dispatcher disp) { dispatcher = disp; opStack = new Stack(); - } - - /** - * @return the toWalk - */ - public List getToWalk() { - return toWalk; + opQueue = new LinkedList(); } /** @@ -112,6 +109,14 @@ public void startWalking(Collection startNodes, nodeOutput.put(nd, retMap.get(nd)); } } + + // Store results + while (!opQueue.isEmpty()) { + Node nd = opQueue.poll(); + if (nodeOutput != null && getDispatchedList().contains(nd)) { + nodeOutput.put(nd, retMap.get(nd)); + } + } } /** @@ -121,23 +126,30 @@ public void startWalking(Collection startNodes, * current operator in the graph * @throws SemanticException */ - public void walk(Node nd) throws SemanticException { - if (opStack.empty() || nd != opStack.peek()) { - opStack.push(nd); - } + public void walk(Node nd) throws SemanticException { + // Push the node in the stack + opStack.push(nd); + + while (!opStack.empty()) { + Node node = opStack.peek(); - if ((nd.getChildren() == null) - || getDispatchedList().containsAll(nd.getChildren())) { - // all children are done or no need to walk the children - if (!getDispatchedList().contains(nd)) { - dispatch(nd, opStack); + if (node.getChildren() == null || + getDispatchedList().containsAll(node.getChildren())) { + // Dispatch current node + if (!getDispatchedList().contains(node)) { + dispatch(node, opStack); + opQueue.add(node); + } + opStack.pop(); + continue; } - opStack.pop(); - return; - } - // add children, self to the front of the queue in that order - getToWalk().add(0, nd); - getToWalk().removeAll(nd.getChildren()); - getToWalk().addAll(0, nd.getChildren()); + + for (Node childNode : node.getChildren()) { + if (!getDispatchedList().contains(childNode)) { + opStack.push(childNode); + } + } // end for + } // end while } + } diff --git ql/src/java/org/apache/hadoop/hive/ql/lib/ForwardWalker.java ql/src/java/org/apache/hadoop/hive/ql/lib/ForwardWalker.java index a2db3b5..67b4700 100644 --- ql/src/java/org/apache/hadoop/hive/ql/lib/ForwardWalker.java +++ ql/src/java/org/apache/hadoop/hive/ql/lib/ForwardWalker.java @@ -19,20 +19,17 @@ package org.apache.hadoop.hive.ql.lib; import org.apache.hadoop.hive.ql.exec.Operator; -import org.apache.hadoop.hive.ql.lib.DefaultGraphWalker; -import org.apache.hadoop.hive.ql.lib.Dispatcher; -import org.apache.hadoop.hive.ql.lib.Node; import org.apache.hadoop.hive.ql.parse.SemanticException; import org.apache.hadoop.hive.ql.plan.OperatorDesc; public class ForwardWalker extends DefaultGraphWalker { /** -* Constructor. -* -* @param disp -* dispatcher to call for each op encountered -*/ + * Constructor. + * + * @param disp + * dispatcher to call for each op encountered + */ public ForwardWalker(Dispatcher disp) { super(disp); } @@ -54,17 +51,17 @@ protected boolean allParentsDispatched(Node nd) { @SuppressWarnings("unchecked") protected void addAllParents(Node nd) { Operator op = (Operator) nd; - getToWalk().removeAll(op.getParentOperators()); - getToWalk().addAll(0, op.getParentOperators()); + toWalk.removeAll(op.getParentOperators()); + toWalk.addAll(0, op.getParentOperators()); } /** -* walk the current operator and its descendants. -* -* @param nd -* current operator in the graph -* @throws SemanticException -*/ + * walk the current operator and its descendants. + * + * @param nd + * current operator in the graph + * @throws SemanticException + */ @Override public void walk(Node nd) throws SemanticException { if (opStack.empty() || nd != opStack.peek()) { @@ -73,14 +70,14 @@ public void walk(Node nd) throws SemanticException { if (allParentsDispatched(nd)) { // all children are done or no need to walk the children if (!getDispatchedList().contains(nd)) { - getToWalk().addAll(nd.getChildren()); + toWalk.addAll(nd.getChildren()); dispatch(nd, opStack); } opStack.pop(); return; } // add children, self to the front of the queue in that order - getToWalk().add(0, nd); + toWalk.add(0, nd); addAllParents(nd); } } \ No newline at end of file diff --git ql/src/java/org/apache/hadoop/hive/ql/optimizer/ColumnPruner.java ql/src/java/org/apache/hadoop/hive/ql/optimizer/ColumnPruner.java index 9a45458..735b448 100644 --- ql/src/java/org/apache/hadoop/hive/ql/optimizer/ColumnPruner.java +++ ql/src/java/org/apache/hadoop/hive/ql/optimizer/ColumnPruner.java @@ -174,10 +174,10 @@ public void walk(Node nd) throws SemanticException { return; } // move all the children to the front of queue - getToWalk().removeAll(nd.getChildren()); - getToWalk().addAll(0, nd.getChildren()); + toWalk.removeAll(nd.getChildren()); + toWalk.addAll(0, nd.getChildren()); // add self to the end of the queue - getToWalk().add(nd); + toWalk.add(nd); opStack.pop(); } } diff --git ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagate.java ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagate.java index dd53ced..b6f1f27 100644 --- ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagate.java +++ ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagate.java @@ -151,17 +151,17 @@ public void walk(Node nd) throws SemanticException { dispatch(nd, opStack); opStack.pop(); } else { - getToWalk().removeAll(parents); - getToWalk().add(0, nd); - getToWalk().addAll(0, parents); + toWalk.removeAll(parents); + toWalk.add(0, nd); + toWalk.addAll(0, parents); return; } // move all the children to the front of queue List children = nd.getChildren(); if (children != null) { - getToWalk().removeAll(children); - getToWalk().addAll(children); + toWalk.removeAll(children); + toWalk.addAll(children); } } }