Search in sources :

Example 1 with SelectOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator 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 2 with SelectOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator in project asterixdb by apache.

the class SqlppExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(HavingClause havingClause, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = langExprToAlgExpression(havingClause.getFilterExpression(), tupSource);
    SelectOperator s = new SelectOperator(new MutableObject<ILogicalExpression>(p.first), false, null);
    s.getInputs().add(p.second);
    return new Pair<>(s, null);
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 3 with SelectOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator in project asterixdb by apache.

the class ConsolidateSelectsRule method rewritePre.

@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.SELECT) {
        return false;
    }
    SelectOperator firstSelect = (SelectOperator) op;
    IFunctionInfo andFn = context.getMetadataProvider().lookupFunction(AlgebricksBuiltinFunctions.AND);
    // New conjuncts for consolidated select.
    AbstractFunctionCallExpression conj = null;
    AbstractLogicalOperator topMostOp = null;
    AbstractLogicalOperator selectParent = null;
    AbstractLogicalOperator nextSelect = firstSelect;
    do {
        // Skip through assigns.
        do {
            selectParent = nextSelect;
            nextSelect = (AbstractLogicalOperator) selectParent.getInputs().get(0).getValue();
        } while (nextSelect.getOperatorTag() == LogicalOperatorTag.ASSIGN && OperatorPropertiesUtil.isMovable(nextSelect));
        // Stop if the child op is not a select.
        if (nextSelect.getOperatorTag() != LogicalOperatorTag.SELECT) {
            break;
        }
        // Remember the top-most op that we are not removing.
        topMostOp = selectParent;
        // Initialize the new conjuncts, if necessary.
        if (conj == null) {
            conj = new ScalarFunctionCallExpression(andFn);
            // Add the first select's condition.
            conj.getArguments().add(new MutableObject<ILogicalExpression>(firstSelect.getCondition().getValue()));
        }
        // Consolidate all following selects.
        do {
            // Add the condition nextSelect to the new list of conjuncts.
            conj.getArguments().add(((SelectOperator) nextSelect).getCondition());
            selectParent = nextSelect;
            nextSelect = (AbstractLogicalOperator) nextSelect.getInputs().get(0).getValue();
        } while (nextSelect.getOperatorTag() == LogicalOperatorTag.SELECT);
        // Hook up the input of the top-most remaining op if necessary.
        if (topMostOp.getOperatorTag() == LogicalOperatorTag.ASSIGN || topMostOp == firstSelect) {
            topMostOp.getInputs().set(0, selectParent.getInputs().get(0));
        }
        // Prepare for next iteration.
        nextSelect = selectParent;
    } while (true);
    // Did we consolidate any selects?
    if (conj == null) {
        return false;
    }
    // Set the new conjuncts.
    firstSelect.getCondition().setValue(conj);
    context.computeAndSetTypeEnvironmentForOperator(firstSelect);
    return true;
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 4 with SelectOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator in project asterixdb by apache.

the class IntroduceTransactionCommitByAssignOpRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.SELECT) {
        return false;
    }
    SelectOperator selectOperator = (SelectOperator) op;
    Mutable<ILogicalOperator> childOfSelect = selectOperator.getInputs().get(0);
    //[Direction] SelectOp(cond1)<--ChildOps... ==> SelectOp(booleanValue of cond1)<--NewAssignOp(cond1)<--ChildOps...
    //#. Create an assign-operator with a new local variable and the condition of the select-operator.
    //#. Set the input(child operator) of the new assign-operator to input(child operator) of the select-operator.
    //   (Later, the newly created assign-operator will apply the condition on behalf of the select-operator,
    //    and set the variable of the assign-operator to a boolean value according to the condition evaluation.)
    //#. Give the select-operator the result boolean value created by the newly created child assign-operator.
    //create an assignOp with a variable and the condition of the select-operator.
    LogicalVariable v = context.newVar();
    AssignOperator assignOperator = new AssignOperator(v, new MutableObject<ILogicalExpression>(selectOperator.getCondition().getValue()));
    //set the input of the new assign-operator to the input of the select-operator.
    assignOperator.getInputs().add(childOfSelect);
    //set the result value of the assign-operator to the condition of the select-operator
    //scalarFunctionCallExpression);
    selectOperator.getCondition().setValue(new VariableReferenceExpression(v));
    selectOperator.getInputs().set(0, new MutableObject<ILogicalOperator>(assignOperator));
    context.computeAndSetTypeEnvironmentForOperator(assignOperator);
    //Once this rule is fired, don't apply again.
    context.addToDontApplySet(this, selectOperator);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) 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)

