Search in sources :

Example 41 with GroupByOperator

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

the class ExtractGbyExpressionsRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.GROUP) {
        return false;
    }
    if (context.checkIfInDontApplySet(this, op1)) {
        return false;
    }
    context.addToDontApplySet(this, op1);
    GroupByOperator g = (GroupByOperator) op1;
    boolean r1 = gbyExprWasRewritten(g, context);
    boolean r2 = decorExprWasRewritten(g, context);
    boolean fired = r1 || r2;
    if (fired) {
        context.computeAndSetTypeEnvironmentForOperator(g);
    }
    return fired;
}
Also used : GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)

Example 42 with GroupByOperator

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

the class CancelUnnestWithNestedListifyRule method applies.

private boolean applies(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> varUsedAbove, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }
    UnnestOperator unnest1 = (UnnestOperator) op1;
    ILogicalExpression expr = unnest1.getExpressionRef().getValue();
    LogicalVariable unnestedVar;
    switch(expr.getExpressionTag()) {
        case VARIABLE:
            unnestedVar = ((VariableReferenceExpression) expr).getVariableReference();
            break;
        case FUNCTION_CALL:
            if (((AbstractFunctionCallExpression) expr).getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
                return false;
            }
            AbstractFunctionCallExpression functionCall = (AbstractFunctionCallExpression) expr;
            ILogicalExpression functionCallArgExpr = functionCall.getArguments().get(0).getValue();
            if (functionCallArgExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
                return false;
            }
            unnestedVar = ((VariableReferenceExpression) functionCallArgExpr).getVariableReference();
            break;
        default:
            return false;
    }
    if (varUsedAbove.contains(unnestedVar)) {
        return false;
    }
    Mutable<ILogicalOperator> opRef2 = op1.getInputs().get(0);
    AbstractLogicalOperator r = (AbstractLogicalOperator) opRef2.getValue();
    if (r.getOperatorTag() != LogicalOperatorTag.GROUP) {
        return false;
    }
    // go inside of a group-by plan
    GroupByOperator gby = (GroupByOperator) r;
    if (gby.getNestedPlans().size() != 1) {
        return false;
    }
    if (gby.getNestedPlans().get(0).getRoots().size() != 1) {
        return false;
    }
    AbstractLogicalOperator nestedPlanRoot = (AbstractLogicalOperator) gby.getNestedPlans().get(0).getRoots().get(0).getValue();
    if (nestedPlanRoot.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    AggregateOperator agg = (AggregateOperator) nestedPlanRoot;
    Mutable<ILogicalOperator> aggInputOpRef = agg.getInputs().get(0);
    if (agg.getVariables().size() > 1) {
        return false;
    }
    if (OperatorManipulationUtil.ancestorOfOperators(agg, ImmutableSet.of(LogicalOperatorTag.LIMIT, LogicalOperatorTag.ORDER, LogicalOperatorTag.GROUP, LogicalOperatorTag.DISTINCT))) {
        return false;
    }
    LogicalVariable aggVar = agg.getVariables().get(0);
    ILogicalExpression aggFun = agg.getExpressions().get(0).getValue();
    if (!aggVar.equals(unnestedVar) || ((AbstractLogicalExpression) aggFun).getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) aggFun;
    if (!BuiltinFunctions.LISTIFY.equals(f.getFunctionIdentifier())) {
        return false;
    }
    if (f.getArguments().size() != 1) {
        return false;
    }
    ILogicalExpression arg0 = f.getArguments().get(0).getValue();
    if (((AbstractLogicalExpression) arg0).getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    LogicalVariable paramVar = ((VariableReferenceExpression) arg0).getVariableReference();
    ArrayList<LogicalVariable> assgnVars = new ArrayList<LogicalVariable>(1);
    assgnVars.add(unnest1.getVariable());
    ArrayList<Mutable<ILogicalExpression>> assgnExprs = new ArrayList<Mutable<ILogicalExpression>>(1);
    assgnExprs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(paramVar)));
    AssignOperator assign = new AssignOperator(assgnVars, assgnExprs);
    LogicalVariable posVar = unnest1.getPositionalVariable();
    if (posVar == null) {
        // Creates assignment for group-by keys.
        ArrayList<LogicalVariable> gbyKeyAssgnVars = new ArrayList<LogicalVariable>();
        ArrayList<Mutable<ILogicalExpression>> gbyKeyAssgnExprs = new ArrayList<Mutable<ILogicalExpression>>();
        for (int i = 0; i < gby.getGroupByList().size(); i++) {
            if (gby.getGroupByList().get(i).first != null) {
                gbyKeyAssgnVars.add(gby.getGroupByList().get(i).first);
                gbyKeyAssgnExprs.add(gby.getGroupByList().get(i).second);
            }
        }
        // Moves the nested pipeline before aggregation out of the group-by op.
        Mutable<ILogicalOperator> bottomOpRef = aggInputOpRef;
        AbstractLogicalOperator bottomOp = (AbstractLogicalOperator) bottomOpRef.getValue();
        while (bottomOp.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
            bottomOpRef = bottomOp.getInputs().get(0);
            bottomOp = (AbstractLogicalOperator) bottomOpRef.getValue();
        }
        // Removes the group-by operator.
        opRef.setValue(assign);
        assign.getInputs().add(aggInputOpRef);
        AssignOperator gbyKeyAssign = new AssignOperator(gbyKeyAssgnVars, gbyKeyAssgnExprs);
        gbyKeyAssign.getInputs().add(gby.getInputs().get(0));
        bottomOpRef.setValue(gbyKeyAssign);
        context.computeAndSetTypeEnvironmentForOperator(gbyKeyAssign);
        context.computeAndSetTypeEnvironmentForOperator(assign);
    } else {
        // if positional variable is used in unnest, the unnest will be pushed into the group-by as a running-aggregate
        // First create assign for the unnest variable
        List<LogicalVariable> nestedAssignVars = new ArrayList<LogicalVariable>();
        List<Mutable<ILogicalExpression>> nestedAssignExprs = new ArrayList<Mutable<ILogicalExpression>>();
        nestedAssignVars.add(unnest1.getVariable());
        nestedAssignExprs.add(new MutableObject<ILogicalExpression>(arg0));
        AssignOperator nestedAssign = new AssignOperator(nestedAssignVars, nestedAssignExprs);
        nestedAssign.getInputs().add(opRef2);
        // Then create running aggregation for the positional variable
        List<LogicalVariable> raggVars = new ArrayList<LogicalVariable>();
        List<Mutable<ILogicalExpression>> raggExprs = new ArrayList<Mutable<ILogicalExpression>>();
        raggVars.add(posVar);
        StatefulFunctionCallExpression fce = new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE);
        raggExprs.add(new MutableObject<ILogicalExpression>(fce));
        RunningAggregateOperator raggOp = new RunningAggregateOperator(raggVars, raggExprs);
        raggOp.setExecutionMode(unnest1.getExecutionMode());
        RunningAggregatePOperator raggPOp = new RunningAggregatePOperator();
        raggOp.setPhysicalOperator(raggPOp);
        raggOp.getInputs().add(nestedPlanRoot.getInputs().get(0));
        gby.getNestedPlans().get(0).getRoots().set(0, new MutableObject<ILogicalOperator>(raggOp));
        opRef.setValue(nestedAssign);
        context.computeAndSetTypeEnvironmentForOperator(nestedAssign);
        context.computeAndSetTypeEnvironmentForOperator(raggOp);
        context.computeAndSetTypeEnvironmentForOperator(gby);
    }
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) StatefulFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) RunningAggregatePOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.RunningAggregatePOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) 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) RunningAggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.RunningAggregateOperator) RunningAggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.RunningAggregateOperator)

