Search in sources :

Example 1 with JdbcExpression

use of io.trino.plugin.jdbc.JdbcExpression in project trino by trinodb.

the class BaseImplementAvgBigint method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(this.argument);
    verify(aggregateFunction.getOutputType() == DOUBLE);
    return Optional.of(new JdbcExpression(format(getRewriteFormatExpression(), context.rewriteExpression(argument).orElseThrow()), new JdbcTypeHandle(Types.DOUBLE, Optional.of("double"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())));
}
Also used : JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) Variable(io.trino.spi.expression.Variable) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 2 with JdbcExpression

use of io.trino.plugin.jdbc.JdbcExpression in project trino by trinodb.

the class ImplementAvgDecimal 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());
    DecimalType type = (DecimalType) columnHandle.getColumnType();
    verify(aggregateFunction.getOutputType().equals(type));
    return Optional.of(new JdbcExpression(format("CAST(avg(%s) AS decimal(%s, %s))", context.rewriteExpression(argument).orElseThrow(), type.getPrecision(), type.getScale()), columnHandle.getJdbcTypeHandle()));
}
Also used : Variable(io.trino.spi.expression.Variable) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) DecimalType(io.trino.spi.type.DecimalType) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 3 with JdbcExpression

use of io.trino.plugin.jdbc.JdbcExpression 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 4 with JdbcExpression

use of io.trino.plugin.jdbc.JdbcExpression 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 5 with JdbcExpression

use of io.trino.plugin.jdbc.JdbcExpression 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)

Aggregations

JdbcExpression (io.trino.plugin.jdbc.JdbcExpression)21 Variable (io.trino.spi.expression.Variable)21 JdbcColumnHandle (io.trino.plugin.jdbc.JdbcColumnHandle)18 JdbcTypeHandle (io.trino.plugin.jdbc.JdbcTypeHandle)2 CharType (io.trino.spi.type.CharType)2 DecimalType (io.trino.spi.type.DecimalType)2 VarcharType (io.trino.spi.type.VarcharType)2 Optional (java.util.Optional)1