Search in sources :

Example 1 with HistoryEntry

use of org.apache.iceberg.HistoryEntry in project presto by prestodb.

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.currentAncestors(icebergTable));
    for (HistoryEntry historyEntry : icebergTable.history()) {
        long snapshotId = historyEntry.snapshotId();
        Snapshot snapshot = icebergTable.snapshot(snapshotId);
        table.addRow(packDateTimeWithZone(historyEntry.timestampMillis(), session.getSqlFunctionProperties().getTimeZoneKey()), 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) InMemoryRecordSet(com.facebook.presto.spi.InMemoryRecordSet)

Example 2 with HistoryEntry

use of org.apache.iceberg.HistoryEntry in project hive by apache.

the class TestHiveIcebergTimeTravel method testSelectAsOfVersion.

@Test
public void testSelectAsOfVersion() throws IOException, InterruptedException {
    Table table = prepareTableWithVersions(2);
    HistoryEntry first = table.history().get(0);
    List<Object[]> rows = shell.executeStatement("SELECT * FROM customers FOR SYSTEM_VERSION AS OF " + first.snapshotId());
    Assert.assertEquals(3, rows.size());
    HistoryEntry second = table.history().get(1);
    rows = shell.executeStatement("SELECT * FROM customers FOR SYSTEM_VERSION AS OF " + second.snapshotId());
    Assert.assertEquals(4, rows.size());
    AssertHelpers.assertThrows("should throw exception", IllegalArgumentException.class, "Cannot find snapshot with ID 1234", () -> {
        shell.executeStatement("SELECT * FROM customers FOR SYSTEM_VERSION AS OF 1234");
    });
}
Also used : Table(org.apache.iceberg.Table) HistoryEntry(org.apache.iceberg.HistoryEntry) Test(org.junit.Test)

Example 3 with HistoryEntry

use of org.apache.iceberg.HistoryEntry in project hive by apache.

the class TestHiveIcebergTimeTravel method timestampAfterSnapshot.

/**
 * Get the timestamp string which we can use in the queries. The timestamp will be after the given snapshot
 * and before the next one
 * @param table The table which we want to query
 * @param snapshotPosition The position of the last snapshot we want to see in the query results
 * @return The timestamp which we can use in the queries
 */
private String timestampAfterSnapshot(Table table, int snapshotPosition) {
    List<HistoryEntry> history = table.history();
    long snapshotTime = history.get(snapshotPosition).timestampMillis();
    long time = snapshotTime + 100;
    if (history.size() > snapshotPosition + 1) {
        time = snapshotTime + ((history.get(snapshotPosition + 1).timestampMillis() - snapshotTime) / 2);
    }
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS000000");
    return simpleDateFormat.format(new Date(time));
}
Also used : HistoryEntry(org.apache.iceberg.HistoryEntry) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with HistoryEntry

use of org.apache.iceberg.HistoryEntry in project drill by apache.

the class IcebergQueriesTest method testSelectHistoryMetadata.

@Test
public void testSelectHistoryMetadata() throws Exception {
    String query = "select * from dfs.tmp.`testAllTypes#history`";
    List<HistoryEntry> entries = table.history();
    testBuilder().sqlQuery(query).unOrdered().baselineColumns("made_current_at", "snapshot_id", "parent_id", "is_current_ancestor").baselineValues(LocalDateTime.ofInstant(Instant.ofEpochMilli(entries.get(0).timestampMillis()), ZoneId.of("UTC")), entries.get(0).snapshotId(), null, true).baselineValues(LocalDateTime.ofInstant(Instant.ofEpochMilli(entries.get(1).timestampMillis()), ZoneId.of("UTC")), entries.get(1).snapshotId(), entries.get(0).snapshotId(), true).go();
}
Also used : HistoryEntry(org.apache.iceberg.HistoryEntry) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ClusterTest(org.apache.drill.test.ClusterTest) Test(org.junit.Test)

Aggregations

HistoryEntry (org.apache.iceberg.HistoryEntry)4 Test (org.junit.Test)2 InMemoryRecordSet (com.facebook.presto.spi.InMemoryRecordSet)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 ClusterTest (org.apache.drill.test.ClusterTest)1 Snapshot (org.apache.iceberg.Snapshot)1 Table (org.apache.iceberg.Table)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1