Example usage for org.apache.commons.lang3.mutable Mutable getValue

List of usage examples for org.apache.commons.lang3.mutable Mutable getValue

Introduction

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

Prototype

T getValue();

Source Link

Document

Gets the value of this mutable.

Usage

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

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    AbstractLogicalOperator op0 = (AbstractLogicalOperator) opRef.getValue();
    if (op0.getOperatorTag() != LogicalOperatorTag.SINK) {
        return false;
    }/*  ww  w.j ava2 s.c  om*/
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) op0.getInputs().get(0).getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE) {
        return false;
    }

    FunctionIdentifier fid = null;
    /** find the record variable */
    InsertDeleteOperator insertOp = (InsertDeleteOperator) op1;
    ILogicalExpression recordExpr = insertOp.getPayloadExpression().getValue();
    List<LogicalVariable> recordVar = new ArrayList<LogicalVariable>();
    /** assume the payload is always a single variable expression */
    recordExpr.getUsedVariables(recordVar);

    /**
     * op2 is the assign operator which extract primary keys from the record
     * variable
     */
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();

    if (recordVar.size() == 0) {
        /**
         * For the case primary key-assignment expressions are constant
         * expressions, find assign op that creates record to be
         * inserted/deleted.
         */
        while (fid != AsterixBuiltinFunctions.OPEN_RECORD_CONSTRUCTOR) {
            if (op2.getInputs().size() == 0) {
                return false;
            }
            op2 = (AbstractLogicalOperator) op2.getInputs().get(0).getValue();
            if (op2.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
                continue;
            }
            AssignOperator assignOp = (AssignOperator) op2;
            ILogicalExpression assignExpr = assignOp.getExpressions().get(0).getValue();
            if (assignExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                ScalarFunctionCallExpression funcExpr = (ScalarFunctionCallExpression) assignOp.getExpressions()
                        .get(0).getValue();
                fid = funcExpr.getFunctionIdentifier();
            }
        }
        AssignOperator assignOp2 = (AssignOperator) op2;
        recordVar.addAll(assignOp2.getVariables());
    }
    AqlDataSource datasetSource = (AqlDataSource) insertOp.getDataSource();
    AqlMetadataProvider mp = (AqlMetadataProvider) context.getMetadataProvider();
    String dataverseName = datasetSource.getId().getDataverseName();
    String datasetName = datasetSource.getId().getDatasourceName();
    Dataset dataset = mp.findDataset(dataverseName, datasetName);
    if (dataset == null) {
        throw new AlgebricksException("Unknown dataset " + datasetName + " in dataverse " + dataverseName);
    }
    if (dataset.getDatasetType() == DatasetType.EXTERNAL) {
        return false;
    }

    // Create operators for secondary index insert/delete.
    String itemTypeName = dataset.getItemTypeName();
    IAType itemType = mp.findType(dataset.getDataverseName(), itemTypeName);
    if (itemType.getTypeTag() != ATypeTag.RECORD) {
        throw new AlgebricksException("Only record types can be indexed.");
    }
    ARecordType recType = (ARecordType) itemType;
    List<Index> indexes = mp.getDatasetIndexes(dataset.getDataverseName(), dataset.getDatasetName());
    ILogicalOperator currentTop = op1;
    boolean hasSecondaryIndex = false;

    // Put an n-gram or a keyword index in the later stage of index-update,
    // since TokenizeOperator needs to be involved.
    Collections.sort(indexes, new Comparator<Index>() {
        @Override
        public int compare(Index o1, Index o2) {
            return o1.getIndexType().ordinal() - o2.getIndexType().ordinal();
        }

    });

    // Check whether multiple keyword or n-gram indexes exist
    int secondaryIndexTotalCnt = 0;
    for (Index index : indexes) {
        if (index.isSecondaryIndex())
            secondaryIndexTotalCnt++;
    }

    // Initialize inputs to the SINK operator
    if (secondaryIndexTotalCnt > 0) {
        op0.getInputs().clear();
    }

    // Replicate Operator is applied only when doing the bulk-load.
    AbstractLogicalOperator replicateOp = null;

    if (secondaryIndexTotalCnt > 1 && insertOp.isBulkload()) {
        // Split the logical plan into "each secondary index update branch"
        // to replicate each <PK,RECORD> pair.
        replicateOp = new ReplicateOperator(secondaryIndexTotalCnt);
        replicateOp.getInputs().add(new MutableObject<ILogicalOperator>(currentTop));
        replicateOp.setExecutionMode(ExecutionMode.PARTITIONED);
        context.computeAndSetTypeEnvironmentForOperator(replicateOp);
        currentTop = replicateOp;
    }

    // Prepare filtering field information
    List<String> additionalFilteringField = ((InternalDatasetDetails) dataset.getDatasetDetails())
            .getFilterField();
    List<LogicalVariable> additionalFilteringVars = null;
    List<Mutable<ILogicalExpression>> additionalFilteringAssignExpressions = null;
    List<Mutable<ILogicalExpression>> additionalFilteringExpressions = null;
    AssignOperator additionalFilteringAssign = null;

    if (additionalFilteringField != null) {
        additionalFilteringVars = new ArrayList<LogicalVariable>();
        additionalFilteringAssignExpressions = new ArrayList<Mutable<ILogicalExpression>>();
        additionalFilteringExpressions = new ArrayList<Mutable<ILogicalExpression>>();
        prepareVarAndExpression(additionalFilteringField, recType.getFieldNames(), recordVar.get(0),
                additionalFilteringAssignExpressions, additionalFilteringVars, context);
        additionalFilteringAssign = new AssignOperator(additionalFilteringVars,
                additionalFilteringAssignExpressions);
        for (LogicalVariable var : additionalFilteringVars) {
            additionalFilteringExpressions
                    .add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(var)));
        }
    }

    // Iterate each secondary index and applying Index Update operations.
    for (Index index : indexes) {
        List<LogicalVariable> projectVars = new ArrayList<LogicalVariable>();
        VariableUtilities.getUsedVariables(op1, projectVars);
        if (!index.isSecondaryIndex()) {
            continue;
        }
        LogicalVariable enforcedRecordVar = recordVar.get(0);
        hasSecondaryIndex = true;
        //if the index is enforcing field types
        if (index.isEnforcingKeyFileds()) {
            try {
                DatasetDataSource ds = (DatasetDataSource) (insertOp.getDataSource());
                ARecordType insertRecType = (ARecordType) ds.getSchemaTypes()[ds.getSchemaTypes().length - 1];
                LogicalVariable castVar = context.newVar();
                ARecordType enforcedType = createEnforcedType(insertRecType, index);
                //introduce casting to enforced type
                AbstractFunctionCallExpression castFunc = new ScalarFunctionCallExpression(
                        FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.CAST_RECORD));

                castFunc.getArguments()
                        .add(new MutableObject<ILogicalExpression>(insertOp.getPayloadExpression().getValue()));
                TypeComputerUtilities.setRequiredAndInputTypes(castFunc, enforcedType, insertRecType);
                AssignOperator newAssignOperator = new AssignOperator(castVar,
                        new MutableObject<ILogicalExpression>(castFunc));
                newAssignOperator.getInputs().add(new MutableObject<ILogicalOperator>(currentTop));
                currentTop = newAssignOperator;
                //project out casted record
                projectVars.add(castVar);
                enforcedRecordVar = castVar;
                context.computeAndSetTypeEnvironmentForOperator(newAssignOperator);
                context.computeAndSetTypeEnvironmentForOperator(currentTop);
                recType = enforcedType;
            } catch (AsterixException e) {
                throw new AlgebricksException(e);
            }
        }

        List<List<String>> secondaryKeyFields = index.getKeyFieldNames();
        List<IAType> secondaryKeyTypes = index.getKeyFieldTypes();
        List<LogicalVariable> secondaryKeyVars = new ArrayList<LogicalVariable>();
        List<Mutable<ILogicalExpression>> expressions = new ArrayList<Mutable<ILogicalExpression>>();
        List<Mutable<ILogicalExpression>> secondaryExpressions = new ArrayList<Mutable<ILogicalExpression>>();

        for (List<String> secondaryKey : secondaryKeyFields) {
            prepareVarAndExpression(secondaryKey, recType.getFieldNames(), enforcedRecordVar, expressions,
                    secondaryKeyVars, context);
        }

        AssignOperator assign = new AssignOperator(secondaryKeyVars, expressions);
        ProjectOperator project = new ProjectOperator(projectVars);

        if (additionalFilteringAssign != null) {
            additionalFilteringAssign.getInputs().add(new MutableObject<ILogicalOperator>(project));
            assign.getInputs().add(new MutableObject<ILogicalOperator>(additionalFilteringAssign));
        } else {
            assign.getInputs().add(new MutableObject<ILogicalOperator>(project));
        }

        // Only apply replicate operator when doing bulk-load
        if (secondaryIndexTotalCnt > 1 && insertOp.isBulkload())
            project.getInputs().add(new MutableObject<ILogicalOperator>(replicateOp));
        else
            project.getInputs().add(new MutableObject<ILogicalOperator>(currentTop));

        context.computeAndSetTypeEnvironmentForOperator(project);

        if (additionalFilteringAssign != null) {
            context.computeAndSetTypeEnvironmentForOperator(additionalFilteringAssign);
        }

        context.computeAndSetTypeEnvironmentForOperator(assign);
        currentTop = assign;

        // BTree, Keyword, or n-gram index case
        if (index.getIndexType() == IndexType.BTREE
                || index.getIndexType() == IndexType.SINGLE_PARTITION_WORD_INVIX
                || index.getIndexType() == IndexType.SINGLE_PARTITION_NGRAM_INVIX
                || index.getIndexType() == IndexType.LENGTH_PARTITIONED_WORD_INVIX
                || index.getIndexType() == IndexType.LENGTH_PARTITIONED_NGRAM_INVIX) {
            for (LogicalVariable secondaryKeyVar : secondaryKeyVars) {
                secondaryExpressions.add(new MutableObject<ILogicalExpression>(
                        new VariableReferenceExpression(secondaryKeyVar)));
            }
            Mutable<ILogicalExpression> filterExpression = createFilterExpression(secondaryKeyVars,
                    context.getOutputTypeEnvironment(currentTop), false);
            AqlIndex dataSourceIndex = new AqlIndex(index, dataverseName, datasetName, mp);

            // Introduce the TokenizeOperator only when doing bulk-load,
            // and index type is keyword or n-gram.
            if (index.getIndexType() != IndexType.BTREE && insertOp.isBulkload()) {

                // Check whether the index is length-partitioned or not.
                // If partitioned, [input variables to TokenizeOperator,
                // token, number of token] pairs will be generated and
                // fed into the IndexInsertDeleteOperator.
                // If not, [input variables, token] pairs will be generated
                // and fed into the IndexInsertDeleteOperator.
                // Input variables are passed since TokenizeOperator is not an
                // filtering operator.
                boolean isPartitioned = false;
                if (index.getIndexType() == IndexType.LENGTH_PARTITIONED_WORD_INVIX
                        || index.getIndexType() == IndexType.LENGTH_PARTITIONED_NGRAM_INVIX)
                    isPartitioned = true;

                // Create a new logical variable - token
                List<LogicalVariable> tokenizeKeyVars = new ArrayList<LogicalVariable>();
                List<Mutable<ILogicalExpression>> tokenizeKeyExprs = new ArrayList<Mutable<ILogicalExpression>>();
                LogicalVariable tokenVar = context.newVar();
                tokenizeKeyVars.add(tokenVar);
                tokenizeKeyExprs
                        .add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(tokenVar)));

                // Check the field type of the secondary key.
                IAType secondaryKeyType = null;
                Pair<IAType, Boolean> keyPairType = Index.getNonNullableKeyFieldType(secondaryKeyFields.get(0),
                        recType);
                secondaryKeyType = keyPairType.first;

                List<Object> varTypes = new ArrayList<Object>();
                varTypes.add(NonTaggedFormatUtil.getTokenType(secondaryKeyType));

                // If the index is a length-partitioned, then create
                // additional variable - number of token.
                // We use a special type for the length-partitioned index.
                // The type is short, and this does not contain type info.
                if (isPartitioned) {
                    LogicalVariable lengthVar = context.newVar();
                    tokenizeKeyVars.add(lengthVar);
                    tokenizeKeyExprs.add(
                            new MutableObject<ILogicalExpression>(new VariableReferenceExpression(lengthVar)));
                    varTypes.add(BuiltinType.SHORTWITHOUTTYPEINFO);
                }

                // TokenizeOperator to tokenize [SK, PK] pairs
                TokenizeOperator tokenUpdate = new TokenizeOperator(dataSourceIndex,
                        insertOp.getPrimaryKeyExpressions(), secondaryExpressions, tokenizeKeyVars,
                        filterExpression, insertOp.getOperation(), insertOp.isBulkload(), isPartitioned,
                        varTypes);
                tokenUpdate.getInputs().add(new MutableObject<ILogicalOperator>(assign));
                context.computeAndSetTypeEnvironmentForOperator(tokenUpdate);

                IndexInsertDeleteOperator indexUpdate = new IndexInsertDeleteOperator(dataSourceIndex,
                        insertOp.getPrimaryKeyExpressions(), tokenizeKeyExprs, filterExpression,
                        insertOp.getOperation(), insertOp.isBulkload());
                indexUpdate.setAdditionalFilteringExpressions(additionalFilteringExpressions);
                indexUpdate.getInputs().add(new MutableObject<ILogicalOperator>(tokenUpdate));

                context.computeAndSetTypeEnvironmentForOperator(indexUpdate);

                currentTop = indexUpdate;
                op0.getInputs().add(new MutableObject<ILogicalOperator>(currentTop));

            } else {
                // When TokenizeOperator is not needed
                IndexInsertDeleteOperator indexUpdate = new IndexInsertDeleteOperator(dataSourceIndex,
                        insertOp.getPrimaryKeyExpressions(), secondaryExpressions, filterExpression,
                        insertOp.getOperation(), insertOp.isBulkload());
                indexUpdate.setAdditionalFilteringExpressions(additionalFilteringExpressions);
                indexUpdate.getInputs().add(new MutableObject<ILogicalOperator>(currentTop));

                currentTop = indexUpdate;
                context.computeAndSetTypeEnvironmentForOperator(indexUpdate);

                if (insertOp.isBulkload())
                    op0.getInputs().add(new MutableObject<ILogicalOperator>(currentTop));

            }

        } else if (index.getIndexType() == IndexType.RTREE) {
            Pair<IAType, Boolean> keyPairType = Index.getNonNullableOpenFieldType(secondaryKeyTypes.get(0),
                    secondaryKeyFields.get(0), recType);
            IAType spatialType = keyPairType.first;
            int dimension = NonTaggedFormatUtil.getNumDimensions(spatialType.getTypeTag());
            int numKeys = dimension * 2;
            List<LogicalVariable> keyVarList = new ArrayList<LogicalVariable>();
            List<Mutable<ILogicalExpression>> keyExprList = new ArrayList<Mutable<ILogicalExpression>>();
            for (int i = 0; i < numKeys; i++) {
                LogicalVariable keyVar = context.newVar();
                keyVarList.add(keyVar);
                AbstractFunctionCallExpression createMBR = new ScalarFunctionCallExpression(
                        FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.CREATE_MBR));
                createMBR.getArguments().add(new MutableObject<ILogicalExpression>(
                        new VariableReferenceExpression(secondaryKeyVars.get(0))));
                createMBR.getArguments().add(new MutableObject<ILogicalExpression>(
                        new ConstantExpression(new AsterixConstantValue(new AInt32(dimension)))));
                createMBR.getArguments().add(new MutableObject<ILogicalExpression>(
                        new ConstantExpression(new AsterixConstantValue(new AInt32(i)))));
                keyExprList.add(new MutableObject<ILogicalExpression>(createMBR));
            }
            for (LogicalVariable secondaryKeyVar : keyVarList) {
                secondaryExpressions.add(new MutableObject<ILogicalExpression>(
                        new VariableReferenceExpression(secondaryKeyVar)));
            }
            AssignOperator assignCoordinates = new AssignOperator(keyVarList, keyExprList);
            assignCoordinates.getInputs().add(new MutableObject<ILogicalOperator>(currentTop));
            context.computeAndSetTypeEnvironmentForOperator(assignCoordinates);
            // We must enforce the filter if the originating spatial type is
            // nullable.
            boolean forceFilter = keyPairType.second;
            Mutable<ILogicalExpression> filterExpression = createFilterExpression(keyVarList,
                    context.getOutputTypeEnvironment(assignCoordinates), forceFilter);
            AqlIndex dataSourceIndex = new AqlIndex(index, dataverseName, datasetName, mp);
            IndexInsertDeleteOperator indexUpdate = new IndexInsertDeleteOperator(dataSourceIndex,
                    insertOp.getPrimaryKeyExpressions(), secondaryExpressions, filterExpression,
                    insertOp.getOperation(), insertOp.isBulkload());
            indexUpdate.setAdditionalFilteringExpressions(additionalFilteringExpressions);
            indexUpdate.getInputs().add(new MutableObject<ILogicalOperator>(assignCoordinates));
            currentTop = indexUpdate;
            context.computeAndSetTypeEnvironmentForOperator(indexUpdate);

            if (insertOp.isBulkload())
                op0.getInputs().add(new MutableObject<ILogicalOperator>(currentTop));

        }

    }
    if (!hasSecondaryIndex) {
        return false;
    }

    if (!insertOp.isBulkload()) {
        op0.getInputs().clear();
        op0.getInputs().add(new MutableObject<ILogicalOperator>(currentTop));
    }
    return true;
}

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

