Example usage for org.apache.commons.lang3.mutable MutableObject MutableObject

List of usage examples for org.apache.commons.lang3.mutable MutableObject MutableObject

Introduction

In this page you can find the example usage for org.apache.commons.lang3.mutable MutableObject MutableObject.

Prototype

public MutableObject(final T value) 

Source Link

Document

Constructs a new MutableObject with the specified value.

Usage

From source file:edu.uci.ics.asterix.optimizer.rules.am.BTreeJobGenParams.java

private void writeBoolean(boolean val, List<Mutable<ILogicalExpression>> funcArgs) {
    ILogicalExpression keyExpr = val ? ConstantExpression.TRUE : ConstantExpression.FALSE;
    funcArgs.add(new MutableObject<ILogicalExpression>(keyExpr));
}

From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.AbstractIntroduceCombinerRule.java

protected Pair<Boolean, Mutable<ILogicalOperator>> tryToPushAgg(AggregateOperator initAgg,
        GroupByOperator newGbyOp, Set<SimilarAggregatesInfo> toReplaceSet, IOptimizationContext context)
        throws AlgebricksException {

    ArrayList<LogicalVariable> pushedVars = new ArrayList<LogicalVariable>();
    ArrayList<Mutable<ILogicalExpression>> pushedExprs = new ArrayList<Mutable<ILogicalExpression>>();

    List<LogicalVariable> initVars = initAgg.getVariables();
    List<Mutable<ILogicalExpression>> initExprs = initAgg.getExpressions();
    int numExprs = initVars.size();

    // First make sure that all agg funcs are two step, otherwise we cannot use local aggs.
    for (int i = 0; i < numExprs; i++) {
        AggregateFunctionCallExpression aggFun = (AggregateFunctionCallExpression) initExprs.get(i).getValue();
        if (!aggFun.isTwoStep()) {
            return new Pair<Boolean, Mutable<ILogicalOperator>>(false, null);
        }/*  w w  w .  j a va 2 s  .c  o  m*/
    }

    boolean haveAggToReplace = false;
    for (int i = 0; i < numExprs; i++) {
        Mutable<ILogicalExpression> expRef = initExprs.get(i);
        AggregateFunctionCallExpression aggFun = (AggregateFunctionCallExpression) expRef.getValue();
        IFunctionInfo fi1 = aggFun.getStepOneAggregate();
        // Clone the aggregate's args.
        List<Mutable<ILogicalExpression>> newArgs = new ArrayList<Mutable<ILogicalExpression>>(
                aggFun.getArguments().size());
        for (Mutable<ILogicalExpression> er : aggFun.getArguments()) {
            newArgs.add(new MutableObject<ILogicalExpression>(er.getValue().cloneExpression()));
        }
        IFunctionInfo fi2 = aggFun.getStepTwoAggregate();

        SimilarAggregatesInfo inf = new SimilarAggregatesInfo();
        LogicalVariable newAggVar = context.newVar();
        pushedVars.add(newAggVar);
        inf.stepOneResult = new VariableReferenceExpression(newAggVar);
        inf.simAggs = new ArrayList<AggregateExprInfo>();
        toReplaceSet.add(inf);
        AggregateFunctionCallExpression aggLocal = new AggregateFunctionCallExpression(fi1, false, newArgs);
        pushedExprs.add(new MutableObject<ILogicalExpression>(aggLocal));
        AggregateExprInfo aei = new AggregateExprInfo();
        aei.aggExprRef = expRef;
        aei.newFunInfo = fi2;
        inf.simAggs.add(aei);
        haveAggToReplace = true;
    }

    if (!pushedVars.isEmpty()) {
        AggregateOperator pushedAgg = new AggregateOperator(pushedVars, pushedExprs);
        pushedAgg.setExecutionMode(ExecutionMode.LOCAL);
        // If newGbyOp is null, then we optimizing an aggregate without group by.
        if (newGbyOp != null) {
            // Cut and paste nested input pipelines of initAgg to pushedAgg's input
            Mutable<ILogicalOperator> inputRef = initAgg.getInputs().get(0);
            Mutable<ILogicalOperator> bottomRef = inputRef;
            while (bottomRef.getValue().getInputs().size() > 0) {
                bottomRef = bottomRef.getValue().getInputs().get(0);
            }
            ILogicalOperator oldNts = bottomRef.getValue();
            initAgg.getInputs().clear();
            initAgg.getInputs().add(new MutableObject<ILogicalOperator>(oldNts));

            // Hook up the nested aggregate op with the outer group by.
            NestedTupleSourceOperator nts = new NestedTupleSourceOperator(
                    new MutableObject<ILogicalOperator>(newGbyOp));
            nts.setExecutionMode(ExecutionMode.LOCAL);
            bottomRef.setValue(nts);
            pushedAgg.getInputs().add(inputRef);
        } else {
            // The local aggregate operator is fed by the input of the original aggregate operator.
            pushedAgg.getInputs()
                    .add(new MutableObject<ILogicalOperator>(initAgg.getInputs().get(0).getValue()));
            // Reintroduce assign op for the global agg partitioning var.
            initAgg.getInputs().get(0).setValue(pushedAgg);
            pushedAgg.setGlobal(false);
            context.computeAndSetTypeEnvironmentForOperator(pushedAgg);
        }
        return new Pair<Boolean, Mutable<ILogicalOperator>>(true,
                new MutableObject<ILogicalOperator>(pushedAgg));
    } else {
        return new Pair<Boolean, Mutable<ILogicalOperator>>(haveAggToReplace, null);
    }
}

