use of io.trino.matching.Match in project trino by trinodb.
the class IterativeOptimizer method transform.
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 = nanoTime();
result = rule.apply(match.capture(nodeCapture), match.captures(), ruleContext(context));
if (LOG.isDebugEnabled() && !result.isEmpty()) {
LOG.debug("Rule: %s\nBefore:\n%s\nAfter:\n%s", rule.getClass().getName(), PlanPrinter.textLogicalPlan(node, context.symbolAllocator.getTypes(), plannerContext.getMetadata(), plannerContext.getFunctionManager(), StatsAndCosts.empty(), context.session, 0, false), PlanPrinter.textLogicalPlan(result.getTransformedPlan().get(), context.symbolAllocator.getTypes(), plannerContext.getMetadata(), plannerContext.getFunctionManager(), StatsAndCosts.empty(), context.session, 0, false));
}
duration = 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();
}
use of io.trino.matching.Match in project trino by trinodb.
the class ConnectorExpressionRewriter method rewrite.
private <ExpressionType extends ConnectorExpression> Optional<Result> rewrite(ConnectorExpressionRule<ExpressionType, Result> rule, ConnectorExpression expression, RewriteContext<Result> context) {
Capture<ExpressionType> expressionCapture = newCapture();
Pattern<ExpressionType> pattern = rule.getPattern().capturedAs(expressionCapture);
Iterator<Match> matches = pattern.match(expression, context).iterator();
while (matches.hasNext()) {
Match match = matches.next();
ExpressionType capturedExpression = match.capture(expressionCapture);
verify(capturedExpression == expression);
Optional<Result> rewritten = rule.rewrite(capturedExpression, match.captures(), context);
if (rewritten.isPresent()) {
return rewritten;
}
}
return Optional.empty();
}
use of io.trino.matching.Match in project trino by trinodb.
the class TestExpressionMatching method testMatchCall.
@Test
public void testMatchCall() {
ConnectorExpression expression = new Call(createDecimalType(21, 2), new FunctionName("add"), List.of(new Variable("first", createDecimalType(10, 2)), new Variable("second", BIGINT)));
ExpressionPattern pattern = expressionPattern("add(foo: decimal(p, s), bar: bigint)");
Match match = pattern.getPattern().match(expression).collect(onlyElement());
MatchContext matchContext = new MatchContext();
pattern.resolve(match.captures(), matchContext);
assertThat(matchContext.keys()).containsExactlyInAnyOrder("p", "s", "foo", "bar");
assertThat(matchContext.get("p")).isEqualTo(10L);
assertThat(matchContext.get("s")).isEqualTo(2L);
assertThat(matchContext.get("foo")).isEqualTo(new Variable("first", createDecimalType(10, 2)));
assertThat(matchContext.get("bar")).isEqualTo(new Variable("second", BIGINT));
}
use of io.trino.matching.Match in project trino by trinodb.
the class TestExpressionMatching method testExpressionCapture.
@Test
public void testExpressionCapture() {
ConnectorExpression expression = new Call(createDecimalType(21, 2), new FunctionName("add"), List.of(new Variable("first", createDecimalType(10, 2)), new Variable("second", BIGINT)));
ExpressionPattern pattern = expressionPattern("foo: decimal(p, s)");
Match match = pattern.getPattern().match(expression).collect(onlyElement());
MatchContext matchContext = new MatchContext();
pattern.resolve(match.captures(), matchContext);
assertThat(matchContext.keys()).containsExactlyInAnyOrder("p", "s", "foo");
assertThat(matchContext.get("p")).isEqualTo(21L);
assertThat(matchContext.get("s")).isEqualTo(2L);
assertThat(matchContext.get("foo")).isSameAs(expression);
}
use of io.trino.matching.Match in project trino by trinodb.
the class TestGenericRewrite method testRewriteCall.
@Test
public void testRewriteCall() {
GenericRewrite rewrite = new GenericRewrite("add(foo: decimal(p, s), bar: bigint): decimal(rp, rs)", "foo + bar::decimal(rp,rs)");
ConnectorExpression expression = new Call(createDecimalType(21, 2), new FunctionName("add"), List.of(new Variable("first", createDecimalType(10, 2)), new Variable("second", BIGINT)));
Match match = rewrite.getPattern().match(expression).collect(onlyElement());
Optional<String> rewritten = rewrite.rewrite(expression, match.captures(), new RewriteContext<>() {
@Override
public Map<String, ColumnHandle> getAssignments() {
throw new UnsupportedOperationException();
}
@Override
public ConnectorSession getSession() {
throw new UnsupportedOperationException();
}
@Override
public Optional<String> defaultRewrite(ConnectorExpression expression) {
if (expression instanceof Variable) {
return Optional.of("\"" + ((Variable) expression).getName().replace("\"", "\"\"") + "\"");
}
return Optional.empty();
}
});
assertThat(rewritten).hasValue("(\"first\") + (\"second\")::decimal(21,2)");
}
Aggregations