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.typecast.StaticTypeCastUtil.java

/**
 * This method statically cast the type of records from their current type to the required type.
 * //from ww w .ja v  a  2  s  .  co m
 * @param func
 *            The record constructor expression.
 * @param reqType
 *            The required type.
 * @param inputType
 *            The current type.
 * @param env
 *            The type environment.
 * @throws AlgebricksException
 */
private static boolean staticRecordTypeCast(AbstractFunctionCallExpression func, ARecordType reqType,
        ARecordType inputType, IVariableTypeEnvironment env) throws AlgebricksException {
    if (!(func.getFunctionIdentifier() == AsterixBuiltinFunctions.OPEN_RECORD_CONSTRUCTOR
            || func.getFunctionIdentifier() == AsterixBuiltinFunctions.CLOSED_RECORD_CONSTRUCTOR)) {
        return false;
    }
    IAType[] reqFieldTypes = reqType.getFieldTypes();
    String[] reqFieldNames = reqType.getFieldNames();
    IAType[] inputFieldTypes = inputType.getFieldTypes();
    String[] inputFieldNames = inputType.getFieldNames();

    int[] fieldPermutation = new int[reqFieldTypes.length];
    boolean[] nullFields = new boolean[reqFieldTypes.length];
    boolean[] openFields = new boolean[inputFieldTypes.length];

    Arrays.fill(nullFields, false);
    Arrays.fill(openFields, true);
    Arrays.fill(fieldPermutation, -1);

    // forward match: match from actual to required
    boolean matched = false;
    for (int i = 0; i < inputFieldNames.length; i++) {
        String fieldName = inputFieldNames[i];
        IAType fieldType = inputFieldTypes[i];

        if (2 * i + 1 > func.getArguments().size()) {
            // it is not a record constructor function
            return false;
        }

        // 2*i+1 is the index of field value expression
        ILogicalExpression arg = func.getArguments().get(2 * i + 1).getValue();
        matched = false;
        for (int j = 0; j < reqFieldNames.length; j++) {
            String reqFieldName = reqFieldNames[j];
            IAType reqFieldType = reqFieldTypes[j];
            if (fieldName.equals(reqFieldName)) {
                //type matched
                if (fieldType.equals(reqFieldType)) {
                    fieldPermutation[j] = i;
                    openFields[i] = false;
                    matched = true;

                    if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                        ScalarFunctionCallExpression scalarFunc = (ScalarFunctionCallExpression) arg;
                        rewriteFuncExpr(scalarFunc, reqFieldType, fieldType, env);
                    }
                    break;
                }

                // match the optional field
                if (reqFieldType.getTypeTag() == ATypeTag.UNION
                        && NonTaggedFormatUtil.isOptionalField((AUnionType) reqFieldType)) {
                    IAType itemType = ((AUnionType) reqFieldType).getUnionList()
                            .get(AUnionType.OPTIONAL_TYPE_INDEX_IN_UNION_LIST);
                    reqFieldType = itemType;
                    if (fieldType.equals(BuiltinType.ANULL) || fieldType.equals(itemType)) {
                        fieldPermutation[j] = i;
                        openFields[i] = false;
                        matched = true;

                        // rewrite record expr
                        if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                            ScalarFunctionCallExpression scalarFunc = (ScalarFunctionCallExpression) arg;
                            rewriteFuncExpr(scalarFunc, reqFieldType, fieldType, env);
                        }
                        break;
                    }
                }

                // match the optional type input for a non-optional field
                // delay that to runtime by calling the not-null function
                if (fieldType.getTypeTag() == ATypeTag.UNION
                        && NonTaggedFormatUtil.isOptionalField((AUnionType) fieldType)) {
                    IAType itemType = ((AUnionType) fieldType).getUnionList()
                            .get(AUnionType.OPTIONAL_TYPE_INDEX_IN_UNION_LIST);
                    if (reqFieldType.equals(itemType)) {
                        fieldPermutation[j] = i;
                        openFields[i] = false;
                        matched = true;

                        ScalarFunctionCallExpression notNullFunc = new ScalarFunctionCallExpression(
                                FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.NOT_NULL));
                        notNullFunc.getArguments().add(new MutableObject<ILogicalExpression>(arg));
                        //wrap the not null function to the original function
                        func.getArguments().get(2 * i + 1).setValue(notNullFunc);
                        break;
                    }
                }

                // match the record field: need cast
                if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                    ScalarFunctionCallExpression scalarFunc = (ScalarFunctionCallExpression) arg;
                    rewriteFuncExpr(scalarFunc, reqFieldType, fieldType, env);
                    fieldPermutation[j] = i;
                    openFields[i] = false;
                    matched = true;
                    break;
                }
            }
        }
        // the input has extra fields
        if (!matched && !reqType.isOpen()) {
            throw new AlgebricksException(
                    "static type mismatch: the input record includes an extra closed field " + fieldName + ":"
                            + fieldType + "! Please check the field name and type.");
        }
    }

    // backward match: match from required to actual
    for (int i = 0; i < reqFieldNames.length; i++) {
        String reqFieldName = reqFieldNames[i];
        IAType reqFieldType = reqFieldTypes[i];
        matched = false;
        for (int j = 0; j < inputFieldNames.length; j++) {
            String fieldName = inputFieldNames[j];
            IAType fieldType = inputFieldTypes[j];
            if (!fieldName.equals(reqFieldName))
                continue;
            // should check open field here
            // because number of entries in fieldPermuations is the
            // number of required schema fields
            // here we want to check if an input field is matched
            // the entry index of fieldPermuatons is req field index
            if (!openFields[j]) {
                matched = true;
                break;
            }

            // match the optional field
            if (reqFieldType.getTypeTag() == ATypeTag.UNION
                    && NonTaggedFormatUtil.isOptionalField((AUnionType) reqFieldType)) {
                IAType itemType = ((AUnionType) reqFieldType).getUnionList()
                        .get(AUnionType.OPTIONAL_TYPE_INDEX_IN_UNION_LIST);
                if (fieldType.equals(BuiltinType.ANULL) || fieldType.equals(itemType)) {
                    matched = true;
                    break;
                }
            }
        }
        if (matched)
            continue;

        if (reqFieldType.getTypeTag() == ATypeTag.UNION
                && NonTaggedFormatUtil.isOptionalField((AUnionType) reqFieldType)) {
            // add a null field
            nullFields[i] = true;
        } else {
            // no matched field in the input for a required closed field
            if (inputType.isOpen()) {
                //if the input type is open, return false, give that to dynamic type cast to defer the error to the runtime
                return false;
            } else {
                throw new AlgebricksException(
                        "static type mismatch: the input record misses a required closed field " + reqFieldName
                                + ":" + reqFieldType + "! Please check the field name and type.");
            }
        }
    }

    List<Mutable<ILogicalExpression>> arguments = func.getArguments();
    List<Mutable<ILogicalExpression>> originalArguments = new ArrayList<Mutable<ILogicalExpression>>();
    originalArguments.addAll(arguments);
    arguments.clear();
    // re-order the closed part and fill in null fields
    for (int i = 0; i < fieldPermutation.length; i++) {
        int pos = fieldPermutation[i];
        if (pos >= 0) {
            arguments.add(originalArguments.get(2 * pos));
            arguments.add(originalArguments.get(2 * pos + 1));
        }
        if (nullFields[i]) {
            // add a null field
            arguments.add(new MutableObject<ILogicalExpression>(
                    new ConstantExpression(new AsterixConstantValue(new AString(reqFieldNames[i])))));
            arguments.add(new MutableObject<ILogicalExpression>(
                    new ConstantExpression(new AsterixConstantValue(ANull.NULL))));
        }
    }

    // add the open part
    for (int i = 0; i < openFields.length; i++) {
        if (openFields[i]) {
            arguments.add(originalArguments.get(2 * i));
            Mutable<ILogicalExpression> expRef = originalArguments.get(2 * i + 1);
            injectCastToRelaxType(expRef, inputFieldTypes[i], env);
            arguments.add(expRef);
        }
    }
    return true;
}