From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.NestedSubplanToJoinRule.java

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    if (context.checkIfInDontApplySet(this, opRef.getValue()))
        return false;
    context.addToDontApplySet(this, opRef.getValue());

    ILogicalOperator op1 = opRef.getValue();
    if (op1.getInputs().size() == 0) {
        return false;
    }/*from  w w w  .  ja  v a 2 s.c o  m*/

    boolean rewritten = false;
    for (int index = 0; index < op1.getInputs().size(); index++) {
        AbstractLogicalOperator child = (AbstractLogicalOperator) op1.getInputs().get(index).getValue();
        if (child.getOperatorTag() != LogicalOperatorTag.SUBPLAN) {
            continue;
        }

        AbstractOperatorWithNestedPlans subplan = (AbstractOperatorWithNestedPlans) child;
        Set<LogicalVariable> freeVars = new HashSet<LogicalVariable>();
        OperatorPropertiesUtil.getFreeVariablesInSubplans(subplan, freeVars);
        if (!freeVars.isEmpty()) {
            /**
             * the subplan is correlated with the outer plan, other rules can deal with it
             */
            continue;
        }

        /** get the input operator of the subplan operator */
        ILogicalOperator subplanInput = subplan.getInputs().get(0).getValue();
        AbstractLogicalOperator subplanInputOp = (AbstractLogicalOperator) subplanInput;

        /** If the other join branch is a trivial plan, do not do the rewriting. */
        if (subplanInputOp.getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE) {
            continue;
        }

        /** get all nested top operators */
        List<ILogicalPlan> nestedPlans = subplan.getNestedPlans();
        List<Mutable<ILogicalOperator>> nestedRoots = new ArrayList<Mutable<ILogicalOperator>>();
        for (ILogicalPlan nestedPlan : nestedPlans) {
            nestedRoots.addAll(nestedPlan.getRoots());
        }
        if (nestedRoots.size() == 0) {
            /** there is no nested top operators */
            continue;
        }

        /** expend the input and roots into a DAG of nested loop joins */
        Mutable<ILogicalExpression> expr = new MutableObject<ILogicalExpression>(ConstantExpression.TRUE);
        Mutable<ILogicalOperator> nestedRootRef = nestedRoots.get(0);
        ILogicalOperator join = new LeftOuterJoinOperator(expr,
                new MutableObject<ILogicalOperator>(subplanInput), nestedRootRef);

        /** rewrite the nested tuple source to be empty tuple source */
        rewriteNestedTupleSource(nestedRootRef);

        for (int i = 1; i < nestedRoots.size(); i++) {
            join = new LeftOuterJoinOperator(expr, new MutableObject<ILogicalOperator>(join),
                    nestedRoots.get(i));
        }
        op1.getInputs().get(index).setValue(join);
        context.computeAndSetTypeEnvironmentForOperator(join);
        rewritten = true;
    }
    return rewritten;
}

