Search in sources :

Example 6 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class TestDateTimeFunctions method testCurrentDateTimezone.

@Test
public void testCurrentDateTimezone() {
    TimeZoneKey kievTimeZoneKey = getTimeZoneKey("Europe/Kiev");
    // The zone has 'gap' on 1970-01-01
    TimeZoneKey bahiaBanderasTimeZoneKey = getTimeZoneKey("America/Bahia_Banderas");
    TimeZoneKey montrealTimeZoneKey = getTimeZoneKey("America/Montreal");
    long timeIncrement = TimeUnit.MINUTES.toMillis(53);
    // We expect UTC millis later on so we have to use UTC chronology
    for (long millis = ISOChronology.getInstanceUTC().getDateTimeMillis(2000, 6, 15, 0, 0, 0, 0); millis < ISOChronology.getInstanceUTC().getDateTimeMillis(2016, 6, 15, 0, 0, 0, 0); millis += timeIncrement) {
        Instant instant = Instant.ofEpochMilli(millis);
        assertCurrentDateAtInstant(kievTimeZoneKey, instant);
        assertCurrentDateAtInstant(bahiaBanderasTimeZoneKey, instant);
        assertCurrentDateAtInstant(montrealTimeZoneKey, instant);
        assertCurrentDateAtInstant(TIME_ZONE_KEY, instant);
    }
}
Also used : ReadableInstant(org.joda.time.ReadableInstant) Instant(java.time.Instant) TimeZoneKey(io.trino.spi.type.TimeZoneKey) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) Test(org.testng.annotations.Test)

Example 7 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class SnapshotsTable method buildPages.

private static List<Page> buildPages(ConnectorTableMetadata tableMetadata, ConnectorSession session, Table icebergTable) {
    PageListBuilder pagesBuilder = PageListBuilder.forTable(tableMetadata);
    TimeZoneKey timeZoneKey = session.getTimeZoneKey();
    icebergTable.snapshots().forEach(snapshot -> {
        pagesBuilder.beginRow();
        pagesBuilder.appendTimestampTzMillis(snapshot.timestampMillis(), timeZoneKey);
        pagesBuilder.appendBigint(snapshot.snapshotId());
        if (checkNonNull(snapshot.parentId(), pagesBuilder)) {
            pagesBuilder.appendBigint(snapshot.parentId());
        }
        if (checkNonNull(snapshot.operation(), pagesBuilder)) {
            pagesBuilder.appendVarchar(snapshot.operation());
        }
        if (checkNonNull(snapshot.manifestListLocation(), pagesBuilder)) {
            pagesBuilder.appendVarchar(snapshot.manifestListLocation());
        }
        if (checkNonNull(snapshot.summary(), pagesBuilder)) {
            pagesBuilder.appendVarcharVarcharMap(snapshot.summary());
        }
        pagesBuilder.endRow();
    });
    return pagesBuilder.build();
}
Also used : PageListBuilder(io.trino.plugin.iceberg.util.PageListBuilder) TimeZoneKey(io.trino.spi.type.TimeZoneKey)

Example 8 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class DateTimeFunctions method fromIso8601TimestampNanos.

@ScalarFunction("from_iso8601_timestamp_nanos")
@LiteralParameters("x")
@SqlType("timestamp(9) with time zone")
public static LongTimestampWithTimeZone fromIso8601TimestampNanos(ConnectorSession session, @SqlType("varchar(x)") Slice iso8601DateTime) {
    java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ISO_DATE_TIME;
    String datetimeString = iso8601DateTime.toStringUtf8();
    TemporalAccessor parsedDatetime;
    try {
        parsedDatetime = formatter.parseBest(datetimeString, ZonedDateTime::from, LocalDateTime::from);
    } catch (DateTimeParseException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, e);
    }
    ZonedDateTime zonedDatetime;
    if (parsedDatetime instanceof ZonedDateTime) {
        zonedDatetime = (ZonedDateTime) parsedDatetime;
    } else {
        zonedDatetime = ((LocalDateTime) parsedDatetime).atZone(session.getTimeZoneKey().getZoneId());
    }
    long picosOfSecond = zonedDatetime.getNano() * ((long) PICOSECONDS_PER_NANOSECOND);
    TimeZoneKey zone = TimeZoneKey.getTimeZoneKey(zonedDatetime.getZone().getId());
    return LongTimestampWithTimeZone.fromEpochSecondsAndFraction(zonedDatetime.toEpochSecond(), picosOfSecond, zone);
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) ZonedDateTime(java.time.ZonedDateTime) String.format(java.lang.String.format) TrinoException(io.trino.spi.TrinoException) TimeZoneKey(io.trino.spi.type.TimeZoneKey) ScalarFunction(io.trino.spi.function.ScalarFunction) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 9 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class HistoryTable method cursor.

