Search in sources :

Example 11 with FunctionalDependency

use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.

the class FDsAndEquivClassesVisitor method visitDistinctOperator.

@Override
public Void visitDistinctOperator(DistinctOperator op, IOptimizationContext ctx) throws AlgebricksException {
    ILogicalOperator op0 = op.getInputs().get(0).getValue();
    List<FunctionalDependency> functionalDependencies = new ArrayList<FunctionalDependency>();
    ctx.putFDList(op, functionalDependencies);
    for (FunctionalDependency inherited : getOrComputeFDs(op0, ctx)) {
        boolean isCoveredByDistinctByVars = true;
        for (LogicalVariable v : inherited.getHead()) {
            if (!op.isDistinctByVar(v)) {
                isCoveredByDistinctByVars = false;
            }
        }
        if (isCoveredByDistinctByVars) {
            List<LogicalVariable> newTail = new ArrayList<LogicalVariable>();
            for (LogicalVariable v2 : inherited.getTail()) {
                if (op.isDistinctByVar(v2)) {
                    newTail.add(v2);
                }
            }
            if (!newTail.isEmpty()) {
                List<LogicalVariable> newHead = new ArrayList<LogicalVariable>(inherited.getHead());
                FunctionalDependency newFd = new FunctionalDependency(newHead, newTail);
                functionalDependencies.add(newFd);
            }
        }
    }
    Set<LogicalVariable> gbySet = new HashSet<LogicalVariable>();
    List<Mutable<ILogicalExpression>> expressions = op.getExpressions();
    for (Mutable<ILogicalExpression> pRef : expressions) {
        ILogicalExpression p = pRef.getValue();
        if (p.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression v = (VariableReferenceExpression) p;
            gbySet.add(v.getVariableReference());
        }
    }
    LocalGroupingProperty lgp = new LocalGroupingProperty(gbySet);
    Map<LogicalVariable, EquivalenceClass> equivalenceClasses = getOrComputeEqClasses(op0, ctx);
    ctx.putEquivalenceClassMap(op, equivalenceClasses);
    ILocalStructuralProperty normalizedLgp = lgp.normalize(equivalenceClasses, functionalDependencies);
    Set<LogicalVariable> normSet = new ListSet<>();
    normalizedLgp.getColumns(normSet);
    List<Mutable<ILogicalExpression>> newDistinctByList = new ArrayList<Mutable<ILogicalExpression>>();
    for (Mutable<ILogicalExpression> p2Ref : expressions) {
        ILogicalExpression p2 = p2Ref.getValue();
        if (p2.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression var2 = (VariableReferenceExpression) p2;
            LogicalVariable v2 = var2.getVariableReference();
            if (normSet.contains(v2)) {
                newDistinctByList.add(p2Ref);
            }
        } else {
            newDistinctByList.add(p2Ref);
        }
    }
    expressions.clear();
    expressions.addAll(newDistinctByList);
    return null;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LocalGroupingProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalGroupingProperty) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) 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) HashSet(java.util.HashSet)

Example 12 with FunctionalDependency

use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.

the class FDsAndEquivClassesVisitor method visitAssignOperator.

@Override
public Void visitAssignOperator(AssignOperator op, IOptimizationContext ctx) throws AlgebricksException {
    ILogicalOperator inp1 = op.getInputs().get(0).getValue();
    Map<LogicalVariable, EquivalenceClass> eqClasses = getOrComputeEqClasses(inp1, ctx);
    ctx.putEquivalenceClassMap(op, eqClasses);
    // Propagates equivalence classes that from expressions.
    // Note that an equivalence class can also contain expression members.
    propagateEquivalenceFromExpressionsToVariables(eqClasses, op.getExpressions(), op.getVariables());
    // Generates FDs.
    List<LogicalVariable> used = new ArrayList<LogicalVariable>();
    VariableUtilities.getUsedVariables(op, used);
    List<FunctionalDependency> fds1 = getOrComputeFDs(inp1, ctx);
    List<FunctionalDependency> eFds = new ArrayList<FunctionalDependency>(fds1.size());
    for (FunctionalDependency fd : fds1) {
        if (fd.getTail().containsAll(used)) {
            List<LogicalVariable> hd = new ArrayList<LogicalVariable>(fd.getHead());
            List<LogicalVariable> tl = new ArrayList<LogicalVariable>(fd.getTail());
            tl.addAll(op.getVariables());
            FunctionalDependency fd2 = new FunctionalDependency(hd, tl);
            eFds.add(fd2);
        } else {
            eFds.add(fd);
        }
    }
    ctx.putFDList(op, eFds);
    return null;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) ArrayList(java.util.ArrayList) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass)

Example 13 with FunctionalDependency

use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.

the class FDsAndEquivClassesVisitor method propagateFDsAndEquivClassesForUsedVars.

/***
     * Propagated equivalent classes from the child to the current operator,
     * based on the used variables of the current operator.
     *
     * @param op
     *            , the current operator
     * @param ctx
     *            , the optimization context which keeps track of all equivalent
     *            classes.
     * @param usedVariables
     *            , used variables.
     * @throws AlgebricksException
     */
