Search in sources :

Example 16 with EquivalenceClass

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

the class FDsAndEquivClassesVisitor method visitSubplanOperator.

@Override
public Void visitSubplanOperator(SubplanOperator op, IOptimizationContext ctx) throws AlgebricksException {
    Map<LogicalVariable, EquivalenceClass> equivalenceClasses = new HashMap<LogicalVariable, EquivalenceClass>();
    List<FunctionalDependency> functionalDependencies = new ArrayList<FunctionalDependency>();
    ctx.putEquivalenceClassMap(op, equivalenceClasses);
    ctx.putFDList(op, functionalDependencies);
    for (ILogicalPlan p : op.getNestedPlans()) {
        for (Mutable<ILogicalOperator> r : p.getRoots()) {
            ILogicalOperator op2 = r.getValue();
            equivalenceClasses.putAll(getOrComputeEqClasses(op2, ctx));
            functionalDependencies.addAll(getOrComputeFDs(op2, ctx));
        }
    }
    return null;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) HashMap(java.util.HashMap) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass)

Example 17 with EquivalenceClass

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

the class FDsAndEquivClassesVisitor method visitInnerJoinOperator.

@Override
public Void visitInnerJoinOperator(InnerJoinOperator op, IOptimizationContext ctx) throws AlgebricksException {
    Map<LogicalVariable, EquivalenceClass> equivalenceClasses = new HashMap<LogicalVariable, EquivalenceClass>();
    List<FunctionalDependency> functionalDependencies = new ArrayList<FunctionalDependency>();
    ctx.putEquivalenceClassMap(op, equivalenceClasses);
    ctx.putFDList(op, functionalDependencies);
    ILogicalOperator op0 = op.getInputs().get(0).getValue();
    ILogicalOperator op1 = op.getInputs().get(1).getValue();
    functionalDependencies.addAll(getOrComputeFDs(op0, ctx));
    functionalDependencies.addAll(getOrComputeFDs(op1, ctx));
    equivalenceClasses.putAll(getOrComputeEqClasses(op0, ctx));
    equivalenceClasses.putAll(getOrComputeEqClasses(op1, ctx));
    ILogicalExpression expr = op.getCondition().getValue();
    expr.getConstraintsAndEquivClasses(functionalDependencies, equivalenceClasses);
    return null;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) HashMap(java.util.HashMap) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass)

Example 18 with EquivalenceClass

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

the class FDsAndEquivClassesVisitor method visitGroupByOperator.

@Override
public Void visitGroupByOperator(GroupByOperator op, IOptimizationContext ctx) throws AlgebricksException {
    Map<LogicalVariable, EquivalenceClass> equivalenceClasses = new HashMap<LogicalVariable, EquivalenceClass>();
    List<FunctionalDependency> functionalDependencies = new ArrayList<FunctionalDependency>();
    ctx.putEquivalenceClassMap(op, equivalenceClasses);
    ctx.putFDList(op, functionalDependencies);
    List<FunctionalDependency> inheritedFDs = new ArrayList<FunctionalDependency>();
    for (ILogicalPlan p : op.getNestedPlans()) {
        for (Mutable<ILogicalOperator> r : p.getRoots()) {
            ILogicalOperator op2 = r.getValue();
            equivalenceClasses.putAll(getOrComputeEqClasses(op2, ctx));
            inheritedFDs.addAll(getOrComputeFDs(op2, ctx));
        }
    }
    ILogicalOperator op0 = op.getInputs().get(0).getValue();
    inheritedFDs.addAll(getOrComputeFDs(op0, ctx));
    Map<LogicalVariable, EquivalenceClass> inheritedEcs = getOrComputeEqClasses(op0, ctx);
    for (FunctionalDependency inherited : inheritedFDs) {
        boolean isCoveredByGbyOrDecorVars = true;
        List<LogicalVariable> newHead = new ArrayList<LogicalVariable>(inherited.getHead().size());
        for (LogicalVariable v : inherited.getHead()) {
            LogicalVariable vnew = getNewGbyVar(op, v);
            if (vnew == null) {
                vnew = getNewDecorVar(op, v);
                if (vnew == null) {
                    isCoveredByGbyOrDecorVars = false;
                }
                break;
            }
            newHead.add(vnew);
        }
        if (isCoveredByGbyOrDecorVars) {
            List<LogicalVariable> newTail = new ArrayList<LogicalVariable>();
            for (LogicalVariable v2 : inherited.getTail()) {
                LogicalVariable v3 = getNewGbyVar(op, v2);
                if (v3 != null) {
                    newTail.add(v3);
                }
            }
            if (!newTail.isEmpty()) {
                FunctionalDependency newFd = new FunctionalDependency(newHead, newTail);
                functionalDependencies.add(newFd);
            }
        }
    }
    List<LogicalVariable> premiseGby = new LinkedList<LogicalVariable>();
    List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> gByList = op.getGroupByList();
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gByList) {
        premiseGby.add(p.first);
    }
    List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorList = op.getDecorList();
    LinkedList<LogicalVariable> conclDecor = new LinkedList<LogicalVariable>();
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : decorList) {
        conclDecor.add(GroupByOperator.getDecorVariable(p));
    }
    if (!conclDecor.isEmpty()) {
        functionalDependencies.add(new FunctionalDependency(premiseGby, conclDecor));
    }
    Set<LogicalVariable> gbySet = new HashSet<LogicalVariable>();
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gByList) {
        ILogicalExpression expr = p.second.getValue();
        if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression v = (VariableReferenceExpression) expr;
            gbySet.add(v.getVariableReference());
        }
    }
    LocalGroupingProperty lgp = new LocalGroupingProperty(gbySet);
    ILocalStructuralProperty normalizedLgp = lgp.normalize(inheritedEcs, inheritedFDs);
    Set<LogicalVariable> normSet = new ListSet<>();
    normalizedLgp.getColumns(normSet);
    List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> newGbyList = new ArrayList<>();
    boolean changed = false;
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gByList) {
        ILogicalExpression expr = p.second.getValue();
        if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression varRef = (VariableReferenceExpression) expr;
            LogicalVariable v2 = varRef.getVariableReference();
            EquivalenceClass ec2 = inheritedEcs.get(v2);
            LogicalVariable v3;
            if (ec2 != null && !ec2.representativeIsConst()) {
                v3 = ec2.getVariableRepresentative();
            } else {
                v3 = v2;
            }
            if (normSet.contains(v3)) {
                newGbyList.add(p);
            } else {
                changed = true;
                decorList.add(p);
            }
        } else {
            newGbyList.add(p);
        }
    }
    if (changed) {
        AlgebricksConfig.ALGEBRICKS_LOGGER.fine(">>>> Group-by list changed from " + GroupByOperator.veListToString(gByList) + " to " + GroupByOperator.veListToString(newGbyList) + ".\n");
    }
    gByList.clear();
    gByList.addAll(newGbyList);
    return null;
}
Also used : HashMap(java.util.HashMap) LocalGroupingProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalGroupingProperty) ArrayList(java.util.ArrayList) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass) ILocalStructuralProperty(org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty) Pair(org.apache.hyracks.algebricks.common.utils.Pair) HashSet(java.util.HashSet) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LinkedList(java.util.LinkedList) 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) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 19 with EquivalenceClass

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

