Search in sources :

Example 6 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class LangExpressionToPlanTranslator method translateSubscribeFeed.

private ILogicalOperator translateSubscribeFeed(CompiledSubscribeFeedStatement sfs, DatasetDataSource targetDatasource, LogicalVariable unnestVar, ILogicalOperator topOp, ArrayList<Mutable<ILogicalExpression>> exprs, LogicalVariable resVar, List<Mutable<ILogicalExpression>> varRefsForLoading, Mutable<ILogicalExpression> varRef, ILogicalOperator assign, List<String> additionalFilteringField, AssignOperator additionalFilteringAssign, List<Mutable<ILogicalExpression>> additionalFilteringExpressions) throws AlgebricksException {
    // if the feed is a change feed (i.e, performs different operations), we need to project op variable
    InsertDeleteUpsertOperator feedModificationOp;
    AssignOperator metaAndKeysAssign;
    List<LogicalVariable> metaAndKeysVars = null;
    List<Mutable<ILogicalExpression>> metaAndKeysExprs = null;
    List<Mutable<ILogicalExpression>> metaExpSingletonList = null;
    Feed feed = metadataProvider.findFeed(sfs.getDataverseName(), sfs.getFeedName());
    boolean isChangeFeed = ExternalDataUtils.isChangeFeed(feed.getAdapterConfiguration());
    boolean isUpsertFeed = ExternalDataUtils.isUpsertFeed(feed.getAdapterConfiguration());
    ProjectOperator project = (ProjectOperator) topOp;
    if (targetDatasource.getDataset().hasMetaPart() || isChangeFeed) {
        metaAndKeysVars = new ArrayList<>();
        metaAndKeysExprs = new ArrayList<>();
        if (targetDatasource.getDataset().hasMetaPart()) {
            // 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);
        }
    }
    if (isChangeFeed) {
        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
        feedModificationOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, metaExpSingletonList, InsertDeleteUpsertOperator.Kind.UPSERT, false);
        // Create and add a new variable used for representing the original record
        feedModificationOp.setPrevRecordVar(context.newVar());
        feedModificationOp.setPrevRecordType(targetDatasource.getItemType());
        if (targetDatasource.getDataset().hasMetaPart()) {
            List<LogicalVariable> metaVars = new ArrayList<>();
            metaVars.add(context.newVar());
            feedModificationOp.setPrevAdditionalNonFilteringVars(metaVars);
            List<Object> metaTypes = new ArrayList<>();
            metaTypes.add(targetDatasource.getMetaItemType());
            feedModificationOp.setPrevAdditionalNonFilteringTypes(metaTypes);
        }
        if (additionalFilteringField != null) {
            feedModificationOp.setPrevFilterVar(context.newVar());
            feedModificationOp.setPrevFilterType(((ARecordType) targetDatasource.getItemType()).getFieldType(additionalFilteringField.get(0)));
            additionalFilteringAssign.getInputs().clear();
            additionalFilteringAssign.getInputs().add(assign.getInputs().get(0));
            feedModificationOp.getInputs().add(new MutableObject<>(additionalFilteringAssign));
        } else {
            feedModificationOp.getInputs().add(assign.getInputs().get(0));
        }
    } else {
        final InsertDeleteUpsertOperator.Kind opKind = isUpsertFeed ? InsertDeleteUpsertOperator.Kind.UPSERT : InsertDeleteUpsertOperator.Kind.INSERT;
        feedModificationOp = new InsertDeleteUpsertOperator(targetDatasource, varRef, varRefsForLoading, metaExpSingletonList, opKind, false);
        if (isUpsertFeed) {
            feedModificationOp.setPrevRecordVar(context.newVar());
            feedModificationOp.setPrevRecordType(targetDatasource.getItemType());
        }
        feedModificationOp.getInputs().add(new MutableObject<>(assign));
    }
    if (targetDatasource.getDataset().hasMetaPart() || isChangeFeed) {
        metaAndKeysAssign = new AssignOperator(metaAndKeysVars, metaAndKeysExprs);
        metaAndKeysAssign.getInputs().add(topOp.getInputs().get(0));
        topOp.getInputs().set(0, new MutableObject<>(metaAndKeysAssign));
    }
    feedModificationOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
    ILogicalOperator leafOperator = new DelegateOperator(new CommitOperator(true));
    leafOperator.getInputs().add(new MutableObject<>(feedModificationOp));
    return leafOperator;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) ProjectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) 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) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) DelegateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DelegateOperator) MutableObject(org.apache.commons.lang3.mutable.MutableObject) CommitOperator(org.apache.asterix.algebra.operators.CommitOperator) Feed(org.apache.asterix.metadata.entities.Feed) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 7 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class AqlExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(ForClause fc, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    LogicalVariable v = context.newVarFromExpression(fc.getVarExpr());
    Expression inExpr = fc.getInExpr();
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(inExpr, tupSource);
    ILogicalOperator returnedOp;
    if (fc.getPosVarExpr() == null) {
        returnedOp = new UnnestOperator(v, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)));
    } else {
        LogicalVariable pVar = context.newVarFromExpression(fc.getPosVarExpr());
        // We set the positional variable type as INT64 type.
        returnedOp = new UnnestOperator(v, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)), pVar, BuiltinType.AINT64, new PositionWriter());
    }
    returnedOp.getInputs().add(eo.second);
    return new Pair<>(returnedOp, v);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) 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) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 8 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class AqlPlusExpressionToPlanTranslator method translate.