From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.SimpleUnnestToProductRule.java

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN
            && op.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }//w  w  w . ja v  a2s . c  o m

    Mutable<ILogicalOperator> opRef2 = op.getInputs().get(0);
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();

    if (!(op2 instanceof AbstractScanOperator) && !descOrSelfIsSourceScan(op2)) {
        return false;
    }
    // Make sure that op does not use any variables produced by op2.
    if (!opsAreIndependent(op, op2)) {
        return false;
    }

    /**
     * finding the boundary between left branch and right branch
     * operator pipeline on-top-of boundaryOpRef (exclusive) is the inner branch
     * operator pipeline under boundaryOpRef (inclusive) is the outer branch
     */
    Mutable<ILogicalOperator> currentOpRef = opRef;
    Mutable<ILogicalOperator> boundaryOpRef = currentOpRef.getValue().getInputs().get(0);
    while (currentOpRef.getValue().getInputs().size() == 1) {
        currentOpRef = currentOpRef.getValue().getInputs().get(0);
    }
    Mutable<ILogicalOperator> tupleSourceOpRef = currentOpRef;
    currentOpRef = opRef;
    if (tupleSourceOpRef.getValue().getOperatorTag() == LogicalOperatorTag.NESTEDTUPLESOURCE) {
        NestedTupleSourceOperator nts = (NestedTupleSourceOperator) tupleSourceOpRef.getValue();
        // If the subplan input is a trivial plan, do not do the rewriting.
        if (nts.getSourceOperator().getOperatorTag() != LogicalOperatorTag.EMPTYTUPLESOURCE) {
            while (currentOpRef.getValue().getInputs().size() == 1
                    && currentOpRef.getValue() instanceof AbstractScanOperator
                    && descOrSelfIsSourceScan((AbstractLogicalOperator) currentOpRef.getValue())) {
                if (opsAreIndependent(currentOpRef.getValue(), tupleSourceOpRef.getValue())) {
                    /** move down the boundary if the operator is independent of the tuple source */
                    boundaryOpRef = currentOpRef.getValue().getInputs().get(0);
                } else {
                    break;
                }
                currentOpRef = currentOpRef.getValue().getInputs().get(0);
            }
        }
    }

    /** join the two independent branches */
    InnerJoinOperator join = new InnerJoinOperator(
            new MutableObject<ILogicalExpression>(ConstantExpression.TRUE),
            new MutableObject<ILogicalOperator>(boundaryOpRef.getValue()),
            new MutableObject<ILogicalOperator>(opRef.getValue()));
    opRef.setValue(join);
    ILogicalOperator ets = new EmptyTupleSourceOperator();
    boundaryOpRef.setValue(ets);
    context.computeAndSetTypeEnvironmentForOperator(boundaryOpRef.getValue());
    context.computeAndSetTypeEnvironmentForOperator(opRef.getValue());
    context.computeAndSetTypeEnvironmentForOperator(join);
    return true;
}

From source file:edu.uci.ics.asterix.optimizer.rules.ExtractFunctionsFromJoinConditionRule.java

