Search in sources :

Example 11 with Variable

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));
}
Also used : Variable(io.trino.spi.expression.Variable) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 12 with Variable

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()));
}
Also used : Variable(io.trino.spi.expression.Variable) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 13 with Variable

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()));
}
Also used : Variable(io.trino.spi.expression.Variable) VarcharType(io.trino.spi.type.VarcharType) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression) CharType(io.trino.spi.type.CharType)

Example 14 with Variable

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()));
}
Also used : Variable(io.trino.spi.expression.Variable) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 15 with Variable

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

Variable (io.trino.spi.expression.Variable)51 JdbcExpression (io.trino.plugin.jdbc.JdbcExpression)21 ConnectorExpression (io.trino.spi.expression.ConnectorExpression)21 JdbcColumnHandle (io.trino.plugin.jdbc.JdbcColumnHandle)18 Test (org.testng.annotations.Test)12 ImmutableList (com.google.common.collect.ImmutableList)11 ColumnHandle (io.trino.spi.connector.ColumnHandle)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)10 Assignment (io.trino.spi.connector.Assignment)10 Map (java.util.Map)9 Optional (java.util.Optional)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 TupleDomain (io.trino.spi.predicate.TupleDomain)8 List (java.util.List)8 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)7 AggregateFunction (io.trino.spi.connector.AggregateFunction)7 ProjectionApplicationResult (io.trino.spi.connector.ProjectionApplicationResult)7 AggregateExpression (io.trino.plugin.pinot.query.AggregateExpression)6 Domain (io.trino.spi.predicate.Domain)6 Verify.verify (com.google.common.base.Verify.verify)5