From source file:edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.visitors.OperatorDeepCopyVisitor.java

@Override
public ILogicalOperator visitIndexInsertDeleteOperator(IndexInsertDeleteOperator op, Void arg)
        throws AlgebricksException {
    List<Mutable<ILogicalExpression>> newPrimaryKeyExpressions = new ArrayList<Mutable<ILogicalExpression>>();
    deepCopyExpressionRefs(newPrimaryKeyExpressions, op.getPrimaryKeyExpressions());
    List<Mutable<ILogicalExpression>> newSecondaryKeyExpressions = new ArrayList<Mutable<ILogicalExpression>>();
    deepCopyExpressionRefs(newSecondaryKeyExpressions, op.getSecondaryKeyExpressions());
    Mutable<ILogicalExpression> newFilterExpression = new MutableObject<ILogicalExpression>(
            ((AbstractLogicalExpression) op.getFilterExpression()).cloneExpression());
    List<Mutable<ILogicalExpression>> newLSMComponentFilterExpressions = new ArrayList<Mutable<ILogicalExpression>>();
    deepCopyExpressionRefs(newLSMComponentFilterExpressions, op.getAdditionalFilteringExpressions());
    IndexInsertDeleteOperator indexInsertDeleteOp = new IndexInsertDeleteOperator(op.getDataSourceIndex(),
            newPrimaryKeyExpressions, newSecondaryKeyExpressions, newFilterExpression, op.getOperation(),
            op.isBulkload());/*from   ww w  .  j a  v  a 2 s.c  o  m*/
    indexInsertDeleteOp.setAdditionalFilteringExpressions(newLSMComponentFilterExpressions);
    return indexInsertDeleteOp;
}

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

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
        throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    // current opperator is join
    if (op.getOperatorTag() != LogicalOperatorTag.INNERJOIN
            && op.getOperatorTag() != LogicalOperatorTag.LEFTOUTERJOIN) {
        return false;
    }/*  ww w  .j  av a2 s .co  m*/

    // Find GET_ITEM function.
    AbstractBinaryJoinOperator joinOp = (AbstractBinaryJoinOperator) op;
    Mutable<ILogicalExpression> expRef = joinOp.getCondition();
    Mutable<ILogicalExpression> getItemExprRef = getSimilarityExpression(expRef);
    if (getItemExprRef == null) {
        return false;
    }
    // Check if the GET_ITEM function is on one of the supported similarity-check functions.
    AbstractFunctionCallExpression getItemFuncExpr = (AbstractFunctionCallExpression) getItemExprRef.getValue();
    Mutable<ILogicalExpression> argRef = getItemFuncExpr.getArguments().get(0);
    AbstractFunctionCallExpression simFuncExpr = (AbstractFunctionCallExpression) argRef.getValue();
    if (!simFuncs.contains(simFuncExpr.getFunctionIdentifier())) {
        return false;
    }
    // Skip this rule based on annotations.
    if (simFuncExpr.getAnnotations().containsKey(IndexedNLJoinExpressionAnnotation.INSTANCE)) {
        return false;
    }

    List<Mutable<ILogicalOperator>> inputOps = joinOp.getInputs();
    ILogicalOperator leftInputOp = inputOps.get(0).getValue();
    ILogicalOperator rightInputOp = inputOps.get(1).getValue();

    List<Mutable<ILogicalExpression>> inputExps = simFuncExpr.getArguments();

    ILogicalExpression inputExp0 = inputExps.get(0).getValue();
    ILogicalExpression inputExp1 = inputExps.get(1).getValue();

    // left and right expressions are variables
    if (inputExp0.getExpressionTag() != LogicalExpressionTag.VARIABLE
            || inputExp1.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }

    LogicalVariable inputVar0 = ((VariableReferenceExpression) inputExp0).getVariableReference();
    LogicalVariable inputVar1 = ((VariableReferenceExpression) inputExp1).getVariableReference();

    LogicalVariable leftInputVar;
    LogicalVariable rightInputVar;

    liveVars.clear();
    VariableUtilities.getLiveVariables(leftInputOp, liveVars);
    if (liveVars.contains(inputVar0)) {
        leftInputVar = inputVar0;
        rightInputVar = inputVar1;
    } else {
        leftInputVar = inputVar1;
        rightInputVar = inputVar0;
    }

    List<LogicalVariable> leftInputPKs = context.findPrimaryKey(leftInputVar);
    List<LogicalVariable> rightInputPKs = context.findPrimaryKey(rightInputVar);
    // Bail if primary keys could not be inferred.
    if (leftInputPKs == null || rightInputPKs == null) {
        return false;
    }
    // primary key has only one variable
    if (leftInputPKs.size() != 1 || rightInputPKs.size() != 1) {
        return false;
    }
    IAType leftType = (IAType) context.getOutputTypeEnvironment(leftInputOp).getVarType(leftInputVar);
    IAType rightType = (IAType) context.getOutputTypeEnvironment(rightInputOp).getVarType(rightInputVar);
    // left-hand side and right-hand side of "~=" has the same type
    IAType left2 = TypeHelper.getNonOptionalType(leftType);
    IAType right2 = TypeHelper.getNonOptionalType(rightType);
    if (!left2.deepEqual(right2)) {
        return false;
    }
    //
    // -- - FIRE - --
    //
    AqlMetadataProvider metadataProvider = ((AqlMetadataProvider) context.getMetadataProvider());
    FunctionIdentifier funcId = FuzzyUtils.getTokenizer(leftType.getTypeTag());
    String tokenizer;
    if (funcId == null) {
        tokenizer = "";
    } else {
        tokenizer = funcId.getName();
    }

    float simThreshold = FuzzyUtils.getSimThreshold(metadataProvider);
    String simFunction = FuzzyUtils.getSimFunction(metadataProvider);

    // finalize AQL+ query
    String prepareJoin;
    switch (joinOp.getJoinKind()) {
    case INNER: {
        prepareJoin = "join" + AQLPLUS;
        break;
    }
    case LEFT_OUTER: {
        // TODO To make it work for Left Outer Joins, we should permute
        // the #LEFT and #RIGHT at the top of the AQL+ query. But, when
        // doing this, the
        // fuzzyjoin/user-vis-int-vis-user-lot-aqlplus_1.aql (the one
        // doing 3-way fuzzy joins) gives a different result. But even
        // if we don't change the FuzzyJoinRule, permuting the for
        // clauses in fuzzyjoin/user-vis-int-vis-user-lot-aqlplus_1.aql
        // leads to different results, which suggests there is some
        // other sort of bug.
        return false;
        // prepareJoin = "loj" + AQLPLUS;
        // break;
    }
    default: {
        throw new IllegalStateException();
    }
    }
    String aqlPlus = String.format(Locale.US, prepareJoin, tokenizer, tokenizer, simFunction, simThreshold,
            tokenizer, tokenizer, simFunction, simThreshold, simFunction, simThreshold, simThreshold);

    LogicalVariable leftPKVar = leftInputPKs.get(0);
    LogicalVariable rightPKVar = rightInputPKs.get(0);

    Counter counter = new Counter(context.getVarCounter());

    AQLPlusParser parser = new AQLPlusParser(new StringReader(aqlPlus));
    parser.initScope();
    parser.setVarCounter(counter);
    List<Clause> clauses;
    try {
        clauses = parser.Clauses();
    } catch (ParseException e) {
        throw new AlgebricksException(e);
    }
    // The translator will compile metadata internally. Run this compilation
    // under the same transaction id as the "outer" compilation.
    AqlPlusExpressionToPlanTranslator translator = new AqlPlusExpressionToPlanTranslator(
            metadataProvider.getJobId(), metadataProvider, counter, null, null);

    LogicalOperatorDeepCopyVisitor deepCopyVisitor = new LogicalOperatorDeepCopyVisitor(counter);

    translator.addOperatorToMetaScope(new Identifier("#LEFT"), leftInputOp);
    translator.addVariableToMetaScope(new Identifier("$$LEFT"), leftInputVar);
    translator.addVariableToMetaScope(new Identifier("$$LEFTPK"), leftPKVar);

    translator.addOperatorToMetaScope(new Identifier("#RIGHT"), rightInputOp);
    translator.addVariableToMetaScope(new Identifier("$$RIGHT"), rightInputVar);
    translator.addVariableToMetaScope(new Identifier("$$RIGHTPK"), rightPKVar);

    translator.addOperatorToMetaScope(new Identifier("#LEFT_1"), deepCopyVisitor.deepCopy(leftInputOp, null));
    translator.addVariableToMetaScope(new Identifier("$$LEFT_1"), deepCopyVisitor.varCopy(leftInputVar));
    translator.addVariableToMetaScope(new Identifier("$$LEFTPK_1"), deepCopyVisitor.varCopy(leftPKVar));
    deepCopyVisitor.updatePrimaryKeys(context);
    deepCopyVisitor.reset();

    // translator.addOperatorToMetaScope(new Identifier("#LEFT_2"),
    // deepCopyVisitor.deepCopy(leftInputOp, null));
    // translator.addVariableToMetaScope(new Identifier("$$LEFT_2"),
    // deepCopyVisitor.varCopy(leftInputVar));
    // translator.addVariableToMetaScope(new Identifier("$$LEFTPK_2"),
    // deepCopyVisitor.varCopy(leftPKVar));
    // deepCopyVisitor.updatePrimaryKeys(context);
    // deepCopyVisitor.reset();
    //
    // translator.addOperatorToMetaScope(new Identifier("#LEFT_3"),
    // deepCopyVisitor.deepCopy(leftInputOp, null));
    // translator.addVariableToMetaScope(new Identifier("$$LEFT_3"),
    // deepCopyVisitor.varCopy(leftInputVar));
    // translator.addVariableToMetaScope(new Identifier("$$LEFTPK_3"),
    // deepCopyVisitor.varCopy(leftPKVar));
    // deepCopyVisitor.updatePrimaryKeys(context);
    // deepCopyVisitor.reset();

    translator.addOperatorToMetaScope(new Identifier("#RIGHT_1"), deepCopyVisitor.deepCopy(rightInputOp, null));
    translator.addVariableToMetaScope(new Identifier("$$RIGHT_1"), deepCopyVisitor.varCopy(rightInputVar));
    translator.addVariableToMetaScope(new Identifier("$$RIGHTPK_1"), deepCopyVisitor.varCopy(rightPKVar));
    deepCopyVisitor.updatePrimaryKeys(context);
    deepCopyVisitor.reset();

    // TODO pick side to run Stage 1, currently always picks RIGHT side
    translator.addOperatorToMetaScope(new Identifier("#RIGHT_2"), deepCopyVisitor.deepCopy(rightInputOp, null));
    translator.addVariableToMetaScope(new Identifier("$$RIGHT_2"), deepCopyVisitor.varCopy(rightInputVar));
    translator.addVariableToMetaScope(new Identifier("$$RIGHTPK_2"), deepCopyVisitor.varCopy(rightPKVar));
    deepCopyVisitor.updatePrimaryKeys(context);
    deepCopyVisitor.reset();

    translator.addOperatorToMetaScope(new Identifier("#RIGHT_3"), deepCopyVisitor.deepCopy(rightInputOp, null));
    translator.addVariableToMetaScope(new Identifier("$$RIGHT_3"), deepCopyVisitor.varCopy(rightInputVar));
    translator.addVariableToMetaScope(new Identifier("$$RIGHTPK_3"), deepCopyVisitor.varCopy(rightPKVar));
    deepCopyVisitor.updatePrimaryKeys(context);
    deepCopyVisitor.reset();

    ILogicalPlan plan;
    try {
        plan = translator.translate(clauses);
    } catch (AsterixException e) {
        throw new AlgebricksException(e);
    }
    context.setVarCounter(counter.get());

    ILogicalOperator outputOp = plan.getRoots().get(0).getValue();

    SelectOperator extraSelect = null;
    if (getItemExprRef != expRef) {
        // more than one join condition
        getItemExprRef.setValue(ConstantExpression.TRUE);
        switch (joinOp.getJoinKind()) {
        case INNER: {
            extraSelect = new SelectOperator(expRef, false, null);
            extraSelect.getInputs().add(new MutableObject<ILogicalOperator>(outputOp));
            outputOp = extraSelect;
            break;
        }
        case LEFT_OUTER: {
            if (((AbstractLogicalOperator) outputOp).getOperatorTag() != LogicalOperatorTag.LEFTOUTERJOIN) {
                throw new IllegalStateException();
            }
            LeftOuterJoinOperator topJoin = (LeftOuterJoinOperator) outputOp;
            topJoin.getCondition().setValue(expRef.getValue());
            break;
        }
        default: {
            throw new IllegalStateException();
        }
        }
    }
    opRef.setValue(outputOp);
    OperatorPropertiesUtil.typeOpRec(opRef, context);
    return true;
}

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