private boolean assignFunctionExpressions(AbstractLogicalOperator joinOp, ILogicalExpression expr,
        IOptimizationContext context) throws AlgebricksException {
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }/*  ww w . j a v  a 2  s .  co  m*/
    AbstractFunctionCallExpression fexp = (AbstractFunctionCallExpression) expr;
    FunctionIdentifier fi = fexp.getFunctionIdentifier();

    boolean modified = false;
    if (fi.equals(AlgebricksBuiltinFunctions.AND) || fi.equals(AlgebricksBuiltinFunctions.OR)
            || fi.equals(AsterixBuiltinFunctions.GET_ITEM)) {
        for (Mutable<ILogicalExpression> a : fexp.getArguments()) {
            if (assignFunctionExpressions(joinOp, a.getValue(), context)) {
                modified = true;
            }
        }
        return modified;
    } else if (AlgebricksBuiltinFunctions.isComparisonFunction(fi)
            || AsterixBuiltinFunctions.isSimilarityFunction(fi)) {
        for (Mutable<ILogicalExpression> exprRef : fexp.getArguments()) {
            if (exprRef.getValue().getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                LogicalVariable newVar = context.newVar();
                AssignOperator newAssign = new AssignOperator(newVar,
                        new MutableObject<ILogicalExpression>(exprRef.getValue().cloneExpression()));
                newAssign.setExecutionMode(joinOp.getExecutionMode());

                // Place assign below joinOp.
                List<LogicalVariable> used = new ArrayList<LogicalVariable>();
                VariableUtilities.getUsedVariables(newAssign, used);

                Mutable<ILogicalOperator> leftBranchRef = joinOp.getInputs().get(0);
                ILogicalOperator leftBranch = leftBranchRef.getValue();
                List<LogicalVariable> leftBranchVariables = new ArrayList<LogicalVariable>();
                VariableUtilities.getLiveVariables(leftBranch, leftBranchVariables);
                if (leftBranchVariables.containsAll(used)) {
                    // place assign on left branch
                    newAssign.getInputs().add(new MutableObject<ILogicalOperator>(leftBranch));
                    leftBranchRef.setValue(newAssign);
                    modified = true;
                } else {
                    Mutable<ILogicalOperator> rightBranchRef = joinOp.getInputs().get(1);
                    ILogicalOperator rightBranch = rightBranchRef.getValue();
                    List<LogicalVariable> rightBranchVariables = new ArrayList<LogicalVariable>();
                    VariableUtilities.getLiveVariables(rightBranch, rightBranchVariables);
                    if (rightBranchVariables.containsAll(used)) {
                        // place assign on right branch
                        newAssign.getInputs().add(new MutableObject<ILogicalOperator>(rightBranch));
                        rightBranchRef.setValue(newAssign);
                        modified = true;
                    }
                }

                if (modified) {
                    // Replace original expr with variable reference.
                    exprRef.setValue(new VariableReferenceExpression(newVar));
                    context.computeAndSetTypeEnvironmentForOperator(newAssign);
                    context.computeAndSetTypeEnvironmentForOperator(joinOp);
                }
            }
        }
        return modified;
    } else {
        return false;
    }
}

From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.PushLimitDownRule.java

/**
 * When a global Limit over a merge-exchange is found, a local Limit is
 * pushed down.//from  w  ww  .  ja  v a2s. co m
 */

