Search in sources :

Example 71 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class LangExpressionToPlanTranslator method translateUpsert.

private ILogicalOperator translateUpsert(DatasetDataSource targetDatasource, Mutable<ILogicalExpression> varRef, List<Mutable<ILogicalExpression>> varRefsForLoading, List<Mutable<ILogicalExpression>> additionalFilteringExpressions, ILogicalOperator assign, List<String> additionalFilteringField, LogicalVariable unnestVar, ILogicalOperator topOp, List<Mutable<ILogicalExpression>> exprs, LogicalVariable resVar, AssignOperator additionalFilteringAssign, ICompiledDmlStatement stmt) throws AlgebricksException {
    if (!targetDatasource.getDataset().allow(topOp, DatasetUtil.OP_UPSERT)) {
        throw new AlgebricksException(targetDatasource.getDataset().getDatasetName() + ": upsert into dataset is not supported on Datasets with Meta records");
    }
    ProjectOperator project = (ProjectOperator) topOp;
    CompiledUpsertStatement compiledUpsert = (CompiledUpsertStatement) stmt;
    Expression returnExpression = compiledUpsert.getReturnExpression();
    InsertDeleteUpsertOperator upsertOp;
    ILogicalOperator rootOperator;
    if (targetDatasource.getDataset().hasMetaPart()) {
        if (returnExpression != null) {
            throw new AlgebricksException("Returning not allowed on datasets with Meta records");
        }
        AssignOperator metaAndKeysAssign;
        List<LogicalVariable> metaAndKeysVars;
        List<Mutable<ILogicalExpression>> metaAndKeysExprs;
        List<Mutable<ILogicalExpression>> metaExpSingletonList;
        metaAndKeysVars = new ArrayList<>();
        metaAndKeysExprs = new ArrayList<>();
        // add the meta function
        IFunctionInfo finfoMeta = FunctionUtil.getFunctionInfo(BuiltinFunctions.META);
        ScalarFunctionCallExpression metaFunction = new ScalarFunctionCallExpression(finfoMeta, new MutableObject<>(new VariableReferenceExpression(unnestVar)));
        // create assign for the meta part
        LogicalVariable metaVar = context.newVar();
        metaExpSingletonList = new ArrayList<>(1);
        metaExpSingletonList.add(new MutableObject<>(new VariableReferenceExpression(metaVar)));
        metaAndKeysVars.add(metaVar);
        metaAndKeysExprs.add(new MutableObject<>(metaFunction));
        project.getVariables().add(metaVar);
        varRefsForLoading.clear();
        for (Mutable<ILogicalExpression> assignExpr : exprs) {
            if (assignExpr.getValue().getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                AbstractFunctionCallExpression funcCall = (AbstractFunctionCallExpression) assignExpr.getValue();
                funcCall.substituteVar(resVar, unnestVar);
                LogicalVariable pkVar = context.newVar();
                metaAndKeysVars.add(pkVar);
                metaAndKeysExprs.add(new MutableObject<>(assignExpr.getValue()));
                project.getVariables().add(pkVar);
                varRefsForLoading.add(new MutableObject<>(new VariableReferenceExpression(pkVar)));
            }
        }
        // A change feed, we don't need the assign to access PKs
        upsertOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, metaExpSingletonList, InsertDeleteUpsertOperator.Kind.UPSERT, false);
        // Create and add a new variable used for representing the original record
        upsertOp.setPrevRecordVar(context.newVar());
        upsertOp.setPrevRecordType(targetDatasource.getItemType());
        if (targetDatasource.getDataset().hasMetaPart()) {
            List<LogicalVariable> metaVars = new ArrayList<>();
            metaVars.add(context.newVar());
            upsertOp.setPrevAdditionalNonFilteringVars(metaVars);
            List<Object> metaTypes = new ArrayList<>();
            metaTypes.add(targetDatasource.getMetaItemType());
            upsertOp.setPrevAdditionalNonFilteringTypes(metaTypes);
        }
        if (additionalFilteringField != null) {
            upsertOp.setPrevFilterVar(context.newVar());
            upsertOp.setPrevFilterType(((ARecordType) targetDatasource.getItemType()).getFieldType(additionalFilteringField.get(0)));
            additionalFilteringAssign.getInputs().clear();
            additionalFilteringAssign.getInputs().add(assign.getInputs().get(0));
            upsertOp.getInputs().add(new MutableObject<>(additionalFilteringAssign));
        } else {
            upsertOp.getInputs().add(assign.getInputs().get(0));
        }
        metaAndKeysAssign = new AssignOperator(metaAndKeysVars, metaAndKeysExprs);
        metaAndKeysAssign.getInputs().add(topOp.getInputs().get(0));
        topOp.getInputs().set(0, new MutableObject<>(metaAndKeysAssign));
        upsertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
    } else {
        upsertOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, InsertDeleteUpsertOperator.Kind.UPSERT, false);
        upsertOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
        upsertOp.getInputs().add(new MutableObject<>(assign));
        // Create and add a new variable used for representing the original record
        ARecordType recordType = (ARecordType) targetDatasource.getItemType();
        upsertOp.setPrevRecordVar(context.newVar());
        upsertOp.setPrevRecordType(recordType);
        if (additionalFilteringField != null) {
            upsertOp.setPrevFilterVar(context.newVar());
            upsertOp.setPrevFilterType(recordType.getFieldType(additionalFilteringField.get(0)));
        }
    }
    rootOperator = new DelegateOperator(new CommitOperator(returnExpression == null));
    rootOperator.getInputs().add(new MutableObject<>(upsertOp));
    // Compiles the return expression.
    return processReturningExpression(rootOperator, upsertOp, compiledUpsert);
}
Also used : IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) ArrayList(java.util.ArrayList) DelegateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DelegateOperator) CommitOperator(org.apache.asterix.algebra.operators.CommitOperator) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) CompiledUpsertStatement(org.apache.asterix.translator.CompiledStatements.CompiledUpsertStatement) ProjectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) InsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) Expression(org.apache.asterix.lang.common.base.Expression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ARecordType(org.apache.asterix.om.types.ARecordType)

Example 72 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class LangExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(QuantifiedExpression qe, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    Mutable<ILogicalOperator> topOp = tupSource;
    ILogicalOperator firstOp = null;
    Mutable<ILogicalOperator> lastOp = null;
    for (QuantifiedPair qt : qe.getQuantifiedList()) {
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo1 = langExprToAlgExpression(qt.getExpr(), topOp);
        topOp = eo1.second;
        LogicalVariable uVar = context.newVarFromExpression(qt.getVarExpr());
        ILogicalOperator u = new UnnestOperator(uVar, new MutableObject<>(makeUnnestExpression(eo1.first)));
        if (firstOp == null) {
            firstOp = u;
        }
        if (lastOp != null) {
            u.getInputs().add(lastOp);
        }
        lastOp = new MutableObject<>(u);
    }
    // We make all the unnest correspond. to quantif. vars. sit on top
    // in the hope of enabling joins & other optimiz.
    firstOp.getInputs().add(topOp);
    topOp = lastOp;
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo2 = langExprToAlgExpression(qe.getSatisfiesExpr(), topOp);
    AggregateFunctionCallExpression fAgg;
    SelectOperator s;
    if (qe.getQuantifier() == Quantifier.SOME) {
        s = new SelectOperator(new MutableObject<>(eo2.first), false, null);
        s.getInputs().add(eo2.second);
        fAgg = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.NON_EMPTY_STREAM, new ArrayList<>());
    } else {
        // EVERY
        List<Mutable<ILogicalExpression>> satExprList = new ArrayList<>(1);
        satExprList.add(new MutableObject<>(eo2.first));
        s = new SelectOperator(new MutableObject<>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(AlgebricksBuiltinFunctions.NOT), satExprList)), false, null);
        s.getInputs().add(eo2.second);
        fAgg = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.EMPTY_STREAM, new ArrayList<>());
    }
    LogicalVariable qeVar = context.newVar();
    AggregateOperator a = new AggregateOperator(mkSingletonArrayList(qeVar), (List) mkSingletonArrayList(new MutableObject<>(fAgg)));
    a.getInputs().add(new MutableObject<>(s));
    return new Pair<>(a, qeVar);
}
Also used : QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

