use of io.trino.spi.expression.Variable in project trino by trinodb.
the class ImplementCount method rewrite.
@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
Variable argument = captures.get(ARGUMENT);
verify(aggregateFunction.getOutputType() == BIGINT);
return Optional.of(new JdbcExpression(format("count(%s)", context.rewriteExpression(argument).orElseThrow()), bigintTypeHandle));
}
use of io.trino.spi.expression.Variable in project trino by trinodb.
the class ImplementCovariancePop method rewrite.
@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
List<Variable> arguments = captures.get(ARGUMENTS);
verify(arguments.size() == 2);
Variable argument1 = arguments.get(0);
Variable argument2 = arguments.get(1);
JdbcColumnHandle columnHandle1 = (JdbcColumnHandle) context.getAssignment(argument1.getName());
verify(aggregateFunction.getOutputType().equals(columnHandle1.getColumnType()));
return Optional.of(new JdbcExpression(format("covar_pop(%s, %s)", context.rewriteExpression(argument1).orElseThrow(), context.rewriteExpression(argument2).orElseThrow()), columnHandle1.getJdbcTypeHandle()));
}
use of io.trino.spi.expression.Variable in project trino by trinodb.
the class ImplementMinMax method rewrite.
@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
Variable argument = captures.get(ARGUMENT);
JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
verify(columnHandle.getColumnType().equals(aggregateFunction.getOutputType()));
// Remote database is case insensitive or sorts values differently from Trino
if (!isRemoteCollationSensitive && (columnHandle.getColumnType() instanceof CharType || columnHandle.getColumnType() instanceof VarcharType)) {
return Optional.empty();
}
return Optional.of(new JdbcExpression(format("%s(%s)", aggregateFunction.getFunctionName(), context.rewriteExpression(argument).orElseThrow()), columnHandle.getJdbcTypeHandle()));
}
use of io.trino.spi.expression.Variable in project trino by trinodb.
the class ImplementStddevSamp method rewrite.
@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
Variable argument = captures.get(ARGUMENT);
JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
verify(aggregateFunction.getOutputType() == columnHandle.getColumnType());
return Optional.of(new JdbcExpression(format("stddev_samp(%s)", context.rewriteExpression(argument).orElseThrow()), columnHandle.getJdbcTypeHandle()));
}
use of io.trino.spi.expression.Variable 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