Search in sources :

Example 6 with AggregateOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator in project asterixdb by apache.

the class PushAggFuncIntoStandaloneAggregateRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    // Pattern to match: assign <-- aggregate <-- !(group-by)
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
        return false;
    }
    AssignOperator assignOp = (AssignOperator) op;
    Mutable<ILogicalOperator> opRef2 = op.getInputs().get(0);
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();
    if (op2.getOperatorTag() == LogicalOperatorTag.AGGREGATE) {
        AggregateOperator aggOp = (AggregateOperator) op2;
        // Make sure the agg expr is a listify.
        return pushAggregateFunction(aggOp, assignOp, context);
    } else if (op2.getOperatorTag() == LogicalOperatorTag.INNERJOIN || op2.getOperatorTag() == LogicalOperatorTag.LEFTOUTERJOIN) {
        AbstractBinaryJoinOperator join = (AbstractBinaryJoinOperator) op2;
        // Tries to push aggregates through the join.
        if (containsAggregate(assignOp.getExpressions()) && pushableThroughJoin(join)) {
            pushAggregateFunctionThroughJoin(join, assignOp, context);
            return true;
        }
    }
    return false;
}
Also used : AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) AbstractBinaryJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractBinaryJoinOperator)

Example 7 with AggregateOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator in project asterixdb by apache.

the class PushAggregateIntoNestedSubplanRule method collectOneVarPerAggFromOpWithNestedPlans.

private List<LogicalVariable> collectOneVarPerAggFromOpWithNestedPlans(AbstractOperatorWithNestedPlans op) {
    List<ILogicalPlan> nPlans = op.getNestedPlans();
    if (nPlans == null || nPlans.isEmpty()) {
        return Collections.emptyList();
    }
    List<LogicalVariable> aggVars = new ArrayList<>();
    // test that the operator computes a "listify" aggregate
    for (int i = 0; i < nPlans.size(); i++) {
        AbstractLogicalOperator topOp = (AbstractLogicalOperator) nPlans.get(i).getRoots().get(0).getValue();
        if (topOp.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
            continue;
        }
        AggregateOperator agg = (AggregateOperator) topOp;
        if (agg.getVariables().size() != 1) {
            continue;
        }
        ILogicalExpression expr = agg.getExpressions().get(0).getValue();
        if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
            continue;
        }
        AbstractFunctionCallExpression fceAgg = (AbstractFunctionCallExpression) expr;
        if (fceAgg.getFunctionIdentifier() != BuiltinFunctions.LISTIFY) {
            continue;
        }
        aggVars.add(agg.getVariables().get(0));
    }
    return aggVars;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 8 with AggregateOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator in project asterixdb by apache.