Example 5 with SelectOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator in project asterixdb by apache.

the class AsterixIntroduceGroupByCombinerRule method processNullTest.

@SuppressWarnings("unchecked")
@Override
protected void processNullTest(IOptimizationContext context, GroupByOperator nestedGby, List<LogicalVariable> aggregateVarsProducedByCombiner) {
    IFunctionInfo finfoEq = context.getMetadataProvider().lookupFunction(BuiltinFunctions.IS_SYSTEM_NULL);
    SelectOperator selectNonSystemNull;
    if (aggregateVarsProducedByCombiner.size() == 1) {
        ILogicalExpression isSystemNullTest = new ScalarFunctionCallExpression(finfoEq, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(aggregateVarsProducedByCombiner.get(0))));
        IFunctionInfo finfoNot = context.getMetadataProvider().lookupFunction(AlgebricksBuiltinFunctions.NOT);
        ScalarFunctionCallExpression nonSystemNullTest = new ScalarFunctionCallExpression(finfoNot, new MutableObject<ILogicalExpression>(isSystemNullTest));
        selectNonSystemNull = new SelectOperator(new MutableObject<ILogicalExpression>(nonSystemNullTest), false, null);
    } else {
        List<Mutable<ILogicalExpression>> isSystemNullTestList = new ArrayList<Mutable<ILogicalExpression>>();
        for (LogicalVariable aggVar : aggregateVarsProducedByCombiner) {
            ILogicalExpression isSystemNullTest = new ScalarFunctionCallExpression(finfoEq, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(aggVar)));
            IFunctionInfo finfoNot = context.getMetadataProvider().lookupFunction(AlgebricksBuiltinFunctions.NOT);
            ScalarFunctionCallExpression nonSystemNullTest = new ScalarFunctionCallExpression(finfoNot, new MutableObject<ILogicalExpression>(isSystemNullTest));
            isSystemNullTestList.add(new MutableObject<ILogicalExpression>(nonSystemNullTest));
        }
        IFunctionInfo finfoAnd = context.getMetadataProvider().lookupFunction(AlgebricksBuiltinFunctions.AND);
        selectNonSystemNull = new SelectOperator(new MutableObject<ILogicalExpression>(new ScalarFunctionCallExpression(finfoAnd, isSystemNullTestList)), false, null);
    }
    //add the not-system-null check into the nested pipeline
    Mutable<ILogicalOperator> ntsBeforeNestedGby = nestedGby.getInputs().get(0);
    nestedGby.getInputs().set(0, new MutableObject<ILogicalOperator>(selectNonSystemNull));
    selectNonSystemNull.getInputs().add(ntsBeforeNestedGby);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) 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)

Aggregations

SelectOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator)36 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)27 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)23 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)19 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)17 Mutable (org.apache.commons.lang3.mutable.Mutable)15 MutableObject (org.apache.commons.lang3.mutable.MutableObject)11 ArrayList (java.util.ArrayList)10 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)10 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)10 AbstractBinaryJoinOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractBinaryJoinOperator)9 Pair (org.apache.hyracks.algebricks.common.utils.Pair)8 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)7 HashSet (java.util.HashSet)6 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)6 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)5 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)5 AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)5 NestedTupleSourceOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator)5 SubplanOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator)5