1919
2020#include " iceberg/inspect/history_table.h"
2121
22+ #include < array>
2223#include < memory>
24+ #include < unordered_map>
25+ #include < unordered_set>
2326#include < utility>
24- #include < vector>
2527
28+ #include " iceberg/arrow_row_builder_internal.h"
29+ #include " iceberg/constants.h"
30+ #include " iceberg/inspect/metadata_scan_task.h"
31+ #include " iceberg/inspect/metadata_scan_util_internal.h"
2632#include " iceberg/schema.h"
2733#include " iceberg/schema_field.h"
34+ #include " iceberg/snapshot.h"
2835#include " iceberg/table.h"
36+ #include " iceberg/table_metadata.h"
2937#include " iceberg/type.h"
3038#include " iceberg/util/macros.h"
3139
3240namespace iceberg {
41+ namespace {
3342
34- HistoryTable::HistoryTable (std::shared_ptr<Table> table)
43+ struct HistoryRow {
44+ const SnapshotLogEntry& entry;
45+ const Snapshot* snapshot;
46+ bool is_current_ancestor;
47+ };
48+
49+ constexpr std::array<internal::MetadataTableColumn<HistoryRow>, 4 > kHistoryColumns = {
50+ {{.field_id = 1 ,
51+ .append =
52+ [](ArrowArray* column, const HistoryRow& row) {
53+ return AppendInt (column,
54+ UnixMsFromTimePointMs (row.entry .timestamp_ms ) * 1000 );
55+ }},
56+ {.field_id = 2 ,
57+ .append =
58+ [](ArrowArray* column, const HistoryRow& row) {
59+ return AppendInt (column, row.entry .snapshot_id );
60+ }},
61+ {.field_id = 3 ,
62+ .append =
63+ [](ArrowArray* column, const HistoryRow& row) {
64+ return row.snapshot != nullptr && row.snapshot ->parent_snapshot_id
65+ ? AppendInt (column, *row.snapshot ->parent_snapshot_id )
66+ : AppendNull (column);
67+ }},
68+ {.field_id = 4 , .append = [](ArrowArray* column, const HistoryRow& row) {
69+ return AppendBoolean (column, row.is_current_ancestor );
70+ }}}};
71+
72+ } // namespace
73+
74+ // HistoryTable implementation
75+ HistoryTable::HistoryTable (std::shared_ptr<const Table> table)
3576 : MetadataTable(std::move(table)) {}
3677
3778HistoryTable::~HistoryTable () = default ;
3879
39- const std::shared_ptr<Schema>& HistoryTable::schema () const {
80+ const std::shared_ptr<Schema>& HistoryTable::schema () const { return CanonicalSchema (); }
81+
82+ const std::shared_ptr<Schema>& HistoryTable::CanonicalSchema () {
4083 static const auto schema = std::make_shared<Schema>(std::vector<SchemaField>{
4184 SchemaField::MakeRequired (1 , " made_current_at" , timestamp_tz ()),
4285 SchemaField::MakeRequired (2 , " snapshot_id" , int64 ()),
@@ -45,14 +88,84 @@ const std::shared_ptr<Schema>& HistoryTable::schema() const {
4588 return schema;
4689}
4790
48- Result<std::unique_ptr<HistoryTable>> HistoryTable::Make (std::shared_ptr<Table> table) {
91+ Result<std::unique_ptr<HistoryTable>> HistoryTable::Make (
92+ std::shared_ptr<const Table> table) {
4993 ICEBERG_PRECHECK (table != nullptr , " Table cannot be null" );
5094 return std::unique_ptr<HistoryTable>(new HistoryTable (std::move (table)));
5195}
5296
53- Result<ArrowArrayStream> HistoryTable::ScanProjection (
54- const Schema& /* projected_schema*/ ) const {
55- return NotSupported (" Scan is not supported for the history table" );
97+ Result<std::unique_ptr<MetadataScan>> HistoryTable::MakeScan (
98+ internal::MetadataScanContext context) const {
99+ return HistoryScan::Make (std::move (context));
100+ }
101+
102+ Result<ArrowArrayStream> HistoryTable::ReadRows (
103+ std::shared_ptr<const TableMetadata> metadata,
104+ const std::shared_ptr<Schema>& projected_schema, int64_t batch_size) {
105+ std::unordered_map<int64_t , const Snapshot*> snapshots_by_id;
106+ for (const auto & snapshot : metadata->snapshots ) {
107+ if (snapshot) {
108+ snapshots_by_id.emplace (snapshot->snapshot_id , snapshot.get ());
109+ }
110+ }
111+
112+ // Walk the current snapshot's ancestry; the insert check stops on cycles.
113+ std::unordered_set<int64_t > ancestors;
114+ for (auto current = metadata->current_snapshot_id ; current != kInvalidSnapshotId ;) {
115+ auto it = snapshots_by_id.find (current);
116+ // Stop at a missing snapshot without adding its ID to the ancestors.
117+ if (it == snapshots_by_id.end () || !ancestors.insert (current).second ) {
118+ break ;
119+ }
120+ if (!it->second ->parent_snapshot_id ) {
121+ break ;
122+ }
123+ current = *it->second ->parent_snapshot_id ;
124+ }
125+
126+ const size_t num_rows = metadata->snapshot_log .size ();
127+ return internal::MakeMetadataBatchStream<HistoryRow>(
128+ CanonicalSchema (), projected_schema, kHistoryColumns , num_rows, batch_size,
129+ [metadata = std::move (metadata), snapshots_by_id = std::move (snapshots_by_id),
130+ ancestors = std::move (ancestors)](size_t index) -> HistoryRow {
131+ const auto & entry = metadata->snapshot_log [index];
132+ auto it = snapshots_by_id.find (entry.snapshot_id );
133+ return {.entry = entry,
134+ .snapshot = it != snapshots_by_id.end () ? it->second : nullptr ,
135+ .is_current_ancestor = ancestors.contains (entry.snapshot_id )};
136+ });
137+ }
138+
139+ // HistoryScan implementation
140+ HistoryScan::HistoryScan (internal::MetadataScanContext context,
141+ std::shared_ptr<MetadataScanTask> task)
142+ : MetadataScan(std::move(context)), task_(std::move(task)) {}
143+
144+ HistoryScan::~HistoryScan () = default ;
145+
146+ Result<std::unique_ptr<HistoryScan>> HistoryScan::Make (
147+ internal::MetadataScanContext context) {
148+ ICEBERG_PRECHECK (context.kind == MetadataTableKind::kHistory ,
149+ " Unexpected metadata table kind for history scan" );
150+ if (context.snapshot_id .has_value () || !context.ref_name .empty () ||
151+ context.as_of_time_in_millis .has_value ()) {
152+ return NotSupported (" Metadata table does not support time travel" );
153+ }
154+ ICEBERG_PRECHECK (context.table_metadata != nullptr , " Table metadata cannot be null" );
155+ ICEBERG_PRECHECK (context.schema != nullptr , " Metadata table schema cannot be null" );
156+ ICEBERG_PRECHECK (context.projected_schema != nullptr ,
157+ " Projected schema cannot be null" );
158+ ICEBERG_RETURN_UNEXPECTED (internal::SelectAppendFns<HistoryRow>(
159+ *HistoryTable::CanonicalSchema (), *context.projected_schema , kHistoryColumns ));
160+ ICEBERG_ASSIGN_OR_RAISE (auto task,
161+ StaticMetadataTask::Make (context.kind , context.table_metadata ,
162+ context.projected_schema ));
163+ return std::unique_ptr<HistoryScan>(
164+ new HistoryScan (std::move (context), std::move (task)));
165+ }
166+
167+ Result<MetadataScanTaskStreamPtr> HistoryScan::PlanTasksStream () const {
168+ return MetadataScanTaskStream::FromVector ({task_});
56169}
57170
58171} // namespace iceberg
0 commit comments