CostProvider costProvider = new CachingCostProvider(costCalculator, statsProvider, Optional.of(context.memo), context.session, context.symbolAllocator.getTypes()); return new Rule.Context()
private boolean exploreNode(int group, Context context, Matcher matcher) { PlanNode node = context.memo.getNode(group); boolean done = false; boolean progress = false; while (!done) { context.checkTimeoutNotExhausted(); done = true; Iterator<Rule<?>> possiblyMatchingRules = ruleIndex.getCandidates(node).iterator(); while (possiblyMatchingRules.hasNext()) { Rule<?> rule = possiblyMatchingRules.next(); if (!rule.isEnabled(context.session)) { continue; } Rule.Result result = transform(node, rule, matcher, context); if (result.getTransformedPlan().isPresent()) { node = context.memo.replace(group, result.getTransformedPlan().get(), rule.getClass().getName()); done = false; progress = true; } } } return progress; }
@Override public PlanNode optimize(PlanNode plan, Session session, TypeProvider types, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector) { // only disable new rules if we have legacy rules to fall back to if (!SystemSessionProperties.isNewOptimizerEnabled(session) && !legacyRules.isEmpty()) { for (PlanOptimizer optimizer : legacyRules) { plan = optimizer.optimize(plan, session, symbolAllocator.getTypes(), symbolAllocator, idAllocator, warningCollector); } return plan; } Memo memo = new Memo(idAllocator, plan); Lookup lookup = Lookup.from(planNode -> Stream.of(memo.resolve(planNode))); Matcher matcher = new PlanNodeMatcher(lookup); Duration timeout = SystemSessionProperties.getOptimizerTimeout(session); Context context = new Context(memo, lookup, idAllocator, symbolAllocator, System.nanoTime(), timeout.toMillis(), session, warningCollector); exploreGroup(memo.getRootGroup(), context, matcher); return memo.extract(); }
@Override public void checkTimeoutNotExhausted() { context.checkTimeoutNotExhausted(); }