Search in sources :

Example 1 with FunctionName

use of io.trino.spi.expression.FunctionName 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 2 with FunctionName

use of io.trino.spi.expression.FunctionName 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 3 with FunctionName

use of io.trino.spi.expression.FunctionName 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)3 Call (io.trino.spi.expression.Call)3 ConnectorExpression (io.trino.spi.expression.ConnectorExpression)3 FunctionName (io.trino.spi.expression.FunctionName)3 Variable (io.trino.spi.expression.Variable)3 Test (org.testng.annotations.Test)3 ConnectorSession (io.trino.spi.connector.ConnectorSession)1 Map (java.util.Map)1 Optional (java.util.Optional)1