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
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * 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 com.google.common.base.Preconditions.checkNotNull;

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
 {
     @Override
     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");
         }
 
         @Override
         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(),
                                         source,
                                         node.getGroupBy(),
                                         aggregations,
                                         node.getFunctions(),
                                         Collections.emptyMap(),
                                         node.getStep(),
                                         node.getSampleWeight(),
                                         node.getConfidence(),
                                         node.getHashSymbol());
         }
 
        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()),
                                                                        node.getDistinctSymbols(),
                                                                        Collections.emptyMap(),
                                                                        Collections.emptyMap(),
                                                                        Collections.emptyMap(),
                                                                        ,
                                                                        Optional.empty(),
                                                                        1.0,
                                                                        node.getHashSymbol());
                ImmutableMap.Builder<SymbolExpressionoutputSymbols = ImmutableMap.builder();
                for (Symbol symbol : aggregationNode.getOutputSymbols()) {
                    Expression expression = new QualifiedNameReference(symbol.toQualifiedName());
                    outputSymbols.put(symbolexpression);
                }
                // add null assignment for mask
                // unused mask will be removed by PruneUnreferencedOutputs
                outputSymbols.put(mask.get(), new NullLiteral());
                return new ProjectNode(.getNextId(),
                                        aggregationNode,
                                        outputSymbols.build());
            }
            return context.defaultRewrite(node, Optional.empty());
        }
    }
New to GrepCode? Check out our FAQ X