Search in sources :

Example 41 with ILogicalPlan

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;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 42 with ILogicalPlan

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;
}
Also used : SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) ArrayList(java.util.ArrayList) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 43 with ILogicalPlan

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;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) UnionAllOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator) LogicalOperatorTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) AbstractOperatorWithNestedPlans(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans)

Example 44 with ILogicalPlan

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);
            }
        }
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) AbstractOperatorWithNestedPlans(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans)

Example 45 with ILogicalPlan

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);
                }
            }
        }
    }
}
Also used : ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Aggregations

ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)89 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)73 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)53 Mutable (org.apache.commons.lang3.mutable.Mutable)44 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)44 ArrayList (java.util.ArrayList)36 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)35 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)27 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)24 AbstractOperatorWithNestedPlans (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans)23 AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)21 HashSet (java.util.HashSet)19 SubplanOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator)19 MutableObject (org.apache.commons.lang3.mutable.MutableObject)17 Pair (org.apache.hyracks.algebricks.common.utils.Pair)16 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)13 NestedTupleSourceOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator)13 ALogicalPlanImpl (org.apache.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl)13 ListSet (org.apache.hyracks.algebricks.common.utils.ListSet)10 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)9