@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.LIMIT) {
        return false;
    }
    LimitOperator opLim = (LimitOperator) op;
    if (!opLim.isTopmostLimitOp()) {
        return false;
    }

    Mutable<ILogicalOperator> opRef2 = opLim.getInputs().get(0);
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();

    if (context.checkAndAddToAlreadyCompared(op, op2)) {
        return false;
    }
    if (op2.getOperatorTag() != LogicalOperatorTag.EXCHANGE) {
        return false;
    }
    PhysicalOperatorTag op2PTag = op2.getPhysicalOperator().getOperatorTag();
    // we should test for any kind of merge
    if (op2PTag != PhysicalOperatorTag.RANDOM_MERGE_EXCHANGE
            && op2PTag != PhysicalOperatorTag.SORT_MERGE_EXCHANGE) {
        return false;
    }

    LinkedList<LogicalVariable> usedVars1 = new LinkedList<LogicalVariable>();
    VariableUtilities.getUsedVariables(opLim, usedVars1);

    do {
        if (op2.getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE
                || op2.getOperatorTag() == LogicalOperatorTag.NESTEDTUPLESOURCE
                || op2.getOperatorTag() == LogicalOperatorTag.LIMIT) {
            return false;
        }
        if (op2.getInputs().size() > 1 || !op2.isMap()) {
            break;
        }
        LinkedList<LogicalVariable> vars2 = new LinkedList<LogicalVariable>();
        VariableUtilities.getProducedVariables(op2, vars2);
        if (!OperatorPropertiesUtil.disjoint(vars2, usedVars1)) {
            return false;
        }
        // we assume pipelineable ops. have only one input
        opRef2 = op2.getInputs().get(0);
        op2 = (AbstractLogicalOperator) opRef2.getValue();
    } while (true);

    LimitOperator clone2 = null;
    if (opLim.getOffset().getValue() == null) {
        clone2 = new LimitOperator(opLim.getMaxObjects().getValue(), false);
    } else {
        // push limit (max+offset)
        IFunctionInfo finfoAdd = context.getMetadataProvider()
                .lookupFunction(AlgebricksBuiltinFunctions.NUMERIC_ADD);
        ScalarFunctionCallExpression maxPlusOffset = new ScalarFunctionCallExpression(finfoAdd,
                opLim.getMaxObjects(), opLim.getOffset());
        clone2 = new LimitOperator(maxPlusOffset, false);
    }
    clone2.setPhysicalOperator(new StreamLimitPOperator(false));
    clone2.getInputs().add(new MutableObject<ILogicalOperator>(op2));
    clone2.setExecutionMode(op2.getExecutionMode());
    clone2.recomputeSchema();
    opRef2.setValue(clone2);
    context.computeAndSetTypeEnvironmentForOperator(clone2);
    return true;
}