Example 73 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class LangExpressionToPlanTranslator method langExprToAlgExpression.

protected Pair<ILogicalExpression, Mutable<ILogicalOperator>> langExprToAlgExpression(Expression expr, Mutable<ILogicalOperator> topOpRef) throws CompilationException {
    switch(expr.getKind()) {
        case VARIABLE_EXPRESSION:
            VariableReferenceExpression ve = new VariableReferenceExpression(context.getVar(((VariableExpr) expr).getVar().getId()));
            return new Pair<>(ve, topOpRef);
        case LITERAL_EXPRESSION:
            LiteralExpr val = (LiteralExpr) expr;
            return new Pair<>(new ConstantExpression(new AsterixConstantValue(ConstantHelper.objectFromLiteral(val.getValue()))), topOpRef);
        default:
            if (expressionNeedsNoNesting(expr)) {
                Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, topOpRef);
                ILogicalExpression exp = ((AssignOperator) p.first).getExpressions().get(0).getValue();
                return new Pair<>(exp, p.first.getInputs().get(0));
            } else {
                Mutable<ILogicalOperator> srcRef = new MutableObject<>();
                Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, srcRef);
                if (p.first.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
                    if (topOpRef.getValue() != null) {
                        srcRef.setValue(topOpRef.getValue());
                    } else {
                        // Re-binds the bottom operator reference to {@code topOpRef}.
                        rebindBottomOpRef(p.first, srcRef, topOpRef);
                    }
                    Mutable<ILogicalOperator> top2 = new MutableObject<>(p.first);
                    return new Pair<>(new VariableReferenceExpression(p.second), top2);
                } else {
                    SubplanOperator s = new SubplanOperator();
                    s.getInputs().add(topOpRef);
                    srcRef.setValue(new NestedTupleSourceOperator(new MutableObject<>(s)));
                    Mutable<ILogicalOperator> planRoot = new MutableObject<>(p.first);
                    s.setRootOp(planRoot);
                    return new Pair<>(new VariableReferenceExpression(p.second), new MutableObject<>(s));
                }
            }
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 74 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class AqlExpressionToPlanTranslator method produceFlworPlan.

