Search in sources :

Example 26 with UnnestOperator

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

the class InsertUpsertCheckUtil method containsDatasetAccessInternal.

// Checks whether a query plan rooted at <code>op</code> contains dataset accesses.
private static boolean containsDatasetAccessInternal(ILogicalOperator op) {
    if (op.getOperatorTag() == LogicalOperatorTag.UNNEST) {
        UnnestOperator unnestOp = (UnnestOperator) op;
        ILogicalExpression unnestExpr = unnestOp.getExpressionRef().getValue();
        UnnestingFunctionCallExpression unnestingFuncExpr = (UnnestingFunctionCallExpression) unnestExpr;
        return unnestingFuncExpr.getFunctionIdentifier().equals(BuiltinFunctions.DATASET);
    }
    if (op.getOperatorTag() == LogicalOperatorTag.SUBPLAN && containsDatasetAccess((SubplanOperator) op)) {
        return true;
    }
    for (Mutable<ILogicalOperator> childRef : op.getInputs()) {
        if (containsDatasetAccessInternal(childRef.getValue())) {
            return true;
        }
    }
    return false;
}
Also used : UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)

Example 27 with UnnestOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator 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 28 with UnnestOperator

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

the class DisjunctivePredicateToJoinRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
    if (metadataProvider.isBlockingOperatorDisabled()) {
        return false;
    }
    SelectOperator select;
    if ((select = asSelectOperator(opRef)) == null) {
        return false;
    }
    AbstractFunctionCallExpression condEx;
    if ((condEx = asFunctionCallExpression(select.getCondition(), AlgebricksBuiltinFunctions.OR)) == null) {
        return false;
    }
    List<Mutable<ILogicalExpression>> args = condEx.getArguments();
    VariableReferenceExpression varEx = null;
    IAType valType = null;
    HashSet<AsterixConstantValue> values = new HashSet<AsterixConstantValue>();
    for (Mutable<ILogicalExpression> arg : args) {
        AbstractFunctionCallExpression fctCall;
        if ((fctCall = asFunctionCallExpression(arg, AlgebricksBuiltinFunctions.EQ)) == null) {
            return false;
        }
        boolean haveConst = false;
        boolean haveVar = false;
        List<Mutable<ILogicalExpression>> fctArgs = fctCall.getArguments();
        for (Mutable<ILogicalExpression> fctArg : fctArgs) {
            final ILogicalExpression argExpr = fctArg.getValue();
            switch(argExpr.getExpressionTag()) {
                case CONSTANT:
                    haveConst = true;
                    AsterixConstantValue value = (AsterixConstantValue) ((ConstantExpression) argExpr).getValue();
                    if (valType == null) {
                        valType = value.getObject().getType();
                    } else if (!isCompatible(valType, value.getObject().getType())) {
                        return false;
                    }
                    values.add(value);
                    break;
                case VARIABLE:
                    haveVar = true;
                    final VariableReferenceExpression varArg = (VariableReferenceExpression) argExpr;
                    if (varEx == null) {
                        varEx = varArg;
                    } else if (!varEx.getVariableReference().equals(varArg.getVariableReference())) {
                        return false;
                    }
                    break;
                default:
                    return false;
            }
        }
        if (!(haveVar && haveConst)) {
            return false;
        }
    }
    AOrderedList list = new AOrderedList(new AOrderedListType(valType, "orderedlist"));
    for (AsterixConstantValue value : values) {
        list.add(value.getObject());
    }
    EmptyTupleSourceOperator ets = new EmptyTupleSourceOperator();
    context.computeAndSetTypeEnvironmentForOperator(ets);
    ILogicalExpression cExp = new ConstantExpression(new AsterixConstantValue(list));
    Mutable<ILogicalExpression> mutCExp = new MutableObject<>(cExp);
    IFunctionInfo scanFctInfo = BuiltinFunctions.getAsterixFunctionInfo(BuiltinFunctions.SCAN_COLLECTION);
    UnnestingFunctionCallExpression scanExp = new UnnestingFunctionCallExpression(scanFctInfo, mutCExp);
    LogicalVariable scanVar = context.newVar();
    UnnestOperator unn = new UnnestOperator(scanVar, new MutableObject<>(scanExp));
    unn.getInputs().add(new MutableObject<>(ets));
    context.computeAndSetTypeEnvironmentForOperator(unn);
    IFunctionInfo eqFctInfo = BuiltinFunctions.getAsterixFunctionInfo(AlgebricksBuiltinFunctions.EQ);
    AbstractFunctionCallExpression eqExp = new ScalarFunctionCallExpression(eqFctInfo);
    eqExp.getArguments().add(new MutableObject<>(new VariableReferenceExpression(scanVar)));
    eqExp.getArguments().add(new MutableObject<>(varEx.cloneExpression()));
    eqExp.getAnnotations().put(IndexedNLJoinExpressionAnnotation.INSTANCE, IndexedNLJoinExpressionAnnotation.INSTANCE);
    BroadcastExpressionAnnotation bcast = new BroadcastExpressionAnnotation();
    // Broadcast the OR predicates branch.
    bcast.setObject(BroadcastExpressionAnnotation.BroadcastSide.LEFT);
    eqExp.getAnnotations().put(BroadcastExpressionAnnotation.BROADCAST_ANNOTATION_KEY, bcast);
    InnerJoinOperator jOp = new InnerJoinOperator(new MutableObject<>(eqExp));
    jOp.getInputs().add(new MutableObject<>(unn));
    jOp.getInputs().add(select.getInputs().get(0));
    opRef.setValue(jOp);
    context.computeAndSetTypeEnvironmentForOperator(jOp);
    return true;
}
Also used : IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) InnerJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InnerJoinOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) BroadcastExpressionAnnotation(org.apache.hyracks.algebricks.core.algebra.expressions.BroadcastExpressionAnnotation) EmptyTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.EmptyTupleSourceOperator) HashSet(java.util.HashSet) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) IAType(org.apache.asterix.om.types.IAType)