Example 43 with GroupByOperator

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

the class AbstractPreclusteredGroupByPOperator method computeDeliveredProperties.

// Obs: We don't propagate properties corresponding to decors, since they
// are func. dep. on the group-by variables.
@Override
public void computeDeliveredProperties(ILogicalOperator op, IOptimizationContext context) {
    List<ILocalStructuralProperty> propsLocal = new ArrayList<>();
    GroupByOperator gby = (GroupByOperator) op;
    ILogicalOperator op2 = gby.getInputs().get(0).getValue();
    IPhysicalPropertiesVector childProp = op2.getDeliveredPhysicalProperties();
    IPartitioningProperty pp = childProp.getPartitioningProperty();
    List<ILocalStructuralProperty> childLocals = childProp.getLocalProperties();
    if (childLocals == null) {
        deliveredProperties = new StructuralPropertiesVector(pp, propsLocal);
        return;
    }
    for (ILocalStructuralProperty lsp : childLocals) {
        ILocalStructuralProperty propagatedLsp = getPropagatedProperty(lsp, gby);
        if (propagatedLsp != null) {
            propsLocal.add(propagatedLsp);
        }
    }
    deliveredProperties = new StructuralPropertiesVector(pp, propsLocal);
}
Also used : StructuralPropertiesVector(org.apache.hyracks.algebricks.core.algebra.properties.StructuralPropertiesVector) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) IPhysicalPropertiesVector(org.apache.hyracks.algebricks.core.algebra.properties.IPhysicalPropertiesVector) ILocalStructuralProperty(org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty) IPartitioningProperty(org.apache.hyracks.algebricks.core.algebra.properties.IPartitioningProperty)

