|
| 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/table_scan.h" |
| 21 | + |
| 22 | +#include "iceberg/manifest_entry.h" |
| 23 | +#include "iceberg/manifest_list.h" |
| 24 | +#include "iceberg/manifest_reader.h" |
| 25 | +#include "iceberg/schema.h" |
| 26 | +#include "iceberg/schema_field.h" |
| 27 | +#include "iceberg/snapshot.h" |
| 28 | +#include "iceberg/table.h" |
| 29 | +#include "iceberg/util/macros.h" |
| 30 | + |
| 31 | +namespace iceberg { |
| 32 | + |
| 33 | +TableScanBuilder::TableScanBuilder(const Table& table) : table_(table) {} |
| 34 | + |
| 35 | +TableScanBuilder& TableScanBuilder::WithColumnNames( |
| 36 | + const std::vector<std::string>& column_names) { |
| 37 | + column_names_ = column_names; |
| 38 | + return *this; |
| 39 | +} |
| 40 | + |
| 41 | +TableScanBuilder& TableScanBuilder::WithSnapshotId(int64_t snapshot_id) { |
| 42 | + snapshot_id_ = snapshot_id; |
| 43 | + return *this; |
| 44 | +} |
| 45 | + |
| 46 | +TableScanBuilder& TableScanBuilder::WithFilter( |
| 47 | + const std::shared_ptr<Expression>& filter) { |
| 48 | + filter_ = filter; |
| 49 | + return *this; |
| 50 | +} |
| 51 | + |
| 52 | +Result<std::unique_ptr<TableScan>> TableScanBuilder::Build() { |
| 53 | + ICEBERG_ASSIGN_OR_RAISE(auto snapshot, snapshot_id_ ? table_.snapshot(*snapshot_id_) |
| 54 | + : Result<std::shared_ptr<Snapshot>>( |
| 55 | + table_.current_snapshot())); |
| 56 | + |
| 57 | + auto ResolveSchema = [&]() -> Result<std::shared_ptr<Schema>> { |
| 58 | + if (snapshot->schema_id) { |
| 59 | + const auto& schemas = table_.schemas(); |
| 60 | + if (auto it = schemas.find(*snapshot->schema_id); it != schemas.end()) { |
| 61 | + return it->second; |
| 62 | + } |
| 63 | + return DataInvalid("Schema {} in snapshot {} is not found", *snapshot->schema_id, |
| 64 | + snapshot->snapshot_id); |
| 65 | + } |
| 66 | + return table_.schema(); |
| 67 | + }; |
| 68 | + |
| 69 | + ICEBERG_ASSIGN_OR_RAISE(auto schema, ResolveSchema()); |
| 70 | + |
| 71 | + std::vector<int32_t> field_ids; |
| 72 | + field_ids.reserve(column_names_.size()); |
| 73 | + for (const auto& column_name : column_names_) { |
| 74 | + auto field_opt = schema->GetFieldByName(column_name); |
| 75 | + if (!field_opt) { |
| 76 | + return InvalidArgument("Column {} not found in schema", column_name); |
| 77 | + } |
| 78 | + field_ids.emplace_back(field_opt.value().get().field_id()); |
| 79 | + } |
| 80 | + |
| 81 | + auto context = std::make_unique<TableScan::ScanContext>( |
| 82 | + std::move(snapshot), std::move(schema), std::move(field_ids), std::move(filter_)); |
| 83 | + return std::make_unique<TableScan>(std::move(context), table_.io()); |
| 84 | +} |
| 85 | + |
| 86 | +TableScan::TableScan(std::unique_ptr<ScanContext> context, |
| 87 | + std::shared_ptr<FileIO> file_io) |
| 88 | + : context_(std::move(context)), file_io_(std::move(file_io)) {} |
| 89 | + |
| 90 | +Result<std::vector<std::unique_ptr<FileScanTask>>> TableScan::PlanFiles() const { |
| 91 | + ICEBERG_ASSIGN_OR_RAISE(auto manifest_list_reader, |
| 92 | + CreateManifestListReader(context_->snapshot_->manifest_list)); |
| 93 | + ICEBERG_ASSIGN_OR_RAISE(auto manifest_files, manifest_list_reader->Files()); |
| 94 | + |
| 95 | + std::vector<std::unique_ptr<FileScanTask>> tasks; |
| 96 | + for (const auto& manifest_file : manifest_files) { |
| 97 | + ICEBERG_ASSIGN_OR_RAISE(auto manifest_reader, |
| 98 | + CreateManifestReader(manifest_file->manifest_path)); |
| 99 | + ICEBERG_ASSIGN_OR_RAISE(auto manifests, manifest_reader->Entries()); |
| 100 | + |
| 101 | + for (const auto& manifest : manifests) { |
| 102 | + const auto& data_file = manifest->data_file; |
| 103 | + tasks.emplace_back(std::make_unique<FileScanTask>( |
| 104 | + data_file.file_path, 0, data_file.file_size_in_bytes, data_file.record_count, |
| 105 | + data_file.content, data_file.file_format, context_->schema_, |
| 106 | + context_->field_ids_, context_->filter_)); |
| 107 | + } |
| 108 | + } |
| 109 | + return tasks; |
| 110 | +} |
| 111 | + |
| 112 | +Result<std::unique_ptr<ManifestListReader>> TableScan::CreateManifestListReader( |
| 113 | + const std::string& file_path) const { |
| 114 | + return NotImplemented("manifest list reader"); |
| 115 | +} |
| 116 | + |
| 117 | +Result<std::unique_ptr<ManifestReader>> TableScan::CreateManifestReader( |
| 118 | + const std::string& file_path) const { |
| 119 | + return NotImplemented("manifest reader"); |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace iceberg |
0 commit comments