Parameterize IterativeOptimizer methods with Matcher
This is going to be used in the next commit to pass
a different Matcher implementation.
@@ -16,6 +16,7 @@ package com.facebook.presto.sql.planner.iterative;
import com.facebook.presto.Session;
import com.facebook.presto.SystemSessionProperties;
import com.facebook.presto.matching.Match;
+import com.facebook.presto.matching.Matcher;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.sql.planner.PlanNodeIdAllocator;
@@ -80,23 +81,23 @@ public class IterativeOptimizer
Duration timeout = SystemSessionProperties.getOptimizerTimeout(session);
Context context = new Context(memo, lookup, idAllocator, symbolAllocator, System.nanoTime(), timeout.toMillis(), session);
- exploreGroup(memo.getRootGroup(), context);
+ exploreGroup(memo.getRootGroup(), context, DEFAULT_MATCHER);
return memo.extract();
}
- private boolean exploreGroup(int group, Context context)
+ private boolean exploreGroup(int group, Context context, Matcher matcher)
{
// tracks whether this group or any children groups change as
// this method executes
- boolean progress = exploreNode(group, context);
+ boolean progress = exploreNode(group, context, matcher);
- while (exploreChildren(group, context)) {
+ while (exploreChildren(group, context, matcher)) {
progress = true;
// if children changed, try current group again
// in case we can match additional rules
- if (!exploreNode(group, context)) {
+ if (!exploreNode(group, context, matcher)) {
// no additional matches, so bail out
break;
}
@@ -105,7 +106,7 @@ public class IterativeOptimizer
return progress;
}
- private boolean exploreNode(int group, Context context)
+ private boolean exploreNode(int group, Context context, Matcher matcher)
{
PlanNode node = context.getMemo().getNode(group);
@@ -126,7 +127,7 @@ public class IterativeOptimizer
continue;
}
- Optional<PlanNode> transformed = transform(node, rule, context);
+ Optional<PlanNode> transformed = transform(node, rule, matcher, context);
if (transformed.isPresent()) {
node = context.getMemo().replace(group, transformed.get(), rule.getClass().getName());
@@ -140,11 +141,11 @@ public class IterativeOptimizer
return progress;
}
- private <T> Optional<PlanNode> transform(PlanNode node, Rule<T> rule, Context context)
+ private <T> Optional<PlanNode> transform(PlanNode node, Rule<T> rule, Matcher matcher, Context context)
{
Optional<PlanNode> transformed;
- Match<T> match = DEFAULT_MATCHER.match(rule.getPattern(), node);
+ Match<T> match = matcher.match(rule.getPattern(), node);
if (match.isEmpty()) {
return Optional.empty();
@@ -170,7 +171,7 @@ public class IterativeOptimizer
return ((System.nanoTime() - context.getStartTimeInNanos()) / 1_000_000) >= context.getTimeoutInMilliseconds();
}
- private boolean exploreChildren(int group, Context context)
+ private boolean exploreChildren(int group, Context context, Matcher matcher)
{
boolean progress = false;
@@ -178,7 +179,7 @@ public class IterativeOptimizer
for (PlanNode child : expression.getSources()) {
checkState(child instanceof GroupReference, "Expected child to be a group reference. Found: " + child.getClass().getName());
- if (exploreGroup(((GroupReference) child).getGroupId(), context)) {
+ if (exploreGroup(((GroupReference) child).getGroupId(), context, matcher)) {
progress = true;
}
}
