-
Notifications
You must be signed in to change notification settings - Fork 52
feat(shredding): add shared-shredding map placement policies #384
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
Open
zjw1111
wants to merge
2
commits into
alibaba:main
Choose a base branch
from
zjw1111:codex/extend-map-column-allocator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/paimon/common/data/shredding/lru_map_shared_shredding_column_allocator.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * 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/common/data/shredding/lru_map_shared_shredding_column_allocator.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <limits> | ||
|
|
||
| namespace paimon { | ||
|
|
||
| LruMapSharedShreddingColumnAllocator::LruMapSharedShreddingColumnAllocator(int32_t num_columns) | ||
| : MapSharedShreddingColumnAllocator(num_columns), | ||
| col_field_(num_columns, -1), | ||
| last_used_(num_columns, 0) {} | ||
|
|
||
| int32_t LruMapSharedShreddingColumnAllocator::SelectColumn( | ||
| const std::vector<int32_t>& candidates, | ||
| const std::vector<int32_t>& planned_col_to_field) const { | ||
| int32_t selected_col = candidates.front(); | ||
| int64_t selected_last_used = std::numeric_limits<int64_t>::max(); | ||
| bool selected_empty = false; | ||
| for (int32_t col : candidates) { | ||
| bool is_empty = planned_col_to_field[col] == -1; | ||
| if (is_empty) { | ||
| if (!selected_empty || col < selected_col) { | ||
| selected_empty = true; | ||
| selected_col = col; | ||
| } | ||
| continue; | ||
| } | ||
| if (!selected_empty && (last_used_[col] < selected_last_used || | ||
| (last_used_[col] == selected_last_used && col < selected_col))) { | ||
| selected_col = col; | ||
| selected_last_used = last_used_[col]; | ||
| } | ||
|
Collaborator
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. Please add comments for the strategy. |
||
| } | ||
| return selected_col; | ||
| } | ||
|
|
||
| void LruMapSharedShreddingColumnAllocator::UpdateLastUsed(const RowAllocation& allocation) { | ||
| bool touched = false; | ||
| for (int32_t col = 0; col < num_columns_; ++col) { | ||
| if (allocation.col_to_field[col] != -1) { | ||
| last_used_[col] = lru_clock_; | ||
| touched = true; | ||
| } | ||
| } | ||
| if (touched) { | ||
| ++lru_clock_; | ||
| } | ||
| } | ||
|
|
||
| RowAllocation LruMapSharedShreddingColumnAllocator::AllocateRow( | ||
| const std::vector<int32_t>& field_ids) { | ||
| std::vector<int32_t> sorted_field_ids = field_ids; | ||
| std::sort(sorted_field_ids.begin(), sorted_field_ids.end()); | ||
|
|
||
| RowAllocation allocation; | ||
| allocation.col_to_field.assign(num_columns_, -1); | ||
| std::vector<int32_t> next_col_to_field = col_field_; | ||
| std::vector<bool> used_cols(num_columns_, false); | ||
| std::vector<int32_t> unassigned; | ||
|
|
||
| for (int32_t field_id : sorted_field_ids) { | ||
| auto it = std::find(col_field_.begin(), col_field_.end(), field_id); | ||
| if (it != col_field_.end()) { | ||
| int32_t col = static_cast<int32_t>(it - col_field_.begin()); | ||
| used_cols[col] = true; | ||
| allocation.col_to_field[col] = field_id; | ||
| } else { | ||
| unassigned.push_back(field_id); | ||
| } | ||
| } | ||
|
|
||
| for (int32_t field_id : unassigned) { | ||
| std::vector<int32_t> candidates; | ||
| candidates.reserve(num_columns_); | ||
| for (int32_t col = 0; col < num_columns_; ++col) { | ||
| if (!used_cols[col]) { | ||
| candidates.push_back(col); | ||
| } | ||
| } | ||
|
|
||
| if (candidates.empty()) { | ||
| allocation.overflow_fields.push_back(field_id); | ||
| continue; | ||
| } | ||
|
|
||
| int32_t col = SelectColumn(candidates, next_col_to_field); | ||
| used_cols[col] = true; | ||
| allocation.col_to_field[col] = field_id; | ||
| next_col_to_field[col] = field_id; | ||
| } | ||
|
|
||
| UpdateLastUsed(allocation); | ||
| col_field_ = next_col_to_field; | ||
| CommitRow(allocation, sorted_field_ids); | ||
| return allocation; | ||
| } | ||
|
|
||
| } // namespace paimon | ||
44 changes: 44 additions & 0 deletions
44
src/paimon/common/data/shredding/lru_map_shared_shredding_column_allocator.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * 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 <cstdint> | ||
| #include <vector> | ||
|
|
||
| #include "paimon/common/data/shredding/map_shared_shredding_column_allocator.h" | ||
|
|
||
| namespace paimon { | ||
|
|
||
| /// Allocator that keeps cross-row column state and evicts least-recently-used columns. | ||
| class LruMapSharedShreddingColumnAllocator : public MapSharedShreddingColumnAllocator { | ||
| public: | ||
| /// @param num_columns Number of available physical columns. | ||
| explicit LruMapSharedShreddingColumnAllocator(int32_t num_columns); | ||
|
|
||
| RowAllocation AllocateRow(const std::vector<int32_t>& field_ids) override; | ||
|
|
||
| private: | ||
| int32_t SelectColumn(const std::vector<int32_t>& candidates, | ||
| const std::vector<int32_t>& planned_col_to_field) const; | ||
| void UpdateLastUsed(const RowAllocation& allocation); | ||
|
|
||
| int64_t lru_clock_ = 0; | ||
| std::vector<int32_t> col_field_; | ||
| std::vector<int64_t> last_used_; | ||
| }; | ||
|
|
||
| } // namespace paimon |
74 changes: 74 additions & 0 deletions
74
src/paimon/common/data/shredding/lru_map_shared_shredding_column_allocator_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * 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/common/data/shredding/lru_map_shared_shredding_column_allocator.h" | ||
|
|
||
| #include <set> | ||
| #include <vector> | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace paimon::test { | ||
| namespace { | ||
|
|
||
| void ExpectAllocation(const RowAllocation& allocation, const std::vector<int32_t>& col_to_field, | ||
| const std::vector<int32_t>& overflow_fields) { | ||
| ASSERT_EQ(col_to_field, allocation.col_to_field); | ||
| ASSERT_EQ(overflow_fields, allocation.overflow_fields); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(LruMapSharedShreddingColumnAllocatorTest, AllocatesWithHitRetainEvictAndOverflow) { | ||
| LruMapSharedShreddingColumnAllocator allocator(3); | ||
|
|
||
| RowAllocation row0 = allocator.AllocateRow({0, 1, 2}); | ||
| ExpectAllocation(row0, {0, 1, 2}, {}); | ||
|
|
||
| RowAllocation row1 = allocator.AllocateRow({0, 1}); | ||
| ExpectAllocation(row1, {0, 1, -1}, {}); | ||
|
|
||
| RowAllocation row2 = allocator.AllocateRow({3, 4, 5}); | ||
| ExpectAllocation(row2, {4, 5, 3}, {}); | ||
|
|
||
| RowAllocation row3 = allocator.AllocateRow({0, 3, 4, 5}); | ||
| ExpectAllocation(row3, {4, 5, 3}, {0}); | ||
|
|
||
| ASSERT_EQ(4, allocator.GetMaxRowWidth()); | ||
|
|
||
| const auto& field_to_columns = allocator.GetFieldToColumns(); | ||
| ASSERT_EQ((std::set<int32_t>{0}), field_to_columns.at(0)); | ||
| ASSERT_EQ((std::set<int32_t>{1}), field_to_columns.at(1)); | ||
| ASSERT_EQ((std::set<int32_t>{2}), field_to_columns.at(2)); | ||
| ASSERT_EQ((std::set<int32_t>{2}), field_to_columns.at(3)); | ||
| ASSERT_EQ((std::set<int32_t>{0}), field_to_columns.at(4)); | ||
| ASSERT_EQ((std::set<int32_t>{1}), field_to_columns.at(5)); | ||
| ASSERT_EQ((std::set<int32_t>{0}), allocator.GetOverflowFieldSet()); | ||
| } | ||
|
|
||
| TEST(LruMapSharedShreddingColumnAllocatorTest, HandlesEmptyRows) { | ||
| LruMapSharedShreddingColumnAllocator allocator(2); | ||
|
|
||
| RowAllocation empty_row = allocator.AllocateRow({}); | ||
| ExpectAllocation(empty_row, {-1, -1}, {}); | ||
| ASSERT_EQ(0, allocator.GetMaxRowWidth()); | ||
|
|
||
| RowAllocation row = allocator.AllocateRow({7}); | ||
| ExpectAllocation(row, {7, -1}, {}); | ||
| ASSERT_EQ(1, allocator.GetMaxRowWidth()); | ||
| } | ||
|
|
||
| } // namespace paimon::test |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I’m not quite following the purpose of the defensive check
col < selected_colhere. At the current call site, candidates is constructed in ascending column order, so could we just return as soon as we find a suitable empty one?