Search in sources :

Example 6 with UnnestOperator

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

the class InlineUnnestFunctionRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    if (context.checkIfInDontApplySet(this, op1)) {
        return false;
    }
    context.addToDontApplySet(this, op1);
    if (op1.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }
    UnnestOperator unnestOperator = (UnnestOperator) op1;
    AbstractFunctionCallExpression expr = (AbstractFunctionCallExpression) unnestOperator.getExpressionRef().getValue();
    //we only inline for the scan-collection function
    if (expr.getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
        return false;
    }
    // inline all variables from an unnesting function call
    AbstractFunctionCallExpression funcExpr = expr;
    List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
    for (int i = 0; i < args.size(); i++) {
        ILogicalExpression argExpr = args.get(i).getValue();
        if (argExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression varExpr = (VariableReferenceExpression) argExpr;
            inlineVariable(varExpr.getVariableReference(), unnestOperator);
        }
    }
    return true;
}
Also used : 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) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)

Example 7 with UnnestOperator

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

the class FeedScanCollectionToUnnest method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (context.checkIfInDontApplySet(this, op)) {
        return false;
    }
    if (op.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        context.addToDontApplySet(this, op);
        return false;
    }
    UnnestOperator unnest = (UnnestOperator) op;
    ILogicalExpression unnestExpr = unnest.getExpressionRef().getValue();
    if (needsScanCollection(unnestExpr, op)) {
        ILogicalExpression newExpr = new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), new MutableObject<ILogicalExpression>(unnestExpr));
        unnest.getExpressionRef().setValue(newExpr);
        context.addToDontApplySet(this, op);
        return true;
    }
    context.addToDontApplySet(this, op);
    return false;
}
Also used : UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)

Example 8 with UnnestOperator

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

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

the class LogicalOperatorDeepCopyWithNewVariablesVisitor method visitUnnestOperator.

@Override
public ILogicalOperator visitUnnestOperator(UnnestOperator op, ILogicalOperator arg) throws AlgebricksException {
    UnnestOperator opCopy = new UnnestOperator(deepCopyVariable(op.getVariable()), exprDeepCopyVisitor.deepCopyExpressionReference(op.getExpressionRef()), deepCopyVariable(op.getPositionalVariable()), op.getPositionalVariableType(), op.getPositionWriter());
    deepCopyInputsAnnotationsAndExecutionMode(op, arg, opCopy);
    return opCopy;
}
Also used : LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator)

Example 10 with UnnestOperator

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

the class InlineAllNtsInSubplanVisitor method createUnnestForAggregatedList.

private Pair<ILogicalOperator, LogicalVariable> createUnnestForAggregatedList(LogicalVariable aggVar) {
    LogicalVariable unnestVar = context.newVar();
    // Creates an unnest function expression.
    Mutable<ILogicalExpression> unnestArg = new MutableObject<ILogicalExpression>(new VariableReferenceExpression(aggVar));
    List<Mutable<ILogicalExpression>> unnestArgList = new ArrayList<Mutable<ILogicalExpression>>();
    unnestArgList.add(unnestArg);
    Mutable<ILogicalExpression> unnestExpr = new MutableObject<ILogicalExpression>(new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), unnestArgList));
    ILogicalOperator unnestOp = new UnnestOperator(unnestVar, unnestExpr);
    return new Pair<ILogicalOperator, LogicalVariable>(unnestOp, unnestVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

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