Search in sources :

Example 26 with Pair

use of org.apache.commons.lang3.tuple.Pair in project asterixdb by apache.

the class LangExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(IndexAccessor ia, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = langExprToAlgExpression(ia.getExpr(), tupSource);
    LogicalVariable v = context.newVar();
    AbstractFunctionCallExpression f;
    if (ia.isAny()) {
        f = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.ANY_COLLECTION_MEMBER));
        f.getArguments().add(new MutableObject<>(p.first));
    } else {
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> indexPair = langExprToAlgExpression(ia.getIndexExpr(), tupSource);
        f = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_ITEM));
        f.getArguments().add(new MutableObject<>(p.first));
        f.getArguments().add(new MutableObject<>(indexPair.first));
    }
    AssignOperator a = new AssignOperator(v, new MutableObject<>(f));
    a.getInputs().add(p.second);
    return new Pair<>(a, v);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) 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 27 with Pair

use of org.apache.commons.lang3.tuple.Pair in project asterixdb by apache.

the class LangExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(IfExpr ifexpr, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    // In the most general case, IfThenElse is translated in the following
    // way.
    //
    // We assign the result of the condition to one variable varCond.
    // We create one subplan which contains the plan for the "then" branch,
    // on top of which there is a selection whose condition is varCond.
    // Similarly, we create one subplan for the "else" branch, in which the
    // selection is not(varCond).
    // Finally, we select the desired result.
    Pair<ILogicalOperator, LogicalVariable> pCond = ifexpr.getCondExpr().accept(this, tupSource);
    LogicalVariable varCond = pCond.second;
    // Creates a subplan for the "then" branch.
    Pair<ILogicalOperator, LogicalVariable> opAndVarForThen = constructSubplanOperatorForBranch(pCond.first, new MutableObject<>(new VariableReferenceExpression(varCond)), ifexpr.getThenExpr());
    // Creates a subplan for the "else" branch.
    AbstractFunctionCallExpression notVarCond = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(AlgebricksBuiltinFunctions.NOT), Collections.singletonList(generateAndNotIsUnknownWrap(new VariableReferenceExpression(varCond))));
    Pair<ILogicalOperator, LogicalVariable> opAndVarForElse = constructSubplanOperatorForBranch(opAndVarForThen.first, new MutableObject<>(notVarCond), ifexpr.getElseExpr());
    // Uses switch-case function to select the results of two branches.
    LogicalVariable selectVar = context.newVar();
    List<Mutable<ILogicalExpression>> arguments = new ArrayList<>();
    arguments.add(new MutableObject<>(new VariableReferenceExpression(varCond)));
    arguments.add(new MutableObject<>(ConstantExpression.TRUE));
    arguments.add(new MutableObject<>(new VariableReferenceExpression(opAndVarForThen.second)));
    arguments.add(new MutableObject<>(new VariableReferenceExpression(opAndVarForElse.second)));
    AbstractFunctionCallExpression swithCaseExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SWITCH_CASE), arguments);
    AssignOperator assignOp = new AssignOperator(selectVar, new MutableObject<>(swithCaseExpr));
    assignOp.getInputs().add(new MutableObject<>(opAndVarForElse.first));
    // Unnests the selected ("if" or "else") result.
    LogicalVariable unnestVar = context.newVar();
    UnnestOperator unnestOp = new UnnestOperator(unnestVar, new MutableObject<>(new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), Collections.singletonList(new MutableObject<>(new VariableReferenceExpression(selectVar))))));
    unnestOp.getInputs().add(new MutableObject<>(assignOp));
    // Produces the final result.
    LogicalVariable resultVar = context.newVar();
    AssignOperator finalAssignOp = new AssignOperator(resultVar, new MutableObject<>(new VariableReferenceExpression(unnestVar)));
    finalAssignOp.getInputs().add(new MutableObject<>(unnestOp));
    return new Pair<>(finalAssignOp, resultVar);
}
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) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) MutableObject(org.apache.commons.lang3.mutable.MutableObject) 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 28 with Pair

use of org.apache.commons.lang3.tuple.Pair in project asterixdb by apache.

the class LangExpressionToPlanTranslator method processReturningExpression.

