Search in sources :

Example 66 with ILogicalPlan

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

the class OperatorPropertiesUtil method typeOpRec.

public static 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);
}
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 67 with ILogicalPlan

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

the class AbstractIntroduceGroupByCombinerRule method tryToPushSubplan.

private Pair<Boolean, ILogicalPlan> tryToPushSubplan(ILogicalPlan nestedPlan, GroupByOperator oldGbyOp, GroupByOperator newGbyOp, BookkeepingInfo bi, List<LogicalVariable> gbyVars, IOptimizationContext context) throws AlgebricksException {
    List<Mutable<ILogicalOperator>> pushedRoots = new ArrayList<Mutable<ILogicalOperator>>();
    Set<SimilarAggregatesInfo> toReplaceSet = new HashSet<SimilarAggregatesInfo>();
    for (Mutable<ILogicalOperator> r : nestedPlan.getRoots()) {
        if (!tryToPushRoot(r, oldGbyOp, newGbyOp, bi, gbyVars, context, pushedRoots, toReplaceSet)) {
            // For now, if we cannot push everything, give up.
            return new Pair<Boolean, ILogicalPlan>(false, null);
        }
    }
    if (pushedRoots.isEmpty()) {
        return new Pair<Boolean, ILogicalPlan>(true, null);
    } else {
        // Replaces the aggregation expressions in the original group-by op with new ones.
        ILogicalPlan newPlan = new ALogicalPlanImpl(pushedRoots);
        ILogicalPlan plan = fingIdenticalPlan(newGbyOp, newPlan);
        replaceOriginalAggFuncs(toReplaceSet);
        if (plan == null) {
            return new Pair<Boolean, ILogicalPlan>(true, newPlan);
        } else {
            // Does not add a nested subplan to newGbyOp if there already exists an isomorphic plan.
            Set<LogicalVariable> originalVars = new ListSet<LogicalVariable>();
            Set<LogicalVariable> newVars = new ListSet<LogicalVariable>();
            for (Mutable<ILogicalOperator> rootRef : pushedRoots) {
                VariableUtilities.getProducedVariables(rootRef.getValue(), originalVars);
            }
            for (Mutable<ILogicalOperator> rootRef : plan.getRoots()) {
                VariableUtilities.getProducedVariables(rootRef.getValue(), newVars);
            }
            // Replaces variable exprs referring to the variables produced by newPlan by
            // those produced by plan.
            Iterator<LogicalVariable> originalVarIter = originalVars.iterator();
            Iterator<LogicalVariable> newVarIter = newVars.iterator();
            while (originalVarIter.hasNext()) {
                LogicalVariable originalVar = originalVarIter.next();
                LogicalVariable newVar = newVarIter.next();
                for (SimilarAggregatesInfo sai : toReplaceSet) {
                    for (AggregateExprInfo aei : sai.simAggs) {
                        ILogicalExpression afce = aei.aggExprRef.getValue();
                        afce.substituteVar(originalVar, newVar);
                    }
                }
            }
            return new Pair<Boolean, ILogicalPlan>(true, null);
        }
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ALogicalPlanImpl(org.apache.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) HashSet(java.util.HashSet) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 68 with ILogicalPlan

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

the class PigletCompiler method compile.

public JobSpecification compile(List<ASTNode> ast) throws AlgebricksException, PigletException {
    ILogicalPlan plan = translate(ast);
    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("Translated Plan:");
        LOGGER.info(getPrettyPrintedPlan(plan));
    }
    ICompiler compiler = cFactory.createCompiler(plan, metadataProvider, varCounter);
    compiler.optimize();
    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("Optimized Plan:");
        LOGGER.info(getPrettyPrintedPlan(plan));
    }
    return compiler.createJob(null, null);
}
Also used : ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) ICompiler(org.apache.hyracks.algebricks.compiler.api.ICompiler)

Example 69 with ILogicalPlan

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

the class AbstractIntroduceGroupByCombinerRule method opToPush.

