in algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/subplan/MoveFreeVariableOperatorOutOfSubplanRule.java [72:182]
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
throws AlgebricksException {
AbstractLogicalOperator op0 = (AbstractLogicalOperator) opRef.getValue();
if (op0.getOperatorTag() != LogicalOperatorTag.SUBPLAN) {
return false;
}
SubplanOperator subplan = (SubplanOperator) op0;
Mutable<ILogicalOperator> leftRef = subplan.getInputs().get(0);
if (((AbstractLogicalOperator) leftRef.getValue()).getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE) {
return false;
}
ListIterator<ILogicalPlan> plansIter = subplan.getNestedPlans().listIterator();
ILogicalPlan p = null;
while (plansIter.hasNext()) {
p = plansIter.next();
}
if (p == null) {
return false;
}
if (p.getRoots().size() != 1) {
return false;
}
Mutable<ILogicalOperator> opRef1 = p.getRoots().get(0);
//The root operator will not be movable. Start with the second op
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef1.getValue();
if (op1.getInputs().size() != 1) {
return false;
}
Mutable<ILogicalOperator> op2Ref = op1.getInputs().get(0);
//Get all variables that come from outside of the loop
Set<LogicalVariable> free = new HashSet<LogicalVariable>();
OperatorPropertiesUtil.getFreeVariablesInSelfOrDesc(op1, free);
while (op2Ref != null) {
//Get the operator that we want to look at
AbstractLogicalOperator op2 = (AbstractLogicalOperator) op2Ref.getValue();
//Make sure we are looking at subplan with a scan/join
if (op2.getInputs().size() != 1 || !descOrSelfIsScanOrJoin(op2)) {
return false;
}
boolean notApplicable = false;
//Get its used variables
Set<LogicalVariable> used = new HashSet<LogicalVariable>();
//not movable if the operator is not an assign or subplan
//Might be helpful in the future for other operations in the future
if (movableOperator(op2.getOperatorTag())) {
if (op2.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
VariableUtilities.getUsedVariables(op2, used);
} else if (op2.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
// Nested plan must have an aggregate root.
ListIterator<ILogicalPlan> subplansIter = ((SubplanOperator) op2).getNestedPlans().listIterator();
ILogicalPlan plan = null;
while (subplansIter.hasNext()) {
plan = subplansIter.next();
}
if (plan == null) {
return false;
}
if (plan.getRoots().size() != 1) {
return false;
}
ILogicalOperator op3 = plan.getRoots().get(0).getValue();
if (op3.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
return false;
}
// Used variables do not include ones created in the subplan.
VariableUtilities.getUsedVariables(op2, used);
Set<LogicalVariable> subplanProducedAndDown = new HashSet<LogicalVariable>();
VariableUtilities.getProducedVariablesInDescendantsAndSelf(op3, subplanProducedAndDown);
used.removeAll(subplanProducedAndDown);
} else {
notApplicable = true;
}
} else {
notApplicable = true;
}
//Make sure that all of its used variables come from outside
for (LogicalVariable var : used) {
if (!free.contains(var)) {
notApplicable = true;
}
}
if (notApplicable) {
op2Ref = op2.getInputs().get(0);
} else {
//Make the input of op2 be the input of op1
op2Ref.setValue(op2.getInputs().get(0).getValue());
//Make the outside of the subplan the input of op2
Mutable<ILogicalOperator> outsideRef = op2.getInputs().get(0);
outsideRef.setValue(op0.getInputs().get(0).getValue());
//Make op2 the input of the subplan
Mutable<ILogicalOperator> op2OutsideRef = op0.getInputs().get(0);
op2OutsideRef.setValue(op2);
return true;
}
}
return false;
}