private void inferTypes(ILogicalOperator op, IOptimizationContext context) throws AlgebricksException {
    for (Mutable<ILogicalOperator> childOpRef : op.getInputs()) {
        inferTypes(childOpRef.getValue(), context);
    }/*from w w w. j  a v  a2 s . c  om*/
    context.computeAndSetTypeEnvironmentForOperator(op);
}

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

private ScalarFunctionCallExpression findTokensFunc(FunctionIdentifier funcId, IOptimizableFuncExpr optFuncExpr,
        int subTreeIndex) {
    //find either a gram-tokens or a word-tokens function that exists in optFuncExpr.subTrees' assignsAndUnnests
    OptimizableOperatorSubTree subTree = null;
    LogicalVariable targetVar = null;// w  ww .  j a  v a  2 s  .co  m

    subTree = optFuncExpr.getOperatorSubTree(subTreeIndex);
    if (subTree == null) {
        return null;
    }

    targetVar = optFuncExpr.getLogicalVar(subTreeIndex);
    if (targetVar == null) {
        return null;
    }

    for (AbstractLogicalOperator op : subTree.assignsAndUnnests) {
        if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN)
            continue;
        List<Mutable<ILogicalExpression>> exprList = ((AssignOperator) op).getExpressions();
        for (Mutable<ILogicalExpression> expr : exprList) {
            if (expr.getValue().getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL)
                continue;
            AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr.getValue();
            if (funcExpr.getFunctionIdentifier() != funcId)
                continue;
            ILogicalExpression varExpr = funcExpr.getArguments().get(0).getValue();
            if (varExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE)
                continue;
            if (((VariableReferenceExpression) varExpr).getVariableReference() == targetVar)
                continue;
            return (ScalarFunctionCallExpression) funcExpr;
        }
    }
    return null;
}

