Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Iceberg statistics loading after snapshot expiration #17356

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.iceberg.BlobMetadata;
import org.apache.iceberg.FileScanTask;
import org.apache.iceberg.Schema;
import org.apache.iceberg.Snapshot;
import org.apache.iceberg.StatisticsFile;
import org.apache.iceberg.Table;
import org.apache.iceberg.TableScan;
Expand All @@ -54,6 +55,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.google.common.base.Verify.verifyNotNull;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.Iterables.getOnlyElement;
import static io.trino.plugin.iceberg.ExpressionConverter.toIcebergExpression;
Expand Down Expand Up @@ -325,8 +327,16 @@ protected Long computeNext(Long previous)
{
requireNonNull(previous, "previous is null");
@Nullable
Long parentId = icebergTable.snapshot(previous).parentId();
return parentId;
Snapshot snapshot = icebergTable.snapshot(previous);
if (snapshot == null) {
// Snapshot referenced by `previous` is expired from table history
return null;
}
if (snapshot.parentId() == null) {
// Snapshot referenced by `previous` had no parent.
return null;
}
return verifyNotNull(snapshot.parentId(), "snapshot.parentId()");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verifyNotNull seem redundant here - given that we check above

if (snapshot.parentId() == null)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, obviously.

it's here for readability & future proofing that we do not return end-of-data on this line accidentally.

otherwise i could just have

return snapshot.parentId();

without preceding if on that value

}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.iceberg.IcebergSessionProperties.COLLECT_EXTENDED_STATISTICS_ON_WRITE;
import static io.trino.plugin.iceberg.IcebergSessionProperties.EXPIRE_SNAPSHOTS_MIN_RETENTION;
import static io.trino.testing.DataProviders.cartesianProduct;
import static io.trino.testing.DataProviders.trueFalse;
import static io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.EXECUTE_TABLE_PROCEDURE;
Expand Down Expand Up @@ -862,6 +863,117 @@ public void testNoScalarColumns()
assertUpdate("DROP TABLE " + tableName);
}

@Test
public void testShowStatsAsOf()
{
Session writeSession = withStatsOnWrite(getSession(), false);
Copy link
Contributor

@findinpath findinpath May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extract "show_stats_as_of" to a variable.

assertUpdate(writeSession, "CREATE TABLE show_stats_as_of(key integer)");

assertUpdate(writeSession, "INSERT INTO show_stats_as_of VALUES 3", 1);
long beforeAnalyzedSnapshot = getCurrentSnapshotId("show_stats_as_of");

assertUpdate(writeSession, "INSERT INTO show_stats_as_of VALUES 4", 1);
assertUpdate("ANALYZE show_stats_as_of");
long analyzedSnapshot = getCurrentSnapshotId("show_stats_as_of");

assertUpdate(writeSession, "INSERT INTO show_stats_as_of VALUES 5", 1);
long laterSnapshot = getCurrentSnapshotId("show_stats_as_of");

assertQuery(
"SHOW STATS FOR (SELECT * FROM show_stats_as_of FOR VERSION AS OF " + beforeAnalyzedSnapshot + ")",
"""
VALUES
('key', null, null, 0, null, '3', '3'), -- NDV not present, as ANALYZE was run on a later snapshot
(null, null, null, null, 1, null, null)""");

assertQuery(
"SHOW STATS FOR (SELECT * FROM show_stats_as_of FOR VERSION AS OF " + analyzedSnapshot + ")",
"""
VALUES
('key', null, 2, 0, null, '3', '4'), -- NDV present, this is the snapshot ANALYZE was run for
(null, null, null, null, 2, null, null)""");

assertQuery(
"SHOW STATS FOR (SELECT * FROM show_stats_as_of FOR VERSION AS OF " + laterSnapshot + ")",
"""
VALUES
('key', null, 2, 0, null, '3', '5'), -- NDV present, stats "inherited" from previous snapshot
(null, null, null, null, 3, null, null)""");

assertUpdate("DROP TABLE show_stats_as_of");
}

@Test
public void testShowStatsAfterExpiration()
{
String catalog = getSession().getCatalog().orElseThrow();
Session writeSession = withStatsOnWrite(getSession(), false);

assertUpdate(writeSession, "CREATE TABLE show_stats_after_expiration(key integer)");
// create several snapshots
assertUpdate(writeSession, "INSERT INTO show_stats_after_expiration VALUES 1", 1);
assertUpdate(writeSession, "INSERT INTO show_stats_after_expiration VALUES 2", 1);
assertUpdate(writeSession, "INSERT INTO show_stats_after_expiration VALUES 3", 1);

long beforeAnalyzedSnapshot = getCurrentSnapshotId("show_stats_after_expiration");

assertUpdate(
Session.builder(getSession())
.setCatalogSessionProperty(catalog, EXPIRE_SNAPSHOTS_MIN_RETENTION, "0s")
.build(),
"ALTER TABLE show_stats_after_expiration EXECUTE expire_snapshots(retention_threshold => '0d')");
assertThat(query("SELECT count(*) FROM \"show_stats_after_expiration$snapshots\""))
.matches("VALUES BIGINT '1'");

assertUpdate(writeSession, "INSERT INTO show_stats_after_expiration VALUES 4", 1);
assertUpdate("ANALYZE show_stats_after_expiration");
long analyzedSnapshot = getCurrentSnapshotId("show_stats_after_expiration");

assertUpdate(writeSession, "INSERT INTO show_stats_after_expiration VALUES 5", 1);
long laterSnapshot = getCurrentSnapshotId("show_stats_after_expiration");

assertQuery(
"SHOW STATS FOR (SELECT * FROM show_stats_after_expiration FOR VERSION AS OF " + beforeAnalyzedSnapshot + ")",
"""
VALUES
('key', null, null, 0, null, '1', '3'), -- NDV not present, as ANALYZE was run on a later snapshot
(null, null, null, null, 3, null, null)""");

assertQuery(
"SHOW STATS FOR (SELECT * FROM show_stats_after_expiration FOR VERSION AS OF " + analyzedSnapshot + ")",
"""
VALUES
('key', null, 4, 0, null, '1', '4'), -- NDV present, this is the snapshot ANALYZE was run for
(null, null, null, null, 4, null, null)""");

assertQuery(
"SHOW STATS FOR (SELECT * FROM show_stats_after_expiration FOR VERSION AS OF " + laterSnapshot + ")",
"""
VALUES
('key', null, 4, 0, null, '1', '5'), -- NDV present, stats "inherited" from previous snapshot
(null, null, null, null, 5, null, null)""");

// Same as laterSnapshot but implicitly
assertQuery(
"SHOW STATS FOR show_stats_after_expiration",
"""
VALUES
('key', null, 4, 0, null, '1', '5'), -- NDV present, stats "inherited" from previous snapshot
(null, null, null, null, 5, null, null)""");

// Re-analyzing after snapshot expired
Copy link
Contributor

@findinpath findinpath May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which "snapshot expired" are you referring to here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some snapshot expired

assertUpdate("ANALYZE show_stats_after_expiration");

assertQuery(
"SHOW STATS FOR show_stats_after_expiration",
"""
VALUES
('key', null, 5, 0, null, '1', '5'), -- NDV present, stats "inherited" from previous snapshot
(null, null, null, null, 5, null, null)""");

assertUpdate("DROP TABLE show_stats_after_expiration");
}

private long getCurrentSnapshotId(String tableName)
{
return (long) computeActual(format("SELECT snapshot_id FROM \"%s$snapshots\" ORDER BY committed_at DESC FETCH FIRST 1 ROW WITH TIES", tableName))
Expand Down