the class NestGroupByRule 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;
    }
    Set<LogicalVariable> free = new HashSet<LogicalVariable>();
    OperatorPropertiesUtil.getFreeVariablesInSubplans(subplan, free);
    if (free.size() != 1) {
        return false;
    }
    LogicalVariable fVar = null;
    for (LogicalVariable v : free) {
        fVar = v;
        break;
    }
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
    if (op2.getOperatorTag() != LogicalOperatorTag.GROUP) {
        return false;
    }
    GroupByOperator gby = (GroupByOperator) op2;
    if (gby.getNestedPlans().size() != 1) {
        return false;
    }
    ILogicalPlan p2 = gby.getNestedPlans().get(0);
    if (p2.getRoots().size() != 1) {
        return false;
    }
    Mutable<ILogicalOperator> r2 = p2.getRoots().get(0);
    AbstractLogicalOperator opr2 = (AbstractLogicalOperator) r2.getValue();
    if (opr2.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    AggregateOperator aggOuter = (AggregateOperator) opr2;
    int posInAggList = aggOuter.getVariables().indexOf(fVar);
    if (posInAggList < 0) {
        return false;
    }
    AbstractLogicalOperator outerAggSon = (AbstractLogicalOperator) aggOuter.getInputs().get(0).getValue();
    if (outerAggSon.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
        return false;
    }
    ILogicalExpression eAgg = aggOuter.getExpressions().get(posInAggList).getValue();
    if (eAgg.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression listifyCall = (AbstractFunctionCallExpression) eAgg;
    if (listifyCall.getFunctionIdentifier() != BuiltinFunctions.LISTIFY) {
        return false;
    }
    ILogicalExpression argListify = listifyCall.getArguments().get(0).getValue();
    if (argListify.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    Mutable<ILogicalOperator> r = p.getRoots().get(0);
    AbstractLogicalOperator opInS = (AbstractLogicalOperator) r.getValue();
    if (opInS.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    AggregateOperator aggInner = (AggregateOperator) opInS;
    do {
        opInS = (AbstractLogicalOperator) opInS.getInputs().get(0).getValue();
    } while (opInS.getOperatorTag() == LogicalOperatorTag.ASSIGN);
    if (opInS.getOperatorTag() != LogicalOperatorTag.GROUP) {
        return false;
    }
    AbstractLogicalOperator unnestParent = opInS;
    AbstractLogicalOperator opUnder = (AbstractLogicalOperator) opInS.getInputs().get(0).getValue();
    // skip Assigns
    while (opUnder.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
        unnestParent = opUnder;
        opUnder = (AbstractLogicalOperator) opUnder.getInputs().get(0).getValue();
    }
    if (opUnder.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }
    UnnestOperator unnest = (UnnestOperator) opUnder;
    AbstractLogicalOperator unnestSon = (AbstractLogicalOperator) unnest.getInputs().get(0).getValue();
    if (unnestSon.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
        return false;
    }
    NestedTupleSourceOperator innerNts = (NestedTupleSourceOperator) unnestSon;
    ILogicalExpression eUnnest = unnest.getExpressionRef().getValue();
    if (eUnnest.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression uf = (AbstractFunctionCallExpression) eUnnest;
    if (uf.getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
        return false;
    }
    ILogicalExpression scanArg = uf.getArguments().get(0).getValue();
    if (scanArg.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    if (((VariableReferenceExpression) scanArg).getVariableReference() != fVar) {
        return false;
    }
    LogicalVariable uVar = unnest.getVariable();
    GroupByOperator innerGby = (GroupByOperator) opInS;
    Set<LogicalVariable> freeInInnerGby = new HashSet<LogicalVariable>();
    OperatorPropertiesUtil.getFreeVariablesInSubplans(innerGby, freeInInnerGby);
    for (LogicalVariable v : freeInInnerGby) {
        if (v != uVar) {
            return false;
        }
    }
    unnestParent.getInputs().get(0).setValue(innerNts);
    LogicalVariable listifiedVar = ((VariableReferenceExpression) argListify).getVariableReference();
    substInSubplan(aggInner, uVar, listifiedVar, context);
    gby.getNestedPlans().add(p);
    innerNts.getDataSourceReference().setValue(gby);
    opRef.setValue(gby);
    OperatorPropertiesUtil.typePlan(p, context);
    OperatorPropertiesUtil.typePlan(p2, context);
    context.computeAndSetTypeEnvironmentForOperator(gby);
    return true;
}
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) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) HashSet(java.util.HashSet)

Example 9 with AggregateOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator in project asterixdb by apache.

the class LogicalOperatorDeepCopyWithNewVariablesVisitor method visitAggregateOperator.

@Override
public ILogicalOperator visitAggregateOperator(AggregateOperator op, ILogicalOperator arg) throws AlgebricksException {
    AggregateOperator opCopy = new AggregateOperator(deepCopyVariableList(op.getVariables()), exprDeepCopyVisitor.deepCopyExpressionReferenceList(op.getExpressions()));
    deepCopyInputsAnnotationsAndExecutionMode(op, arg, opCopy);
    return opCopy;
}
Also used : AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) RunningAggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.RunningAggregateOperator)

Example 10 with AggregateOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator in project asterixdb by apache.

the class InlineAllNtsInSubplanVisitor method wrapLimitInGroupBy.

private Pair<ILogicalOperator, LogicalVariable> wrapLimitInGroupBy(ILogicalOperator op, LogicalVariable recordVar, Set<LogicalVariable> inputLiveVars) throws AlgebricksException {
    GroupByOperator gbyOp = new GroupByOperator();
    List<Pair<LogicalVariable, LogicalVariable>> keyVarNewVarPairs = new ArrayList<>();
    for (LogicalVariable keyVar : correlatedKeyVars) {
        // This limits the visitor can only be applied to a nested logical
        // plan inside a Subplan operator,
        // where the keyVarsToEnforce forms a candidate key which can
        // uniquely identify a tuple out of the nested-tuple-source.
        LogicalVariable newVar = context.newVar();
        gbyOp.getGroupByList().add(new Pair<>(newVar, new MutableObject<>(new VariableReferenceExpression(keyVar))));
        keyVarNewVarPairs.add(new Pair<>(keyVar, newVar));
    }
    // Creates an aggregate operator doing LISTIFY, as the root of the
    // nested plan of the added group-by operator.
    List<LogicalVariable> aggVarList = new ArrayList<LogicalVariable>();
    List<Mutable<ILogicalExpression>> aggExprList = new ArrayList<Mutable<ILogicalExpression>>();
    LogicalVariable aggVar = context.newVar();
    List<Mutable<ILogicalExpression>> aggArgList = new ArrayList<>();
    aggVarList.add(aggVar);
    // Creates an aggregation function expression.
    aggArgList.add(new MutableObject<>(new VariableReferenceExpression(recordVar)));
    ILogicalExpression aggExpr = new AggregateFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.LISTIFY), false, aggArgList);
    aggExprList.add(new MutableObject<>(aggExpr));
    AggregateOperator aggOp = new AggregateOperator(aggVarList, aggExprList);
    // Adds the original limit operator as the input operator to the added
    // aggregate operator.
    aggOp.getInputs().add(new MutableObject<>(op));
    op.getInputs().clear();
    ILogicalOperator currentOp = op;
    if (!orderingExprs.isEmpty()) {
        OrderOperator orderOp = new OrderOperator(cloneOrderingExpression(orderingExprs));
        op.getInputs().add(new MutableObject<>(orderOp));
        currentOp = orderOp;
    }
    // Adds a nested tuple source operator as the input operator to the
    // limit operator.
    NestedTupleSourceOperator nts = new NestedTupleSourceOperator(new MutableObject<ILogicalOperator>(gbyOp));
    currentOp.getInputs().add(new MutableObject<>(nts));
    // Sets the root of the added nested plan to the aggregate operator.
    ILogicalPlan nestedPlan = new ALogicalPlanImpl();
    nestedPlan.getRoots().add(new MutableObject<>(aggOp));
    // Sets the nested plan for the added group-by operator.
    gbyOp.getNestedPlans().add(nestedPlan);
    // Updates variable mapping for ancestor operators.
    for (Pair<LogicalVariable, LogicalVariable> keyVarNewVar : keyVarNewVarPairs) {
        updateInputToOutputVarMapping(keyVarNewVar.first, keyVarNewVar.second, false);
    }
    return new Pair<>(gbyOp, aggVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) 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) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) RunningAggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.RunningAggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) Pair(org.apache.hyracks.algebricks.common.utils.Pair) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Aggregations

AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)40 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)28 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)28 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)23 Mutable (org.apache.commons.lang3.mutable.Mutable)21 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)21 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)20 ArrayList (java.util.ArrayList)19 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)17 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)14 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)13 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)10 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)10 NestedTupleSourceOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator)10 UnnestOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator)10 MutableObject (org.apache.commons.lang3.mutable.MutableObject)9 Pair (org.apache.hyracks.algebricks.common.utils.Pair)9 SubplanOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator)7 HashSet (java.util.HashSet)6 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)6