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
Open
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.
+559
−10
Open
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
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,179 @@ | ||
| /* | ||
| * 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 <array> | ||
| #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_scan_task.h" | ||
| #include "iceberg/inspect/metadata_scan_util_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)); | ||
| } | ||
|
|
||
| constexpr std::array<internal::MetadataTableColumn<RefRow>, 6> kRefColumns = { | ||
| {{.field_id = 1, | ||
| .append = [](ArrowArray* column, | ||
| const RefRow& row) { return AppendString(column, row.name); }}, | ||
| {.field_id = 2, | ||
| .append = [](ArrowArray* column, | ||
| const RefRow& row) { return AppendString(column, row.type); }}, | ||
| {.field_id = 3, | ||
| .append = [](ArrowArray* column, | ||
| const RefRow& row) { return AppendInt(column, row.snapshot_id); }}, | ||
| {.field_id = 4, | ||
| .append = | ||
| [](ArrowArray* column, const RefRow& row) { | ||
| return AppendOptional(column, row.max_ref_age_ms); | ||
| }}, | ||
| {.field_id = 5, | ||
| .append = | ||
| [](ArrowArray* column, const RefRow& row) { | ||
| return AppendOptional(column, row.min_snapshots_to_keep); | ||
| }}, | ||
| {.field_id = 6, .append = [](ArrowArray* column, const RefRow& row) { | ||
| return AppendOptional(column, row.max_snapshot_age_ms); | ||
| }}}}; | ||
|
|
||
| } // namespace | ||
|
|
||
| // RefsTable implementation | ||
| RefsTable::RefsTable(std::shared_ptr<const Table> table) | ||
| : MetadataTable(std::move(table)) {} | ||
|
|
||
| RefsTable::~RefsTable() = default; | ||
|
|
||
| const std::shared_ptr<Schema>& RefsTable::schema() const { return CanonicalSchema(); } | ||
|
|
||
| const std::shared_ptr<Schema>& RefsTable::CanonicalSchema() { | ||
| 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<const Table> table) { | ||
| ICEBERG_PRECHECK(table != nullptr, "Table cannot be null"); | ||
| return std::unique_ptr<RefsTable>(new RefsTable(std::move(table))); | ||
| } | ||
|
|
||
| Result<std::unique_ptr<MetadataScan>> RefsTable::MakeScan( | ||
| internal::MetadataScanContext context) const { | ||
| return RefsScan::Make(std::move(context)); | ||
| } | ||
|
|
||
| Result<ArrowArrayStream> RefsTable::ReadRows( | ||
| std::shared_ptr<const TableMetadata> metadata, | ||
| const std::shared_ptr<Schema>& projected_schema, int64_t batch_size) { | ||
| std::vector<RefRow> rows; | ||
| const auto& refs = 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.
Resolved
|
||
| const size_t num_rows = rows.size(); | ||
| return internal::MakeMetadataBatchStream<RefRow>( | ||
| CanonicalSchema(), projected_schema, kRefColumns, num_rows, batch_size, | ||
| [rows = std::move(rows)](size_t index) -> const RefRow& { return rows[index]; }); | ||
| } | ||
|
|
||
| // RefsScan implementation | ||
| RefsScan::RefsScan(internal::MetadataScanContext context, | ||
| std::shared_ptr<MetadataScanTask> task) | ||
| : MetadataScan(std::move(context)), task_(std::move(task)) {} | ||
|
|
||
| RefsScan::~RefsScan() = default; | ||
|
|
||
| Result<std::unique_ptr<RefsScan>> RefsScan::Make(internal::MetadataScanContext context) { | ||
| ICEBERG_PRECHECK(context.kind == MetadataTableKind::kRefs, | ||
| "Unexpected metadata table kind for refs scan"); | ||
| if (context.snapshot_id.has_value() || !context.ref_name.empty() || | ||
| context.as_of_time_in_millis.has_value()) { | ||
| return NotSupported("Metadata table does not support time travel"); | ||
| } | ||
| ICEBERG_PRECHECK(context.table_metadata != nullptr, "Table metadata cannot be null"); | ||
| ICEBERG_PRECHECK(context.schema != nullptr, "Metadata table schema cannot be null"); | ||
| ICEBERG_PRECHECK(context.projected_schema != nullptr, | ||
| "Projected schema cannot be null"); | ||
| ICEBERG_RETURN_UNEXPECTED(internal::SelectAppendFns<RefRow>( | ||
| *RefsTable::CanonicalSchema(), *context.projected_schema, kRefColumns)); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto task, | ||
| StaticMetadataTask::Make(context.kind, context.table_metadata, | ||
| context.projected_schema)); | ||
| return std::unique_ptr<RefsScan>(new RefsScan(std::move(context), std::move(task))); | ||
| } | ||
|
|
||
| Result<MetadataScanTaskStreamPtr> RefsScan::PlanTasksStream() const { | ||
| return MetadataScanTaskStream::FromVector({task_}); | ||
| } | ||
|
|
||
| } // 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,81 @@ | ||
| /* | ||
| * 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 refs metadata table. | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
|
|
||
| #include "iceberg/arrow_c_data.h" | ||
| #include "iceberg/iceberg_export.h" | ||
| #include "iceberg/inspect/metadata_scan.h" | ||
| #include "iceberg/inspect/metadata_table.h" | ||
| #include "iceberg/result.h" | ||
| #include "iceberg/type_fwd.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| /// \brief Refs metadata table. | ||
| class ICEBERG_EXPORT RefsTable : public MetadataTable { | ||
| public: | ||
| static Result<std::unique_ptr<RefsTable>> Make(std::shared_ptr<const Table> table); | ||
|
|
||
| /// \brief Return the canonical schema of the refs metadata table. | ||
| static const std::shared_ptr<Schema>& CanonicalSchema(); | ||
|
|
||
| ~RefsTable() override; | ||
|
|
||
| MetadataTableKind kind() const noexcept override { return MetadataTableKind::kRefs; } | ||
|
|
||
| const std::shared_ptr<Schema>& schema() const override; | ||
|
|
||
| protected: | ||
| Result<std::unique_ptr<MetadataScan>> MakeScan( | ||
| internal::MetadataScanContext context) const override; | ||
|
|
||
| private: | ||
| friend class MetadataScanTaskReader; | ||
|
|
||
| static Result<ArrowArrayStream> ReadRows( | ||
| std::shared_ptr<const TableMetadata> metadata, | ||
| const std::shared_ptr<Schema>& projected_schema, int64_t batch_size); | ||
|
|
||
| explicit RefsTable(std::shared_ptr<const Table> table); | ||
| }; | ||
|
|
||
| /// \brief Scan the refs metadata captured from a table. | ||
| class ICEBERG_EXPORT RefsScan final : public MetadataScan { | ||
| public: | ||
| /// \brief Create a scan from captured metadata table settings. | ||
| static Result<std::unique_ptr<RefsScan>> Make(internal::MetadataScanContext context); | ||
|
|
||
| ~RefsScan() override; | ||
|
|
||
| Result<MetadataScanTaskStreamPtr> PlanTasksStream() const override; | ||
|
|
||
| private: | ||
| RefsScan(internal::MetadataScanContext context, std::shared_ptr<MetadataScanTask> task); | ||
|
|
||
| const std::shared_ptr<MetadataScanTask> task_; | ||
| }; | ||
|
|
||
| } // 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
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
Oops, something went wrong.
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.