the class AddEquivalenceClassForRecordConstructorRule method propagateEquivalenceClassesForRecordConstructor.

@SuppressWarnings("unchecked")
private boolean propagateEquivalenceClassesForRecordConstructor(LogicalVariable recordVar, AbstractFunctionCallExpression funcExpr, AssignOperator assignOp, IOptimizationContext context) {
    List<Mutable<ILogicalExpression>> argRefs = funcExpr.getArguments();
    boolean changed = false;
    // Only odd position arguments are field value expressions.
    for (int parameterIndex = 1; parameterIndex < argRefs.size(); parameterIndex += 2) {
        ILogicalExpression fieldExpr = argRefs.get(parameterIndex).getValue();
        // Adds equivalent classes if a field is from a variable reference.
        if (fieldExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression varExpr = (VariableReferenceExpression) fieldExpr;
            LogicalVariable fieldVar = varExpr.getVariableReference();
            Map<LogicalVariable, EquivalenceClass> ecs = context.getEquivalenceClassMap(assignOp);
            if (ecs == null) {
                ecs = new HashMap<LogicalVariable, EquivalenceClass>();
                context.putEquivalenceClassMap(assignOp, ecs);
            }
            ILogicalExpression expr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX), new MutableObject<ILogicalExpression>(new VariableReferenceExpression(recordVar)), new MutableObject<ILogicalExpression>(// Every two parameters corresponds to a field.
            new ConstantExpression(new AsterixConstantValue(new AInt32(parameterIndex / 2)))));
            EquivalenceClass equivClass = new EquivalenceClass(Collections.singletonList(fieldVar), fieldVar, Collections.singletonList(expr));
            ecs.put(fieldVar, equivClass);
            changed = true;
        }
    }
    return changed;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AInt32(org.apache.asterix.om.base.AInt32) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 20 with EquivalenceClass

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

the class AbstractFunctionCallExpression method getFDsAndEquivClassesForEqWithConstant.

private static final void getFDsAndEquivClassesForEqWithConstant(ConstantExpression c, VariableReferenceExpression v, Collection<FunctionalDependency> fds, Map<LogicalVariable, EquivalenceClass> equivClasses) {
    LogicalVariable var = v.getVariableReference();
    LinkedList<LogicalVariable> head = new LinkedList<LogicalVariable>();
    // empty set in the head
    LinkedList<LogicalVariable> tail = new LinkedList<LogicalVariable>();
    tail.add(var);
    FunctionalDependency fd = new FunctionalDependency(head, tail);
    fds.add(fd);
    EquivalenceClass ec = equivClasses.get(var);
    if (ec == null) {
        LinkedList<LogicalVariable> members = new LinkedList<LogicalVariable>();
        members.add(var);
        EquivalenceClass eclass = new EquivalenceClass(members, c);
        equivClasses.put(var, eclass);
    } else {
        if (ec.representativeIsConst()) {
            ILogicalExpression c1 = ec.getConstRepresentative();
            if (!c1.equals(c)) {
                // here I could also rewrite to FALSE
                return;
            }
        }
        ec.setConstRepresentative(c);
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass) LinkedList(java.util.LinkedList)

Aggregations

EquivalenceClass (org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass)21 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)21 FunctionalDependency (org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency)16 ArrayList (java.util.ArrayList)14 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)14 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)11 HashMap (java.util.HashMap)7 LinkedList (java.util.LinkedList)5 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)4 HashSet (java.util.HashSet)3 Mutable (org.apache.commons.lang3.mutable.Mutable)3 ListSet (org.apache.hyracks.algebricks.common.utils.ListSet)3 List (java.util.List)2 AInt32 (org.apache.asterix.om.base.AInt32)2 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)2 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)2 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)2 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)2 ILocalStructuralProperty (org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty)2