public <S> S capture(Capture<S> capture) { return captures().get(capture); }
public <T> T capture(Capture<T> capture) { return captures().get(capture); }
public final <C> Stream<Match> match(Object object, Captures captures, C context) { if (previous.isPresent()) { return previous.get().match(object, captures, context) .flatMap(match -> accept(object, match.captures(), context)); } else { return accept(object, captures, context); } }
private <T> Rule.Result transform(PlanNode node, Rule<T> rule, Context context) { Capture<T> nodeCapture = newCapture(); Pattern<T> pattern = rule.getPattern().capturedAs(nodeCapture); Iterator<Match> matches = pattern.match(node, context.lookup).iterator(); while (matches.hasNext()) { Match match = matches.next(); long duration; Rule.Result result; try { long start = System.nanoTime(); result = rule.apply(match.capture(nodeCapture), match.captures(), ruleContext(context)); duration = System.nanoTime() - start; } catch (RuntimeException e) { stats.recordFailure(rule); throw e; } stats.record(rule, duration, !result.isEmpty()); if (result.getTransformedPlan().isPresent()) { return result; } } return Rule.Result.empty(); }
@Override public <T> Match<T> match(Pattern<T> pattern, Object object, Captures captures) { if (pattern.previous() != null) { Match<?> match = match(pattern.previous(), object, captures); return match.flatMap((value) -> pattern.accept(this, value, match.captures())); } else { return pattern.accept(this, object, captures); } }
private <T> Rule.Result transform(PlanNode node, Rule<T> rule, Matcher matcher, Context context) { Rule.Result result; Match<T> match = matcher.match(rule.getPattern(), node); if (match.isEmpty()) { return Rule.Result.empty(); } long duration; try { long start = System.nanoTime(); result = rule.apply(match.value(), match.captures(), ruleContext(context)); duration = System.nanoTime() - start; } catch (RuntimeException e) { stats.recordFailure(rule); throw e; } stats.record(rule, duration, !result.isEmpty()); return result; }
private static <T> RuleApplication applyRule(Rule<T> rule, PlanNode planNode, Rule.Context context) { Capture<T> planNodeCapture = newCapture(); Pattern<T> pattern = rule.getPattern().capturedAs(planNodeCapture); Optional<Match> match = pattern.match(planNode, context.getLookup()) .collect(toOptional()); Rule.Result result; if (!rule.isEnabled(context.getSession()) || !match.isPresent()) { result = Rule.Result.empty(); } else { result = rule.apply(match.get().capture(planNodeCapture), match.get().captures(), context); } return new RuleApplication(context.getLookup(), context.getStatsProvider(), context.getSymbolAllocator().getTypes(), result); }
private static <T> RuleApplication applyRule(Rule<T> rule, PlanNode planNode, Rule.Context context) { PlanNodeMatcher matcher = new PlanNodeMatcher(context.getLookup()); Match<T> match = matcher.match(rule.getPattern(), planNode); Rule.Result result; if (!rule.isEnabled(context.getSession()) || match.isEmpty()) { result = Rule.Result.empty(); } else { result = rule.apply(match.value(), match.captures(), context); } return new RuleApplication(context.getLookup(), context.getStatsProvider(), context.getSymbolAllocator().getTypes(), result); }