Skip to content

Commit f889702

Browse files
committed
fix(inspect): align metadata tables with Java
1 parent cf28ec0 commit f889702

22 files changed

Lines changed: 538 additions & 381 deletions

‎src/iceberg/CMakeLists.txt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,14 @@ set(ICEBERG_SOURCES
5353
file_reader.cc
5454
file_writer.cc
5555
geospatial.cc
56-
inspect/branches_table.cc
5756
inspect/files_table.cc
5857
inspect/history_table.cc
5958
inspect/manifests_table.cc
6059
inspect/metadata_table.cc
6160
inspect/metadata_table_util_internal.cc
6261
inspect/partitions_table.cc
62+
inspect/refs_table.cc
6363
inspect/snapshots_table.cc
64-
inspect/tags_table.cc
6564
inheritable_metadata.cc
6665
json_serde.cc
6766
location_provider.cc

‎src/iceberg/arrow_row_builder.cc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "iceberg/nanoarrow_status_internal.h"
2929
#include "iceberg/schema.h"
3030
#include "iceberg/schema_internal.h"
31+
#include "iceberg/util/decimal.h"
3132

3233
namespace iceberg {
3334

@@ -123,6 +124,15 @@ Status AppendDouble(ArrowArray* array, double value) {
123124
return {};
124125
}
125126

127+
Status AppendDecimal(ArrowArray* array, const Decimal& value) {
128+
// Append the unscaled integer; the array schema supplies precision and scale.
129+
ArrowDecimal decimal;
130+
ArrowDecimalInit(&decimal, Decimal::kBitWidth, Decimal::kMaxPrecision, 0);
131+
ArrowDecimalSetBytes(&decimal, value.native_endian_bytes());
132+
ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayAppendDecimal(array, &decimal));
133+
return {};
134+
}
135+
126136
Status AppendString(ArrowArray* array, std::string_view value) {
127137
ArrowStringView view(value.data(), static_cast<int64_t>(value.size()));
128138
ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayAppendString(array, view));

‎src/iceberg/arrow_row_builder_internal.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ ICEBERG_EXPORT Status AppendUInt(ArrowArray* array, uint64_t value);
125125
/// \brief Append a double value to a nanoarrow array builder.
126126
ICEBERG_EXPORT Status AppendDouble(ArrowArray* array, double value);
127127

128+
/// \brief Append an unscaled decimal value to a decimal128 array builder.
129+
ICEBERG_EXPORT Status AppendDecimal(ArrowArray* array, const Decimal& value);
130+
128131
/// \brief Append a string value to a nanoarrow array builder.
129132
ICEBERG_EXPORT Status AppendString(ArrowArray* array, std::string_view value);
130133

‎src/iceberg/inspect/branches_table.h‎

Lines changed: 0 additions & 51 deletions
This file was deleted.

‎src/iceberg/inspect/files_table.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Result<ArrowArrayStream> FilesTable::ScanSnapshot(
5858
const SnapshotSelection& snapshot_selection) {
5959
ICEBERG_ASSIGN_OR_RAISE(auto snapshot, internal::ResolveMetadataTableSnapshot(
6060
*source_table(), snapshot_selection));
61-
ICEBERG_ASSIGN_OR_RAISE(auto files, internal::LoadLiveFiles(*source_table(), snapshot));
61+
ICEBERG_ASSIGN_OR_RAISE(auto files, internal::LiveFiles(*source_table(), snapshot));
6262
auto schema = schema_;
6363
auto table_schema = table_schema_;
6464
auto partition_type = partition_type_;

‎src/iceberg/inspect/manifests_table.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ Status AppendPartitionSummaries(ArrowArray* array, const ManifestRow& row,
9090
HumanReadableBound(*row.spec, *partition_type, index, *summary.lower_bound));
9191
ICEBERG_RETURN_UNEXPECTED(AppendString(entries->children[2], lower));
9292
} else {
93-
ICEBERG_RETURN_UNEXPECTED(AppendNull(entries->children[2]));
93+
ICEBERG_RETURN_UNEXPECTED(AppendString(entries->children[2], "null"));
9494
}
9595
if (summary.upper_bound.has_value()) {
9696
ICEBERG_ASSIGN_OR_RAISE(
9797
auto upper,
9898
HumanReadableBound(*row.spec, *partition_type, index, *summary.upper_bound));
9999
ICEBERG_RETURN_UNEXPECTED(AppendString(entries->children[3], upper));
100100
} else {
101-
ICEBERG_RETURN_UNEXPECTED(AppendNull(entries->children[3]));
101+
ICEBERG_RETURN_UNEXPECTED(AppendString(entries->children[3], "null"));
102102
}
103103
ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayFinishElement(entries));
104104
}

‎src/iceberg/inspect/meson.build‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717

1818
install_headers(
1919
[
20-
'branches_table.h',
2120
'files_table.h',
2221
'history_table.h',
2322
'manifests_table.h',
2423
'metadata_table.h',
2524
'partitions_table.h',
25+
'refs_table.h',
2626
'snapshots_table.h',
27-
'tags_table.h',
2827
],
2928
subdir: 'iceberg/inspect',
3029
)

