Search in sources :

Example 86 with AssignOperator

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

the class PushAssignBelowUnionAllRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (!op.hasInputs()) {
        return false;
    }
    boolean modified = false;
    for (int i = 0; i < op.getInputs().size(); i++) {
        AbstractLogicalOperator childOp = (AbstractLogicalOperator) op.getInputs().get(i).getValue();
        if (childOp.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
            continue;
        }
        AssignOperator assignOp = (AssignOperator) childOp;
        for (Mutable<ILogicalExpression> expr : assignOp.getExpressions()) {
            if (!expr.getValue().isFunctional()) {
                return false;
            }
        }
        AbstractLogicalOperator childOfChildOp = (AbstractLogicalOperator) assignOp.getInputs().get(0).getValue();
        if (childOfChildOp.getOperatorTag() != LogicalOperatorTag.UNIONALL) {
            continue;
        }
        UnionAllOperator unionOp = (UnionAllOperator) childOfChildOp;
        Set<LogicalVariable> assignUsedVars = new HashSet<LogicalVariable>();
        VariableUtilities.getUsedVariables(assignOp, assignUsedVars);
        List<LogicalVariable> assignVars = assignOp.getVariables();
        AssignOperator[] newAssignOps = new AssignOperator[2];
        for (int j = 0; j < unionOp.getInputs().size(); j++) {
            newAssignOps[j] = createAssignBelowUnionAllBranch(unionOp, j, assignOp, assignUsedVars, context);
        }
        // Add original assign variables to the union variable mappings.
        for (int j = 0; j < assignVars.size(); j++) {
            LogicalVariable first = newAssignOps[0].getVariables().get(j);
            LogicalVariable second = newAssignOps[1].getVariables().get(j);
            Triple<LogicalVariable, LogicalVariable, LogicalVariable> varMapping = new Triple<LogicalVariable, LogicalVariable, LogicalVariable>(first, second, assignVars.get(j));
            unionOp.getVariableMappings().add(varMapping);
        }
        context.computeAndSetTypeEnvironmentForOperator(unionOp);
        // Remove original assign operator.
        op.getInputs().set(i, assignOp.getInputs().get(0));
        context.computeAndSetTypeEnvironmentForOperator(op);
        modified = true;
    }
    return modified;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) Triple(org.apache.hyracks.algebricks.common.utils.Triple) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) UnionAllOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator) HashSet(java.util.HashSet)

Example 87 with AssignOperator

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

the class PushAssignBelowUnionAllRule method cloneAssignOperator.

/**
     * Clones the given assign operator changing the returned variables to be new ones.
     * Also, leaves the inputs of the clone clear.
     */
private AssignOperator cloneAssignOperator(AssignOperator assignOp, IOptimizationContext context) {
    List<LogicalVariable> vars = new ArrayList<LogicalVariable>();
    List<Mutable<ILogicalExpression>> exprs = new ArrayList<Mutable<ILogicalExpression>>();
    int numVars = assignOp.getVariables().size();
    for (int i = 0; i < numVars; i++) {
        vars.add(context.newVar());
        exprs.add(new MutableObject<ILogicalExpression>(assignOp.getExpressions().get(i).getValue().cloneExpression()));
    }
    AssignOperator assignCloneOp = new AssignOperator(vars, exprs);
    assignCloneOp.setExecutionMode(assignOp.getExecutionMode());
    return assignCloneOp;
}
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) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)

Example 88 with AssignOperator

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

the class PushFunctionsBelowJoin method pushDownFunctions.