private static void generateMergeAggregationExpressions(GroupByOperator gby, IOptimizationContext context)
        throws AlgebricksException {
    if (gby.getNestedPlans().size() != 1) {
        throw new AlgebricksException(
                "External group-by currently works only for one nested plan with one root containing"
                        + "an aggregate and a nested-tuple-source.");
    }/*from  w w  w . j  a  v a2 s  .  c om*/
    ILogicalPlan p0 = gby.getNestedPlans().get(0);
    if (p0.getRoots().size() != 1) {
        throw new AlgebricksException(
                "External group-by currently works only for one nested plan with one root containing"
                        + "an aggregate and a nested-tuple-source.");
    }
    IMergeAggregationExpressionFactory mergeAggregationExpressionFactory = context
            .getMergeAggregationExpressionFactory();
    Mutable<ILogicalOperator> r0 = p0.getRoots().get(0);
    AbstractLogicalOperator r0Logical = (AbstractLogicalOperator) r0.getValue();
    if (r0Logical.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        throw new AlgebricksException("The merge aggregation expression generation should not process a "
                + r0Logical.getOperatorTag() + " operator.");
    }
    AggregateOperator aggOp = (AggregateOperator) r0.getValue();
    List<Mutable<ILogicalExpression>> aggFuncRefs = aggOp.getExpressions();
    List<LogicalVariable> aggProducedVars = aggOp.getVariables();
    int n = aggOp.getExpressions().size();
    List<Mutable<ILogicalExpression>> mergeExpressionRefs = new ArrayList<Mutable<ILogicalExpression>>();
    for (int i = 0; i < n; i++) {
        ILogicalExpression mergeExpr = mergeAggregationExpressionFactory
                .createMergeAggregation(aggProducedVars.get(i), aggFuncRefs.get(i).getValue(), context);
        if (mergeExpr == null) {
            throw new AlgebricksException("The aggregation function " + aggFuncRefs.get(i).getValue()
                    + " does not have a registered intermediate aggregation function.");
        }
        mergeExpressionRefs.add(new MutableObject<ILogicalExpression>(mergeExpr));
    }
    aggOp.setMergeExpressions(mergeExpressionRefs);
}