private GroupByOperator opToPush(GroupByOperator gbyOp, BookkeepingInfo bi, IOptimizationContext context) throws AlgebricksException {
    // Hook up input to new group-by.
    Mutable<ILogicalOperator> opRef3 = gbyOp.getInputs().get(0);
    ILogicalOperator op3 = opRef3.getValue();
    GroupByOperator newGbyOp = new GroupByOperator();
    newGbyOp.getInputs().add(new MutableObject<ILogicalOperator>(op3));
    // Copy annotations.
    Map<String, Object> annotations = newGbyOp.getAnnotations();
    annotations.putAll(gbyOp.getAnnotations());
    List<LogicalVariable> gbyVars = gbyOp.getGbyVarList();
    // Backup nested plans since tryToPushSubplan(...) may mutate them.
    List<ILogicalPlan> copiedNestedPlans = new ArrayList<>();
    for (ILogicalPlan nestedPlan : gbyOp.getNestedPlans()) {
        ILogicalPlan copiedNestedPlan = OperatorManipulationUtil.deepCopy(nestedPlan, gbyOp);
        OperatorManipulationUtil.computeTypeEnvironment(copiedNestedPlan, context);
        copiedNestedPlans.add(copiedNestedPlan);
    }
    for (ILogicalPlan p : gbyOp.getNestedPlans()) {
        // NOTE: tryToPushSubplan(...) can mutate the nested subplan p.
        Pair<Boolean, ILogicalPlan> bip = tryToPushSubplan(p, gbyOp, newGbyOp, bi, gbyVars, context);
        if (!bip.first) {
            // For now, if we cannot push everything, give up.
            // Resets the group-by operator with backup nested plans.
            gbyOp.getNestedPlans().clear();
            gbyOp.getNestedPlans().addAll(copiedNestedPlans);
            return null;
        }
        ILogicalPlan pushedSubplan = bip.second;
        if (pushedSubplan != null) {
            newGbyOp.getNestedPlans().add(pushedSubplan);
        }
    }
    ArrayList<LogicalVariable> newOpGbyList = new ArrayList<LogicalVariable>();
    ArrayList<LogicalVariable> replGbyList = new ArrayList<LogicalVariable>();
    // Find maximal sequence of variable.
    for (Map.Entry<GroupByOperator, List<LogicalVariable>> e : bi.modifyGbyMap.entrySet()) {
        List<LogicalVariable> varList = e.getValue();
        boolean see1 = true;
        int sz1 = newOpGbyList.size();
        int i = 0;
        for (LogicalVariable v : varList) {
            if (see1) {
                if (i < sz1) {
                    LogicalVariable v2 = newOpGbyList.get(i);
                    if (v != v2) {
                        // cannot linearize
                        return null;
                    }
                } else {
                    see1 = false;
                    newOpGbyList.add(v);
                    replGbyList.add(context.newVar());
                }
                i++;
            } else {
                newOpGbyList.add(v);
                replGbyList.add(context.newVar());
            }
        }
    }
    // set the vars in the new op
    int n = newOpGbyList.size();
    for (int i = 0; i < n; i++) {
        newGbyOp.addGbyExpression(replGbyList.get(i), new VariableReferenceExpression(newOpGbyList.get(i)));
        VariableUtilities.substituteVariables(gbyOp, newOpGbyList.get(i), replGbyList.get(i), false, context);
    }
    // Sets the global flag to be false.
    newGbyOp.setGlobal(false);
    // Sets the group all flag.
    newGbyOp.setGroupAll(gbyOp.isGroupAll());
    return newGbyOp;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Map(java.util.Map)

Example 70 with ILogicalPlan

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

the class AbstractRuleController method rewriteOperatorRef.

protected boolean rewriteOperatorRef(Mutable<ILogicalOperator> opRef, IAlgebraicRewriteRule rule, boolean enterNestedPlans, boolean fullDFS) throws AlgebricksException {
    String preBeforePlan = getPlanString(opRef);
    if (rule.rewritePre(opRef, context)) {
        String preAfterPlan = getPlanString(opRef);
        printRuleApplication(rule, preBeforePlan, preAfterPlan);
        return true;
    }
    boolean rewritten = false;
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    for (Mutable<ILogicalOperator> inp : op.getInputs()) {
        if (rewriteOperatorRef(inp, rule, enterNestedPlans, fullDFS)) {
            rewritten = true;
            if (!fullDFS) {
                break;
            }
        }
    }
    if (op.hasNestedPlans() && enterNestedPlans) {
        AbstractOperatorWithNestedPlans o2 = (AbstractOperatorWithNestedPlans) op;
        for (ILogicalPlan p : o2.getNestedPlans()) {
            for (Mutable<ILogicalOperator> r : p.getRoots()) {
                if (rewriteOperatorRef(r, rule, enterNestedPlans, fullDFS)) {
                    rewritten = true;
                    if (!fullDFS) {
                        break;
                    }
                }
            }
            if (rewritten && !fullDFS) {
                break;
            }
        }
    }
    String postBeforePlan = getPlanString(opRef);
    if (rule.rewritePost(opRef, context)) {
        String postAfterPlan = getPlanString(opRef);
        printRuleApplication(rule, postBeforePlan, postAfterPlan);
        return true;
    }
    return rewritten;
}
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