|
20 | 20 | #pragma once |
21 | 21 |
|
22 | 22 | /// \file iceberg/inspect/metadata_table.h |
23 | | -/// \brief Define base APIs for metadata tables. |
| 23 | +/// \brief Base APIs for inspecting Iceberg metadata tables. |
24 | 24 |
|
| 25 | +#include <concepts> |
25 | 26 | #include <memory> |
26 | | -#include <optional> |
27 | 27 | #include <string> |
| 28 | +#include <utility> |
| 29 | +#include <variant> |
28 | 30 |
|
29 | 31 | #include "iceberg/arrow_c_data.h" |
30 | 32 | #include "iceberg/iceberg_export.h" |
31 | 33 | #include "iceberg/result.h" |
32 | | -#include "iceberg/table_identifier.h" |
33 | 34 | #include "iceberg/type_fwd.h" |
34 | 35 | #include "iceberg/util/timepoint.h" |
35 | 36 |
|
36 | 37 | namespace iceberg { |
37 | 38 |
|
38 | | -/// \brief Parameters for snapshot selection (time travel). |
39 | | -struct SnapshotSelection { |
40 | | - /// \brief The snapshot ID to read. |
41 | | - std::optional<int64_t> snapshot_id; |
42 | | - /// \brief Read the snapshot that was current at this timestamp. |
43 | | - std::optional<TimePointMs> as_of_timestamp; |
44 | | - /// \brief Read the snapshot referenced by this named ref (branch or tag). |
45 | | - std::optional<std::string> ref_name; |
46 | | -}; |
47 | | - |
48 | | -/// \brief Base class for Iceberg metadata tables. |
| 39 | +/// \brief Base interface for an Iceberg metadata table. |
49 | 40 | class ICEBERG_EXPORT MetadataTable { |
50 | 41 | public: |
| 42 | + /// \brief Supported metadata table kinds. |
51 | 43 | enum class Kind { |
52 | 44 | kSnapshots, |
53 | 45 | kHistory, |
54 | 46 | }; |
55 | 47 |
|
56 | | - static Result<std::unique_ptr<MetadataTable>> Make(std::shared_ptr<Table> table, |
57 | | - Kind kind); |
| 48 | + /// \brief Maximum number of rows emitted in each Arrow batch. |
| 49 | + static constexpr int64_t kBatchSize = 1024; |
58 | 50 |
|
| 51 | + /// \brief Create a metadata table of the requested concrete type. |
| 52 | + /// |
| 53 | + /// \tparam MetadataTableType Concrete class derived from MetadataTable. |
| 54 | + /// \param table Source table whose metadata will be exposed. |
| 55 | + /// \return The constructed metadata table, or an error. |
| 56 | + template <typename MetadataTableType> |
| 57 | + requires std::derived_from<MetadataTableType, MetadataTable> |
| 58 | + static Result<std::unique_ptr<MetadataTableType>> Make(std::shared_ptr<Table> table) { |
| 59 | + return MetadataTableType::Make(std::move(table)); |
| 60 | + } |
| 61 | + |
| 62 | + /// \brief Destroy this metadata table. |
59 | 63 | virtual ~MetadataTable(); |
60 | 64 |
|
| 65 | + /// \brief Return this metadata table's kind. |
61 | 66 | virtual Kind kind() const noexcept = 0; |
62 | 67 |
|
63 | | - /// \brief Whether this metadata table supports time-travel queries. |
64 | | - /// |
65 | | - /// The currently supported snapshots and history metadata tables do not |
66 | | - /// support time travel. |
67 | | - bool supports_time_travel() const noexcept; |
| 68 | + /// \brief Return the schema of rows emitted by scans. |
| 69 | + virtual const std::shared_ptr<Schema>& schema() const = 0; |
| 70 | + |
| 71 | + /// \brief Return the source table whose metadata is exposed. |
| 72 | + const std::shared_ptr<Table>& source_table() const; |
68 | 73 |
|
69 | | - /// \brief Scan the metadata table using the current snapshot. |
| 74 | + /// \brief Return whether this metadata table supports time travel. |
| 75 | + virtual bool supports_time_travel() const noexcept; |
| 76 | + |
| 77 | + /// \brief Scan the metadata table without time travel. |
70 | 78 | /// |
71 | | - /// Convenience overload — delegates to Scan(std::nullopt). |
72 | | - Result<ArrowArray> Scan() { return Scan(std::nullopt); } |
| 79 | + /// The caller owns the returned stream and must release it with |
| 80 | + /// ArrowArrayStreamRelease. |
| 81 | + virtual Result<ArrowArrayStream> Scan() = 0; |
| 82 | + |
| 83 | + protected: |
| 84 | + explicit MetadataTable(std::shared_ptr<Table> source_table); |
73 | 85 |
|
74 | | - /// \brief Scan the metadata table and return all rows as an Arrow struct array. |
| 86 | + private: |
| 87 | + std::shared_ptr<Table> source_table_; |
| 88 | +}; |
| 89 | + |
| 90 | +/// \brief Snapshot selection parameters for a time-travel scan. |
| 91 | +struct SnapshotSelection { |
| 92 | + /// \brief Select the current snapshot, a snapshot ID, or an as-of timestamp. |
75 | 93 | /// |
76 | | - /// The returned ArrowArray is a struct array where each element is one row. |
77 | | - /// The caller takes ownership and must call ArrowArrayRelease when done. |
| 94 | + /// std::monostate selects the current snapshot. |
| 95 | + std::variant<std::monostate, int64_t, TimePointMs> snapshot; |
| 96 | + |
| 97 | + /// \brief Resolve the snapshot relative to this branch or tag. |
78 | 98 | /// |
79 | | - /// The default implementation returns NotSupported. Subclasses override this |
80 | | - /// to materialize their data. |
81 | | - virtual Result<ArrowArray> Scan( |
82 | | - const std::optional<SnapshotSelection>& snapshot_selection); |
| 99 | + /// An empty string uses the main branch. |
| 100 | + std::string ref_name; |
| 101 | +}; |
83 | 102 |
|
84 | | - const TableIdentifier& name() const { return identifier_; } |
| 103 | +/// \brief Base interface for metadata tables that support time travel. |
| 104 | +class ICEBERG_EXPORT TimeTravelMetadataTable : public MetadataTable { |
| 105 | + public: |
| 106 | + ~TimeTravelMetadataTable() override; |
| 107 | + |
| 108 | + /// \brief Return true because this interface supports time travel. |
| 109 | + bool supports_time_travel() const noexcept final; |
85 | 110 |
|
86 | | - const std::shared_ptr<Schema>& schema() const { return schema_; } |
| 111 | + /// \brief Scan using the current snapshot on the main branch. |
| 112 | + Result<ArrowArrayStream> Scan() final; |
87 | 113 |
|
88 | | - const std::shared_ptr<Table>& source_table() const { return source_table_; } |
| 114 | + /// \brief Scan using the requested snapshot selection. |
| 115 | + /// |
| 116 | + /// \param snapshot_selection Snapshot ID, timestamp, and optional ref selection. |
| 117 | + /// \return An Arrow stream containing the metadata table rows, or an error. |
| 118 | + Result<ArrowArrayStream> Scan(const SnapshotSelection& snapshot_selection); |
89 | 119 |
|
90 | 120 | protected: |
91 | | - explicit MetadataTable(std::shared_ptr<Table> source_table, TableIdentifier identifier, |
92 | | - std::shared_ptr<Schema> schema); |
| 121 | + explicit TimeTravelMetadataTable(std::shared_ptr<Table> source_table); |
93 | 122 |
|
94 | | - private: |
95 | | - TableIdentifier identifier_; |
96 | | - std::shared_ptr<Schema> schema_; |
97 | | - std::shared_ptr<Table> source_table_; |
| 123 | + /// \brief Implement a scan for the requested snapshot selection. |
| 124 | + virtual Result<ArrowArrayStream> ScanSnapshot( |
| 125 | + const SnapshotSelection& snapshot_selection) = 0; |
98 | 126 | }; |
99 | 127 |
|
100 | 128 | } // namespace iceberg |
0 commit comments