From source file:enumj.Enumerator.java

/**
 * Returns an infinite enumerator obtained by applying repeatedly the
 * provided unary operator.//from w w w  .  j  a  va  2 s . com
 * <p>
 * The resulted enumerator returns <code>seed</code>,
 * <code>f(seed)</code>, <code>f(f(seed))</code> ...
 * </p>
 *
 * @param <E> the type of enumerated elements
 * @param seed the initial element
 * @param f state-less {@link Function} instance to apply on the previous
 * element to obtain the next element
 * @return the iterated enumerator
 * @exception IllegalArgumentException <code>f</code> is null.
 */
public static <E> Enumerator<E> iterate(E seed, UnaryOperator<E> f) {
    Checks.ensureNotNull(f, Messages.NULL_ENUMERATOR_GENERATOR);
    final Mutable<E> result = new MutableObject(seed);
    final MutableBoolean first = new MutableBoolean(true);
    return of(() -> {
        if (first.booleanValue()) {
            first.setValue(false);
        } else {
            result.setValue(f.apply(result.getValue()));
        }
        return Optional.of(result.getValue());
    });
}

From source file:edu.uci.ics.asterix.runtime.formats.NonTaggedDataFormat.java

private boolean[] computeOpenFields(AbstractFunctionCallExpression expr, ARecordType recType) {
    int n = expr.getArguments().size() / 2;
    boolean[] open = new boolean[n];
    for (int i = 0; i < n; i++) {
        Mutable<ILogicalExpression> argRef = expr.getArguments().get(2 * i);
        ILogicalExpression arg = argRef.getValue();
        if (arg.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
            String fn = ((AString) ((AsterixConstantValue) ((ConstantExpression) arg).getValue()).getObject())
                    .getStringValue();// ww w  .  j  av  a 2  s.  c  o m
            open[i] = true;
            for (String s : recType.getFieldNames()) {
                if (s.equals(fn)) {
                    open[i] = false;
                    break;
                }
            }
        } else {
            open[i] = true;
        }
    }
    return open;
}

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

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    Collection<LogicalVariable> joinLiveVarsLeft = new HashSet<LogicalVariable>();
    Collection<LogicalVariable> joinLiveVarsRight = new HashSet<LogicalVariable>();
    Collection<LogicalVariable> liveInOpsToPushLeft = new HashSet<LogicalVariable>();
    Collection<LogicalVariable> liveInOpsToPushRight = new HashSet<LogicalVariable>();

    List<ILogicalOperator> pushedOnLeft = new ArrayList<ILogicalOperator>();
    List<ILogicalOperator> pushedOnRight = new ArrayList<ILogicalOperator>();
    LinkedList<ILogicalOperator> notPushedStack = new LinkedList<ILogicalOperator>();
    Collection<LogicalVariable> usedVars = new HashSet<LogicalVariable>();
    Collection<LogicalVariable> producedVars = new HashSet<LogicalVariable>();

    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.SELECT) {
        return false;
    }//from   w w w .j  a v  a2 s.  co m
    SelectOperator select = (SelectOperator) op;
    Mutable<ILogicalOperator> opRef2 = op.getInputs().get(0);
    AbstractLogicalOperator son = (AbstractLogicalOperator) opRef2.getValue();
    AbstractLogicalOperator op2 = son;
    boolean needToPushOps = false;
    while (son.isMap()) {
        needToPushOps = true;
        Mutable<ILogicalOperator> opRefLink = son.getInputs().get(0);
        son = (AbstractLogicalOperator) opRefLink.getValue();
    }

    if (son.getOperatorTag() != LogicalOperatorTag.INNERJOIN
            && son.getOperatorTag() != LogicalOperatorTag.LEFTOUTERJOIN) {
        return false;
    }
    boolean isLoj = son.getOperatorTag() == LogicalOperatorTag.LEFTOUTERJOIN;
    AbstractBinaryJoinOperator join = (AbstractBinaryJoinOperator) son;

    Mutable<ILogicalOperator> joinBranchLeftRef = join.getInputs().get(0);
    Mutable<ILogicalOperator> joinBranchRightRef = join.getInputs().get(1);

    if (needToPushOps) {
        ILogicalOperator joinBranchLeft = joinBranchLeftRef.getValue();
        ILogicalOperator joinBranchRight = joinBranchRightRef.getValue();
        VariableUtilities.getLiveVariables(joinBranchLeft, joinLiveVarsLeft);
        VariableUtilities.getLiveVariables(joinBranchRight, joinLiveVarsRight);
        Mutable<ILogicalOperator> opIterRef = opRef2;
        ILogicalOperator opIter = op2;
        while (opIter != join) {
            LogicalOperatorTag tag = ((AbstractLogicalOperator) opIter).getOperatorTag();
            if (tag == LogicalOperatorTag.PROJECT) {
                notPushedStack.addFirst(opIter);
            } else {
                VariableUtilities.getUsedVariables(opIter, usedVars);
                VariableUtilities.getProducedVariables(opIter, producedVars);
                if (joinLiveVarsLeft.containsAll(usedVars)) {
                    pushedOnLeft.add(opIter);
                    liveInOpsToPushLeft.addAll(producedVars);
                } else if (joinLiveVarsRight.containsAll(usedVars)) {
                    pushedOnRight.add(opIter);
                    liveInOpsToPushRight.addAll(producedVars);
                } else {
                    return false;
                }
            }
            opIterRef = opIter.getInputs().get(0);
            opIter = opIterRef.getValue();
        }
        if (isLoj && pushedOnLeft.isEmpty()) {
            return false;
        }
    }

    boolean intersectsAllBranches = true;
    boolean[] intersectsBranch = new boolean[join.getInputs().size()];
    LinkedList<LogicalVariable> selectVars = new LinkedList<LogicalVariable>();
    select.getCondition().getValue().getUsedVariables(selectVars);
    int i = 0;
    for (Mutable<ILogicalOperator> branch : join.getInputs()) {
        LinkedList<LogicalVariable> branchVars = new LinkedList<LogicalVariable>();
        VariableUtilities.getLiveVariables(branch.getValue(), branchVars);
        if (i == 0) {
            branchVars.addAll(liveInOpsToPushLeft);
        } else {
            branchVars.addAll(liveInOpsToPushRight);
        }
        if (OperatorPropertiesUtil.disjoint(selectVars, branchVars)) {
            intersectsAllBranches = false;
        } else {
            intersectsBranch[i] = true;
        }
        i++;
    }
    if (!intersectsBranch[0] && !intersectsBranch[1]) {
        return false;
    }
    if (needToPushOps) {
        pushOps(pushedOnLeft, joinBranchLeftRef, context);
        pushOps(pushedOnRight, joinBranchRightRef, context);
    }
    if (intersectsAllBranches) {
        addCondToJoin(select, join, context);
    } else { // push down
        Iterator<Mutable<ILogicalOperator>> branchIter = join.getInputs().iterator();
        ILogicalExpression selectCondition = select.getCondition().getValue();
        boolean lojToInner = false;
        for (int j = 0; j < intersectsBranch.length; j++) {
            Mutable<ILogicalOperator> branch = branchIter.next();
            boolean inter = intersectsBranch[j];
            if (inter) {
                if (j > 0 && isLoj) {
                    // if a left outer join, if the select condition is not-null filtering,
                    // we rewrite left outer join
                    // to inner join for this case.
                    if (containsNotNullFiltering(selectCondition)) {
                        lojToInner = true;
                    }
                }
                if ((j > 0 && isLoj) && containsNullFiltering(selectCondition)) {
                    // Select is-null($$var) cannot be pushed in the right branch of a LOJ;
                    notPushedStack.addFirst(select);
                } else {
                    // Conditions for the left branch can always be pushed.
                    // Other conditions can be pushed to the right branch of a LOJ.
                    copySelectToBranch(select, branch, context);
                }
            }
        }
        if (lojToInner) {
            // Rewrites left outer join  to inner join.
            InnerJoinOperator innerJoin = new InnerJoinOperator(join.getCondition());
            innerJoin.getInputs().addAll(join.getInputs());
            join = innerJoin;
            context.computeAndSetTypeEnvironmentForOperator(join);
        }
    }
    ILogicalOperator top = join;
    for (ILogicalOperator npOp : notPushedStack) {
        List<Mutable<ILogicalOperator>> npInpList = npOp.getInputs();
        npInpList.clear();
        npInpList.add(new MutableObject<ILogicalOperator>(top));
        context.computeAndSetTypeEnvironmentForOperator(npOp);
        top = npOp;
    }
    opRef.setValue(top);
    return true;

}

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