private void propagateFDsAndEquivClassesForUsedVars(ILogicalOperator op, IOptimizationContext ctx, List<LogicalVariable> usedVariables) throws AlgebricksException {
    ILogicalOperator op2 = op.getInputs().get(0).getValue();
    Map<LogicalVariable, EquivalenceClass> eqClasses = getOrCreateEqClasses(op, ctx);
    List<FunctionalDependency> fds = new ArrayList<FunctionalDependency>();
    ctx.putFDList(op, fds);
    Map<LogicalVariable, EquivalenceClass> chldClasses = getOrComputeEqClasses(op2, ctx);
    // Propagate equivalent classes that contain the used variables.
    for (LogicalVariable v : usedVariables) {
        EquivalenceClass ec = eqClasses.get(v);
        if (ec == null) {
            EquivalenceClass oc = chldClasses.get(v);
            if (oc == null) {
                continue;
            }
            List<LogicalVariable> m = new LinkedList<LogicalVariable>();
            for (LogicalVariable v2 : oc.getMembers()) {
                if (usedVariables.contains(v2)) {
                    m.add(v2);
                }
            }
            EquivalenceClass nc;
            if (oc.representativeIsConst()) {
                nc = new EquivalenceClass(m, oc.getConstRepresentative());
            } else if (m.contains(oc.getVariableRepresentative())) {
                nc = new EquivalenceClass(m, oc.getVariableRepresentative());
            } else {
                nc = new EquivalenceClass(m, v);
            }
            for (LogicalVariable v3 : m) {
                eqClasses.put(v3, nc);
            }
        }
    }
    // Propagates equivalent classes that contain expressions that use the
    // used variables.
    // Note that for the case variable $v is not in the used variables but
    // it is
    // equivalent to field-access($t, i) and $t is a used variable, the
    // equivalent
    // class should still be propagated (kept).
    Set<LogicalVariable> usedVarSet = new HashSet<LogicalVariable>(usedVariables);
    for (Entry<LogicalVariable, EquivalenceClass> entry : chldClasses.entrySet()) {
        EquivalenceClass ec = entry.getValue();
        for (ILogicalExpression expr : ec.getExpressionMembers()) {
            Set<LogicalVariable> exprUsedVars = new HashSet<LogicalVariable>();
            expr.getUsedVariables(exprUsedVars);
            exprUsedVars.retainAll(usedVarSet);
            // Check if the expression member uses a used variable.
            if (!exprUsedVars.isEmpty()) {
                for (LogicalVariable v : ec.getMembers()) {
                    eqClasses.put(v, ec);
                    // variable should be a used variable.
                    if (usedVarSet.contains(v)) {
                        ec.setVariableRepresentative(v);
                    }
                }
            }
        }
    }
    List<FunctionalDependency> chldFds = getOrComputeFDs(op2, ctx);
    for (FunctionalDependency fd : chldFds) {
        if (!usedVariables.containsAll(fd.getHead())) {
            continue;
        }
        List<LogicalVariable> tl = new LinkedList<LogicalVariable>();
        for (LogicalVariable v : fd.getTail()) {
            if (usedVariables.contains(v)) {
                tl.add(v);
            }
        }
        if (!tl.isEmpty()) {
            FunctionalDependency newFd = new FunctionalDependency(fd.getHead(), tl);
            fds.add(newFd);
        }
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) 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) HashSet(java.util.HashSet)

Example 14 with FunctionalDependency

use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.

the class FDsAndEquivClassesVisitor method visitNestedTupleSourceOperator.

@Override
public Void visitNestedTupleSourceOperator(NestedTupleSourceOperator op, IOptimizationContext ctx) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) op.getDataSourceReference().getValue();
    ILogicalOperator inp1 = op1.getInputs().get(0).getValue();
    Map<LogicalVariable, EquivalenceClass> eqClasses = getOrComputeEqClasses(inp1, ctx);
    ctx.putEquivalenceClassMap(op, eqClasses);
    List<FunctionalDependency> fds = new ArrayList<FunctionalDependency>(getOrComputeFDs(inp1, ctx));
    if (op1.getOperatorTag() == LogicalOperatorTag.GROUP) {
        GroupByOperator gby = (GroupByOperator) op1;
        LinkedList<LogicalVariable> tail = new LinkedList<LogicalVariable>();
        for (LogicalVariable v : gby.getGbyVarList()) {
            tail.add(v);
        // all values for gby vars. are the same
        }
        FunctionalDependency gbyfd = new FunctionalDependency(new LinkedList<LogicalVariable>(), tail);
        fds.add(gbyfd);
    }
    ctx.putFDList(op, fds);
    return null;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) ArrayList(java.util.ArrayList) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass) LinkedList(java.util.LinkedList)

Example 15 with FunctionalDependency

use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.

the class FDsAndEquivClassesVisitor method visitSelectOperator.

@Override
public Void visitSelectOperator(SelectOperator op, IOptimizationContext ctx) throws AlgebricksException {
    ILogicalOperator childOp = op.getInputs().get(0).getValue();
    Map<LogicalVariable, EquivalenceClass> equivalenceClasses = getOrComputeEqClasses(childOp, ctx);
    ctx.putEquivalenceClassMap(op, equivalenceClasses);
    List<FunctionalDependency> functionalDependencies = new ArrayList<FunctionalDependency>();
    ctx.putFDList(op, functionalDependencies);
    functionalDependencies.addAll(getOrComputeFDs(childOp, ctx));
    equivalenceClasses.putAll(getOrComputeEqClasses(childOp, 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) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) ArrayList(java.util.ArrayList) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass)

Aggregations

FunctionalDependency (org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency)29 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)27 ArrayList (java.util.ArrayList)22 EquivalenceClass (org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass)16 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)15 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)10 HashSet (java.util.HashSet)8 HashMap (java.util.HashMap)7 LinkedList (java.util.LinkedList)6 Mutable (org.apache.commons.lang3.mutable.Mutable)4 ILocalStructuralProperty (org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty)4 ListSet (org.apache.hyracks.algebricks.common.utils.ListSet)3 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)3 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)3 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)3 LocalGroupingProperty (org.apache.hyracks.algebricks.core.algebra.properties.LocalGroupingProperty)3 Map (java.util.Map)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)2 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)2 StructuralPropertiesVector (org.apache.hyracks.algebricks.core.algebra.properties.StructuralPropertiesVector)2