use of com.facebook.presto.sql.planner.plan.MarkDistinctNode 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()));
}
use of com.facebook.presto.sql.planner.plan.MarkDistinctNode in project presto by prestodb.
the class PushLimitThroughMarkDistinct method apply.
@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
if (!(node instanceof LimitNode)) {
return Optional.empty();
}
LimitNode parent = (LimitNode) node;
PlanNode child = lookup.resolve(parent.getSource());
if (!(child instanceof MarkDistinctNode)) {
return Optional.empty();
}
return Optional.of(transpose(parent, child));
}
Aggregations