public static <T> Capture<T> newCapture() { return newCapture(""); }
public static <T> Capture<T> newCapture() { return newCapture(""); }
@Test public void unknownCaptureIsAnError() { Pattern<?> pattern = any(); Capture<?> unknownCapture = newCapture(); Match<?> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(unknownCapture)); assertTrue(throwable.getMessage().contains("unknown Capture")); }
protected ManipulateAdjacentWindowsOverProjects(int numProjects) { PropertyPattern<PlanNode, ?, ?> childPattern = source().matching(window().capturedAs(childCapture)); ImmutableList.Builder<Capture<ProjectNode>> projectCapturesBuilder = ImmutableList.builder(); for (int i = 0; i < numProjects; ++i) { Capture<ProjectNode> projectCapture = newCapture(); projectCapturesBuilder.add(projectCapture); childPattern = source().matching(project().capturedAs(projectCapture).with(childPattern)); } this.projectCaptures = projectCapturesBuilder.build(); this.pattern = window().with(childPattern); }
protected ManipulateAdjacentWindowsOverProjects(int numProjects) { PropertyPattern<PlanNode, ?> childPattern = source().matching(window().capturedAs(childCapture)); ImmutableList.Builder<Capture<ProjectNode>> projectCapturesBuilder = ImmutableList.builder(); for (int i = 0; i < numProjects; ++i) { Capture<ProjectNode> projectCapture = newCapture(); projectCapturesBuilder.add(projectCapture); childPattern = source().matching(project().capturedAs(projectCapture).with(childPattern)); } this.projectCaptures = projectCapturesBuilder.build(); this.pattern = window().with(childPattern); }
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(); }
@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 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); }
@Test public void capturingMatchesInATypesafeManner() { Capture<FilterNode> filter = newCapture(); Capture<ScanNode> scan = newCapture(); Capture<String> name = newCapture(); Pattern<ProjectNode> pattern = project() .with(source().matching(filter().capturedAs(filter) .with(source().matching(scan().capturedAs(scan) .with(tableName().capturedAs(name)))))); ProjectNode tree = new ProjectNode(new FilterNode(new ScanNode("orders"), null)); Match<ProjectNode> match = assertMatch(pattern, tree); //notice the concrete type despite no casts: FilterNode capturedFilter = match.capture(filter); assertEquals(tree.getSource(), capturedFilter); assertEquals(((FilterNode) tree.getSource()).getSource(), match.capture(scan)); assertEquals("orders", match.capture(name)); }