‎src/iceberg/inspect/metadata_table.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ class ICEBERG_EXPORT MetadataTable {
4343
enum class Kind {
4444
kSnapshots,
4545
kHistory,
46-
kBranches,
47-
kTags,
46+
kRefs,
4847
kFiles,
4948
kPartitions,
5049
kManifests,
@@ -98,9 +97,10 @@ struct SnapshotSelection {
9897
/// std::monostate selects the current snapshot.
9998
std::variant<std::monostate, int64_t, TimePointMs> snapshot;
10099

101-
/// \brief Resolve the snapshot relative to this branch or tag.
100+
/// \brief Select the head of this branch or tag.
102101
///
103-
/// An empty string uses the main branch.
102+
/// An empty string or "main" uses the main branch. A non-main reference cannot
103+
/// be combined with a snapshot ID or timestamp.
104104
std::string ref_name;
105105
};
106106

‎src/iceberg/inspect/metadata_table_stream_internal.h‎

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@
3434
#include "iceberg/nanoarrow_status_internal.h"
3535
#include "iceberg/schema.h"
3636
#include "iceberg/schema_internal.h"
37+
#include "iceberg/util/iterator.h"
3738
#include "iceberg/util/macros.h"
3839

3940
namespace iceberg::internal {
4041

41-
/// \brief Arrow stream backed by a fixed set of metadata-table rows.
42+
/// \brief Arrow stream backed by a metadata-table row iterator.
4243
template <typename Row>
4344
class MetadataTableRowsStream {
4445
public:
4546
using AppendRow = std::function<Status(ArrowRowBuilder&, const Row&)>;
4647

47-
static Result<std::unique_ptr<MetadataTableRowsStream>> Make(const Schema& schema,
48-
std::vector<Row> rows,
49-
AppendRow append_row) {
48+
static Result<std::unique_ptr<MetadataTableRowsStream>> Make(
49+
const Schema& schema, std::unique_ptr<Iterator<Row>> rows, AppendRow append_row) {
5050
ArrowSchema arrow_schema{};
5151
ICEBERG_RETURN_UNEXPECTED(ToArrowSchema(schema, &arrow_schema));
5252
return std::unique_ptr<MetadataTableRowsStream>(new MetadataTableRowsStream(
@@ -59,7 +59,7 @@ class MetadataTableRowsStream {
5959
}
6060

6161
Status Close() {
62-
rows_.clear();
62+
rows_.reset();
6363
append_row_ = nullptr;
6464
if (arrow_schema_.release != nullptr) {
6565
ArrowSchemaRelease(&arrow_schema_);
@@ -70,13 +70,16 @@ class MetadataTableRowsStream {
7070
Result<std::optional<ArrowArray>> Next() {
7171
ICEBERG_PRECHECK(arrow_schema_.release != nullptr,
7272
"Cannot read from a closed metadata table stream");
73-
if (next_row_ == rows_.size()) {
74-
return std::nullopt;
75-
}
76-
7773
ICEBERG_ASSIGN_OR_RAISE(auto builder, ArrowRowBuilder::Make(&arrow_schema_));
78-
while (next_row_ < rows_.size() && builder.num_rows() < MetadataTable::kBatchSize) {
79-
ICEBERG_RETURN_UNEXPECTED(append_row_(builder, rows_[next_row_++]));
74+
while (builder.num_rows() < MetadataTable::kBatchSize) {
75+
ICEBERG_ASSIGN_OR_RAISE(auto row, rows_->Next());
76+
if (!row.has_value()) {
77+
break;
78+
}
79+
ICEBERG_RETURN_UNEXPECTED(append_row_(builder, *row));
80+
}
81+
if (builder.num_rows() == 0) {
82+
return std::nullopt;
8083
}
8184

8285
ICEBERG_ASSIGN_OR_RAISE(auto array, std::move(builder).Finish());
@@ -93,21 +96,20 @@ class MetadataTableRowsStream {
9396
}
9497

9598
private:
96-
MetadataTableRowsStream(std::vector<Row> rows, AppendRow append_row,
99+
MetadataTableRowsStream(std::unique_ptr<Iterator<Row>> rows, AppendRow append_row,
97100
ArrowSchema arrow_schema)
98101
: rows_(std::move(rows)),
99102
append_row_(std::move(append_row)),
100103
arrow_schema_(std::move(arrow_schema)) {}
101104

102-
std::vector<Row> rows_;
105+
std::unique_ptr<Iterator<Row>> rows_;
103106
AppendRow append_row_;
104107
ArrowSchema arrow_schema_{};
105-
size_t next_row_ = 0;
106108
};
107109

108110
template <typename Row, typename AppendRow>
109111
Result<ArrowArrayStream> MakeMetadataTableStream(const Schema& schema,
110-
std::vector<Row> rows,
112+
std::unique_ptr<Iterator<Row>> rows,
111113
AppendRow append_row) {
112114
ICEBERG_ASSIGN_OR_RAISE(
113115
auto stream,
@@ -117,4 +119,31 @@ Result<ArrowArrayStream> MakeMetadataTableStream(const Schema& schema,
117119
return MakeArrowArrayStream(std::move(stream));
118120
}
119121

122+
template <typename Row>
123+
class MetadataTableVectorIterator : public Iterator<Row> {
124+
public:
125+
explicit MetadataTableVectorIterator(std::vector<Row> rows) : rows_(std::move(rows)) {}
126+
127+
protected:
128+
Result<std::optional<Row>> NextImpl() override {
129+
if (next_row_ == rows_.size()) {
130+
return std::nullopt;
131+
}
132+
return std::move(rows_[next_row_++]);
133+
}
134+
135+
private:
136+
std::vector<Row> rows_;
137+
size_t next_row_ = 0;
138+
};
139+
140+
template <typename Row, typename AppendRow>
141+
Result<ArrowArrayStream> MakeMetadataTableStream(const Schema& schema,
142+
std::vector<Row> rows,
143+
AppendRow append_row) {
144+
std::unique_ptr<Iterator<Row>> iterator =
145+
std::make_unique<MetadataTableVectorIterator<Row>>(std::move(rows));
146+
return MakeMetadataTableStream(schema, std::move(iterator), std::move(append_row));
147+
}
148+
120149
} // namespace iceberg::internal

0 commit comments

Comments
 (0)