Search in sources :

Example 1 with LongWriteFunction

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

the class OracleClient method trinoTimestampToOracleDateWriteFunction.

private static LongWriteFunction trinoTimestampToOracleDateWriteFunction() {
    return new LongWriteFunction() {

        @Override
        public String getBindExpression() {
            // Oracle's DATE stores year, month, day, hour, minute, seconds, but not second fraction
            return "TO_DATE(?, 'SYYYY-MM-DD HH24:MI:SS')";
        }

        @Override
        public void set(PreparedStatement statement, int index, long value) throws SQLException {
            long epochSecond = floorDiv(value, MICROSECONDS_PER_SECOND);
            int microsOfSecond = floorMod(value, MICROSECONDS_PER_SECOND);
            verify(microsOfSecond == 0, "Micros of second must be zero: '%s'", value);
            LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.UTC);
            statement.setString(index, TIMESTAMP_SECONDS_FORMATTER.format(localDateTime));
        }
    };
}
Also used : LocalDateTime(java.time.LocalDateTime) LongWriteFunction(io.trino.plugin.jdbc.LongWriteFunction) PreparedStatement(java.sql.PreparedStatement) OraclePreparedStatement(oracle.jdbc.OraclePreparedStatement)

Example 2 with LongWriteFunction

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

the class PostgreSqlClient method timeWriteFunction.

private static LongWriteFunction timeWriteFunction(int precision) {
    // PostgreSQL limit but also assumption within this method
    checkArgument(precision <= 6, "Unsupported precision: %s", precision);
    String bindExpression = format("CAST(? AS time(%s))", precision);
    return new LongWriteFunction() {

        @Override
        public String getBindExpression() {
            return bindExpression;
        }

        @Override
        public void set(PreparedStatement statement, int index, long picosOfDay) throws SQLException {
            picosOfDay = round(picosOfDay, 12 - precision);
            if (picosOfDay == PICOSECONDS_PER_DAY) {
                picosOfDay = 0;
            }
            LocalTime localTime = LocalTime.ofNanoOfDay(picosOfDay / PICOSECONDS_PER_NANOSECOND);
            // statement.setObject(.., localTime) would yield incorrect end result for 23:59:59.999000
            statement.setString(index, TIME_FORMATTER.format(localTime));
        }
    };
}
Also used : LocalTime(java.time.LocalTime) LongWriteFunction(io.trino.plugin.jdbc.LongWriteFunction) PreparedStatement(java.sql.PreparedStatement)

Example 3 with LongWriteFunction

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

the class OracleClient method trinoTimestampToOracleTimestampWriteFunction.

public static LongWriteFunction trinoTimestampToOracleTimestampWriteFunction() {
    return new LongWriteFunction() {

        @Override
        public String getBindExpression() {
            return "TO_TIMESTAMP(?, 'SYYYY-MM-DD HH24:MI:SS.FF')";
        }

        @Override
        public void set(PreparedStatement statement, int index, long utcMillis) throws SQLException {
            long epochSecond = floorDiv(utcMillis, MICROSECONDS_PER_SECOND);
            int nanoFraction = floorMod(utcMillis, MICROSECONDS_PER_SECOND) * NANOSECONDS_PER_MICROSECOND;
            LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(epochSecond, nanoFraction, ZoneOffset.UTC);
            statement.setString(index, TIMESTAMP_MILLIS_FORMATTER.format(localDateTime));
        }
    };
}
Also used : LocalDateTime(java.time.LocalDateTime) LongWriteFunction(io.trino.plugin.jdbc.LongWriteFunction) PreparedStatement(java.sql.PreparedStatement) OraclePreparedStatement(oracle.jdbc.OraclePreparedStatement)

Example 4 with LongWriteFunction

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

the class OracleClient method trinoDateToOracleDateWriteFunction.

public static LongWriteFunction trinoDateToOracleDateWriteFunction() {
    return new LongWriteFunction() {

        @Override
        public String getBindExpression() {
            return "TO_DATE(?, 'SYYYY-MM-DD')";
        }

        @Override
        public void set(PreparedStatement statement, int index, long value) throws SQLException {
            long utcMillis = DAYS.toMillis(value);
            LocalDateTime date = LocalDateTime.from(Instant.ofEpochMilli(utcMillis).atZone(ZoneOffset.UTC));
            statement.setString(index, DATE_FORMATTER.format(date));
        }
    };
}
Also used : LocalDateTime(java.time.LocalDateTime) LongWriteFunction(io.trino.plugin.jdbc.LongWriteFunction) PreparedStatement(java.sql.PreparedStatement) OraclePreparedStatement(oracle.jdbc.OraclePreparedStatement)

Example 5 with LongWriteFunction

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

the class SqlServerClient method sqlServerTimeWriteFunction.

private LongWriteFunction sqlServerTimeWriteFunction(int precision) {
    return new LongWriteFunction() {

        @Override
        public String getBindExpression() {
            // when write function is used for predicate pushdown.
            return format("CAST(? AS time(%s))", precision);
        }

        @Override
        public void set(PreparedStatement statement, int index, long picosOfDay) throws SQLException {
            picosOfDay = round(picosOfDay, 12 - precision);
            if (picosOfDay == PICOSECONDS_PER_DAY) {
                picosOfDay = 0;
            }
            LocalTime localTime = fromTrinoTime(picosOfDay);
            statement.setString(index, localTime.toString());
        }
    };
}
Also used : LocalTime(java.time.LocalTime) LongWriteFunction(io.trino.plugin.jdbc.LongWriteFunction) PreparedStatement(java.sql.PreparedStatement)

Aggregations

LongWriteFunction (io.trino.plugin.jdbc.LongWriteFunction)5 PreparedStatement (java.sql.PreparedStatement)5 LocalDateTime (java.time.LocalDateTime)3 OraclePreparedStatement (oracle.jdbc.OraclePreparedStatement)3 LocalTime (java.time.LocalTime)2