Search in sources :

Example 6 with UnionAllOperator

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

the class RemoveUnusedAssignAndAggregateRule method removeFromAssigns.

private int removeFromAssigns(AbstractLogicalOperator op, Set<LogicalVariable> toRemove, IOptimizationContext context) throws AlgebricksException {
    switch(op.getOperatorTag()) {
        case ASSIGN:
            AssignOperator assign = (AssignOperator) op;
            if (removeUnusedVarsAndExprs(toRemove, assign.getVariables(), assign.getExpressions())) {
                context.computeAndSetTypeEnvironmentForOperator(assign);
                isTransformed = true;
            }
            return assign.getVariables().size();
        case AGGREGATE:
            AggregateOperator agg = (AggregateOperator) op;
            if (removeUnusedVarsAndExprs(toRemove, agg.getVariables(), agg.getExpressions())) {
                context.computeAndSetTypeEnvironmentForOperator(agg);
                isTransformed = true;
            }
            return agg.getVariables().size();
        case UNNEST:
            UnnestOperator uOp = (UnnestOperator) op;
            LogicalVariable pVar = uOp.getPositionalVariable();
            if (pVar != null && toRemove != null && toRemove.contains(pVar)) {
                uOp.setPositionalVariable(null);
                assignedVarSet.remove(pVar);
                isTransformed = true;
            }
            break;
        case UNIONALL:
            UnionAllOperator unionOp = (UnionAllOperator) op;
            if (removeUnusedVarsFromUnionAll(unionOp, toRemove)) {
                context.computeAndSetTypeEnvironmentForOperator(unionOp);
                isTransformed = true;
            }
            return unionOp.getVariableMappings().size();
        case GROUP:
            GroupByOperator groupByOp = (GroupByOperator) op;
            if (removeUnusedVarsFromGroupBy(groupByOp, toRemove)) {
                context.computeAndSetTypeEnvironmentForOperator(groupByOp);
                isTransformed = true;
            }
            return groupByOp.getGroupByList().size() + groupByOp.getNestedPlans().size() + groupByOp.getDecorList().size();
        default:
            break;
    }
    return -1;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) UnionAllOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)

Example 7 with UnionAllOperator

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

the class PushUnnestDownThroughUnionRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator unnest = (AbstractLogicalOperator) opRef.getValue();
    if (unnest.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }
    UnnestOperator unnestOpRef = (UnnestOperator) opRef.getValue();
    Mutable<ILogicalOperator> unionOp = unnest.getInputs().get(0);
    AbstractLogicalOperator unionAbstractOp = (AbstractLogicalOperator) unionOp.getValue();
    if (unionAbstractOp.getOperatorTag() != LogicalOperatorTag.UNIONALL) {
        return false;
    }
    LogicalVariable unnestVar1 = context.newVar();
    UnnestOperator unnest1 = new UnnestOperator(unnestVar1, new MutableObject<ILogicalExpression>(unnestOpRef.getExpressionRef().getValue().cloneExpression()));
    LogicalVariable unnestVar2 = context.newVar();
    UnnestOperator unnest2 = new UnnestOperator(unnestVar2, new MutableObject<ILogicalExpression>(unnestOpRef.getExpressionRef().getValue().cloneExpression()));
    //Getting the two topmost branched and adding them as an input to the unnests:
    Mutable<ILogicalOperator> branch1 = unionAbstractOp.getInputs().get(0);
    ILogicalOperator agg1 = branch1.getValue();
    List<LogicalVariable> agg1_var = new ArrayList<LogicalVariable>();
    VariableUtilities.getLiveVariables(agg1, agg1_var);
    Mutable<ILogicalOperator> branch2 = unionAbstractOp.getInputs().get(1);
    ILogicalOperator agg2 = branch2.getValue();
    List<LogicalVariable> agg2_var = new ArrayList<LogicalVariable>();
    VariableUtilities.getLiveVariables(agg2, agg2_var);
    //Modifying the unnest so it has the right variable
    List<LogicalVariable> var_unnest_1 = new ArrayList<LogicalVariable>();
    unnest1.getExpressionRef().getValue().getUsedVariables(var_unnest_1);
    unnest1.getExpressionRef().getValue().substituteVar(var_unnest_1.get(0), agg1_var.get(0));
    List<LogicalVariable> var_unnest2 = new ArrayList<LogicalVariable>();
    unnest2.getExpressionRef().getValue().getUsedVariables(var_unnest2);
    unnest2.getExpressionRef().getValue().substituteVar(var_unnest2.get(0), agg2_var.get(0));
    unnest1.getInputs().add(branch1);
    unnest2.getInputs().add(branch2);
    context.computeAndSetTypeEnvironmentForOperator(unnest1);
    context.computeAndSetTypeEnvironmentForOperator(unnest2);
    //creating a new union operator with the updated logical variables
    List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> varMap = new ArrayList<Triple<LogicalVariable, LogicalVariable, LogicalVariable>>(1);
    Triple<LogicalVariable, LogicalVariable, LogicalVariable> union_triple_vars = new Triple<LogicalVariable, LogicalVariable, LogicalVariable>(unnestVar1, unnestVar2, unnestOpRef.getVariables().get(0));
    varMap.add(union_triple_vars);
    UnionAllOperator unionOpFinal = new UnionAllOperator(varMap);
    unionOpFinal.getInputs().add(new MutableObject<ILogicalOperator>(unnest1));
    unionOpFinal.getInputs().add(new MutableObject<ILogicalOperator>(unnest2));
    context.computeAndSetTypeEnvironmentForOperator(unionOpFinal);
    opRef.setValue(unionOpFinal);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) Triple(org.apache.hyracks.algebricks.common.utils.Triple) 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)

