|
| 1 | +/* |
| 2 | + * Copyright 2026-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "paimon/core/table/system/read_optimized_system_table.h" |
| 18 | + |
| 19 | +#include <memory> |
| 20 | +#include <string> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +#include "paimon/common/types/data_field.h" |
| 24 | +#include "paimon/core/schema/table_schema.h" |
| 25 | +#include "paimon/core/table/source/read_optimized_scan_options.h" |
| 26 | +#include "paimon/defs.h" |
| 27 | +#include "paimon/read_context.h" |
| 28 | +#include "paimon/scan_context.h" |
| 29 | +#include "paimon/table/source/table_read.h" |
| 30 | +#include "paimon/table/source/table_scan.h" |
| 31 | + |
| 32 | +namespace paimon { |
| 33 | + |
| 34 | +ReadOptimizedSystemTable::ReadOptimizedSystemTable(std::string table_path, |
| 35 | + std::shared_ptr<TableSchema> table_schema, |
| 36 | + std::map<std::string, std::string> options) |
| 37 | + : table_path_(std::move(table_path)), |
| 38 | + table_schema_(std::move(table_schema)), |
| 39 | + options_(std::move(options)) {} |
| 40 | + |
| 41 | +std::string ReadOptimizedSystemTable::Name() const { |
| 42 | + return kName; |
| 43 | +} |
| 44 | + |
| 45 | +Result<std::shared_ptr<arrow::Schema>> ReadOptimizedSystemTable::ArrowSchema() const { |
| 46 | + return DataField::ConvertDataFieldsToArrowSchema(table_schema_->Fields()); |
| 47 | +} |
| 48 | + |
| 49 | +std::map<std::string, std::string> ReadOptimizedSystemTable::ReadOptimizedOptions() const { |
| 50 | + auto options = options_; |
| 51 | + options[kReadOptimizedScanOption] = "true"; |
| 52 | + return options; |
| 53 | +} |
| 54 | + |
| 55 | +Result<std::unique_ptr<TableScan>> ReadOptimizedSystemTable::NewScan( |
| 56 | + const std::shared_ptr<ScanContext>& context) const { |
| 57 | + auto options = ReadOptimizedOptions(); |
| 58 | + ScanContextBuilder builder(table_path_); |
| 59 | + builder.SetOptions(options) |
| 60 | + .WithStreamingMode(context->IsStreamingMode()) |
| 61 | + .WithMemoryPool(context->GetMemoryPool()) |
| 62 | + .WithExecutor(context->GetExecutor()) |
| 63 | + .WithFileSystem(context->GetSpecificFileSystem()) |
| 64 | + .WithCache(context->GetCache()); |
| 65 | + if (context->GetLimit().has_value()) { |
| 66 | + builder.SetLimit(context->GetLimit().value()); |
| 67 | + } |
| 68 | + if (context->GetScanFilters()) { |
| 69 | + if (context->GetScanFilters()->GetBucketFilter().has_value()) { |
| 70 | + builder.SetBucketFilter(context->GetScanFilters()->GetBucketFilter().value()); |
| 71 | + } |
| 72 | + builder.SetPartitionFilter(context->GetScanFilters()->GetPartitionFilters()); |
| 73 | + builder.SetPredicate(context->GetScanFilters()->GetPredicate()); |
| 74 | + } |
| 75 | + if (context->GetGlobalIndexResult()) { |
| 76 | + builder.SetGlobalIndexResult(context->GetGlobalIndexResult()); |
| 77 | + } |
| 78 | + if (context->GetSpecificTableSchema().has_value()) { |
| 79 | + builder.SetTableSchema(context->GetSpecificTableSchema().value()); |
| 80 | + } |
| 81 | + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<ScanContext> base_context, builder.Finish()); |
| 82 | + return TableScan::Create(std::move(base_context)); |
| 83 | +} |
| 84 | + |
| 85 | +Result<std::unique_ptr<TableRead>> ReadOptimizedSystemTable::NewRead( |
| 86 | + const std::shared_ptr<ReadContext>& context) const { |
| 87 | + auto options = options_; |
| 88 | + std::string branch = context->GetBranch(); |
| 89 | + // SystemTableLoader injects Options::BRANCH when parsing paths such as `T$branch_dev$ro`. |
| 90 | + auto branch_iter = options.find(Options::BRANCH); |
| 91 | + if (branch_iter != options.end()) { |
| 92 | + branch = branch_iter->second; |
| 93 | + } |
| 94 | + ReadContextBuilder builder(table_path_); |
| 95 | + builder.SetOptions(options) |
| 96 | + .WithBranch(branch) |
| 97 | + .SetPredicate(context->GetPredicate()) |
| 98 | + .EnablePredicateFilter(context->EnablePredicateFilter()) |
| 99 | + .EnablePrefetch(context->EnablePrefetch()) |
| 100 | + .SetPrefetchBatchCount(context->GetPrefetchBatchCount()) |
| 101 | + .SetPrefetchMaxParallelNum(context->GetPrefetchMaxParallelNum()) |
| 102 | + .EnableMultiThreadRowToBatch(context->EnableMultiThreadRowToBatch()) |
| 103 | + .SetRowToBatchThreadNumber(context->GetRowToBatchThreadNumber()) |
| 104 | + .WithMemoryPool(context->GetMemoryPool()) |
| 105 | + .WithExecutor(context->GetExecutor()) |
| 106 | + .WithFileSystem(context->GetSpecificFileSystem()) |
| 107 | + .WithFileSystemSchemeToIdentifierMap(context->GetFileSystemSchemeToIdentifierMap()) |
| 108 | + .SetPrefetchCacheMode(context->GetPrefetchCacheMode()) |
| 109 | + .WithCacheConfig(context->GetCacheConfig()) |
| 110 | + .WithCache(context->GetCache()); |
| 111 | + if (!context->GetReadFieldIds().empty()) { |
| 112 | + builder.SetReadFieldIds(context->GetReadFieldIds()); |
| 113 | + } else { |
| 114 | + builder.SetReadFieldNames(context->GetReadFieldNames()); |
| 115 | + } |
| 116 | + if (context->GetSpecificTableSchema().has_value()) { |
| 117 | + builder.SetTableSchema(context->GetSpecificTableSchema().value()); |
| 118 | + } |
| 119 | + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<ReadContext> base_context, builder.Finish()); |
| 120 | + return TableRead::Create(std::move(base_context)); |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace paimon |
0 commit comments