Repository navigation
feat(inspect): add refs metadata table #1007
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
Draft
manuzhang
wants to merge
2
commits into
apache:main
Choose a base branch
from
manuzhang:codex/add-refs-metadata-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+761
−66
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <nanoarrow/nanoarrow.h> | ||
|
|
||
| #include "iceberg/arrow_c_data_util_internal.h" | ||
| #include "iceberg/arrow_row_builder_internal.h" | ||
| #include "iceberg/inspect/metadata_table.h" | ||
| #include "iceberg/nanoarrow_status_internal.h" | ||
| #include "iceberg/schema.h" | ||
| #include "iceberg/schema_internal.h" | ||
| #include "iceberg/util/macros.h" | ||
| #include "iceberg/util/stream.h" | ||
|
|
||
| namespace iceberg::internal { | ||
|
|
||
| /// \brief Arrow stream backed by a metadata-table row iterator. | ||
| template <typename Row, typename AppendRow> | ||
| class MetadataTableRowsStream : public Stream<ArrowArray> { | ||
| public: | ||
| static Result<std::unique_ptr<MetadataTableRowsStream>> Make( | ||
| const iceberg::Schema& schema, std::unique_ptr<Stream<Row>> rows, | ||
| AppendRow append_row) { | ||
| ICEBERG_PRECHECK(rows != nullptr, "Metadata table row source cannot be null"); | ||
| if constexpr (requires { static_cast<bool>(append_row); }) { | ||
| ICEBERG_PRECHECK(static_cast<bool>(append_row), | ||
| "Metadata table row callback cannot be empty"); | ||
| } | ||
| ArrowSchema arrow_schema{}; | ||
| ICEBERG_RETURN_UNEXPECTED(ToArrowSchema(schema, &arrow_schema)); | ||
| return std::unique_ptr<MetadataTableRowsStream>(new MetadataTableRowsStream( | ||
| std::move(rows), std::move(append_row), std::move(arrow_schema))); | ||
| } | ||
|
|
||
| ~MetadataTableRowsStream() override { | ||
| if (arrow_schema_.release != nullptr) { | ||
| ArrowSchemaRelease(&arrow_schema_); | ||
| } | ||
| } | ||
|
|
||
| Status Close() { | ||
| rows_.reset(); | ||
| append_row_.reset(); | ||
| if (arrow_schema_.release != nullptr) { | ||
| ArrowSchemaRelease(&arrow_schema_); | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| Result<ArrowSchema> Schema() { | ||
| ICEBERG_PRECHECK(arrow_schema_.release != nullptr, | ||
| "Cannot read schema from a closed metadata table stream"); | ||
| ArrowSchema schema_copy{}; | ||
| ICEBERG_NANOARROW_RETURN_UNEXPECTED( | ||
| ArrowSchemaDeepCopy(&arrow_schema_, &schema_copy)); | ||
| return schema_copy; | ||
| } | ||
|
|
||
| protected: | ||
| Result<std::optional<ArrowArray>> NextImpl() override { | ||
| ICEBERG_PRECHECK(rows_ != nullptr, "Cannot read from a closed metadata table stream"); | ||
| // Check for exhaustion before allocating a builder, including after a partial batch. | ||
| ICEBERG_ASSIGN_OR_RAISE(auto row, rows_->Next()); | ||
| if (!row.has_value()) { | ||
| return std::nullopt; | ||
| } | ||
| ICEBERG_ASSIGN_OR_RAISE(auto builder, ArrowRowBuilder::Make(&arrow_schema_)); | ||
| ICEBERG_RETURN_UNEXPECTED((*append_row_)(builder, *row)); | ||
| while (builder.num_rows() < MetadataTable::kBatchSize) { | ||
| ICEBERG_ASSIGN_OR_RAISE(auto next_row, rows_->Next()); | ||
| if (!next_row.has_value()) { | ||
| break; | ||
| } | ||
| ICEBERG_RETURN_UNEXPECTED((*append_row_)(builder, *next_row)); | ||
| } | ||
| ICEBERG_ASSIGN_OR_RAISE(auto array, std::move(builder).Finish()); | ||
| return array; | ||
| } | ||
|
|
||
| private: | ||
| MetadataTableRowsStream(std::unique_ptr<Stream<Row>> rows, AppendRow append_row, | ||
| ArrowSchema arrow_schema) | ||
| : rows_(std::move(rows)), | ||
| append_row_(std::move(append_row)), | ||
| arrow_schema_(std::move(arrow_schema)) {} | ||
|
|
||
| std::unique_ptr<Stream<Row>> rows_; | ||
| std::optional<AppendRow> append_row_; | ||
| ArrowSchema arrow_schema_{}; | ||
| }; | ||
|
|
||
| template <typename Row, typename AppendRow> | ||
| Result<ArrowArrayStream> MakeMetadataTableStream(const Schema& schema, | ||
| std::unique_ptr<Stream<Row>> rows, | ||
| AppendRow append_row) { | ||
| using RowsStream = MetadataTableRowsStream<Row, AppendRow>; | ||
| ICEBERG_ASSIGN_OR_RAISE( | ||
| auto stream, RowsStream::Make(schema, std::move(rows), std::move(append_row))); | ||
| return MakeArrowArrayStream(std::move(stream)); | ||
| } | ||
|
|
||
| template <typename Row> | ||
| class MetadataTableVectorIterator : public Stream<Row> { | ||
| public: | ||
| explicit MetadataTableVectorIterator(std::vector<Row> rows) : rows_(std::move(rows)) {} | ||
|
|
||
| protected: | ||
| Result<std::optional<Row>> NextImpl() override { | ||
| if (next_row_ == rows_.size()) { | ||
| return std::nullopt; | ||
| } | ||
| return std::move(rows_[next_row_++]); | ||
| } | ||
|
|
||
| private: | ||
| std::vector<Row> rows_; | ||
| size_t next_row_ = 0; | ||
| }; | ||
|
|
||
| template <typename Row, typename AppendRow> | ||
| Result<ArrowArrayStream> MakeMetadataTableStream(const Schema& schema, | ||
| std::vector<Row> rows, | ||
| AppendRow append_row) { | ||
| std::unique_ptr<Stream<Row>> iterator = | ||
| std::make_unique<MetadataTableVectorIterator<Row>>(std::move(rows)); | ||
| return MakeMetadataTableStream(schema, std::move(iterator), std::move(append_row)); | ||
| } | ||
|
|
||
| } // namespace iceberg::internal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/inspect/refs_table.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <variant> | ||
| #include <vector> | ||
|
|
||
| #include "iceberg/arrow_row_builder_internal.h" | ||
| #include "iceberg/inspect/metadata_table_stream_internal.h" | ||
| #include "iceberg/schema.h" | ||
| #include "iceberg/schema_field.h" | ||
| #include "iceberg/snapshot.h" | ||
| #include "iceberg/table.h" | ||
| #include "iceberg/table_metadata.h" | ||
| #include "iceberg/type.h" | ||
| #include "iceberg/util/macros.h" | ||
|
|
||
| namespace iceberg { | ||
| namespace { | ||
|
|
||
| struct RefRow { | ||
| std::string name; | ||
| std::string type; | ||
| int64_t snapshot_id; | ||
| std::optional<int64_t> max_ref_age_ms; | ||
| std::optional<int32_t> min_snapshots_to_keep; | ||
| std::optional<int64_t> max_snapshot_age_ms; | ||
| }; | ||
|
|
||
| Status AppendOptional(ArrowArray* array, const auto& value) { | ||
| if (!value.has_value()) { | ||
| return AppendNull(array); | ||
| } | ||
| return AppendInt(array, static_cast<int64_t>(*value)); | ||
| } | ||
|
|
||
| Status AppendRef(ArrowRowBuilder& builder, const RefRow& ref) { | ||
| ICEBERG_RETURN_UNEXPECTED(AppendString(builder.column(0), ref.name)); | ||
| ICEBERG_RETURN_UNEXPECTED(AppendString(builder.column(1), ref.type)); | ||
| ICEBERG_RETURN_UNEXPECTED(AppendInt(builder.column(2), ref.snapshot_id)); | ||
| ICEBERG_RETURN_UNEXPECTED(AppendOptional(builder.column(3), ref.max_ref_age_ms)); | ||
| ICEBERG_RETURN_UNEXPECTED(AppendOptional(builder.column(4), ref.min_snapshots_to_keep)); | ||
| ICEBERG_RETURN_UNEXPECTED(AppendOptional(builder.column(5), ref.max_snapshot_age_ms)); | ||
| return builder.FinishRow(); | ||
| } | ||
|
Comment on lines
+53
to
+68
|
||
|
|
||
| } // namespace | ||
|
|
||
| RefsTable::RefsTable(std::shared_ptr<Table> table) : MetadataTable(std::move(table)) {} | ||
|
|
||
| RefsTable::~RefsTable() = default; | ||
|
|
||
| const std::shared_ptr<Schema>& RefsTable::schema() const { | ||
| static const auto schema = std::make_shared<Schema>(std::vector<SchemaField>{ | ||
| SchemaField::MakeRequired(1, "name", string()), | ||
| SchemaField::MakeRequired(2, "type", string()), | ||
| SchemaField::MakeRequired(3, "snapshot_id", int64()), | ||
| SchemaField::MakeOptional(4, "max_reference_age_in_ms", int64()), | ||
| SchemaField::MakeOptional(5, "min_snapshots_to_keep", int32()), | ||
| SchemaField::MakeOptional(6, "max_snapshot_age_in_ms", int64()), | ||
| }); | ||
| return schema; | ||
| } | ||
|
|
||
| Result<std::unique_ptr<RefsTable>> RefsTable::Make(std::shared_ptr<Table> table) { | ||
| ICEBERG_PRECHECK(table != nullptr, "Table cannot be null"); | ||
| return std::unique_ptr<RefsTable>(new RefsTable(std::move(table))); | ||
| } | ||
|
|
||
| Result<ArrowArrayStream> RefsTable::Scan() { | ||
| std::vector<RefRow> rows; | ||
| const auto& refs = source_table()->metadata()->refs; | ||
| rows.reserve(refs.size()); | ||
| for (const auto& [name, ref] : refs) { | ||
| if (ref == nullptr) { | ||
| return Invalid("Snapshot reference '{}' is null", name); | ||
| } | ||
| const auto* branch = std::get_if<SnapshotRef::Branch>(&ref->retention); | ||
| RefRow row{.name = name, | ||
| .type = branch != nullptr ? "BRANCH" : "TAG", | ||
| .snapshot_id = ref->snapshot_id, | ||
| .max_ref_age_ms = | ||
| branch != nullptr | ||
| ? branch->max_ref_age_ms | ||
| : std::get<SnapshotRef::Tag>(ref->retention).max_ref_age_ms}; | ||
| if (branch != nullptr) { | ||
| row.min_snapshots_to_keep = branch->min_snapshots_to_keep; | ||
| row.max_snapshot_age_ms = branch->max_snapshot_age_ms; | ||
| } | ||
| rows.push_back(std::move(row)); | ||
| } | ||
| std::ranges::sort(rows, {}, &RefRow::name); | ||
|
manuzhang marked this conversation as resolved.
|
||
| return internal::MakeMetadataTableStream( | ||
| *schema(), std::move(rows), [](ArrowRowBuilder& builder, const RefRow& ref) { | ||
| return AppendRef(builder, ref); | ||
| }); | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| /// \file iceberg/inspect/refs_table.h | ||
| /// \brief Define the snapshot references metadata table. | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "iceberg/iceberg_export.h" | ||
| #include "iceberg/inspect/metadata_table.h" | ||
| #include "iceberg/result.h" | ||
| #include "iceberg/type_fwd.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| /// \brief Metadata table containing the table's branch and tag snapshot references. | ||
| class ICEBERG_EXPORT RefsTable : public MetadataTable { | ||
| public: | ||
| static Result<std::unique_ptr<RefsTable>> Make(std::shared_ptr<Table> table); | ||
|
|
||
| ~RefsTable() override; | ||
|
|
||
| Kind kind() const noexcept override { return Kind::kRefs; } | ||
|
|
||
| const std::shared_ptr<Schema>& schema() const override; | ||
|
|
||
| Result<ArrowArrayStream> Scan() override; | ||
|
|
||
| private: | ||
| explicit RefsTable(std::shared_ptr<Table> table); | ||
| }; | ||
|
|
||
| } // namespace iceberg |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.