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.plan;
 
 
 import java.util.List;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public abstract class PlanRewriter<C>
         extends PlanVisitor<PlanRewriter.RewriteContext<C>, PlanNode>
 {
     public static <C> PlanNode rewriteWith(PlanRewriter<C> rewriterPlanNode node)
     {
         return node.accept(rewriternew RewriteContext<>(rewriternull));
     }
 
     public static <C> PlanNode rewriteWith(PlanRewriter<C> rewriterPlanNode node, C context)
     {
         return node.accept(rewriternew RewriteContext<>(rewritercontext));
     }
 
     @Override
     protected PlanNode visitPlan(PlanNode nodeRewriteContext<C> context)
     {
         return context.defaultRewrite(nodecontext.get());
     }
 
     public static class RewriteContext<C>
     {
         private final C userContext;
         private final PlanRewriter<C> nodeRewriter;
 
         private RewriteContext(PlanRewriter<C> nodeRewriter, C userContext)
         {
             this. = nodeRewriter;
             this. = userContext;
         }
 
         public C get()
         {
             return ;
         }

        
Invoke the rewrite logic recursively on children of the given node and swap it out with an identical copy with the rewritten children
 
         public PlanNode defaultRewrite(PlanNode node)
         {
             return defaultRewrite(nodenull);
         }

        
Invoke the rewrite logic recursively on children of the given node and swap it out with an identical copy with the rewritten children
 
         public PlanNode defaultRewrite(PlanNode node, C context)
         {
             List<PlanNodechildren = node.getSources().stream()
                     .map(child -> rewrite(childcontext))
                     .collect(ImmutableCollectors.toImmutableList());
 
             return replaceChildren(nodechildren);
         }

        
Return an identical copy of the given node with its children replaced
 
         public PlanNode replaceChildren(PlanNode nodeList<PlanNodenewChildren)
         {
             for (int i = 0; i < node.getSources().size(); i++) {
                 if (newChildren.get(i) != node.getSources().get(i)) {
                     return ChildReplacer.replaceChildren(nodenewChildren);
                 }
             }
 
             // children haven't change, so make this a no-op
             return node;
         }

        
This method is meant for invoking the rewrite logic on children while processing a node
 
         public PlanNode rewrite(PlanNode node, C userContext)
         {
             PlanNode result = node.accept(new RewriteContext<>(userContext));
            checkNotNull(result"nodeRewriter returned null for %s"node.getClass().getName());
            return result;
        }

        
This method is meant for invoking the rewrite logic on children while processing a node
        public PlanNode rewrite(PlanNode node)
        {
            return rewrite(nodenull);
        }
    }
New to GrepCode? Check out our FAQ X