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