Search in sources :

Example 26 with ILocalStructuralProperty

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

the class AbstractPreclusteredGroupByPOperator method computeDeliveredProperties.

// Obs: We don't propagate properties corresponding to decors, since they
// are func. dep. on the group-by variables.
@Override
public void computeDeliveredProperties(ILogicalOperator op, IOptimizationContext context) {
    List<ILocalStructuralProperty> propsLocal = new ArrayList<>();
    GroupByOperator gby = (GroupByOperator) op;
    ILogicalOperator op2 = gby.getInputs().get(0).getValue();
    IPhysicalPropertiesVector childProp = op2.getDeliveredPhysicalProperties();
    IPartitioningProperty pp = childProp.getPartitioningProperty();
    List<ILocalStructuralProperty> childLocals = childProp.getLocalProperties();
    if (childLocals == null) {
        deliveredProperties = new StructuralPropertiesVector(pp, propsLocal);
        return;
    }
    for (ILocalStructuralProperty lsp : childLocals) {
        ILocalStructuralProperty propagatedLsp = getPropagatedProperty(lsp, gby);
        if (propagatedLsp != null) {
            propsLocal.add(propagatedLsp);
        }
    }
    deliveredProperties = new StructuralPropertiesVector(pp, propsLocal);
}
Also used : StructuralPropertiesVector(org.apache.hyracks.algebricks.core.algebra.properties.StructuralPropertiesVector) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) IPhysicalPropertiesVector(org.apache.hyracks.algebricks.core.algebra.properties.IPhysicalPropertiesVector) ILocalStructuralProperty(org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty) IPartitioningProperty(org.apache.hyracks.algebricks.core.algebra.properties.IPartitioningProperty)

Example 27 with ILocalStructuralProperty

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

the class EnforceStructuralPropertiesRule method addLocalEnforcers.

private void addLocalEnforcers(AbstractLogicalOperator op, int i, List<ILocalStructuralProperty> localProperties, boolean nestedPlan, IOptimizationContext context) throws AlgebricksException {
    if (AlgebricksConfig.DEBUG) {
        AlgebricksConfig.ALGEBRICKS_LOGGER.fine(">>>> Adding local enforcers for local props = " + localProperties + "\n");
    }
    if (localProperties == null || localProperties.isEmpty()) {
        return;
    }
    Mutable<ILogicalOperator> topOp = new MutableObject<>();
    topOp.setValue(op.getInputs().get(i).getValue());
    LinkedList<LocalOrderProperty> oList = new LinkedList<>();
    for (ILocalStructuralProperty prop : localProperties) {
        switch(prop.getPropertyType()) {
            case LOCAL_ORDER_PROPERTY:
                {
                    oList.add((LocalOrderProperty) prop);
                    break;
                }
            case LOCAL_GROUPING_PROPERTY:
                {
                    LocalGroupingProperty g = (LocalGroupingProperty) prop;
                    Collection<LogicalVariable> vars = (g.getPreferredOrderEnforcer() != null) ? g.getPreferredOrderEnforcer() : g.getColumnSet();
                    List<OrderColumn> orderColumns = new ArrayList<>();
                    for (LogicalVariable v : vars) {
                        OrderColumn oc = new OrderColumn(v, OrderKind.ASC);
                        orderColumns.add(oc);
                    }
                    LocalOrderProperty lop = new LocalOrderProperty(orderColumns);
                    oList.add(lop);
                    break;
                }
            default:
                {
                    throw new IllegalStateException();
                }
        }
    }
    if (!oList.isEmpty()) {
        topOp = enforceOrderProperties(oList, topOp, nestedPlan, context);
    }
    op.getInputs().set(i, topOp);
    OperatorPropertiesUtil.computeSchemaAndPropertiesRecIfNull((AbstractLogicalOperator) topOp.getValue(), context);
    OperatorManipulationUtil.setOperatorMode(op);
    printOp((AbstractLogicalOperator) topOp.getValue());
}
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) OrderColumn(org.apache.hyracks.algebricks.core.algebra.properties.OrderColumn) LinkedList(java.util.LinkedList) LocalOrderProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalOrderProperty) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ILocalStructuralProperty(org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 28 with ILocalStructuralProperty

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

the class EnforceStructuralPropertiesRule method rewritePre.

@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    // wait for the physical operators to be set first
    if (op.getPhysicalOperator() == null) {
        return false;
    }
    if (context.checkIfInDontApplySet(this, op)) {
        return false;
    }
    List<FunctionalDependency> fds = context.getFDList(op);
    if (fds != null && !fds.isEmpty()) {
        return false;
    }
    // These are actually logical constraints, so they could be pre-computed
    // somewhere else, too.
    physicalOptimizationConfig = context.getPhysicalOptimizationConfig();
    AlgebricksConfig.ALGEBRICKS_LOGGER.fine(">>>> Optimizing operator " + op.getPhysicalOperator() + ".\n");
    PhysicalOptimizationsUtil.computeFDsAndEquivalenceClasses(op, context);
    StructuralPropertiesVector pvector = new StructuralPropertiesVector(new RandomPartitioningProperty(context.getComputationNodeDomain()), new LinkedList<ILocalStructuralProperty>());
    boolean changed = physOptimizeOp(opRef, pvector, false, context);
    op.computeDeliveredPhysicalProperties(context);
    AlgebricksConfig.ALGEBRICKS_LOGGER.finest(">>>> Structural properties for " + op.getPhysicalOperator() + ": " + op.getDeliveredPhysicalProperties() + "\n");
    context.addToDontApplySet(this, opRef.getValue());
    return changed;
}
Also used : StructuralPropertiesVector(org.apache.hyracks.algebricks.core.algebra.properties.StructuralPropertiesVector) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) RandomPartitioningProperty(org.apache.hyracks.algebricks.core.algebra.properties.RandomPartitioningProperty) ILocalStructuralProperty(org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty)