From source file:edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.visitors.IsomorphismVariableMappingVisitor.java

private Mutable<ILogicalExpression> copyExpressionAndSubtituteVars(Mutable<ILogicalExpression> expr) {
    ILogicalExpression copy = ((AbstractLogicalExpression) expr.getValue()).cloneExpression();
    for (Entry<LogicalVariable, LogicalVariable> entry : variableMapping.entrySet())
        copy.substituteVar(entry.getKey(), entry.getValue());
    return new MutableObject<ILogicalExpression>(copy);
}

From source file:edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.visitors.OperatorDeepCopyVisitor.java

@Override
public ILogicalOperator visitTokenizeOperator(TokenizeOperator op, Void arg) throws AlgebricksException {
    List<Mutable<ILogicalExpression>> newPrimaryKeyExpressions = new ArrayList<Mutable<ILogicalExpression>>();
    deepCopyExpressionRefs(newPrimaryKeyExpressions, op.getPrimaryKeyExpressions());
    List<Mutable<ILogicalExpression>> newSecondaryKeyExpressions = new ArrayList<Mutable<ILogicalExpression>>();
    deepCopyExpressionRefs(newSecondaryKeyExpressions, op.getSecondaryKeyExpressions());
    List<LogicalVariable> newTokenizeVars = new ArrayList<LogicalVariable>();
    deepCopyVars(newTokenizeVars, op.getTokenizeVars());
    Mutable<ILogicalExpression> newFilterExpression = new MutableObject<ILogicalExpression>(
            ((AbstractLogicalExpression) op.getFilterExpression()).cloneExpression());
    List<Object> newTokenizeVarTypes = new ArrayList<Object>();
    deepCopyObjects(newTokenizeVarTypes, op.getTokenizeVarTypes());

    TokenizeOperator tokenizeOp = new TokenizeOperator(op.getDataSourceIndex(), newPrimaryKeyExpressions,
            newSecondaryKeyExpressions, newTokenizeVars, newFilterExpression, op.getOperation(),
            op.isBulkload(), op.isPartitioned(), newTokenizeVarTypes);
    return tokenizeOp;
}

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