Example 8 with UnionAllOperator

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

the class IsomorphismOperatorVisitor method visitUnionOperator.

@Override
public Boolean visitUnionOperator(UnionAllOperator op, ILogicalOperator arg) throws AlgebricksException {
    AbstractLogicalOperator aop = (AbstractLogicalOperator) arg;
    if (aop.getOperatorTag() != LogicalOperatorTag.UNIONALL) {
        return Boolean.FALSE;
    }
    UnionAllOperator unionOpArg = (UnionAllOperator) copyAndSubstituteVar(op, arg);
    List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> mapping = op.getVariableMappings();
    List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> mappingArg = unionOpArg.getVariableMappings();
    if (mapping.size() != mappingArg.size()) {
        return Boolean.FALSE;
    }
    return VariableUtilities.varListEqualUnordered(mapping, mappingArg);
}
Also used : Triple(org.apache.hyracks.algebricks.common.utils.Triple) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) UnionAllOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator)

Example 9 with UnionAllOperator

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

the class InsertProjectBeforeUnionRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.UNIONALL) {
        return false;
    }
    UnionAllOperator opUnion = (UnionAllOperator) op;
    List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> varMap = opUnion.getVariableMappings();
    ArrayList<LogicalVariable> usedVariablesFromOne = new ArrayList<>();
    ArrayList<LogicalVariable> usedVariablesFromTwo = new ArrayList<>();
    for (Triple<LogicalVariable, LogicalVariable, LogicalVariable> triple : varMap) {
        usedVariablesFromOne.add(triple.first);
        usedVariablesFromTwo.add(triple.second);
    }
    ArrayList<LogicalVariable> inputSchemaOne = new ArrayList<>();
    VariableUtilities.getLiveVariables(opUnion.getInputs().get(0).getValue(), inputSchemaOne);
    ArrayList<LogicalVariable> inputSchemaTwo = new ArrayList<>();
    VariableUtilities.getLiveVariables(opUnion.getInputs().get(1).getValue(), inputSchemaTwo);
    boolean rewritten = false;
    if (!isIdentical(usedVariablesFromOne, inputSchemaOne)) {
        insertProjectOperator(opUnion, 0, usedVariablesFromOne, context);
        rewritten = true;
    }
    if (!isIdentical(usedVariablesFromTwo, inputSchemaTwo)) {
        insertProjectOperator(opUnion, 1, usedVariablesFromTwo, context);
        rewritten = true;
    }
    return rewritten;
}
Also used : Triple(org.apache.hyracks.algebricks.common.utils.Triple) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) UnionAllOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator) ArrayList(java.util.ArrayList)

Example 10 with UnionAllOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator 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)

Aggregations

UnionAllOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator)12 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)9 Triple (org.apache.hyracks.algebricks.common.utils.Triple)7 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)7 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)6 ArrayList (java.util.ArrayList)5 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)5 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)4 UnnestOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator)4 Mutable (org.apache.commons.lang3.mutable.Mutable)3 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)3 HashSet (java.util.HashSet)2 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)2 AbstractOperatorWithNestedPlans (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans)2 AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)2 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)2 List (java.util.List)1 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)1 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)1 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)1