-
Notifications
You must be signed in to change notification settings - Fork 52
feat: add read-optimized system table #403
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| .. Copyright 2026-present Alibaba Inc. | ||
|
|
||
| .. Licensed 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. | ||
|
|
||
| System Tables | ||
| ============= | ||
|
|
||
| Paimon C++ supports reading system tables by appending a system table suffix to | ||
| the data table path. For example, ``/warehouse/db.db/orders$snapshots`` reads | ||
| the snapshots system table for ``orders``. | ||
|
|
||
| Branch-qualified paths are also supported. For example, | ||
| ``/warehouse/db.db/orders$branch_audit$files`` reads the files system table from | ||
| the ``audit`` branch. | ||
|
|
||
| Read-Optimized System Table | ||
| --------------------------- | ||
|
|
||
| The read-optimized system table is addressed by the ``$ro`` suffix: | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| /warehouse/db.db/orders$ro | ||
|
|
||
| For primary-key tables, ``$ro`` only plans data files from the highest LSM | ||
| level, which is the level produced by full compaction. This avoids merging data | ||
| from multiple LSM levels during query planning and reading. The tradeoff is that | ||
| the result may lag behind the latest committed data until a full compaction | ||
| publishes the newest records into the highest level. | ||
|
|
||
| This stale view is not guaranteed to correspond to any single historical table | ||
| snapshot. Full compaction may finish independently for different buckets. When | ||
| ``$ro`` scans the latest snapshot, the selected highest-level files can | ||
| therefore combine bucket states produced by full-compaction commits at different | ||
| snapshot IDs. | ||
|
|
||
| For primary-key tables, ``$ro`` also enables value-stats filtering, so file-level | ||
| pruning of the reader predicate can be more aggressive than the base table. | ||
|
|
||
| For append-only tables, ``$ro`` has the same read behavior as the base table, | ||
| including streaming reads. | ||
|
|
||
| Limitations | ||
| ~~~~~~~~~~~ | ||
|
|
||
| - Primary-key ``$ro`` scans are batch-only. Streaming scans are not supported. | ||
| - Freshness depends on full compaction frequency, and the result may not match | ||
| any single historical snapshot across buckets. | ||
| - Primary-key tables in bucket-unaware mode are not supported by the current C++ | ||
| scan path. | ||
|
|
||
| Typical Usage | ||
| ~~~~~~~~~~~~~ | ||
|
|
||
| Use ``$ro`` for OLAP or batch workloads that can tolerate stale results and | ||
| prefer reading compacted files directly. Use the base table path when the query | ||
| must see the latest committed data. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
| * Licensed 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 | ||
|
|
||
| namespace paimon { | ||
|
|
||
| /// Internal scan option used by `T$ro` to request top-level-only planning. | ||
| inline constexpr char kReadOptimizedScanOption[] = "__paimon.internal.read-optimized"; | ||
|
|
||
| } // namespace paimon |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
| * Licensed 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 "paimon/core/table/system/read_optimized_system_table.h" | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "arrow/c/bridge.h" | ||
| #include "paimon/common/types/data_field.h" | ||
| #include "paimon/core/schema/table_schema.h" | ||
| #include "paimon/core/table/source/read_optimized_scan_options.h" | ||
| #include "paimon/defs.h" | ||
| #include "paimon/read_context.h" | ||
| #include "paimon/scan_context.h" | ||
| #include "paimon/table/source/table_read.h" | ||
| #include "paimon/table/source/table_scan.h" | ||
|
|
||
| namespace paimon { | ||
|
|
||
| ReadOptimizedSystemTable::ReadOptimizedSystemTable(std::string table_path, | ||
| std::shared_ptr<TableSchema> table_schema, | ||
| std::map<std::string, std::string> options) | ||
| : table_path_(std::move(table_path)), | ||
| table_schema_(std::move(table_schema)), | ||
| options_(std::move(options)) {} | ||
|
|
||
| std::string ReadOptimizedSystemTable::Name() const { | ||
| return kName; | ||
| } | ||
|
|
||
| Result<std::shared_ptr<arrow::Schema>> ReadOptimizedSystemTable::ArrowSchema() const { | ||
| return DataField::ConvertDataFieldsToArrowSchema(table_schema_->Fields()); | ||
| } | ||
|
|
||
| std::map<std::string, std::string> ReadOptimizedSystemTable::ReadOptimizedOptions() const { | ||
| auto options = options_; | ||
| options[kReadOptimizedScanOption] = "true"; | ||
| return options; | ||
| } | ||
|
|
||
| Result<std::unique_ptr<TableScan>> ReadOptimizedSystemTable::NewScan( | ||
| const std::shared_ptr<ScanContext>& context) const { | ||
| auto options = ReadOptimizedOptions(); | ||
| ScanContextBuilder builder(table_path_); | ||
| builder.SetOptions(options) | ||
| .WithStreamingMode(context->IsStreamingMode()) | ||
| .WithMemoryPool(context->GetMemoryPool()) | ||
| .WithExecutor(context->GetExecutor()) | ||
| .WithFileSystem(context->GetSpecificFileSystem()) | ||
| .WithCache(context->GetCache()); | ||
| if (context->GetLimit().has_value()) { | ||
| builder.SetLimit(context->GetLimit().value()); | ||
| } | ||
| if (context->GetScanFilters()) { | ||
| if (context->GetScanFilters()->GetBucketFilter().has_value()) { | ||
| builder.SetBucketFilter(context->GetScanFilters()->GetBucketFilter().value()); | ||
| } | ||
| builder.SetPartitionFilter(context->GetScanFilters()->GetPartitionFilters()); | ||
| builder.SetPredicate(context->GetScanFilters()->GetPredicate()); | ||
| } | ||
| if (context->GetGlobalIndexResult()) { | ||
| builder.SetGlobalIndexResult(context->GetGlobalIndexResult()); | ||
| } | ||
| if (context->GetSpecificTableSchema().has_value()) { | ||
| builder.SetTableSchema(context->GetSpecificTableSchema().value()); | ||
| } | ||
| PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<ScanContext> base_context, builder.Finish()); | ||
| return TableScan::Create(std::move(base_context)); | ||
| } | ||
|
|
||
| Result<std::unique_ptr<TableRead>> ReadOptimizedSystemTable::NewRead( | ||
| const std::shared_ptr<ReadContext>& context) const { | ||
| auto options = options_; | ||
| std::string branch = context->GetBranch(); | ||
| // SystemTableLoader injects Options::BRANCH when parsing paths such as `T$branch_dev$ro`. | ||
| auto branch_iter = options.find(Options::BRANCH); | ||
| if (branch_iter != options.end()) { | ||
| branch = branch_iter->second; | ||
| } | ||
| ReadContextBuilder builder(table_path_); | ||
| builder.SetOptions(options) | ||
| .WithBranch(branch) | ||
| .SetPredicate(context->GetPredicate()) | ||
| .EnablePredicateFilter(context->EnablePredicateFilter()) | ||
| .EnablePrefetch(context->EnablePrefetch()) | ||
| .SetPrefetchBatchCount(context->GetPrefetchBatchCount()) | ||
| .SetPrefetchMaxParallelNum(context->GetPrefetchMaxParallelNum()) | ||
| .EnableMultiThreadRowToBatch(context->EnableMultiThreadRowToBatch()) | ||
| .SetRowToBatchThreadNumber(context->GetRowToBatchThreadNumber()) | ||
| .WithMemoryPool(context->GetMemoryPool()) | ||
| .WithExecutor(context->GetExecutor()) | ||
| .WithFileSystem(context->GetSpecificFileSystem()) | ||
| .WithFileSystemSchemeToIdentifierMap(context->GetFileSystemSchemeToIdentifierMap()) | ||
| .SetPrefetchCacheMode(context->GetPrefetchCacheMode()) | ||
| .WithCacheConfig(context->GetCacheConfig()) | ||
| .WithCache(context->GetCache()) | ||
| .SetReadFieldNames(context->GetReadFieldNames()) | ||
| .SetReadFieldIds(context->GetReadFieldIds()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we preserve the projected Arrow schema from |
||
| if (context->HasReadSchema()) { | ||
| PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::Schema> read_schema, | ||
| arrow::ImportSchema(context->GetReadSchema())); | ||
| auto c_read_schema = std::make_unique<::ArrowSchema>(); | ||
| PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*read_schema, c_read_schema.get())); | ||
| builder.SetReadSchema(std::move(c_read_schema)); | ||
| } | ||
| if (context->GetSpecificTableSchema().has_value()) { | ||
| builder.SetTableSchema(context->GetSpecificTableSchema().value()); | ||
| } | ||
| PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<ReadContext> base_context, builder.Finish()); | ||
| return TableRead::Create(std::move(base_context)); | ||
| } | ||
|
|
||
| } // namespace paimon | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
| * Licensed 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 | ||
|
|
||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "paimon/core/table/system/system_table.h" | ||
|
|
||
| namespace paimon { | ||
| class TableSchema; | ||
|
|
||
| /// System table for `T$ro`, exposing read-optimized data. | ||
| class ReadOptimizedSystemTable : public SystemTable { | ||
| public: | ||
| static constexpr const char* kName = "ro"; | ||
|
|
||
| ReadOptimizedSystemTable(std::string table_path, std::shared_ptr<TableSchema> table_schema, | ||
| std::map<std::string, std::string> options); | ||
|
|
||
| std::string Name() const override; | ||
| Result<std::shared_ptr<arrow::Schema>> ArrowSchema() const override; | ||
| Result<std::unique_ptr<TableScan>> NewScan( | ||
| const std::shared_ptr<ScanContext>& context) const override; | ||
| Result<std::unique_ptr<TableRead>> NewRead( | ||
| const std::shared_ptr<ReadContext>& context) const override; | ||
|
|
||
| private: | ||
| std::map<std::string, std::string> ReadOptimizedOptions() const; | ||
|
|
||
| std::string table_path_; | ||
| std::shared_ptr<TableSchema> table_schema_; | ||
| std::map<std::string, std::string> options_; | ||
| }; | ||
|
|
||
| } // namespace paimon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we document that
$rodoes not necessarily represent one coherent older snapshot? Different buckets may complete full compaction at different times, so the top-level files selected from the latest snapshot can contain bucket states produced at different snapshots. The current “may lag” wording does not make this consistency caveat clear.