From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.SubplanOutOfGroupRule.java

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    AbstractLogicalOperator op0 = (AbstractLogicalOperator) opRef.getValue();
    if (op0.getOperatorTag() != LogicalOperatorTag.GROUP) {
        return false;
    }/*from   w  ww. ja va 2 s. c  om*/
    GroupByOperator gby = (GroupByOperator) op0;

    Iterator<ILogicalPlan> plansIter = gby.getNestedPlans().iterator();
    ILogicalPlan p = null;
    while (plansIter.hasNext()) {
        p = plansIter.next();
    }
    if (p == null) {
        return false;
    }
    if (p.getRoots().size() != 1) {
        return false;
    }
    Mutable<ILogicalOperator> op1Ref = p.getRoots().get(0);
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) op1Ref.getValue();
    boolean found = false;
    while (op1.getInputs().size() == 1) {
        if (op1.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
            SubplanOperator subplan = (SubplanOperator) op1;
            AbstractLogicalOperator op2 = (AbstractLogicalOperator) subplan.getInputs().get(0).getValue();
            if (OperatorPropertiesUtil.isNullTest(op2)) {
                if (subplan.getNestedPlans().size() == 1) {
                    ILogicalPlan p1 = subplan.getNestedPlans().get(0);
                    if (p1.getRoots().size() == 1) {
                        AbstractLogicalOperator r1 = (AbstractLogicalOperator) p1.getRoots().get(0).getValue();
                        if (r1.getOperatorTag() == LogicalOperatorTag.INNERJOIN
                                || r1.getOperatorTag() == LogicalOperatorTag.LEFTOUTERJOIN) {
                            // now, check that it propagates all variables,
                            // so it can be pushed
                            List<LogicalVariable> op2Vars = new ArrayList<LogicalVariable>();
                            VariableUtilities.getLiveVariables(op2, op2Vars);
                            List<LogicalVariable> op1Vars = new ArrayList<LogicalVariable>();
                            VariableUtilities.getLiveVariables(subplan, op1Vars);
                            if (op1Vars.containsAll(op2Vars)) {
                                found = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
        op1Ref = op1.getInputs().get(0);
        op1 = (AbstractLogicalOperator) op1Ref.getValue();
    }
    if (!found) {
        return false;
    }

    ILogicalOperator subplan = op1;
    ILogicalOperator op2 = op1.getInputs().get(0).getValue();
    op1Ref.setValue(op2);
    Mutable<ILogicalOperator> opUnderRef = gby.getInputs().get(0);
    ILogicalOperator opUnder = opUnderRef.getValue();
    subplan.getInputs().clear();
    subplan.getInputs().add(new MutableObject<ILogicalOperator>(opUnder));
    opUnderRef.setValue(subplan);

    return true;
}

From source file:edu.uci.ics.asterix.optimizer.rules.FuzzyEqRule.java

private boolean expandFuzzyEq(Mutable<ILogicalExpression> expRef, IOptimizationContext context,
        IVariableTypeEnvironment env, AqlMetadataProvider metadataProvider) throws AlgebricksException {
    ILogicalExpression exp = expRef.getValue();

    if (exp.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }/*from   w  ww .  jav a2 s  .  c  om*/

    boolean expanded = false;
    AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) exp;
    FunctionIdentifier fi = funcExp.getFunctionIdentifier();
    if (fi.equals(AsterixBuiltinFunctions.FUZZY_EQ)) {
        List<Mutable<ILogicalExpression>> inputExps = funcExp.getArguments();

        String simFuncName = FuzzyUtils.getSimFunction(metadataProvider);
        ArrayList<Mutable<ILogicalExpression>> similarityArgs = new ArrayList<Mutable<ILogicalExpression>>();
        for (int i = 0; i < inputExps.size(); ++i) {
            Mutable<ILogicalExpression> inputExpRef = inputExps.get(i);
            similarityArgs.add(inputExpRef);
        }

        FunctionIdentifier simFunctionIdentifier = FuzzyUtils.getFunctionIdentifier(simFuncName);
        ScalarFunctionCallExpression similarityExp = new ScalarFunctionCallExpression(
                FunctionUtils.getFunctionInfo(simFunctionIdentifier), similarityArgs);
        // Add annotations from the original fuzzy-eq function.
        similarityExp.getAnnotations().putAll(funcExp.getAnnotations());
        ArrayList<Mutable<ILogicalExpression>> cmpArgs = new ArrayList<Mutable<ILogicalExpression>>();
        cmpArgs.add(new MutableObject<ILogicalExpression>(similarityExp));
        IAObject simThreshold = FuzzyUtils.getSimThreshold(metadataProvider, simFuncName);
        cmpArgs.add(new MutableObject<ILogicalExpression>(
                new ConstantExpression(new AsterixConstantValue(simThreshold))));
        ScalarFunctionCallExpression cmpExpr = FuzzyUtils.getComparisonExpr(simFuncName, cmpArgs);
        expRef.setValue(cmpExpr);
        return true;
    } else if (fi.equals(AlgebricksBuiltinFunctions.AND) || fi.equals(AlgebricksBuiltinFunctions.OR)) {
        for (int i = 0; i < 2; i++) {
            if (expandFuzzyEq(funcExp.getArguments().get(i), context, env, metadataProvider)) {
                expanded = true;
            }
        }
    }
    return expanded;
}

From source file:edu.uci.ics.asterix.optimizer.rules.IfElseToSwitchCaseFunctionRule.java

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.ASSIGN)
        return false;

    AssignOperator assignOp = (AssignOperator) op1;
    List<Mutable<ILogicalExpression>> assignExprs = assignOp.getExpressions();
    if (assignExprs.size() > 1)
        return false;
    ILogicalExpression expr = assignExprs.get(0).getValue();
    if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
        AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
        if (!funcExpr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.CONCAT_NON_NULL))
            return false;
    }//from   w  w  w  . j a v  a 2  s. c  o  m

    AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
    if (op2.getOperatorTag() != LogicalOperatorTag.SUBPLAN)
        return false;

    SubplanOperator subplan = (SubplanOperator) op2;
    List<ILogicalPlan> subPlans = subplan.getNestedPlans();
    List<Mutable<ILogicalExpression>> arguments = new ArrayList<Mutable<ILogicalExpression>>();
    for (ILogicalPlan plan : subPlans) {
        List<Mutable<ILogicalOperator>> roots = plan.getRoots();

        AbstractLogicalOperator nestedRoot = (AbstractLogicalOperator) roots.get(0).getValue();
        if (nestedRoot.getOperatorTag() != LogicalOperatorTag.SELECT)
            return false;
        SelectOperator selectOp = (SelectOperator) nestedRoot;

        AbstractLogicalOperator nestedNextOp = (AbstractLogicalOperator) nestedRoot.getInputs().get(0)
                .getValue();
        if (nestedNextOp.getOperatorTag() != LogicalOperatorTag.ASSIGN)
            return false;
        AssignOperator assignRoot = (AssignOperator) nestedNextOp;
        Mutable<ILogicalExpression> actionExprRef = assignRoot.getExpressions().get(0);

        arguments.add(selectOp.getCondition());
        arguments.add(actionExprRef);
        AbstractLogicalOperator nestedBottomOp = (AbstractLogicalOperator) assignRoot.getInputs().get(0)
                .getValue();

        if (nestedBottomOp.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE)
            return false;
    }

    AbstractLogicalOperator op3 = (AbstractLogicalOperator) op2.getInputs().get(0).getValue();
    if (op3.getOperatorTag() != LogicalOperatorTag.ASSIGN)
        return false;

    AssignOperator bottomAssign = (AssignOperator) op3;
    LogicalVariable conditionVar = bottomAssign.getVariables().get(0);
    Mutable<ILogicalExpression> switchCondition = new MutableObject<ILogicalExpression>(
            new VariableReferenceExpression(conditionVar));
    List<Mutable<ILogicalExpression>> argumentRefs = new ArrayList<Mutable<ILogicalExpression>>();
    argumentRefs.add(switchCondition);
    argumentRefs.addAll(arguments);

    /** replace the branch conditions */
    for (int i = 0; i < arguments.size(); i += 2) {
        if (arguments.get(i).getValue().equals(switchCondition.getValue())) {
            arguments.get(i).setValue(ConstantExpression.TRUE);
        } else {
            arguments.get(i).setValue(ConstantExpression.FALSE);
        }
    }

    ILogicalExpression callExpr = new ScalarFunctionCallExpression(
            FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.SWITCH_CASE), argumentRefs);

    assignOp.getInputs().get(0).setValue(op3);
    assignOp.getExpressions().get(0).setValue(callExpr);
    context.computeAndSetTypeEnvironmentForOperator(assignOp);
    return true;
}