private boolean replaceWithVariableArg(Mutable<ILogicalExpression> expRef, FunctionIdentifier normFuncIdent,
        AsterixConstantValue constVal, VariableReferenceExpression varRefExpr, List<AssignOperator> assigns,
        IOptimizationContext context) throws AlgebricksException {

    // Find variable in assigns to determine its originating function.
    LogicalVariable var = varRefExpr.getVariableReference();
    Mutable<ILogicalExpression> simFuncExprRef = null;
    ScalarFunctionCallExpression simCheckFuncExpr = null;
    AssignOperator matchingAssign = null;
    for (int i = 0; i < assigns.size(); i++) {
        AssignOperator assign = assigns.get(i);
        for (int j = 0; j < assign.getVariables().size(); j++) {
            // Check if variables match.
            if (var != assign.getVariables().get(j)) {
                continue;
            }/*from   w  w  w .j  a  v  a  2s .c o  m*/
            // Check if corresponding expr is a function call.
            if (assign.getExpressions().get(j).getValue()
                    .getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
                continue;
            }
            simFuncExprRef = assign.getExpressions().get(j);
            // Analyze function expression and get equivalent similarity check function.
            simCheckFuncExpr = getSimilarityCheckExpr(normFuncIdent, constVal,
                    (AbstractFunctionCallExpression) simFuncExprRef.getValue());
            matchingAssign = assign;
            break;
        }
        if (simCheckFuncExpr != null) {
            break;
        }
    }

    // Only non-null if we found that varRefExpr refers to an optimizable similarity function call.
    if (simCheckFuncExpr != null) {
        // Create a new assign under matchingAssign which assigns the result of our similarity-check function to a variable.
        LogicalVariable newVar = context.newVar();
        AssignOperator newAssign = new AssignOperator(newVar,
                new MutableObject<ILogicalExpression>(simCheckFuncExpr));
        // Hook up inputs.
        newAssign.getInputs()
                .add(new MutableObject<ILogicalOperator>(matchingAssign.getInputs().get(0).getValue()));
        matchingAssign.getInputs().get(0).setValue(newAssign);

        // Replace select condition with a get-item on newVar.
        List<Mutable<ILogicalExpression>> selectGetItemArgs = new ArrayList<Mutable<ILogicalExpression>>();
        // First arg is a variable reference expr on newVar.
        selectGetItemArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newVar)));
        // Second arg is the item index to be accessed, here 0.
        selectGetItemArgs.add(new MutableObject<ILogicalExpression>(
                new ConstantExpression(new AsterixConstantValue(new AInt32(0)))));
        ILogicalExpression selectGetItemExpr = new ScalarFunctionCallExpression(
                FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.GET_ITEM), selectGetItemArgs);
        // Replace the old similarity function call with the new getItemExpr.
        expRef.setValue(selectGetItemExpr);

        // Replace expr corresponding to original variable in the original assign with a get-item on newVar.
        List<Mutable<ILogicalExpression>> assignGetItemArgs = new ArrayList<Mutable<ILogicalExpression>>();
        // First arg is a variable reference expr on newVar.
        assignGetItemArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newVar)));
        // Second arg is the item index to be accessed, here 1.
        assignGetItemArgs.add(new MutableObject<ILogicalExpression>(
                new ConstantExpression(new AsterixConstantValue(new AInt32(1)))));
        ILogicalExpression assignGetItemExpr = new ScalarFunctionCallExpression(
                FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.GET_ITEM), assignGetItemArgs);
        // Replace the original assign expr with the get-item expr.
        simFuncExprRef.setValue(assignGetItemExpr);

        context.computeAndSetTypeEnvironmentForOperator(newAssign);
        context.computeAndSetTypeEnvironmentForOperator(matchingAssign);

        return true;
    }

    return false;
}

