@Test public void noMatchMeansNoCaptures() { Capture<Void> impossible = newCapture(); Pattern<Void> pattern = typeOf(Void.class).capturedAs(impossible); Match<Void> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); assertTrue(match.isEmpty()); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(impossible)); assertTrue(throwable.getMessage().contains("Captures are undefined for an empty Match")); }
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) { 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); }
@Test public void noMatchMeansNoCaptures() { Capture<Void> impossible = newCapture(); Pattern<Void> pattern = typeOf(Void.class).capturedAs(impossible); Match<Void> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); assertTrue(match.isEmpty()); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(impossible)); assertTrue(throwable.getMessage().contains("Captures are undefined for an empty Match")); }