|
23 | 23 | #include <cstddef> |
24 | 24 | #include <memory> |
25 | 25 | #include <optional> |
| 26 | +#include <tuple> |
26 | 27 | #include <utility> |
27 | 28 | #include <vector> |
28 | 29 |
|
|
36 | 37 | #include "iceberg/schema_internal.h" |
37 | 38 | #include "iceberg/snapshot.h" |
38 | 39 | #include "iceberg/table.h" |
| 40 | +#include "iceberg/table_metadata.h" |
39 | 41 | #include "iceberg/type.h" |
40 | 42 | #include "iceberg/util/macros.h" |
41 | 43 |
|
@@ -78,25 +80,28 @@ Status AppendSnapshot(ArrowRowBuilder& builder, const Snapshot& snapshot) { |
78 | 80 | class SnapshotsTableStream { |
79 | 81 | public: |
80 | 82 | static Result<std::unique_ptr<SnapshotsTableStream>> Make( |
81 | | - std::shared_ptr<Table> table, const iceberg::Schema& schema) { |
| 83 | + std::shared_ptr<const TableMetadata> metadata, const iceberg::Schema& schema) { |
82 | 84 | ArrowSchema arrow_schema{}; |
83 | 85 | ICEBERG_RETURN_UNEXPECTED(ToArrowSchema(schema, &arrow_schema)); |
84 | 86 | return std::unique_ptr<SnapshotsTableStream>( |
85 | | - new SnapshotsTableStream(std::move(table), std::move(arrow_schema))); |
| 87 | + new SnapshotsTableStream(std::move(metadata), std::move(arrow_schema))); |
86 | 88 | } |
87 | 89 |
|
88 | 90 | ~SnapshotsTableStream() { std::ignore = Close(); } |
89 | 91 |
|
90 | 92 | Status Close() { |
91 | | - table_.reset(); |
| 93 | + metadata_.reset(); |
92 | 94 | if (arrow_schema_.release != nullptr) { |
93 | 95 | ArrowSchemaRelease(&arrow_schema_); |
94 | 96 | } |
95 | 97 | return {}; |
96 | 98 | } |
97 | 99 |
|
98 | 100 | Result<std::optional<ArrowArray>> Next() { |
99 | | - const auto& snapshots = table_->snapshots(); |
| 101 | + if (metadata_ == nullptr) [[unlikely]] { |
| 102 | + return InvalidArgument("Cannot read from a closed snapshots table stream"); |
| 103 | + } |
| 104 | + const auto& snapshots = metadata_->snapshots; |
100 | 105 | if (next_snapshot_ == snapshots.size()) { |
101 | 106 | return std::nullopt; |
102 | 107 | } |
@@ -129,10 +134,11 @@ class SnapshotsTableStream { |
129 | 134 | } |
130 | 135 |
|
131 | 136 | private: |
132 | | - SnapshotsTableStream(std::shared_ptr<Table> table, ArrowSchema arrow_schema) |
133 | | - : table_(std::move(table)), arrow_schema_(std::move(arrow_schema)) {} |
| 137 | + SnapshotsTableStream(std::shared_ptr<const TableMetadata> metadata, |
| 138 | + ArrowSchema arrow_schema) |
| 139 | + : metadata_(std::move(metadata)), arrow_schema_(std::move(arrow_schema)) {} |
134 | 140 |
|
135 | | - std::shared_ptr<Table> table_; |
| 141 | + std::shared_ptr<const TableMetadata> metadata_; |
136 | 142 | ArrowSchema arrow_schema_{}; |
137 | 143 | size_t next_snapshot_ = 0; |
138 | 144 | }; |
@@ -165,8 +171,8 @@ Result<std::unique_ptr<SnapshotsTable>> SnapshotsTable::Make( |
165 | 171 | } |
166 | 172 |
|
167 | 173 | Result<ArrowArrayStream> SnapshotsTable::Scan() { |
168 | | - ICEBERG_ASSIGN_OR_RAISE(auto stream, |
169 | | - SnapshotsTableStream::Make(source_table(), *schema())); |
| 174 | + ICEBERG_ASSIGN_OR_RAISE( |
| 175 | + auto stream, SnapshotsTableStream::Make(source_table()->metadata(), *schema())); |
170 | 176 | return MakeArrowArrayStream(std::move(stream)); |
171 | 177 | } |
172 | 178 |
|
|
0 commit comments