Search in sources :

Example 1 with FunctionalDependency

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

the class AlgebricksOptimizationContext method updatePrimaryKeys.

@Override
public void updatePrimaryKeys(Map<LogicalVariable, LogicalVariable> mappedVars) {
    for (Map.Entry<LogicalVariable, FunctionalDependency> me : varToPrimaryKey.entrySet()) {
        FunctionalDependency fd = me.getValue();
        List<LogicalVariable> hd = new ArrayList<>();
        for (LogicalVariable v : fd.getHead()) {
            LogicalVariable v2 = mappedVars.get(v);
            if (v2 == null) {
                hd.add(v);
            } else {
                hd.add(v2);
            }
        }
        List<LogicalVariable> tl = new ArrayList<>();
        for (LogicalVariable v : fd.getTail()) {
            LogicalVariable v2 = mappedVars.get(v);
            if (v2 == null) {
                tl.add(v);
            } else {
                tl.add(v2);
            }
        }
        me.setValue(new FunctionalDependency(hd, tl));
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with FunctionalDependency

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

the class UnnestToDataScanRule method addPrimaryKey.

private void addPrimaryKey(List<LogicalVariable> scanVariables, DataSource dataSource, IOptimizationContext context) {
    List<LogicalVariable> primaryKey = dataSource.getPrimaryKeyVariables(scanVariables);
    List<LogicalVariable> tail = new ArrayList<>();
    tail.addAll(scanVariables);
    FunctionalDependency pk = new FunctionalDependency(primaryKey, tail);
    context.addPrimaryKey(pk);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) ArrayList(java.util.ArrayList)

Example 3 with FunctionalDependency

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

the class AbstractPreclusteredGroupByPOperator method getRequiredPropertiesForChildren.

@Override
public PhysicalRequirements getRequiredPropertiesForChildren(ILogicalOperator op, IPhysicalPropertiesVector reqdByParent, IOptimizationContext context) {
    GroupByOperator gby = (GroupByOperator) op;
    StructuralPropertiesVector[] pv = new StructuralPropertiesVector[1];
    if (gby.isGroupAll() && gby.isGlobal()) {
        if (op.getExecutionMode() == ExecutionMode.UNPARTITIONED) {
            pv[0] = new StructuralPropertiesVector(IPartitioningProperty.UNPARTITIONED, null);
            return new PhysicalRequirements(pv, IPartitioningRequirementsCoordinator.NO_COORDINATION);
        } else {
            return emptyUnaryRequirements();
        }
    }
    List<ILocalStructuralProperty> localProps = new ArrayList<>();
    Set<LogicalVariable> gbvars = new ListSet<>(columnList);
    LocalGroupingProperty groupProp = new LocalGroupingProperty(gbvars, new ArrayList<>(columnList));
    boolean goon = true;
    for (ILogicalPlan p : gby.getNestedPlans()) {
        // groupings
        for (Mutable<ILogicalOperator> r : p.getRoots()) {
            AbstractLogicalOperator op1 = (AbstractLogicalOperator) r.getValue();
            if (op1.getOperatorTag() == LogicalOperatorTag.AGGREGATE) {
                AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
                IPhysicalOperator pop2 = op2.getPhysicalOperator();
                if (pop2 instanceof AbstractPreclusteredGroupByPOperator) {
                    List<LogicalVariable> gbyColumns = ((AbstractPreclusteredGroupByPOperator) pop2).getGbyColumns();
                    List<LogicalVariable> sndOrder = new ArrayList<>();
                    sndOrder.addAll(gbyColumns);
                    Set<LogicalVariable> freeVars = new HashSet<>();
                    try {
                        OperatorPropertiesUtil.getFreeVariablesInSelfOrDesc(op2, freeVars);
                    } catch (AlgebricksException e) {
                        throw new IllegalStateException(e);
                    }
                    // Only considers group key variables defined out-side the outer-most group-by operator.
                    sndOrder.retainAll(freeVars);
                    groupProp.getColumnSet().addAll(sndOrder);
                    groupProp.getPreferredOrderEnforcer().addAll(sndOrder);
                    goon = false;
                    break;
                }
            }
        }
        if (!goon) {
            break;
        }
    }
    localProps.add(groupProp);
    if (reqdByParent != null) {
        // propagate parent requirements
        List<ILocalStructuralProperty> lpPar = reqdByParent.getLocalProperties();
        if (lpPar != null) {
            boolean allOk = true;
            List<ILocalStructuralProperty> props = new ArrayList<>(lpPar.size());
            for (ILocalStructuralProperty prop : lpPar) {
                if (prop.getPropertyType() != PropertyType.LOCAL_ORDER_PROPERTY) {
                    allOk = false;
                    break;
                }
                LocalOrderProperty lop = (LocalOrderProperty) prop;
                List<OrderColumn> orderColumns = new ArrayList<>();
                List<OrderColumn> ords = lop.getOrderColumns();
                for (OrderColumn ord : ords) {
                    Pair<LogicalVariable, Mutable<ILogicalExpression>> p = getGbyPairByRhsVar(gby, ord.getColumn());
                    if (p == null) {
                        p = getDecorPairByRhsVar(gby, ord.getColumn());
                        if (p == null) {
                            allOk = false;
                            break;
                        }
                    }
                    ILogicalExpression e = p.second.getValue();
                    if (e.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
                        throw new IllegalStateException("Right hand side of group-by assignment should have been normalized to a variable reference.");
                    }
                    LogicalVariable v = ((VariableReferenceExpression) e).getVariableReference();
                    orderColumns.add(new OrderColumn(v, ord.getOrder()));
                }
                props.add(new LocalOrderProperty(orderColumns));
            }
            List<FunctionalDependency> fdList = new ArrayList<>();
            for (Pair<LogicalVariable, Mutable<ILogicalExpression>> decorPair : gby.getDecorList()) {
                List<LogicalVariable> hd = gby.getGbyVarList();
                List<LogicalVariable> tl = new ArrayList<>();
                tl.add(((VariableReferenceExpression) decorPair.second.getValue()).getVariableReference());
                fdList.add(new FunctionalDependency(hd, tl));
            }
            if (allOk && PropertiesUtil.matchLocalProperties(localProps, props, new HashMap<>(), fdList)) {
                localProps = props;
            }
        }
    }
    IPartitioningProperty pp = null;
    AbstractLogicalOperator aop = (AbstractLogicalOperator) op;
    if (aop.getExecutionMode() == ExecutionMode.PARTITIONED) {
        pp = new UnorderedPartitionedProperty(new ListSet<>(columnList), context.getComputationNodeDomain());
    }
    pv[0] = new StructuralPropertiesVector(pp, localProps);
    return new PhysicalRequirements(pv, IPartitioningRequirementsCoordinator.NO_COORDINATION);
}
Also used : HashMap(java.util.HashMap) LocalGroupingProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalGroupingProperty) ArrayList(java.util.ArrayList) IPartitioningProperty(org.apache.hyracks.algebricks.core.algebra.properties.IPartitioningProperty) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) IPhysicalOperator(org.apache.hyracks.algebricks.core.algebra.base.IPhysicalOperator) ILocalStructuralProperty(org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty) HashSet(java.util.HashSet) StructuralPropertiesVector(org.apache.hyracks.algebricks.core.algebra.properties.StructuralPropertiesVector) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) UnorderedPartitionedProperty(org.apache.hyracks.algebricks.core.algebra.properties.UnorderedPartitionedProperty) 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) OrderColumn(org.apache.hyracks.algebricks.core.algebra.properties.OrderColumn) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) PhysicalRequirements(org.apache.hyracks.algebricks.core.algebra.properties.PhysicalRequirements) 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) LocalOrderProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalOrderProperty) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 4 with FunctionalDependency

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