From source file:edu.uci.ics.asterix.translator.AqlExpressionToPlanTranslator.java

private Pair<ILogicalOperator, LogicalVariable> produceFlwrResult(boolean noForClause, boolean isTop,
        Mutable<ILogicalOperator> resOpRef, LogicalVariable resVar) {
    if (isTop) {/* w  w w  . jav a  2 s.c o  m*/
        ProjectOperator pr = new ProjectOperator(resVar);
        pr.getInputs().add(resOpRef);
        return new Pair<ILogicalOperator, LogicalVariable>(pr, resVar);

    } else if (noForClause) {
        return new Pair<ILogicalOperator, LogicalVariable>(resOpRef.getValue(), resVar);
    } else {
        return aggListify(resVar, resOpRef, false);
    }
}

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

private void addLocalEnforcers(AbstractLogicalOperator op, int i,
        List<ILocalStructuralProperty> localProperties, boolean nestedPlan, IOptimizationContext context)
        throws AlgebricksException {
    if (AlgebricksConfig.DEBUG) {
        AlgebricksConfig.ALGEBRICKS_LOGGER
                .fine(">>>> Adding local enforcers for local props = " + localProperties + "\n");
    }//w  w  w.  j a v  a 2s .c  o m

    if (localProperties == null || localProperties.isEmpty()) {
        return;
    }

    Mutable<ILogicalOperator> topOp = new MutableObject<ILogicalOperator>();
    topOp.setValue(op.getInputs().get(i).getValue());
    LinkedList<LocalOrderProperty> oList = new LinkedList<LocalOrderProperty>();

    for (ILocalStructuralProperty prop : localProperties) {
        switch (prop.getPropertyType()) {
        case LOCAL_ORDER_PROPERTY: {
            oList.add((LocalOrderProperty) prop);
            break;
        }
        case LOCAL_GROUPING_PROPERTY: {
            LocalGroupingProperty g = (LocalGroupingProperty) prop;
            Collection<LogicalVariable> vars = (g.getPreferredOrderEnforcer() != null)
                    ? g.getPreferredOrderEnforcer()
                    : g.getColumnSet();
            List<OrderColumn> orderColumns = new ArrayList<OrderColumn>();
            for (LogicalVariable v : vars) {
                OrderColumn oc = new OrderColumn(v, OrderKind.ASC);
                orderColumns.add(oc);
            }
            LocalOrderProperty lop = new LocalOrderProperty(orderColumns);
            oList.add(lop);
            break;
        }
        default: {
            throw new IllegalStateException();
        }
        }
    }
    if (!oList.isEmpty()) {
        topOp = enforceOrderProperties(oList, topOp, nestedPlan, context);
    }

    op.getInputs().set(i, topOp);
    OperatorPropertiesUtil.computeSchemaAndPropertiesRecIfNull((AbstractLogicalOperator) topOp.getValue(),
            context);
    printOp((AbstractLogicalOperator) topOp.getValue());
}

