Search in sources :

Example 1 with Match

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();
}
Also used : OPTIMIZER_TIMEOUT(io.trino.spi.StandardErrorCode.OPTIMIZER_TIMEOUT) Match(io.trino.matching.Match)

Example 2 with Match

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();
}
Also used : Match(io.trino.matching.Match)

Example 3 with Match

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));
}
Also used : Call(io.trino.spi.expression.Call) FunctionName(io.trino.spi.expression.FunctionName) Variable(io.trino.spi.expression.Variable) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) Match(io.trino.matching.Match) Test(org.testng.annotations.Test)

Example 4 with Match

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);
}
Also used : Call(io.trino.spi.expression.Call) FunctionName(io.trino.spi.expression.FunctionName) Variable(io.trino.spi.expression.Variable) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) Match(io.trino.matching.Match) Test(org.testng.annotations.Test)

Example 5 with Match

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)");
}
Also used : Call(io.trino.spi.expression.Call) Variable(io.trino.spi.expression.Variable) Optional(java.util.Optional) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) Match(io.trino.matching.Match) FunctionName(io.trino.spi.expression.FunctionName) ConnectorSession(io.trino.spi.connector.ConnectorSession) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

Match (io.trino.matching.Match)7 ConnectorExpression (io.trino.spi.expression.ConnectorExpression)4 Test (org.testng.annotations.Test)4 Call (io.trino.spi.expression.Call)3 FunctionName (io.trino.spi.expression.FunctionName)3 Variable (io.trino.spi.expression.Variable)3 RewriteContext (io.trino.plugin.base.aggregation.AggregateFunctionRule.RewriteContext)1 OPTIMIZER_TIMEOUT (io.trino.spi.StandardErrorCode.OPTIMIZER_TIMEOUT)1 ColumnHandle (io.trino.spi.connector.ColumnHandle)1 ConnectorSession (io.trino.spi.connector.ConnectorSession)1 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)1 Type (io.trino.spi.type.Type)1 Map (java.util.Map)1 Optional (java.util.Optional)1