|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/inspect/branches_table.h" |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <memory> |
| 24 | +#include <optional> |
| 25 | +#include <string> |
| 26 | +#include <utility> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +#include "iceberg/arrow_row_builder_internal.h" |
| 30 | +#include "iceberg/inspect/metadata_table_stream_internal.h" |
| 31 | +#include "iceberg/schema.h" |
| 32 | +#include "iceberg/schema_field.h" |
| 33 | +#include "iceberg/snapshot.h" |
| 34 | +#include "iceberg/table.h" |
| 35 | +#include "iceberg/table_metadata.h" |
| 36 | +#include "iceberg/type.h" |
| 37 | +#include "iceberg/util/macros.h" |
| 38 | + |
| 39 | +namespace iceberg { |
| 40 | +namespace { |
| 41 | + |
| 42 | +struct BranchRow { |
| 43 | + std::string name; |
| 44 | + int64_t snapshot_id; |
| 45 | + std::optional<int64_t> max_ref_age_ms; |
| 46 | + std::optional<int32_t> min_snapshots_to_keep; |
| 47 | + std::optional<int64_t> max_snapshot_age_ms; |
| 48 | +}; |
| 49 | + |
| 50 | +Status AppendOptional(ArrowArray* array, const auto& value) { |
| 51 | + if (!value.has_value()) { |
| 52 | + return AppendNull(array); |
| 53 | + } |
| 54 | + return AppendInt(array, static_cast<int64_t>(*value)); |
| 55 | +} |
| 56 | + |
| 57 | +Status AppendBranch(ArrowRowBuilder& builder, const BranchRow& branch) { |
| 58 | + ICEBERG_RETURN_UNEXPECTED(AppendString(builder.column(0), branch.name)); |
| 59 | + ICEBERG_RETURN_UNEXPECTED(AppendInt(builder.column(1), branch.snapshot_id)); |
| 60 | + ICEBERG_RETURN_UNEXPECTED(AppendOptional(builder.column(2), branch.max_ref_age_ms)); |
| 61 | + ICEBERG_RETURN_UNEXPECTED( |
| 62 | + AppendOptional(builder.column(3), branch.min_snapshots_to_keep)); |
| 63 | + ICEBERG_RETURN_UNEXPECTED( |
| 64 | + AppendOptional(builder.column(4), branch.max_snapshot_age_ms)); |
| 65 | + return builder.FinishRow(); |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace |
| 69 | + |
| 70 | +BranchesTable::BranchesTable(std::shared_ptr<Table> table) |
| 71 | + : MetadataTable(std::move(table)) {} |
| 72 | + |
| 73 | +BranchesTable::~BranchesTable() = default; |
| 74 | + |
| 75 | +const std::shared_ptr<Schema>& BranchesTable::schema() const { |
| 76 | + static const auto schema = std::make_shared<Schema>(std::vector<SchemaField>{ |
| 77 | + SchemaField::MakeRequired(1, "name", string()), |
| 78 | + SchemaField::MakeRequired(2, "snapshot_id", int64()), |
| 79 | + SchemaField::MakeOptional(3, "max_reference_age_in_ms", int64()), |
| 80 | + SchemaField::MakeOptional(4, "min_snapshots_to_keep", int32()), |
| 81 | + SchemaField::MakeOptional(5, "max_snapshot_age_in_ms", int64()), |
| 82 | + }); |
| 83 | + return schema; |
| 84 | +} |
| 85 | + |
| 86 | +Result<std::unique_ptr<BranchesTable>> BranchesTable::Make(std::shared_ptr<Table> table) { |
| 87 | + ICEBERG_PRECHECK(table != nullptr, "Table cannot be null"); |
| 88 | + return std::unique_ptr<BranchesTable>(new BranchesTable(std::move(table))); |
| 89 | +} |
| 90 | + |
| 91 | +Result<ArrowArrayStream> BranchesTable::Scan() { |
| 92 | + std::vector<BranchRow> rows; |
| 93 | + for (const auto& [name, ref] : source_table()->metadata()->refs) { |
| 94 | + if (ref == nullptr || ref->type() != SnapshotRefType::kBranch) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + const auto& retention = std::get<SnapshotRef::Branch>(ref->retention); |
| 98 | + rows.push_back(BranchRow{.name = name, |
| 99 | + .snapshot_id = ref->snapshot_id, |
| 100 | + .max_ref_age_ms = retention.max_ref_age_ms, |
| 101 | + .min_snapshots_to_keep = retention.min_snapshots_to_keep, |
| 102 | + .max_snapshot_age_ms = retention.max_snapshot_age_ms}); |
| 103 | + } |
| 104 | + std::ranges::sort(rows, {}, &BranchRow::name); |
| 105 | + return internal::MakeMetadataTableStream(*schema(), std::move(rows), AppendBranch); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace iceberg |
0 commit comments