public ILogicalPlan translate(List<Clause> clauses) throws AlgebricksException, AsterixException {

    if (clauses == null) {
        return null;
    }//from   www .j a  v a2 s  .c om

    Mutable<ILogicalOperator> opRef = new MutableObject<ILogicalOperator>(new EmptyTupleSourceOperator());
    Pair<ILogicalOperator, LogicalVariable> p = null;
    for (Clause c : clauses) {
        p = c.accept(this, opRef);
        opRef = new MutableObject<ILogicalOperator>(p.first);
    }

    ArrayList<Mutable<ILogicalOperator>> globalPlanRoots = new ArrayList<Mutable<ILogicalOperator>>();

    ILogicalOperator topOp = p.first;

    globalPlanRoots.add(new MutableObject<ILogicalOperator>(topOp));
    ILogicalPlan plan = new ALogicalPlanImpl(globalPlanRoots);
    return plan;
}

From source file:edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.visitors.OperatorDeepCopyVisitor.java

private void deepCopyExpressionRefs(List<Mutable<ILogicalExpression>> newExprs,
        List<Mutable<ILogicalExpression>> oldExprs) {
    for (Mutable<ILogicalExpression> oldExpr : oldExprs)
        newExprs.add(new MutableObject<ILogicalExpression>(
                ((AbstractLogicalExpression) oldExpr.getValue()).cloneExpression()));
}

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

