Search in sources :

Example 1 with SINGLE

use of com.facebook.presto.sql.planner.plan.AggregationNode.Step.SINGLE in project presto by prestodb.

the class SingleMarkDistinctToGroupBy method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof AggregationNode)) {
        return Optional.empty();
    }
    AggregationNode parent = (AggregationNode) node;
    PlanNode source = lookup.resolve(parent.getSource());
    if (!(source instanceof MarkDistinctNode)) {
        return Optional.empty();
    }
    MarkDistinctNode child = (MarkDistinctNode) source;
    boolean hasFilters = parent.getAggregations().values().stream().map(FunctionCall::getFilter).anyMatch(Optional::isPresent);
    if (hasFilters) {
        return Optional.empty();
    }
    // optimize if and only if
    // all aggregation functions have a single common distinct mask symbol
    // AND all aggregation functions have mask
    Set<Symbol> masks = ImmutableSet.copyOf(parent.getMasks().values());
    if (masks.size() != 1 || parent.getMasks().size() != parent.getAggregations().size()) {
        return Optional.empty();
    }
    Symbol mask = Iterables.getOnlyElement(masks);
    if (!child.getMarkerSymbol().equals(mask)) {
        return Optional.empty();
    }
    return Optional.of(new AggregationNode(idAllocator.getNextId(), new AggregationNode(idAllocator.getNextId(), child.getSource(), Collections.emptyMap(), ImmutableList.of(child.getDistinctSymbols()), SINGLE, child.getHashSymbol(), Optional.empty()), // remove DISTINCT flag from function calls
    parent.getAssignments().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> removeDistinct(e.getValue()))), parent.getGroupingSets(), parent.getStep(), parent.getHashSymbol(), parent.getGroupIdSymbol()));
}
Also used : PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) Iterables(com.google.common.collect.Iterables) ImmutableSet(com.google.common.collect.ImmutableSet) Rule(com.facebook.presto.sql.planner.iterative.Rule) Set(java.util.Set) Collectors(java.util.stream.Collectors) Lookup(com.facebook.presto.sql.planner.iterative.Lookup) SINGLE(com.facebook.presto.sql.planner.plan.AggregationNode.Step.SINGLE) MarkDistinctNode(com.facebook.presto.sql.planner.plan.MarkDistinctNode) ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Map(java.util.Map) Optional(java.util.Optional) SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Collections(java.util.Collections) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) MarkDistinctNode(com.facebook.presto.sql.planner.plan.MarkDistinctNode) Optional(java.util.Optional) Symbol(com.facebook.presto.sql.planner.Symbol) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Map(java.util.Map)

Aggregations

PlanNodeIdAllocator (com.facebook.presto.sql.planner.PlanNodeIdAllocator)1 Symbol (com.facebook.presto.sql.planner.Symbol)1 SymbolAllocator (com.facebook.presto.sql.planner.SymbolAllocator)1 Lookup (com.facebook.presto.sql.planner.iterative.Lookup)1 Rule (com.facebook.presto.sql.planner.iterative.Rule)1 AggregationNode (com.facebook.presto.sql.planner.plan.AggregationNode)1 SINGLE (com.facebook.presto.sql.planner.plan.AggregationNode.Step.SINGLE)1 MarkDistinctNode (com.facebook.presto.sql.planner.plan.MarkDistinctNode)1 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)1 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1