Search in sources :

Example 21 with ILogicalPlan

use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.

the class EliminateSubplanWithInputCardinalityOneRule method rewritePre.

@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    if (!invoked) {
        rootRef = opRef;
        invoked = true;
    }
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getInputs().size() <= 0) {
        return false;
    }
    boolean changed = false;
    for (Mutable<ILogicalOperator> subplanRef : op.getInputs()) {
        AbstractLogicalOperator op1 = (AbstractLogicalOperator) subplanRef.getValue();
        if (op1.getOperatorTag() != LogicalOperatorTag.SUBPLAN) {
            continue;
        }
        SubplanOperator subplan = (SubplanOperator) op1;
        Set<LogicalVariable> usedVarsUp = new ListSet<LogicalVariable>();
        OperatorPropertiesUtil.getFreeVariablesInPath(rootRef.getValue(), subplan, usedVarsUp);
        // TODO(buyingyi): figure out the rewriting for subplan operators with multiple subplans.
        if (subplan.getNestedPlans().size() != 1) {
            continue;
        }
        ILogicalOperator subplanInputOperator = subplan.getInputs().get(0).getValue();
        Set<LogicalVariable> subplanInputVars = new ListSet<LogicalVariable>();
        VariableUtilities.getLiveVariables(subplanInputOperator, subplanInputVars);
        int subplanInputVarSize = subplanInputVars.size();
        subplanInputVars.removeAll(usedVarsUp);
        // Makes sure the free variables are only used in the subplan.
        if (subplanInputVars.size() < subplanInputVarSize) {
            continue;
        }
        Set<LogicalVariable> freeVars = new ListSet<LogicalVariable>();
        OperatorPropertiesUtil.getFreeVariablesInSubplans(subplan, freeVars);
        boolean cardinalityOne = isCardinalityOne(subplan.getInputs().get(0), freeVars);
        if (cardinalityOne) {
            /** If the cardinality of freeVars in the subplan is one, the subplan can be removed. */
            ILogicalPlan plan = subplan.getNestedPlans().get(0);
            List<Mutable<ILogicalOperator>> rootRefs = plan.getRoots();
            // TODO(buyingyi): investigate the case of multi-root plans.
            if (rootRefs.size() != 1) {
                continue;
            }
            // Replaces all Nts' in the nested plan with the Subplan input operator or its deep copy.
            ILogicalOperator topOperator = rootRefs.get(0).getValue();
            ReplaceNtsWithSubplanInputOperatorVisitor visitor = new ReplaceNtsWithSubplanInputOperatorVisitor(context, subplan);
            ILogicalOperator newTopOperator = topOperator.accept(visitor, null);
            subplanRef.setValue(newTopOperator);
            OperatorManipulationUtil.computeTypeEnvironmentBottomUp(newTopOperator, context);
            changed = true;
        } else {
            continue;
        }
    }
    return changed;
}
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) 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) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 22 with ILogicalPlan

use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.

the class ReinferAllTypesRule method typeOpRec.

private void typeOpRec(Mutable<ILogicalOperator> r, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) r.getValue();
    for (Mutable<ILogicalOperator> i : op.getInputs()) {
        typeOpRec(i, context);
    }
    if (op.hasNestedPlans()) {
        for (ILogicalPlan p : ((AbstractOperatorWithNestedPlans) op).getNestedPlans()) {
            typePlan(p, context);
        }
    }
    context.computeAndSetTypeEnvironmentForOperator(op);
    context.addToDontApplySet(this, op);
}
Also used : AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) AbstractOperatorWithNestedPlans(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans)

Example 23 with ILogicalPlan

use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.