private boolean pushDownFunctions(AbstractBinaryJoinOperator joinOp, int inputIndex, List<Mutable<ILogicalExpression>> funcExprs, IOptimizationContext context) throws AlgebricksException {
    ILogicalOperator joinInputOp = joinOp.getInputs().get(inputIndex).getValue();
    liveVars.clear();
    VariableUtilities.getLiveVariables(joinInputOp, liveVars);
    Iterator<Mutable<ILogicalExpression>> funcIter = funcExprs.iterator();
    List<LogicalVariable> assignVars = null;
    List<Mutable<ILogicalExpression>> assignExprs = null;
    while (funcIter.hasNext()) {
        Mutable<ILogicalExpression> funcExprRef = funcIter.next();
        ILogicalExpression funcExpr = funcExprRef.getValue();
        usedVars.clear();
        funcExpr.getUsedVariables(usedVars);
        // Check if we can push the function down this branch.
        if (liveVars.containsAll(usedVars)) {
            if (assignVars == null) {
                assignVars = new ArrayList<LogicalVariable>();
                assignExprs = new ArrayList<Mutable<ILogicalExpression>>();
            }
            // Replace the original expression with a variable reference expression.
            LogicalVariable replacementVar = context.newVar();
            assignVars.add(replacementVar);
            assignExprs.add(new MutableObject<ILogicalExpression>(funcExpr));
            funcExprRef.setValue(new VariableReferenceExpression(replacementVar));
            funcIter.remove();
        }
    }
    // Create new assign operator below the join if any functions can be pushed.
    if (assignVars != null) {
        AssignOperator newAssign = new AssignOperator(assignVars, assignExprs);
        newAssign.getInputs().add(new MutableObject<ILogicalOperator>(joinInputOp));
        newAssign.setExecutionMode(joinOp.getExecutionMode());
        joinOp.getInputs().get(inputIndex).setValue(newAssign);
        context.computeAndSetTypeEnvironmentForOperator(newAssign);
        return true;
    }
    return false;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) 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) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)

Example 89 with AssignOperator

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

the class FactorRedundantGroupAndDecorVarsRule method factorRedundantRhsVars.

private boolean factorRedundantRhsVars(List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> veList, Mutable<ILogicalOperator> opRef, Map<LogicalVariable, LogicalVariable> varRhsToLhs, IOptimizationContext context) throws AlgebricksException {
    varRhsToLhs.clear();
    ListIterator<Pair<LogicalVariable, Mutable<ILogicalExpression>>> iter = veList.listIterator();
    boolean changed = false;
    while (iter.hasNext()) {
        Pair<LogicalVariable, Mutable<ILogicalExpression>> p = iter.next();
        if (p.second.getValue().getExpressionTag() != LogicalExpressionTag.VARIABLE) {
            continue;
        }
        LogicalVariable v = GroupByOperator.getDecorVariable(p);
        LogicalVariable lhs = varRhsToLhs.get(v);
        if (lhs != null) {
            if (p.first != null) {
                AssignOperator assign = new AssignOperator(p.first, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(lhs)));
                ILogicalOperator op = opRef.getValue();
                assign.getInputs().add(new MutableObject<ILogicalOperator>(op));
                opRef.setValue(assign);
                context.computeAndSetTypeEnvironmentForOperator(assign);
            }
            iter.remove();
            changed = true;
        } else {
            varRhsToLhs.put(v, p.first);
        }
    }
    return changed;
}
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) 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)

Example 90 with AssignOperator

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

the class InlineAssignIntoAggregateRule method inlined.

private boolean inlined(Mutable<ILogicalOperator> r) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) r.getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
    if (op2.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
        return false;
    }
    AggregateOperator agg = (AggregateOperator) op1;
    AssignOperator assign = (AssignOperator) op2;
    VarExprSubstitution ves = new VarExprSubstitution(assign.getVariables(), assign.getExpressions());
    for (Mutable<ILogicalExpression> exprRef : agg.getExpressions()) {
        ILogicalExpression expr = exprRef.getValue();
        Pair<Boolean, ILogicalExpression> p = expr.accept(ves, null);
        if (p.first == true) {
            exprRef.setValue(p.second);
        }
    // AbstractLogicalExpression ale = (AbstractLogicalExpression) expr;
    // ale.accept(ves, null);
    }
    List<Mutable<ILogicalOperator>> op1InpList = op1.getInputs();
    op1InpList.clear();
    op1InpList.add(op2.getInputs().get(0));
    return true;
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)

Aggregations

AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)95 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)79 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)75 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)58 Mutable (org.apache.commons.lang3.mutable.Mutable)56 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)52 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)42 ArrayList (java.util.ArrayList)41 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)41 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)32 Pair (org.apache.hyracks.algebricks.common.utils.Pair)30 MutableObject (org.apache.commons.lang3.mutable.MutableObject)24 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)24 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)19 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)14 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)14 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)14 List (java.util.List)12 AString (org.apache.asterix.om.base.AString)12 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)12