Search in sources :

Example 21 with ListSet

use of org.apache.hyracks.algebricks.common.utils.ListSet 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 22 with ListSet

use of org.apache.hyracks.algebricks.common.utils.ListSet in project asterixdb by apache.

the class PushSubplanIntoGroupByRule method rewritePre.

@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    ILogicalOperator parentOperator = opRef.getValue();
    if (context.checkIfInDontApplySet(this, parentOperator)) {
        return false;
    }
    context.addToDontApplySet(this, parentOperator);
    VariableUtilities.getUsedVariables(parentOperator, usedVarsSoFar);
    if (parentOperator.getInputs().size() <= 0) {
        return false;
    }
    boolean changed = false;
    GroupByOperator gby = null;
    for (Mutable<ILogicalOperator> ref : parentOperator.getInputs()) {
        AbstractLogicalOperator op = (AbstractLogicalOperator) ref.getValue();
        /** Only processes subplan operator. */
        List<SubplanOperator> subplans = new ArrayList<SubplanOperator>();
        if (op.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
            while (op.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
                SubplanOperator currentSubplan = (SubplanOperator) op;
                subplans.add(currentSubplan);
                op = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
            }
            /** Only processes the case a group-by operator is the input of the subplan operators. */
            if (op.getOperatorTag() == LogicalOperatorTag.GROUP) {
                gby = (GroupByOperator) op;
                List<ILogicalPlan> newGbyNestedPlans = new ArrayList<ILogicalPlan>();
                for (SubplanOperator subplan : subplans) {
                    List<ILogicalPlan> subplanNestedPlans = subplan.getNestedPlans();
                    List<ILogicalPlan> gbyNestedPlans = gby.getNestedPlans();
                    List<ILogicalPlan> subplanNestedPlansToRemove = new ArrayList<ILogicalPlan>();
                    for (ILogicalPlan subplanNestedPlan : subplanNestedPlans) {
                        List<Mutable<ILogicalOperator>> rootOpRefs = subplanNestedPlan.getRoots();
                        List<Mutable<ILogicalOperator>> rootOpRefsToRemove = new ArrayList<Mutable<ILogicalOperator>>();
                        for (Mutable<ILogicalOperator> rootOpRef : rootOpRefs) {
                            /** Gets free variables in the root operator of a nested plan and its descent. */
                            Set<LogicalVariable> freeVars = new ListSet<LogicalVariable>();
                            VariableUtilities.getUsedVariablesInDescendantsAndSelf(rootOpRef.getValue(), freeVars);
                            Set<LogicalVariable> producedVars = new ListSet<LogicalVariable>();
                            VariableUtilities.getProducedVariablesInDescendantsAndSelf(rootOpRef.getValue(), producedVars);
                            freeVars.removeAll(producedVars);
                            /** * Checks whether the above freeVars are all contained in live variables * of one nested plan inside the group-by operator. * If yes, then the subplan can be pushed into the nested plan of the group-by. */
                            for (ILogicalPlan gbyNestedPlanOriginal : gbyNestedPlans) {
                                // add a subplan in the original gby
                                if (!newGbyNestedPlans.contains(gbyNestedPlanOriginal)) {
                                    newGbyNestedPlans.add(gbyNestedPlanOriginal);
                                }
                                // add a pushed subplan
                                ILogicalPlan gbyNestedPlan = OperatorManipulationUtil.deepCopy(gbyNestedPlanOriginal, context);
                                List<Mutable<ILogicalOperator>> gbyRootOpRefs = gbyNestedPlan.getRoots();
                                for (int rootIndex = 0; rootIndex < gbyRootOpRefs.size(); rootIndex++) {
                                    //set the nts for a original subplan
                                    Mutable<ILogicalOperator> originalGbyRootOpRef = gbyNestedPlanOriginal.getRoots().get(rootIndex);
                                    Mutable<ILogicalOperator> originalGbyNtsRef = downToNts(originalGbyRootOpRef);
                                    NestedTupleSourceOperator originalNts = (NestedTupleSourceOperator) originalGbyNtsRef.getValue();
                                    originalNts.setDataSourceReference(new MutableObject<ILogicalOperator>(gby));
                                    //push a new subplan if possible
                                    Mutable<ILogicalOperator> gbyRootOpRef = gbyRootOpRefs.get(rootIndex);
                                    Set<LogicalVariable> liveVars = new ListSet<LogicalVariable>();
                                    VariableUtilities.getLiveVariables(gbyRootOpRef.getValue(), liveVars);
                                    if (liveVars.containsAll(freeVars)) {
                                        /** Does the actual push. */
                                        Mutable<ILogicalOperator> ntsRef = downToNts(rootOpRef);
                                        ntsRef.setValue(gbyRootOpRef.getValue());
                                        // Removes unused vars.
                                        AggregateOperator aggOp = (AggregateOperator) gbyRootOpRef.getValue();
                                        for (int varIndex = aggOp.getVariables().size() - 1; varIndex >= 0; varIndex--) {
                                            if (!freeVars.contains(aggOp.getVariables().get(varIndex))) {
                                                aggOp.getVariables().remove(varIndex);
                                                aggOp.getExpressions().remove(varIndex);
                                            }
                                        }
                                        gbyRootOpRef.setValue(rootOpRef.getValue());
                                        rootOpRefsToRemove.add(rootOpRef);
                                        // Sets the nts for a new pushed plan.
                                        Mutable<ILogicalOperator> oldGbyNtsRef = downToNts(gbyRootOpRef);
                                        NestedTupleSourceOperator nts = (NestedTupleSourceOperator) oldGbyNtsRef.getValue();
                                        nts.setDataSourceReference(new MutableObject<ILogicalOperator>(gby));
                                        newGbyNestedPlans.add(gbyNestedPlan);
                                        changed = true;
                                        continue;
                                    }
                                }
                            }
                        }
                        rootOpRefs.removeAll(rootOpRefsToRemove);
                        if (rootOpRefs.size() == 0) {
                            subplanNestedPlansToRemove.add(subplanNestedPlan);
                        }
                    }
                    subplanNestedPlans.removeAll(subplanNestedPlansToRemove);
                }
                if (changed) {
                    ref.setValue(gby);
                    gby.getNestedPlans().clear();
                    gby.getNestedPlans().addAll(newGbyNestedPlans);
                }
            }
        }
    }
    if (changed) {
        cleanup(gby);
        context.computeAndSetTypeEnvironmentForOperator(gby);
        context.computeAndSetTypeEnvironmentForOperator(parentOperator);
    }
    return changed;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) 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) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Aggregations

ListSet (org.apache.hyracks.algebricks.common.utils.ListSet)22 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)22 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)13 ArrayList (java.util.ArrayList)12 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)10 Mutable (org.apache.commons.lang3.mutable.Mutable)9 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)9 ILocalStructuralProperty (org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty)9 StructuralPropertiesVector (org.apache.hyracks.algebricks.core.algebra.properties.StructuralPropertiesVector)7 HashSet (java.util.HashSet)6 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)6 LocalGroupingProperty (org.apache.hyracks.algebricks.core.algebra.properties.LocalGroupingProperty)6 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)5 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)5 IPartitioningProperty (org.apache.hyracks.algebricks.core.algebra.properties.IPartitioningProperty)5 LocalOrderProperty (org.apache.hyracks.algebricks.core.algebra.properties.LocalOrderProperty)5 UnorderedPartitionedProperty (org.apache.hyracks.algebricks.core.algebra.properties.UnorderedPartitionedProperty)5 LinkedList (java.util.LinkedList)4 IPhysicalPropertiesVector (org.apache.hyracks.algebricks.core.algebra.properties.IPhysicalPropertiesVector)4 OrderColumn (org.apache.hyracks.algebricks.core.algebra.properties.OrderColumn)4