the class PrimaryKeyVariablesVisitor method visitAssignOperator.

@Override
public Void visitAssignOperator(AssignOperator op, IOptimizationContext ctx) throws AlgebricksException {
    // Obtain used variables on the right-hand side of an assign.
    Set<LogicalVariable> usedVars = new HashSet<>();
    VariableUtilities.getUsedVariables(op, usedVars);
    Set<LogicalVariable> primaryKeyVars = null;
    for (LogicalVariable usedVar : usedVars) {
        List<LogicalVariable> keyVars = ctx.findPrimaryKey(usedVar);
        if (keyVars == null) {
            // No key variables can uniquely identify usedVar.
            return null;
        }
        if (primaryKeyVars == null) {
            primaryKeyVars = new HashSet<>(keyVars);
        } else {
            // The primary key is the union of all the key header variables.
            primaryKeyVars.addAll(keyVars);
        }
    }
    if (primaryKeyVars != null && !primaryKeyVars.isEmpty()) {
        List<LogicalVariable> producedVars = new ArrayList<>();
        VariableUtilities.getProducedVariables(op, producedVars);
        // Generates new primary keys.
        ctx.addPrimaryKey(new FunctionalDependency(new ArrayList<LogicalVariable>(primaryKeyVars), producedVars));
    }
    return null;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 5 with FunctionalDependency

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

the class PrimaryKeyVariablesVisitor method visitGroupByOperator.

@Override
public Void visitGroupByOperator(GroupByOperator op, IOptimizationContext ctx) throws AlgebricksException {
    List<LogicalVariable> header = new ArrayList<>();
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> gbyTerm : op.getGroupByList()) {
        header.add(gbyTerm.first);
    }
    List<LogicalVariable> liveVars = new ArrayList<>();
    VariableUtilities.getSubplanLocalLiveVariables(op, liveVars);
    ctx.addPrimaryKey(new FunctionalDependency(header, liveVars));
    return null;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) ArrayList(java.util.ArrayList)

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