public ILogicalPlan translate(Query expr) throws AlgebricksException, AsterixException {
    Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this,
            new MutableObject<ILogicalOperator>(new EmptyTupleSourceOperator()));
    ArrayList<Mutable<ILogicalOperator>> globalPlanRoots = new ArrayList<Mutable<ILogicalOperator>>();
    ILogicalOperator topOp = p.first;//w w  w  . j  a va2  s .  c o m
    ProjectOperator project = (ProjectOperator) topOp;
    LogicalVariable resVar = project.getVariables().get(0);

    if (outputDatasetName == null) {
        FileSplit outputFileSplit = metadataProvider.getOutputFile();
        if (outputFileSplit == null) {
            outputFileSplit = getDefaultOutputFileLocation();
        }
        metadataProvider.setOutputFile(outputFileSplit);

        List<Mutable<ILogicalExpression>> writeExprList = new ArrayList<Mutable<ILogicalExpression>>(1);
        writeExprList.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(resVar)));
        ResultSetSinkId rssId = new ResultSetSinkId(metadataProvider.getResultSetId());
        ResultSetDataSink sink = new ResultSetDataSink(rssId, null);
        topOp = new DistributeResultOperator(writeExprList, sink);
        topOp.getInputs().add(new MutableObject<ILogicalOperator>(project));

        // Retrieve the Output RecordType (if any) and store it on
        // the DistributeResultOperator
        IAType outputRecordType = metadataProvider.findOutputRecordType();
        if (outputRecordType != null) {
            topOp.getAnnotations().put("output-record-type", outputRecordType);
        }
    } else {
        /**
         * add the collection-to-sequence right before the final project,
         * because dataset only accept non-collection records
         */
        LogicalVariable seqVar = context.newVar();
        @SuppressWarnings("unchecked")
        /** This assign adds a marker function collection-to-sequence: if the input is a singleton collection, unnest it; otherwise do nothing. */
        AssignOperator assignCollectionToSequence = new AssignOperator(seqVar,
                new MutableObject<ILogicalExpression>(new ScalarFunctionCallExpression(
                        FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.COLLECTION_TO_SEQUENCE),
                        new MutableObject<ILogicalExpression>(new VariableReferenceExpression(resVar)))));
        assignCollectionToSequence.getInputs()
                .add(new MutableObject<ILogicalOperator>(project.getInputs().get(0).getValue()));
        project.getInputs().get(0).setValue(assignCollectionToSequence);
        project.getVariables().set(0, seqVar);
        resVar = seqVar;

        DatasetDataSource targetDatasource = validateDatasetInfo(metadataProvider, stmt.getDataverseName(),
                stmt.getDatasetName());
        ArrayList<LogicalVariable> vars = new ArrayList<LogicalVariable>();
        ArrayList<Mutable<ILogicalExpression>> exprs = new ArrayList<Mutable<ILogicalExpression>>();
        List<Mutable<ILogicalExpression>> varRefsForLoading = new ArrayList<Mutable<ILogicalExpression>>();
        List<List<String>> partitionKeys = DatasetUtils.getPartitioningKeys(targetDatasource.getDataset());
        for (List<String> keyFieldName : partitionKeys) {
            prepareVarAndExpression(keyFieldName, resVar, vars, exprs, varRefsForLoading);
        }

        List<String> additionalFilteringField = DatasetUtils.getFilterField(targetDatasource.getDataset());
        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, resVar, additionalFilteringVars,
                    additionalFilteringAssignExpressions, additionalFilteringExpressions);

            additionalFilteringAssign = new AssignOperator(additionalFilteringVars,
                    additionalFilteringAssignExpressions);
        }

        AssignOperator assign = new AssignOperator(vars, exprs);

        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));
        }

        Mutable<ILogicalExpression> varRef = new MutableObject<ILogicalExpression>(
                new VariableReferenceExpression(resVar));
        ILogicalOperator leafOperator = null;

        switch (stmt.getKind()) {
        case INSERT: {
            InsertDeleteOperator insertOp = new InsertDeleteOperator(targetDatasource, varRef,
                    varRefsForLoading, InsertDeleteOperator.Kind.INSERT, false);
            insertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
            insertOp.getInputs().add(new MutableObject<ILogicalOperator>(assign));
            leafOperator = new SinkOperator();
            leafOperator.getInputs().add(new MutableObject<ILogicalOperator>(insertOp));
            break;
        }
        case DELETE: {
            InsertDeleteOperator deleteOp = new InsertDeleteOperator(targetDatasource, varRef,
                    varRefsForLoading, InsertDeleteOperator.Kind.DELETE, false);
            deleteOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
            deleteOp.getInputs().add(new MutableObject<ILogicalOperator>(assign));
            leafOperator = new SinkOperator();
            leafOperator.getInputs().add(new MutableObject<ILogicalOperator>(deleteOp));
            break;
        }
        case CONNECT_FEED: {
            InsertDeleteOperator insertOp = new InsertDeleteOperator(targetDatasource, varRef,
                    varRefsForLoading, InsertDeleteOperator.Kind.INSERT, false);
            insertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
            insertOp.getInputs().add(new MutableObject<ILogicalOperator>(assign));
            leafOperator = new SinkOperator();
            leafOperator.getInputs().add(new MutableObject<ILogicalOperator>(insertOp));
            break;
        }
        case SUBSCRIBE_FEED: {
            ILogicalOperator insertOp = new InsertDeleteOperator(targetDatasource, varRef, varRefsForLoading,
                    InsertDeleteOperator.Kind.INSERT, false);
            insertOp.getInputs().add(new MutableObject<ILogicalOperator>(assign));
            leafOperator = new SinkOperator();
            leafOperator.getInputs().add(new MutableObject<ILogicalOperator>(insertOp));
            break;
        }
        }
        topOp = leafOperator;
    }
    globalPlanRoots.add(new MutableObject<ILogicalOperator>(topOp));
    ILogicalPlan plan = new ALogicalPlanImpl(globalPlanRoots);
    return plan;
}

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

@Override
public Pair<ILogicalOperator, LogicalVariable> visitForClause(ForClause fc, Mutable<ILogicalOperator> tupSource)
        throws AsterixException {
    LogicalVariable v = context.newVar(fc.getVarExpr());

    Expression inExpr = fc.getInExpr();
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = aqlExprToAlgExpression(inExpr, tupSource);
    ILogicalOperator returnedOp;/* w w w.ja v  a 2 s . c  om*/

    if (fc.getPosVarExpr() == null) {
        returnedOp = new UnnestOperator(v,
                new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)));
    } else {
        LogicalVariable pVar = context.newVar(fc.getPosVarExpr());
        returnedOp = new UnnestOperator(v,
                new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)), pVar, BuiltinType.AINT32,
                new AqlPositionWriter());
    }
    returnedOp.getInputs().add(eo.second);

    return new Pair<ILogicalOperator, LogicalVariable>(returnedOp, v);
}