private Pair<ILogicalOperator, LogicalVariable> produceFlworPlan(boolean noForClause, boolean isTop, Mutable<ILogicalOperator> resOpRef, LogicalVariable resVar) {
    if (isTop) {
        ProjectOperator pr = new ProjectOperator(resVar);
        pr.getInputs().add(resOpRef);
        return new Pair<>(pr, resVar);
    } else if (noForClause) {
        ILogicalOperator resOp = resOpRef.getValue();
        if (resOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
            return new Pair<>(resOp, resVar);
        }
        LogicalVariable newResVar = context.newVar();
        ILogicalOperator assign = new AssignOperator(newResVar, new MutableObject<>(new VariableReferenceExpression(resVar)));
        assign.getInputs().add(resOpRef);
        return new Pair<>(assign, newResVar);
    } else {
        return aggListifyForSubquery(resVar, resOpRef, false);
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ProjectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) Pair(org.apache.hyracks.algebricks.common.utils.Pair) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 75 with MutableObject

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.

the class AqlPlusExpressionToPlanTranslator method visitJoinClause.

@Override
public Pair<ILogicalOperator, LogicalVariable> visitJoinClause(JoinClause jc, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    Mutable<ILogicalOperator> opRef = tupSource;
    Pair<ILogicalOperator, LogicalVariable> leftSide = null;
    for (Clause c : jc.getLeftClauses()) {
        leftSide = c.accept(this, opRef);
        opRef = new MutableObject<ILogicalOperator>(leftSide.first);
    }
    opRef = tupSource;
    Pair<ILogicalOperator, LogicalVariable> rightSide = null;
    for (Clause c : jc.getRightClauses()) {
        rightSide = c.accept(this, opRef);
        opRef = new MutableObject<ILogicalOperator>(rightSide.first);
    }
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> whereCond = langExprToAlgExpression(jc.getWhereExpr(), tupSource);
    AbstractBinaryJoinOperator join;
    switch(jc.getKind()) {
        case INNER:
            join = new InnerJoinOperator(new MutableObject<ILogicalExpression>(whereCond.first));
            break;
        case LEFT_OUTER:
            join = new LeftOuterJoinOperator(new MutableObject<ILogicalExpression>(whereCond.first));
            break;
        default:
            throw new CompilationException(ErrorCode.COMPILATION_AQLPLUS_NO_SUCH_JOIN_TYPE);
    }
    join.getInputs().add(new MutableObject<ILogicalOperator>(leftSide.first));
    join.getInputs().add(new MutableObject<ILogicalOperator>(rightSide.first));
    return new Pair<ILogicalOperator, LogicalVariable>(join, null);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) CompilationException(org.apache.asterix.common.exceptions.CompilationException) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LeftOuterJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterJoinOperator) InnerJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InnerJoinOperator) AbstractBinaryJoinOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractBinaryJoinOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) MetaVariableClause(org.apache.asterix.lang.aql.clause.MetaVariableClause) JoinClause(org.apache.asterix.lang.aql.clause.JoinClause) Clause(org.apache.asterix.lang.common.base.Clause) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

MutableObject (org.apache.commons.lang3.mutable.MutableObject)111 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)60 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)58 ArrayList (java.util.ArrayList)55 Mutable (org.apache.commons.lang3.mutable.Mutable)55 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)52 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)49 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)28 Pair (org.apache.hyracks.algebricks.common.utils.Pair)27 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)23 List (java.util.List)22 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)21 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)18 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)17 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)14 Test (org.junit.Test)14 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)13 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)13 NestedTupleSourceOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator)13 HashSet (java.util.HashSet)12