@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) {
    InMemoryRecordSet.Builder table = InMemoryRecordSet.builder(COLUMNS);
    Set<Long> ancestorIds = ImmutableSet.copyOf(SnapshotUtil.currentAncestorIds(icebergTable));
    TimeZoneKey timeZoneKey = session.getTimeZoneKey();
    for (HistoryEntry historyEntry : icebergTable.history()) {
        long snapshotId = historyEntry.snapshotId();
        Snapshot snapshot = icebergTable.snapshot(snapshotId);
        table.addRow(packDateTimeWithZone(historyEntry.timestampMillis(), timeZoneKey), snapshotId, snapshot != null ? snapshot.parentId() : null, ancestorIds.contains(snapshotId));
    }
    return table.build().cursor();
}
Also used : Snapshot(org.apache.iceberg.Snapshot) HistoryEntry(org.apache.iceberg.HistoryEntry) TimeZoneKey(io.trino.spi.type.TimeZoneKey) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet)

Example 10 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class TestTrinoDriver method testSetTimeZoneId.

@Test
public void testSetTimeZoneId() throws Exception {
    TimeZoneKey defaultZoneKey = TimeZoneKey.getTimeZoneKey(TimeZone.getDefault().getID());
    DateTimeZone defaultZone = DateTimeZone.forTimeZone(TimeZone.getDefault());
    String sql = "SELECT current_timezone() zone, TIMESTAMP '2001-02-03 3:04:05' ts";
    try (Connection connection = createConnection()) {
        try (Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql)) {
            assertTrue(rs.next());
            assertEquals(rs.getString("zone"), defaultZoneKey.getId());
            assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, defaultZone).getMillis()));
        }
        connection.unwrap(TrinoConnection.class).setTimeZoneId("UTC");
        try (Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql)) {
            assertTrue(rs.next());
            assertEquals(rs.getString("zone"), "UTC");
            // setting the session timezone has no effect on the interpretation of timestamps in the JDBC driver
            assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, defaultZone).getMillis()));
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) TimeZoneKey(io.trino.spi.type.TimeZoneKey) Timestamp(java.sql.Timestamp) DateTimeZone(org.joda.time.DateTimeZone) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

TimeZoneKey (io.trino.spi.type.TimeZoneKey)16 TimeZoneKey.getTimeZoneKey (io.trino.spi.type.TimeZoneKey.getTimeZoneKey)11 LiteralParameters (io.trino.spi.function.LiteralParameters)9 SqlType (io.trino.spi.function.SqlType)9 TrinoException (io.trino.spi.TrinoException)6 TimeZoneNotSupportedException (io.trino.spi.type.TimeZoneNotSupportedException)4 LongTimeWithTimeZone (io.trino.spi.type.LongTimeWithTimeZone)3 DateTimeZone (org.joda.time.DateTimeZone)3 Type (io.trino.spi.type.Type)2 VarcharType (io.trino.spi.type.VarcharType)2 DateTimeZoneIndex.getDateTimeZone (io.trino.util.DateTimeZoneIndex.getDateTimeZone)2 ZonedDateTime (java.time.ZonedDateTime)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Slice (io.airlift.slice.Slice)1 PageListBuilder (io.trino.plugin.iceberg.util.PageListBuilder)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 InMemoryRecordSet (io.trino.spi.connector.InMemoryRecordSet)1 ScalarFunction (io.trino.spi.function.ScalarFunction)1