From source file:edu.uci.ics.asterix.translator.AqlExpressionToPlanTranslator.java

@Override
public Pair<ILogicalOperator, LogicalVariable> visitFlworExpression(FLWOGRExpression flwor,
        Mutable<ILogicalOperator> tupSource) throws AsterixException {
    Mutable<ILogicalOperator> flworPlan = tupSource;
    boolean isTop = context.isTopFlwor();
    if (isTop) {//from   w w  w. j  a v a2 s.co  m
        context.setTopFlwor(false);
    }
    for (Clause c : flwor.getClauseList()) {
        Pair<ILogicalOperator, LogicalVariable> pC = c.accept(this, flworPlan);
        flworPlan = new MutableObject<ILogicalOperator>(pC.first);
    }

    Expression r = flwor.getReturnExpr();
    boolean noFlworClause = flwor.noForClause();

    if (r.getKind() == Kind.VARIABLE_EXPRESSION) {
        VariableExpr v = (VariableExpr) r;
        LogicalVariable var = context.getVar(v.getVar().getId());

        return produceFlwrResult(noFlworClause, isTop, flworPlan, var);

    } else {
        Mutable<ILogicalOperator> baseOp = new MutableObject<ILogicalOperator>(flworPlan.getValue());
        Pair<ILogicalOperator, LogicalVariable> rRes = r.accept(this, baseOp);
        ILogicalOperator rOp = rRes.first;
        ILogicalOperator resOp;
        if (expressionNeedsNoNesting(r)) {
            baseOp.setValue(flworPlan.getValue());
            resOp = rOp;
        } else {
            SubplanOperator s = new SubplanOperator(rOp);
            s.getInputs().add(flworPlan);
            resOp = s;
            baseOp.setValue(new NestedTupleSourceOperator(new MutableObject<ILogicalOperator>(s)));
        }
        Mutable<ILogicalOperator> resOpRef = new MutableObject<ILogicalOperator>(resOp);
        return produceFlwrResult(noFlworClause, isTop, resOpRef, rRes.second);
    }
}