Example 29 with UnnestOperator

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

the class IntroduceUnnestForCollectionToSequenceRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
        return false;
    }
    AssignOperator assign = (AssignOperator) op;
    List<Mutable<ILogicalExpression>> exprs = assign.getExpressions();
    if (exprs.size() != 1) {
        return false;
    }
    ILogicalExpression expr = exprs.get(0).getValue();
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression func = (AbstractFunctionCallExpression) expr;
    if (func.getFunctionIdentifier() != BuiltinFunctions.COLLECTION_TO_SEQUENCE) {
        return false;
    }
    IVariableTypeEnvironment env = assign.computeInputTypeEnvironment(context);
    ILogicalExpression argExpr = func.getArguments().get(0).getValue();
    IAType outerExprType = (IAType) env.getType(expr);
    IAType innerExprType = (IAType) env.getType(argExpr);
    if (outerExprType.equals(innerExprType)) {
        /** nothing is changed with the collection-to-sequence function, remove the collection-sequence function call */
        assign.getExpressions().set(0, new MutableObject<ILogicalExpression>(argExpr));
        return true;
    }
    /** change the assign operator to an unnest operator */
    LogicalVariable var = assign.getVariables().get(0);
    @SuppressWarnings("unchecked") UnnestOperator unnest = new UnnestOperator(var, new MutableObject<ILogicalExpression>(new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), new MutableObject<ILogicalExpression>(argExpr))));
    unnest.getInputs().addAll(assign.getInputs());
    opRef.setValue(unnest);
    context.computeAndSetTypeEnvironmentForOperator(unnest);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IAType(org.apache.asterix.om.types.IAType)

Aggregations

UnnestOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator)29 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)24 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)23 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)19 Mutable (org.apache.commons.lang3.mutable.Mutable)18 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)17 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)16 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)14 ArrayList (java.util.ArrayList)11 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)11 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)11 AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)10 MutableObject (org.apache.commons.lang3.mutable.MutableObject)9 Pair (org.apache.hyracks.algebricks.common.utils.Pair)8 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)7 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)6 LeftOuterUnnestOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator)6 HashSet (java.util.HashSet)5 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)5 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)5