Example 29 with ILocalStructuralProperty

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

the class EnforceStructuralPropertiesRule method getOrderColumnsFromGroupingProperties.

private List<OrderColumn> getOrderColumnsFromGroupingProperties(List<ILocalStructuralProperty> reqd, List<ILocalStructuralProperty> dlvd) {
    List<OrderColumn> returnedProperties = new ArrayList<>();
    List<LogicalVariable> rqdCols = new ArrayList<>();
    List<LogicalVariable> dlvdCols = new ArrayList<>();
    for (ILocalStructuralProperty r : reqd) {
        r.getVariables(rqdCols);
    }
    for (ILocalStructuralProperty d : dlvd) {
        d.getVariables(dlvdCols);
    }
    int prefix = dlvdCols.size() - 1;
    while (prefix >= 0) {
        if (!rqdCols.contains(dlvdCols.get(prefix))) {
            prefix--;
        } else {
            break;
        }
    }
    LocalOrderProperty orderProp = (LocalOrderProperty) dlvd.get(0);
    List<OrderColumn> orderColumns = orderProp.getOrderColumns();
    for (int j = 0; j <= prefix; j++) {
        returnedProperties.add(new OrderColumn(orderColumns.get(j).getColumn(), orderColumns.get(j).getOrder()));
    }
    // maintain other order columns after the required order columns
    if (!returnedProperties.isEmpty()) {
        for (int j = prefix + 1; j < dlvdCols.size(); j++) {
            OrderColumn oc = orderColumns.get(j);
            returnedProperties.add(new OrderColumn(oc.getColumn(), oc.getOrder()));
        }
    }
    return returnedProperties;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) OrderColumn(org.apache.hyracks.algebricks.core.algebra.properties.OrderColumn) LocalOrderProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalOrderProperty) ArrayList(java.util.ArrayList) ILocalStructuralProperty(org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty)

Example 30 with ILocalStructuralProperty

use of org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty 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)

Aggregations

ILocalStructuralProperty (org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty)30 ArrayList (java.util.ArrayList)24 StructuralPropertiesVector (org.apache.hyracks.algebricks.core.algebra.properties.StructuralPropertiesVector)22 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)19 LocalOrderProperty (org.apache.hyracks.algebricks.core.algebra.properties.LocalOrderProperty)15 OrderColumn (org.apache.hyracks.algebricks.core.algebra.properties.OrderColumn)14 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)13 IPhysicalPropertiesVector (org.apache.hyracks.algebricks.core.algebra.properties.IPhysicalPropertiesVector)11 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)10 IPartitioningProperty (org.apache.hyracks.algebricks.core.algebra.properties.IPartitioningProperty)10 ListSet (org.apache.hyracks.algebricks.common.utils.ListSet)9 LinkedList (java.util.LinkedList)8 PhysicalRequirements (org.apache.hyracks.algebricks.core.algebra.properties.PhysicalRequirements)8 LocalGroupingProperty (org.apache.hyracks.algebricks.core.algebra.properties.LocalGroupingProperty)7 UnorderedPartitionedProperty (org.apache.hyracks.algebricks.core.algebra.properties.UnorderedPartitionedProperty)6 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)5 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 FunctionalDependency (org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency)4