use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.
the class SubplanOutOfGroupRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op0 = (AbstractLogicalOperator) opRef.getValue();
if (op0.getOperatorTag() != LogicalOperatorTag.GROUP) {
return false;
}
GroupByOperator gby = (GroupByOperator) op0;
Iterator<ILogicalPlan> plansIter = gby.getNestedPlans().iterator();
ILogicalPlan p = null;
while (plansIter.hasNext()) {
p = plansIter.next();
}
if (p == null) {
return false;
}
if (p.getRoots().size() != 1) {
return false;
}
Mutable<ILogicalOperator> op1Ref = p.getRoots().get(0);
AbstractLogicalOperator op1 = (AbstractLogicalOperator) op1Ref.getValue();
boolean found = false;
while (op1.getInputs().size() == 1) {
if (op1.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
SubplanOperator subplan = (SubplanOperator) op1;
AbstractLogicalOperator op2 = (AbstractLogicalOperator) subplan.getInputs().get(0).getValue();
if (OperatorPropertiesUtil.isMissingTest(op2)) {
if (subplan.getNestedPlans().size() == 1) {
ILogicalPlan p1 = subplan.getNestedPlans().get(0);
if (p1.getRoots().size() == 1) {
AbstractLogicalOperator r1 = (AbstractLogicalOperator) p1.getRoots().get(0).getValue();
if (r1.getOperatorTag() == LogicalOperatorTag.INNERJOIN || r1.getOperatorTag() == LogicalOperatorTag.LEFTOUTERJOIN) {
// now, check that it propagates all variables,
// so it can be pushed
List<LogicalVariable> op2Vars = new ArrayList<LogicalVariable>();
VariableUtilities.getLiveVariables(op2, op2Vars);
List<LogicalVariable> op1Vars = new ArrayList<LogicalVariable>();
VariableUtilities.getLiveVariables(subplan, op1Vars);
if (op1Vars.containsAll(op2Vars)) {
found = true;
break;
}
}
}
}
}
}
op1Ref = op1.getInputs().get(0);
op1 = (AbstractLogicalOperator) op1Ref.getValue();
}
if (!found) {
return false;
}
ILogicalOperator subplan = op1;
ILogicalOperator op2 = op1.getInputs().get(0).getValue();
op1Ref.setValue(op2);
Mutable<ILogicalOperator> opUnderRef = gby.getInputs().get(0);
ILogicalOperator opUnder = opUnderRef.getValue();
subplan.getInputs().clear();
subplan.getInputs().add(new MutableObject<ILogicalOperator>(opUnder));
opUnderRef.setValue(subplan);
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.
the class LogicalOperatorDeepCopyWithNewVariablesVisitor method visitSubplanOperator.
@Override
public ILogicalOperator visitSubplanOperator(SubplanOperator op, ILogicalOperator arg) throws AlgebricksException {
List<ILogicalPlan> nestedPlansCopy = new ArrayList<ILogicalPlan>();
SubplanOperator opCopy = new SubplanOperator(nestedPlansCopy);
deepCopyInputsAnnotationsAndExecutionMode(op, arg, opCopy);
deepCopyPlanList(op.getNestedPlans(), nestedPlansCopy, opCopy);
return opCopy;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.
the class RemoveRedundantVariablesRule method removeRedundantVariables.
private boolean removeRedundantVariables(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
LogicalOperatorTag opTag = op.getOperatorTag();
boolean modified = false;
// Update equivalence class map.
if (opTag == LogicalOperatorTag.ASSIGN) {
AssignOperator assignOp = (AssignOperator) op;
int numVars = assignOp.getVariables().size();
for (int i = 0; i < numVars; i++) {
ILogicalExpression expr = assignOp.getExpressions().get(i).getValue();
if (expr.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
continue;
}
VariableReferenceExpression rhsVarRefExpr = (VariableReferenceExpression) expr;
// Update equivalence class map.
LogicalVariable lhs = assignOp.getVariables().get(i);
LogicalVariable rhs = rhsVarRefExpr.getVariableReference();
updateEquivalenceClassMap(lhs, rhs);
}
}
// Replace variable references with their first representative.
if (opTag == LogicalOperatorTag.PROJECT) {
// The project operator does not use expressions, so we need to replace it's variables manually.
if (replaceProjectVars((ProjectOperator) op)) {
modified = true;
}
} else if (op.getOperatorTag() == LogicalOperatorTag.UNIONALL) {
// Replace redundant variables manually in the UnionAll operator.
if (replaceUnionAllVars((UnionAllOperator) op)) {
modified = true;
}
} else {
if (op.acceptExpressionTransform(substVisitor)) {
modified = true;
}
}
// Perform variable replacement in nested plans.
if (op.hasNestedPlans()) {
AbstractOperatorWithNestedPlans opWithNestedPlan = (AbstractOperatorWithNestedPlans) op;
for (ILogicalPlan nestedPlan : opWithNestedPlan.getNestedPlans()) {
for (Mutable<ILogicalOperator> rootRef : nestedPlan.getRoots()) {
if (removeRedundantVariables(rootRef, context)) {
modified = true;
}
}
}
}
// Deal with re-mapping of variables in group by.
if (opTag == LogicalOperatorTag.GROUP) {
if (handleGroupByVarRemapping((GroupByOperator) op)) {
modified = true;
}
}
if (modified) {
context.computeAndSetTypeEnvironmentForOperator(op);
context.addToDontApplySet(this, op);
}
// in the query plan.
if (opTag == LogicalOperatorTag.DISTRIBUTE_RESULT || opTag == LogicalOperatorTag.SINK) {
equivalentVarsMap.clear();
}
return modified;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.
the class RemoveUnusedAssignAndAggregateRule method removeUnusedAssigns.
private void removeUnusedAssigns(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
Set<LogicalVariable> assignVarsSetForThisOp = removeAssignVarFromConsideration(opRef);
while (removeFromAssigns(op, assignVarsSetForThisOp, context) == 0) {
if (op.getOperatorTag() == LogicalOperatorTag.AGGREGATE) {
break;
}
op = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
opRef.setValue(op);
assignVarsSetForThisOp = removeAssignVarFromConsideration(opRef);
}
Iterator<Mutable<ILogicalOperator>> childIter = op.getInputs().iterator();
while (childIter.hasNext()) {
Mutable<ILogicalOperator> cRef = childIter.next();
removeUnusedAssigns(cRef, context);
}
if (op.hasNestedPlans()) {
AbstractOperatorWithNestedPlans opWithNest = (AbstractOperatorWithNestedPlans) op;
Iterator<ILogicalPlan> planIter = opWithNest.getNestedPlans().iterator();
while (planIter.hasNext()) {
ILogicalPlan p = planIter.next();
for (Mutable<ILogicalOperator> r : p.getRoots()) {
removeUnusedAssigns(r, context);
}
}
// Removes redundant nested plans that produces nothing
for (int i = opWithNest.getNestedPlans().size() - 1; i >= 0; i--) {
ILogicalPlan nestedPlan = opWithNest.getNestedPlans().get(i);
List<Mutable<ILogicalOperator>> rootsToBeRemoved = new ArrayList<Mutable<ILogicalOperator>>();
for (Mutable<ILogicalOperator> r : nestedPlan.getRoots()) {
ILogicalOperator topOp = r.getValue();
Set<LogicalVariable> producedVars = new ListSet<LogicalVariable>();
VariableUtilities.getProducedVariablesInDescendantsAndSelf(topOp, producedVars);
if (producedVars.size() == 0) {
rootsToBeRemoved.add(r);
}
}
// (because a lot of places uses this assumption, TODO(yingyib): clean them up).
if (nestedPlan.getRoots().size() == rootsToBeRemoved.size() && opWithNest.getNestedPlans().size() > 1) {
nestedPlan.getRoots().removeAll(rootsToBeRemoved);
opWithNest.getNestedPlans().remove(nestedPlan);
}
}
}
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.
the class PushSubplanIntoGroupByRule method cleanup.
/**
* Removes unused aggregation variables (and expressions)
*
* @param gby
* @throws AlgebricksException
*/
private void cleanup(GroupByOperator gby) throws AlgebricksException {
for (ILogicalPlan nestedPlan : gby.getNestedPlans()) {
for (Mutable<ILogicalOperator> rootRef : nestedPlan.getRoots()) {
AggregateOperator aggOp = (AggregateOperator) rootRef.getValue();
for (int varIndex = aggOp.getVariables().size() - 1; varIndex >= 0; varIndex--) {
if (!usedVarsSoFar.contains(aggOp.getVariables().get(varIndex))) {
aggOp.getVariables().remove(varIndex);
aggOp.getExpressions().remove(varIndex);
}
}
}
}
}
Aggregations