Skip to content

Commit

Permalink
Verify exactly one snapshot when reading last in tests
Browse files Browse the repository at this point in the history
- use `computeScalar` wherever possible
- use `FETCH FIRST 1 ROW WITH TIES` instead of just `LIMIT 1`, so that
  test fails if there is a tie (ambiguity).
  • Loading branch information
findepi committed Aug 3, 2022
1 parent 362f691 commit 6e921d1
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1049,8 +1049,7 @@ public void testRollbackSnapshot()

private long getLatestSnapshotId(String tableName)
{
return (long) computeActual(format("SELECT snapshot_id FROM \"%s$snapshots\" ORDER BY committed_at DESC LIMIT 1", tableName))
.getOnlyValue();
return (long) computeScalar(format("SELECT snapshot_id FROM \"%s$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES", tableName));
}

@Override
Expand Down Expand Up @@ -5278,7 +5277,7 @@ private List<Long> getSnapshotIds(String tableName)

private long getCurrentSnapshotId(String tableName)
{
return (long) computeScalar("SELECT snapshot_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC LIMIT 1");
return (long) computeScalar("SELECT snapshot_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES");
}

private Path getIcebergTableDataPath(String tableLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,12 @@ public void testTimeTravelWithRedirection()

private long getLatestSnapshotId(String schema)
{
return (long) computeActual(format("SELECT snapshot_id FROM iceberg.%s.\"nation_test$snapshots\" ORDER BY committed_at DESC LIMIT 1", schema))
.getOnlyValue();
return (long) computeScalar(format("SELECT snapshot_id FROM iceberg.%s.\"nation_test$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES", schema));
}

private long getCommittedAtInEpochMilliSeconds(long snapshotId, String schema)
{
return ((ZonedDateTime) computeActual(format("SELECT committed_at FROM iceberg.%s.\"nation_test$snapshots\" WHERE snapshot_id=%s LIMIT 1", schema, snapshotId)).getOnlyValue())
return ((ZonedDateTime) computeScalar(format("SELECT committed_at FROM iceberg.%s.\"nation_test$snapshots\" WHERE snapshot_id=%s", schema, snapshotId)))
.toInstant().toEpochMilli();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ private Multiset<FileOperation> getOperations()

private long getLatestSnapshotId(String tableName)
{
return (long) computeActual(format("SELECT snapshot_id FROM \"%s$snapshots\" ORDER BY committed_at DESC LIMIT 1", tableName))
.getOnlyValue();
return (long) computeScalar(format("SELECT snapshot_id FROM \"%s$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES", tableName));
}

static class FileOperation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ public void testSystemTables()

private long getLatestSnapshotId(String tableName)
{
return (long) computeActual(format("SELECT snapshot_id FROM \"%s$snapshots\" ORDER BY committed_at DESC LIMIT 1", tableName))
.getOnlyValue();
return (long) computeScalar(format("SELECT snapshot_id FROM \"%s$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES", tableName));
}

private long getCommittedAtInEpochMilliSeconds(String tableName, long snapshotId)
{
return ((ZonedDateTime) computeActual(format("SELECT committed_at FROM \"%s$snapshots\" WHERE snapshot_id=%s LIMIT 1", tableName, snapshotId)).getOnlyValue())
return ((ZonedDateTime) computeScalar(format("SELECT committed_at FROM \"%s$snapshots\" WHERE snapshot_id=%s", tableName, snapshotId)))
.toInstant().toEpochMilli();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,9 @@ public void testDeletingEntireFileWithMultipleSplits()

assertThat(this.loadTable(tableName).newScan().planFiles()).hasSize(1);
// Ensure only one snapshot is committed to the table
Long initialSnapshotId = (Long) computeActual("SELECT snapshot_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC LIMIT 1").getOnlyValue();
long initialSnapshotId = (long) computeScalar("SELECT snapshot_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES");
assertUpdate("DELETE FROM " + tableName + " WHERE regionkey < 10", 25);
Long parentSnapshotId = (Long) computeActual("SELECT parent_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC LIMIT 1").getOnlyValue();
long parentSnapshotId = (long) computeScalar("SELECT parent_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES");
assertEquals(initialSnapshotId, parentSnapshotId);
assertThat(query("SELECT * FROM " + tableName)).returnsEmptyResult();
assertThat(this.loadTable(tableName).newScan().planFiles()).hasSize(0);
Expand All @@ -439,9 +439,9 @@ public void testMultipleDeletes()
assertUpdate("CREATE TABLE " + tableName + " AS SELECT * FROM tpch.tiny.nation", 25);
assertThat(this.loadTable(tableName).newScan().planFiles()).hasSize(1);
// Ensure only one snapshot is committed to the table
Long initialSnapshotId = (Long) computeActual("SELECT snapshot_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC LIMIT 1").getOnlyValue();
long initialSnapshotId = (long) computeScalar("SELECT snapshot_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES");
assertUpdate("DELETE FROM " + tableName + " WHERE regionkey % 2 = 1", "SELECT count(*) FROM nation WHERE regionkey % 2 = 1");
Long parentSnapshotId = (Long) computeActual("SELECT parent_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC LIMIT 1").getOnlyValue();
long parentSnapshotId = (long) computeScalar("SELECT parent_id FROM \"" + tableName + "$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES");
assertEquals(initialSnapshotId, parentSnapshotId);

assertUpdate("DELETE FROM " + tableName + " WHERE regionkey % 2 = 0", "SELECT count(*) FROM nation WHERE regionkey % 2 = 0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ private List<String> getActiveFiles(String catalog, String schema, String tableN

private long getCurrentSnapshotId(String catalog, String schema, String tableName)
{
return (long) getOnlyElement(getOnlyElement(onTrino().executeQuery(format("SELECT snapshot_id FROM %s.%s.\"%s$snapshots\" ORDER BY committed_at DESC LIMIT 1", catalog, schema, tableName)).rows()));
return (long) getOnlyElement(getOnlyElement(onTrino().executeQuery(
format("SELECT snapshot_id FROM %s.%s.\"%s$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES", catalog, schema, tableName)).rows()));
}

private static String sparkTableName(String tableName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testRollbackToSnapshot()
private long getSecondOldestTableSnapshot(String tableName)
{
return (Long) onTrino().executeQuery(
format("SELECT snapshot_id FROM iceberg.default.\"%s$snapshots\" WHERE parent_id IS NOT NULL ORDER BY committed_at LIMIT 1", tableName))
format("SELECT snapshot_id FROM iceberg.default.\"%s$snapshots\" WHERE parent_id IS NOT NULL ORDER BY committed_at FETCH FIRST 1 ROW WITH TIES", tableName))
.row(0)
.get(0);
}
Expand Down

0 comments on commit 6e921d1

Please sign in to comment.