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

[core] Add statistics to snapshot system table #2836

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions docs/content/how-to/system-tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ You can query the snapshot history information of the table through snapshots ta
SELECT * FROM MyTable$snapshots;

/*
+--------------+------------+-----------------+-------------------+--------------+-------------------------+--------------------------------+------------------------------- +--------------------------------+---------------------+---------------------+-------------------------+-------------------+--------------------+----------------+
| snapshot_id | schema_id | commit_user | commit_identifier | commit_kind | commit_time | base_manifest_list | delta_manifest_list | changelog_manifest_list | total_record_count | delta_record_count | changelog_record_count | added_file_count | delete_file_count | watermark |
+--------------+------------+-----------------+-------------------+--------------+-------------------------+--------------------------------+------------------------------- +--------------------------------+---------------------+---------------------+-------------------------+-------------------+--------------------+----------------+
| 2 | 0 | 7ca4cd28-98e... | 2 | APPEND | 2022-10-26 11:44:15.600 | manifest-list-31323d5f-76e6... | manifest-list-31323d5f-76e6... | manifest-list-31323d5f-76e6... | 2 | 2 | 0 | 2 | 0 | 1666755855600 |
| 1 | 0 | 870062aa-3e9... | 1 | APPEND | 2022-10-26 11:44:15.148 | manifest-list-31593d5f-76e6... | manifest-list-31593d5f-76e6... | manifest-list-31593d5f-76e6... | 1 | 1 | 0 | 1 | 0 | 1666755855148 |
+--------------+------------+-----------------+-------------------+--------------+-------------------------+--------------------------------+------------------------------- +--------------------------------+---------------------+---------------------+-------------------------+-------------------+--------------------+----------------+
+--------------+------------+-----------------+-------------------+--------------+-------------------------+--------------------------------+------------------------------- +--------------------------------+---------------------+---------------------+-------------------------+-------------------+--------------------+----------------+--------------------+
| snapshot_id | schema_id | commit_user | commit_identifier | commit_kind | commit_time | base_manifest_list | delta_manifest_list | changelog_manifest_list | total_record_count | delta_record_count | changelog_record_count | added_file_count | delete_file_count | watermark | statistics |
+--------------+------------+-----------------+-------------------+--------------+-------------------------+--------------------------------+------------------------------- +--------------------------------+---------------------+---------------------+-------------------------+-------------------+--------------------+----------------+--------------------+
| 2 | 0 | 7ca4cd28-98e... | 2 | APPEND | 2022-10-26 11:44:15.600 | manifest-list-31323d5f-76e6... | manifest-list-31323d5f-76e6... | manifest-list-31323d5f-76e6... | 2 | 2 | 0 | 2 | 0 | 1666755855600 |{\n "snapshotId"...|
| 1 | 0 | 870062aa-3e9... | 1 | APPEND | 2022-10-26 11:44:15.148 | manifest-list-31593d5f-76e6... | manifest-list-31593d5f-76e6... | manifest-list-31593d5f-76e6... | 1 | 1 | 0 | 1 | 0 | 1666755855148 || NULL|
+--------------+------------+-----------------+-------------------+--------------+-------------------------+--------------------------------+------------------------------- +--------------------------------+---------------------+---------------------+-------------------------+-------------------+--------------------+----------------+--------------------+
2 rows in set
*/
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.apache.paimon.operation.FileStoreScan;
import org.apache.paimon.predicate.Predicate;
import org.apache.paimon.reader.RecordReader;
import org.apache.paimon.stats.Statistics;
import org.apache.paimon.stats.StatsFileHandler;
import org.apache.paimon.table.FileStoreTable;
import org.apache.paimon.table.ReadonlyTable;
import org.apache.paimon.table.Table;
Expand Down Expand Up @@ -98,7 +100,9 @@ public class SnapshotsTable implements ReadonlyTable {
new DataField(11, "changelog_record_count", new BigIntType(true)),
new DataField(12, "added_file_count", new IntType(true)),
new DataField(13, "delete_file_count", new IntType(true)),
new DataField(14, "watermark", new BigIntType(true))));
new DataField(14, "watermark", new BigIntType(true)),
new DataField(
15, "statistics", SerializationUtils.newStringType(true))));

private final FileIO fileIO;
private final Path location;
Expand Down Expand Up @@ -230,8 +234,10 @@ public RecordReader<InternalRow> createReader(Split split) throws IOException {
}
Path location = ((SnapshotsSplit) split).location;
Iterator<Snapshot> snapshots = new SnapshotManager(fileIO, location).snapshots();
StatsFileHandler statsFileHandler = dataTable.store().newStatsFileHandler();
Iterator<InternalRow> rows =
Iterators.transform(snapshots, snapshot -> toRow(snapshot, dataTable));
Iterators.transform(
snapshots, snapshot -> toRow(snapshot, dataTable, statsFileHandler));
if (projection != null) {
rows =
Iterators.transform(
Expand All @@ -240,7 +246,8 @@ public RecordReader<InternalRow> createReader(Split split) throws IOException {
return new IteratorRecordReader<>(rows);
}

private InternalRow toRow(Snapshot snapshot, FileStoreTable dataTable) {
private InternalRow toRow(
Snapshot snapshot, FileStoreTable dataTable, StatsFileHandler statsFileHandler) {
FileStoreScan.Plan plan = dataTable.store().newScan().withSnapshot(snapshot).plan();
return GenericRow.of(
snapshot.id(),
Expand All @@ -260,7 +267,12 @@ private InternalRow toRow(Snapshot snapshot, FileStoreTable dataTable) {
snapshot.changelogRecordCount(),
plan.files(FileKind.ADD).size(),
plan.files(FileKind.DELETE).size(),
snapshot.watermark());
snapshot.watermark(),
statsFileHandler
.readStats(snapshot)
.map(Statistics::toString)
Copy link
Contributor

@JingsongLi JingsongLi Feb 2, 2024

Choose a reason for hiding this comment

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

Maybe it is not a good idea to make string for a whole statistics... Too large.

Maybe we can have some other system table to show full statistics?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1.

.map(BinaryString::fromString)
.orElse(null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ private List<InternalRow> getExceptedResult(long[] snapshotIds) {
snapshot.changelogRecordCount(),
plan.files(FileKind.ADD).size(),
plan.files(FileKind.DELETE).size(),
snapshot.watermark()));
snapshot.watermark(),
null));
}

return expectedRow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ abstract class AnalyzeTableTestBase extends PaimonSparkTestBase {
Assertions.assertEquals(2L, stats.mergedRecordCount().getAsLong)
Assertions.assertTrue(stats.mergedRecordSize().isPresent)
Assertions.assertTrue(stats.colStats().isEmpty)

checkAnswer(
spark.sql(
"SELECT commit_kind, substring(statistics, 1, 13) FROM `T$snapshots` WHERE snapshot_id = '3'"),
Row("ANALYZE", "{\n \"snapshot") :: Nil)
}

test("Paimon analyze: analyze no scan") {
Expand Down