the class PushNestedOrderByUnderPreSortedGroupByRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.GROUP) {
        return false;
    }
    if (op.getPhysicalOperator() == null) {
        return false;
    }
    AbstractPhysicalOperator pOp = (AbstractPhysicalOperator) op.getPhysicalOperator();
    if (pOp.getOperatorTag() != PhysicalOperatorTag.PRE_CLUSTERED_GROUP_BY) {
        return false;
    }
    GroupByOperator gby = (GroupByOperator) op;
    ILogicalPlan plan = gby.getNestedPlans().get(0);
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) plan.getRoots().get(0).getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    Mutable<ILogicalOperator> opRef2 = op1.getInputs().get(0);
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();
    if (op2.getOperatorTag() != LogicalOperatorTag.ORDER) {
        return false;
    }
    OrderOperator order1 = (OrderOperator) op2;
    if (!isIndependentFromChildren(order1)) {
        return false;
    }
    AbstractPhysicalOperator pOrder1 = (AbstractPhysicalOperator) op2.getPhysicalOperator();
    if (pOrder1.getOperatorTag() != PhysicalOperatorTag.STABLE_SORT && pOrder1.getOperatorTag() != PhysicalOperatorTag.IN_MEMORY_STABLE_SORT) {
        return false;
    }
    // StableSortPOperator sort1 = (StableSortPOperator) pOrder1;
    AbstractLogicalOperator op3 = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
    if (op3.getOperatorTag() != LogicalOperatorTag.ORDER) {
        return false;
    }
    AbstractPhysicalOperator pOp3 = (AbstractPhysicalOperator) op3.getPhysicalOperator();
    if (pOp3.getOperatorTag() != PhysicalOperatorTag.STABLE_SORT) {
        return false;
    }
    OrderOperator order2 = (OrderOperator) op3;
    StableSortPOperator sort2 = (StableSortPOperator) pOp3;
    // int k = 0;
    for (Pair<IOrder, Mutable<ILogicalExpression>> oe : order1.getOrderExpressions()) {
        order2.getOrderExpressions().add(oe);
    // sortColumns[n2 + k] = sort1.getSortColumns()[k];
    // ++k;
    }
    // sort2.setSortColumns(sortColumns);
    sort2.computeDeliveredProperties(order2, null);
    // remove order1
    ILogicalOperator underOrder1 = order1.getInputs().get(0).getValue();
    opRef2.setValue(underOrder1);
    return true;
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) IOrder(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator.IOrder) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AbstractPhysicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.AbstractPhysicalOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) StableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.StableSortPOperator)

Example 24 with ILogicalPlan

use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.

the class PushSubplanWithAggregateDownThroughProductRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.SUBPLAN) {
        return false;
    }
    SubplanOperator subplan = (SubplanOperator) op1;
    if (subplan.getNestedPlans().size() != 1) {
        return false;
    }
    ILogicalPlan p = subplan.getNestedPlans().get(0);
    if (p.getRoots().size() != 1) {
        return false;
    }
    Mutable<ILogicalOperator> r = p.getRoots().get(0);
    if (((AbstractLogicalOperator) r.getValue()).getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    Set<LogicalVariable> free = new HashSet<LogicalVariable>();
    OperatorPropertiesUtil.getFreeVariablesInSubplans(subplan, free);
    Mutable<ILogicalOperator> op2Ref = op1.getInputs().get(0);
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) op2Ref.getValue();
    if (op2.getOperatorTag() != LogicalOperatorTag.INNERJOIN) {
        return false;
    }
    AbstractBinaryJoinOperator join = (AbstractBinaryJoinOperator) op2;
    if (!OperatorPropertiesUtil.isAlwaysTrueCond(join.getCondition().getValue())) {
        return false;
    }
    Mutable<ILogicalOperator> b0Ref = op2.getInputs().get(0);
    ILogicalOperator b0 = b0Ref.getValue();
    List<LogicalVariable> b0Scm = new ArrayList<LogicalVariable>();
    VariableUtilities.getLiveVariables(b0, b0Scm);
    if (b0Scm.containsAll(free)) {
        // push subplan on left branch
        op2Ref.setValue(b0);
        b0Ref.setValue(op1);
        opRef.setValue(op2);
        return true;
    } else {
        Mutable<ILogicalOperator> b1Ref = op2.getInputs().get(1);
        ILogicalOperator b1 = b1Ref.getValue();
        List<LogicalVariable> b1Scm = new ArrayList<LogicalVariable>();
        VariableUtilities.getLiveVariables(b1, b1Scm);
        if (b1Scm.containsAll(free)) {
            // push subplan on right branch
            op2Ref.setValue(b1);
            b1Ref.setValue(op1);
            opRef.setValue(op2);
            return true;
        } else {
            return false;
        }
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) 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) HashSet(java.util.HashSet) AbstractBinaryJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractBinaryJoinOperator)

Example 25 with ILogicalPlan

use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan in project asterixdb by apache.

the class CancelUnnestWithNestedListifyRule method applyRuleDown.

private boolean applyRuleDown(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> varSet, IOptimizationContext context) throws AlgebricksException {
    boolean changed = applies(opRef, varSet, context);
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    VariableUtilities.getUsedVariables(op, varSet);
    if (op.hasNestedPlans()) {
        AbstractOperatorWithNestedPlans aonp = (AbstractOperatorWithNestedPlans) op;
        for (ILogicalPlan p : aonp.getNestedPlans()) {
            for (Mutable<ILogicalOperator> r : p.getRoots()) {
                if (applyRuleDown(r, varSet, context)) {
                    changed = true;
                }
                context.addToDontApplySet(this, r.getValue());
            }
        }
    }
    for (Mutable<ILogicalOperator> i : op.getInputs()) {
        if (applyRuleDown(i, varSet, context)) {
            changed = true;
        }
        context.addToDontApplySet(this, i.getValue());
    }
    return changed;
}
Also used : AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) AbstractOperatorWithNestedPlans(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans)

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