|
| 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/files_table.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <mutex> |
| 24 | +#include <utility> |
| 25 | + |
| 26 | +#include "iceberg/inspect/files_table_util_internal.h" |
| 27 | +#include "iceberg/inspect/metadata_scan_util_internal.h" |
| 28 | +#include "iceberg/inspect/metadata_table_util_internal.h" |
| 29 | +#include "iceberg/schema.h" |
| 30 | +#include "iceberg/table.h" |
| 31 | +#include "iceberg/table_metadata.h" |
| 32 | +#include "iceberg/type.h" |
| 33 | +#include "iceberg/util/macros.h" |
| 34 | + |
| 35 | +namespace iceberg { |
| 36 | + |
| 37 | +FilesTable::FilesTable(std::shared_ptr<const Table> table) |
| 38 | + : MetadataTable(std::move(table)) {} |
| 39 | + |
| 40 | +FilesTable::~FilesTable() = default; |
| 41 | + |
| 42 | +std::shared_ptr<Schema> FilesTable::schema() const { |
| 43 | + std::lock_guard lock(schema_mutex_); |
| 44 | + return schema_; |
| 45 | +} |
| 46 | + |
| 47 | +Result<std::unique_ptr<FilesTable>> FilesTable::Make(std::shared_ptr<const Table> table) { |
| 48 | + ICEBERG_PRECHECK(table != nullptr, "Table cannot be null"); |
| 49 | + auto files = std::unique_ptr<FilesTable>(new FilesTable(std::move(table))); |
| 50 | + ICEBERG_RETURN_UNEXPECTED(files->SchemaForScan(*files->source_table()->metadata())); |
| 51 | + return files; |
| 52 | +} |
| 53 | + |
| 54 | +Result<std::shared_ptr<Schema>> FilesTable::SchemaForScan( |
| 55 | + const TableMetadata& metadata) const { |
| 56 | + ICEBERG_ASSIGN_OR_RAISE(auto table_schema, metadata.Schema()); |
| 57 | + ICEBERG_ASSIGN_OR_RAISE(auto partition_type, internal::UnifiedPartitionType(metadata)); |
| 58 | + ICEBERG_ASSIGN_OR_RAISE(auto schema, |
| 59 | + internal::FilesTableSchema(*table_schema, partition_type)); |
| 60 | + { |
| 61 | + std::lock_guard lock(schema_mutex_); |
| 62 | + schema_ = schema; |
| 63 | + } |
| 64 | + return schema; |
| 65 | +} |
| 66 | + |
| 67 | +Result<std::unique_ptr<MetadataScan>> FilesTable::MakeScan( |
| 68 | + internal::MetadataScanContext context) const { |
| 69 | + return internal::MakeSnapshotMetadataScan(std::move(context), ReadRows); |
| 70 | +} |
| 71 | + |
| 72 | +Result<ArrowArrayStream> FilesTable::ReadRows( |
| 73 | + const internal::MetadataScanContext& context, const std::shared_ptr<FileIO>& io, |
| 74 | + int64_t batch_size) { |
| 75 | + ICEBERG_ASSIGN_OR_RAISE(auto partition_type, |
| 76 | + internal::UnifiedPartitionType(*context.table_metadata)); |
| 77 | + ICEBERG_ASSIGN_OR_RAISE(auto table_schema, context.table_metadata->Schema()); |
| 78 | + ICEBERG_ASSIGN_OR_RAISE(auto snapshot, internal::ResolveMetadataTableSnapshot( |
| 79 | + *context.table_metadata, context)); |
| 80 | + ICEBERG_ASSIGN_OR_RAISE(auto files, |
| 81 | + internal::LiveFiles(*context.table_metadata, io, snapshot)); |
| 82 | + auto schema = context.projected_schema; |
| 83 | + const int32_t readable_metrics_id = context.schema->fields().back().field_id(); |
| 84 | + return internal::MakeMetadataBatchStream( |
| 85 | + schema, std::move(files), batch_size, |
| 86 | + [schema, readable_metrics_id, table_schema = std::move(table_schema), |
| 87 | + partition_type = std::move(partition_type)](ArrowRowBuilder& builder, |
| 88 | + const internal::LiveFile& file) { |
| 89 | + return internal::AppendDataFile(builder, *schema, *table_schema, *partition_type, |
| 90 | + readable_metrics_id, file); |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace iceberg |
0 commit comments