Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package com.facebook.presto.sql.planner.optimizations;
 import java.util.Map;
 import java.util.Set;
 import static com.facebook.presto.sql.planner.plan.AggregationNode.Step.SINGLE;
 import static;

Converts Single Distinct Aggregation into GroupBy Rewrite if and only if 1 all aggregation functions have a single common distinct mask symbol 2 all aggregation functions have mask Rewrite MarkDistinctNode into AggregationNode(use DistinctSymbols as GroupBy) Add ProjectNode on top of the new AggregationNode, which adds null assignment for mask All unused mask will be removed by PruneUnreferencedOutputs Remove Distincts in the original AggregationNode
 public class SingleDistinctOptimizer
         extends PlanOptimizer
     public PlanNode optimize(PlanNode planSession sessionMap<SymbolTypetypesSymbolAllocator symbolAllocatorPlanNodeIdAllocator idAllocator)
         return PlanRewriter.rewriteWith(new Optimizer(idAllocator), plan, Optional.empty());
     private static class Optimizer
             extends PlanRewriter<Optional<Symbol>>
         private final PlanNodeIdAllocator idAllocator;
         private Optimizer(PlanNodeIdAllocator idAllocator)
             this. = checkNotNull(idAllocator"idAllocator is null");
         public PlanNode visitAggregation(AggregationNode nodeRewriteContext<Optional<Symbol>> context)
             // optimize if and only if
             // all aggregation functions have a single common distinct mask symbol
             // AND all aggregation functions have mask
             Set<Symbolmasks = ImmutableSet.copyOf(node.getMasks().values());
             if (masks.size() != 1 || node.getMasks().size() != node.getAggregations().size()) {
                 return context.defaultRewrite(node, Optional.empty());
             PlanNode source = context.rewrite(node.getSource(), Optional.of(Iterables.getOnlyElement(masks)));
             Map<SymbolFunctionCallaggregations = ImmutableMap.copyOf(Maps.transformValues(node.getAggregations(), call -> new FunctionCall(call.getName(), call.getWindow(), falsecall.getArguments())));
             return new AggregationNode(.getNextId(),
        public PlanNode visitMarkDistinct(MarkDistinctNode nodeRewriteContext<Optional<Symbol>> context)
            Optional<Symbolmask = context.get();
            if (mask.isPresent() && mask.get().equals(node.getMarkerSymbol())) {
                // rewrite Distinct into GroupBy
                AggregationNode aggregationNode = new AggregationNode(.getNextId(),
                                                                        context.rewrite(node.getSource(), Optional.empty()),
                ImmutableMap.Builder<SymbolExpressionoutputSymbols = ImmutableMap.builder();
                for (Symbol symbol : aggregationNode.getOutputSymbols()) {
                    Expression expression = new QualifiedNameReference(symbol.toQualifiedName());
                // add null assignment for mask
                // unused mask will be removed by PruneUnreferencedOutputs
                outputSymbols.put(mask.get(), new NullLiteral());
                return new ProjectNode(.getNextId(),
            return context.defaultRewrite(node, Optional.empty());
New to GrepCode? Check out our FAQ X