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.List;
 import java.util.Map;
 
 import static com.facebook.presto.sql.ExpressionUtils.combineConjuncts;
 import static java.util.Objects.requireNonNull;

Pick an arbitrary layout if none has been chosen
 
 public class PickLayout
         extends PlanOptimizer
 {
     private final Metadata metadata;
 
     public PickLayout(Metadata metadata)
     {
         requireNonNull(metadata"metadata is null");
 
         this. = metadata;
     }
 
     @Override
     public PlanNode optimize(PlanNode planSession sessionMap<SymbolTypetypesSymbolAllocator symbolAllocatorPlanNodeIdAllocator idAllocator)
     {
         return PlanRewriter.rewriteWith(new Rewriter(sessionsymbolAllocatoridAllocator), plan);
     }
 
     private static class Rewriter
             extends PlanRewriter<Void>
     {
         private final Metadata metadata;
         private final Session session;
         private final SymbolAllocator symbolAllocator;
         private final PlanNodeIdAllocator idAllocator;
 
         public Rewriter(Metadata metadataSession sessionSymbolAllocator symbolAllocatorPlanNodeIdAllocator idAllocator)
         {
             this. = metadata;
             this. = session;
             this. = symbolAllocator;
             this. = idAllocator;
         }
 
         @Override
         protected PlanNode visitPlan(PlanNode nodeRewriteContext<Voidcontext)
         {
             return context.defaultRewrite(node);
         }
 
         @Override
         public PlanNode visitFilter(FilterNode nodeRewriteContext<Voidcontext)
         {
             if (node.getSource() instanceof TableScanNode && !((TableScanNodenode.getSource()).getLayout().isPresent()) {
                 return planTableScan((TableScanNodenode.getSource(), node.getPredicate());
             }
 
             return context.defaultRewrite(node);
         }
 
         @Override
         public PlanNode visitTableScan(TableScanNode nodeRewriteContext<Voidcontext)
        {
            if (node.getLayout().isPresent()) {
                return node;
            }
            return planTableScan(node.);
        }
        private PlanNode planTableScan(TableScanNode nodeExpression predicate)
        {
            DomainTranslator.ExtractionResult decomposedPredicate = DomainTranslator.fromPredicate(
                    ,
                    ,
                    predicate,
                    .getTypes());
            TupleDomain<ColumnHandlesimplifiedConstraint = decomposedPredicate.getTupleDomain()
                    .transform(node.getAssignments()::get)
                    .intersect(node.getCurrentConstraint());
            List<TableLayoutResultlayouts = .getLayouts(
                    node.getTable(),
                    new Constraint<>(simplifiedConstraintbindings -> true),
                    Optional.of(ImmutableSet.copyOf(node.getAssignments().values())));
            if (layouts.isEmpty()) {
                return new ValuesNode(.getNextId(), node.getOutputSymbols(), ImmutableList.of());
            }
            TableLayoutResult layout = layouts.get(0);
            TableScanNode result = new TableScanNode(
                    node.getId(),
                    node.getTable(),
                    node.getOutputSymbols(),
                    node.getAssignments(),
                    Optional.of(layout.getLayout().getHandle()),
                    simplifiedConstraint.intersect(layout.getLayout().getPredicate()),
                    Optional.ofNullable(node.getOriginalConstraint()).orElse(predicate));
            Map<ColumnHandleSymbolassignments = ImmutableBiMap.copyOf(node.getAssignments()).inverse();
            Expression resultingPredicate = combineConjuncts(
                    decomposedPredicate.getRemainingExpression(),
                    DomainTranslator.toPredicate(
                            layout.getUnenforcedConstraint().transform(assignments::get),
                            .getTypes()));
            if (!..equals(resultingPredicate)) {
                return new FilterNode(.getNextId(), resultresultingPredicate);
            }
            return result;
        }
    }
New to GrepCode? Check out our FAQ X