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));
}
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);
}
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)");
}
Aggregations