-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -862,6 +863,117 @@ public void testNoScalarColumns() | |
assertUpdate("DROP TABLE " + tableName); | ||
} | ||
|
||
@Test | ||
public void testShowStatsAsOf() | ||
{ | ||
Session writeSession = withStatsOnWrite(getSession(), false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which "snapshot expired" are you referring to here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
There was a problem hiding this comment.
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 aboveThere was a problem hiding this comment.
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
without preceding
if
on that value