public ILogicalPlan translate(List<Clause> clauses) throws AlgebricksException, CompilationException {
    if (clauses == null) {
        return null;
    }
    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;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ALogicalPlanImpl(org.apache.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) MetaVariableClause(org.apache.asterix.lang.aql.clause.MetaVariableClause) JoinClause(org.apache.asterix.lang.aql.clause.JoinClause) Clause(org.apache.asterix.lang.common.base.Clause) EmptyTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.EmptyTupleSourceOperator) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 9 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class AqlPlusExpressionToPlanTranslator method visitMetaVariableExpr.

@Override
public Pair<ILogicalOperator, LogicalVariable> visitMetaVariableExpr(MetaVariableExpr me, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    LogicalVariable var = context.newVar();
    AssignOperator a = new AssignOperator(var, new MutableObject<ILogicalExpression>(metaScopeExp.getVariableReferenceExpression(me.getVar())));
    a.getInputs().add(tupSource);
    return new Pair<ILogicalOperator, LogicalVariable>(a, var);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 10 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class LangExpressionToPlanTranslator method translateUnionAllFromInputExprs.

// Generates the plan for "UNION ALL" or union expression from its input expressions.
protected Pair<ILogicalOperator, LogicalVariable> translateUnionAllFromInputExprs(List<ILangExpression> inputExprs, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    List<Mutable<ILogicalOperator>> inputOpRefsToUnion = new ArrayList<>();
    List<LogicalVariable> vars = new ArrayList<>();
    for (ILangExpression expr : inputExprs) {
        // Visits the expression of one branch.
        Pair<ILogicalOperator, LogicalVariable> opAndVar = expr.accept(this, tupSource);
        // Creates an unnest operator.
        LogicalVariable unnestVar = context.newVar();
        List<Mutable<ILogicalExpression>> args = new ArrayList<>();
        args.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(opAndVar.second)));
        UnnestOperator unnestOp = new UnnestOperator(unnestVar, new MutableObject<ILogicalExpression>(new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), args)));
        unnestOp.getInputs().add(new MutableObject<>(opAndVar.first));
        inputOpRefsToUnion.add(new MutableObject<ILogicalOperator>(unnestOp));
        vars.add(unnestVar);
    }
    // Creates a tree of binary union-all operators.
    UnionAllOperator topUnionAllOp = null;
    LogicalVariable topUnionVar = null;
    Iterator<Mutable<ILogicalOperator>> inputOpRefIterator = inputOpRefsToUnion.iterator();
    Mutable<ILogicalOperator> leftInputBranch = inputOpRefIterator.next();
    Iterator<LogicalVariable> inputVarIterator = vars.iterator();
    LogicalVariable leftInputVar = inputVarIterator.next();
    while (inputOpRefIterator.hasNext()) {
        // Generates the variable triple <leftVar, rightVar, outputVar> .
        topUnionVar = context.newVar();
        Triple<LogicalVariable, LogicalVariable, LogicalVariable> varTriple = new Triple<>(leftInputVar, inputVarIterator.next(), topUnionVar);
        List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> varTriples = new ArrayList<>();
        varTriples.add(varTriple);
        // Creates a binary union-all operator.
        topUnionAllOp = new UnionAllOperator(varTriples);
        topUnionAllOp.getInputs().add(leftInputBranch);
        topUnionAllOp.getInputs().add(inputOpRefIterator.next());
        // Re-assigns leftInputBranch and leftInputVar.
        leftInputBranch = new MutableObject<>(topUnionAllOp);
        leftInputVar = topUnionVar;
    }
    return new Pair<>(topUnionAllOp, topUnionVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) Triple(org.apache.hyracks.algebricks.common.utils.Triple) 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) UnionAllOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

Aggregations

LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)376 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)196 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)182 ArrayList (java.util.ArrayList)171 Mutable (org.apache.commons.lang3.mutable.Mutable)136 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)127 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)92 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)79 Pair (org.apache.hyracks.algebricks.common.utils.Pair)75 HashSet (java.util.HashSet)60 MutableObject (org.apache.commons.lang3.mutable.MutableObject)60 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)60 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)54 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)46 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)36 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)33 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)32 AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)28 FunctionalDependency (org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency)28 IAType (org.apache.asterix.om.types.IAType)27