diff --git a/ql/src/java/org/apache/hadoop/hive/ql/lib/LevelOrderWalker.java b/ql/src/java/org/apache/hadoop/hive/ql/lib/LevelOrderWalker.java index e6a5d55..bccd9fb 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/lib/LevelOrderWalker.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/lib/LevelOrderWalker.java @@ -23,8 +23,10 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.Stack; +import org.apache.commons.collections.CollectionUtils; import org.apache.hadoop.hive.ql.exec.Operator; import org.apache.hadoop.hive.ql.parse.SemanticException; import org.apache.hadoop.hive.ql.plan.OperatorDesc; @@ -47,13 +49,15 @@ public class LevelOrderWalker extends DefaultGraphWalker { // Only specified nodes of these types will be walked. // Empty set means all the nodes will be walked. - private HashSet> nodeTypes = new HashSet>(); + private Set> nodeTypes = new HashSet<>(); // How many levels of ancestors to keep in the stack during dispatching private final int numLevels; /** - * Constructor with keeping all the ancestors in the operator stack during dispatching. + * Constructor with keeping all the ancestors in the operator stack during + * dispatching. + * * @param disp Dispatcher to call for each op encountered */ public LevelOrderWalker(Dispatcher disp) { @@ -62,9 +66,10 @@ public LevelOrderWalker(Dispatcher disp) { } /** - * Constructor with specified number of ancestor levels to keep in the operator - * stack during dispatching. - * @param disp Dispatcher to call for each op encountered + * Constructor with specified number of ancestor levels to keep in the + * operator stack during dispatching. + * + * @param disp Dispatcher to call for each op encountered * @param numLevels Number of ancestor levels */ public LevelOrderWalker(Dispatcher disp, int numLevels) { @@ -73,7 +78,7 @@ public LevelOrderWalker(Dispatcher disp, int numLevels) { } @SuppressWarnings("unchecked") - public void setNodeTypes(Class ...nodeTypes) { + public void setNodeTypes(Class... nodeTypes) { this.nodeTypes.addAll(Arrays.asList(nodeTypes)); } @@ -90,20 +95,18 @@ public void startWalking(Collection startNodes, // Starting from the startNodes, add the children whose parents have been // included in the list. - HashSet addedNodes = new HashSet(); - for (Node node : startNodes) { - addedNodes.add(node); - } + Set addedNodes = new HashSet<>(startNodes); int index = 0; - while(index < toWalk.size()) { - if (toWalk.get(index).getChildren() != null) { - for(Node child : toWalk.get(index).getChildren()) { + while (index < toWalk.size()) { + List children = toWalk.get(index).getChildren(); + if (CollectionUtils.isNotEmpty(children)) { + for (Node child : children) { Operator childOP = (Operator) child; - if (!addedNodes.contains(child) && - (childOP.getParentOperators() == null || - addedNodes.containsAll(childOP.getParentOperators()))) { + if (!addedNodes.contains(child) + && (childOP.getParentOperators() == null + || addedNodes.containsAll(childOP.getParentOperators()))) { toWalk.add(child); addedNodes.add(child); } @@ -112,7 +115,7 @@ public void startWalking(Collection startNodes, ++index; } - for(Node nd : toWalk) { + for (Node nd : toWalk) { if (!nodeTypes.isEmpty() && !nodeTypes.contains(nd.getClass())) { continue; } @@ -129,24 +132,26 @@ public void startWalking(Collection startNodes, /** * Enumerate numLevels of ancestors by putting them in the stack and dispatch * the current node. + * * @param nd current operator in the ancestor tree * @param level how many level of ancestors included in the stack * @param stack operator stack * @throws SemanticException */ @SuppressWarnings("unchecked") - private void walk(Node nd, int level, Stack stack) throws SemanticException { + private void walk(Node nd, int level, Stack stack) + throws SemanticException { List> parents = - ((Operator)nd).getParentOperators(); + ((Operator) nd).getParentOperators(); - if (level >= numLevels || parents == null || parents.isEmpty()) { + if (level >= numLevels || CollectionUtils.isEmpty(parents)) { dispatch(stack.peek(), stack); return; } - for(Node parent : parents) { + for (Node parent : parents) { stack.add(0, parent); - walk(parent, level+1, stack); + walk(parent, level + 1, stack); stack.remove(0); } }