// Stitches the translated operators for the returning expression into the query plan.
private ILogicalOperator processReturningExpression(ILogicalOperator inputOperator, InsertDeleteUpsertOperator insertOp, CompiledInsertStatement compiledInsert) throws AlgebricksException {
    Expression returnExpression = compiledInsert.getReturnExpression();
    if (returnExpression == null) {
        return inputOperator;
    }
    ILogicalOperator rootOperator = inputOperator;
    //Makes the id of the insert var point to the record variable.
    context.newVarFromExpression(compiledInsert.getVar());
    context.setVar(compiledInsert.getVar(), ((VariableReferenceExpression) insertOp.getPayloadExpression().getValue()).getVariableReference());
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = langExprToAlgExpression(returnExpression, new MutableObject<>(rootOperator));
    // Adds an assign operator for the returning expression.
    LogicalVariable resultVar = context.newVar();
    AssignOperator assignOperator = new AssignOperator(resultVar, new MutableObject<>(p.first));
    assignOperator.getInputs().add(p.second);
    // Adds a distribute result operator.
    List<Mutable<ILogicalExpression>> expressions = new ArrayList<>();
    expressions.add(new MutableObject<>(new VariableReferenceExpression(resultVar)));
    ResultSetSinkId rssId = new ResultSetSinkId(metadataProvider.getResultSetId());
    ResultSetDataSink sink = new ResultSetDataSink(rssId, null);
    rootOperator = new DistributeResultOperator(expressions, sink);
    rootOperator.getInputs().add(new MutableObject<>(assignOperator));
    return rootOperator;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ResultSetDataSink(org.apache.asterix.metadata.declared.ResultSetDataSink) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) DistributeResultOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DistributeResultOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) 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) ResultSetSinkId(org.apache.asterix.metadata.declared.ResultSetSinkId)

Example 29 with Pair

use of org.apache.commons.lang3.tuple.Pair in project asterixdb by apache.

the class LangExpressionToPlanTranslator method constructSubplanOperatorForBranch.

/**
     * Constructs a subplan operator for a branch in a if-else (or case) expression.
     *
     * @param inputOp,
     *            the input operator.
     * @param selectExpr,
     *            the expression to select tuples that are processed by this branch.
     * @param branchExpression,
     *            the expression to be evaluated in this branch.
     * @return a pair of the constructed subplan operator and the output variable for the branch.
     * @throws CompilationException
     */
protected Pair<ILogicalOperator, LogicalVariable> constructSubplanOperatorForBranch(ILogicalOperator inputOp, Mutable<ILogicalExpression> selectExpr, Expression branchExpression) throws CompilationException {
    context.enterSubplan();
    SubplanOperator subplanOp = new SubplanOperator();
    subplanOp.getInputs().add(new MutableObject<>(inputOp));
    Mutable<ILogicalOperator> nestedSource = new MutableObject<>(new NestedTupleSourceOperator(new MutableObject<>(subplanOp)));
    SelectOperator select = new SelectOperator(selectExpr, false, null);
    // The select operator cannot be moved up and down, otherwise it will cause typing issues (ASTERIXDB-1203).
    OperatorPropertiesUtil.markMovable(select, false);
    select.getInputs().add(nestedSource);
    Pair<ILogicalOperator, LogicalVariable> pBranch = branchExpression.accept(this, new MutableObject<>(select));
    LogicalVariable branchVar = context.newVar();
    AggregateOperator aggOp = new AggregateOperator(Collections.singletonList(branchVar), Collections.singletonList(new MutableObject<>(new AggregateFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.LISTIFY), false, Collections.singletonList(new MutableObject<>(new VariableReferenceExpression(pBranch.second)))))));
    aggOp.getInputs().add(new MutableObject<>(pBranch.first));
    ILogicalPlan planForBranch = new ALogicalPlanImpl(new MutableObject<>(aggOp));
    subplanOp.getNestedPlans().add(planForBranch);
    context.exitSubplan();
    return new Pair<>(subplanOp, branchVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) ALogicalPlanImpl(org.apache.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) MutableObject(org.apache.commons.lang3.mutable.MutableObject) 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 30 with Pair

use of org.apache.commons.lang3.tuple.Pair in project asterixdb by apache.

the class AqlExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(DistinctClause dc, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    List<Mutable<ILogicalExpression>> exprList = new ArrayList<>();
    Mutable<ILogicalOperator> input = null;
    for (Expression expr : dc.getDistinctByExpr()) {
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = langExprToAlgExpression(expr, tupSource);
        exprList.add(new MutableObject<ILogicalExpression>(p.first));
        input = p.second;
    }
    DistinctOperator opDistinct = new DistinctOperator(exprList);
    opDistinct.getInputs().add(input);
    return new Pair<>(opDistinct, null);
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) 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) ArrayList(java.util.ArrayList) DistinctOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DistinctOperator) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

Pair (org.apache.commons.lang3.tuple.Pair)111 ArrayList (java.util.ArrayList)98 Mutable (org.apache.commons.lang3.mutable.Mutable)97 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)87 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)86 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)75 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)73 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)63 Pair (org.apache.hyracks.algebricks.common.utils.Pair)62 MutableObject (org.apache.commons.lang3.mutable.MutableObject)42 List (java.util.List)35 HashMap (java.util.HashMap)34 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)32 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)30 Collectors (java.util.stream.Collectors)29 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)29 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)29 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)27 HashSet (java.util.HashSet)25 File (java.io.File)24