From source file:edu.uci.ics.hyracks.algebricks.rewriter.rules.EliminateSubplanRule.java

private void elimSubplanOverEts(Mutable<ILogicalOperator> opRef, IOptimizationContext ctx)
        throws AlgebricksException {
    SubplanOperator subplan = (SubplanOperator) opRef.getValue();
    for (ILogicalPlan p : subplan.getNestedPlans()) {
        for (Mutable<ILogicalOperator> r : p.getRoots()) {
            OperatorManipulationUtil.ntsToEts(r, ctx);
        }//from   ww w  . j  a  v a 2  s . co m
    }
    LinkedList<Mutable<ILogicalOperator>> allRoots = subplan.allRootsInReverseOrder();
    if (allRoots.size() == 1) {
        opRef.setValue(allRoots.get(0).getValue());
    } else {
        ILogicalOperator topOp = null;
        for (Mutable<ILogicalOperator> r : allRoots) {
            if (topOp == null) {
                topOp = r.getValue();
            } else {
                LeftOuterJoinOperator j = new LeftOuterJoinOperator(
                        new MutableObject<ILogicalExpression>(ConstantExpression.TRUE));
                j.getInputs().add(new MutableObject<ILogicalOperator>(topOp));
                j.getInputs().add(r);
                ctx.setOutputTypeEnvironment(j, j.computeOutputTypeEnvironment(ctx));
                topOp = j;
            }
        }
        opRef.setValue(topOp);
    }
}