Example 44 with GroupByOperator

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

the class OperatorDeepCopyVisitor method visitGroupByOperator.

@Override
public ILogicalOperator visitGroupByOperator(GroupByOperator op, Void arg) throws AlgebricksException {
    List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> groupByList = new ArrayList<>();
    List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decoList = new ArrayList<>();
    ArrayList<ILogicalPlan> newSubplans = new ArrayList<>();
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> pair : op.getGroupByList()) {
        groupByList.add(new Pair<>(pair.first, deepCopyExpressionRef(pair.second)));
    }
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> pair : op.getDecorList()) {
        decoList.add(new Pair<>(pair.first, deepCopyExpressionRef(pair.second)));
    }
    GroupByOperator gbyOp = new GroupByOperator(groupByList, decoList, newSubplans, op.isGroupAll());
    for (ILogicalPlan plan : op.getNestedPlans()) {
        newSubplans.add(OperatorManipulationUtil.deepCopy(plan, gbyOp));
    }
    return gbyOp;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) ArrayList(java.util.ArrayList) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 45 with GroupByOperator

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

the class LogicalOperatorDeepCopyWithNewVariablesVisitor method visitGroupByOperator.

@Override
public ILogicalOperator visitGroupByOperator(GroupByOperator op, ILogicalOperator arg) throws AlgebricksException {
    List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> groupByListCopy = deepCopyVariableExpressionReferencePairList(op.getGroupByList());
    List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorListCopy = deepCopyVariableExpressionReferencePairList(op.getDecorList());
    List<ILogicalPlan> nestedPlansCopy = new ArrayList<ILogicalPlan>();
    GroupByOperator opCopy = new GroupByOperator(groupByListCopy, decorListCopy, nestedPlansCopy, op.isGroupAll());
    deepCopyInputsAnnotationsAndExecutionMode(op, arg, opCopy);
    deepCopyPlanList(op.getNestedPlans(), nestedPlansCopy, opCopy);
    return opCopy;
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) ArrayList(java.util.ArrayList) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)49 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)36 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)33 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)27 ArrayList (java.util.ArrayList)26 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)24 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)23 Mutable (org.apache.commons.lang3.mutable.Mutable)22 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)20 Pair (org.apache.hyracks.algebricks.common.utils.Pair)15 AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)14 HashSet (java.util.HashSet)10 MutableObject (org.apache.commons.lang3.mutable.MutableObject)10 NestedTupleSourceOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator)10 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)8 LinkedList (java.util.LinkedList)7 ListSet (org.apache.hyracks.algebricks.common.utils.ListSet)6 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)6 SubplanOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator)6 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)5