|
| 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/common/data/shredding/lru_map_shared_shredding_column_allocator.h" |
| 18 | + |
| 19 | +#include <set> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#include "gtest/gtest.h" |
| 23 | + |
| 24 | +namespace paimon::test { |
| 25 | +namespace { |
| 26 | + |
| 27 | +void ExpectAllocation(const RowAllocation& allocation, const std::vector<int32_t>& col_to_field, |
| 28 | + const std::vector<int32_t>& overflow_fields) { |
| 29 | + ASSERT_EQ(col_to_field, allocation.col_to_field); |
| 30 | + ASSERT_EQ(overflow_fields, allocation.overflow_fields); |
| 31 | +} |
| 32 | + |
| 33 | +} // namespace |
| 34 | + |
| 35 | +TEST(LruMapSharedShreddingColumnAllocatorTest, AllocatesWithHitRetainEvictAndOverflow) { |
| 36 | + LruMapSharedShreddingColumnAllocator allocator(3); |
| 37 | + |
| 38 | + RowAllocation row0 = allocator.AllocateRow({0, 1, 2}); |
| 39 | + ExpectAllocation(row0, {0, 1, 2}, {}); |
| 40 | + |
| 41 | + RowAllocation row1 = allocator.AllocateRow({0, 1}); |
| 42 | + ExpectAllocation(row1, {0, 1, -1}, {}); |
| 43 | + |
| 44 | + RowAllocation row2 = allocator.AllocateRow({3, 4, 5}); |
| 45 | + ExpectAllocation(row2, {4, 5, 3}, {}); |
| 46 | + |
| 47 | + RowAllocation row3 = allocator.AllocateRow({0, 3, 4, 5}); |
| 48 | + ExpectAllocation(row3, {4, 5, 3}, {0}); |
| 49 | + |
| 50 | + ASSERT_EQ(4, allocator.GetMaxRowWidth()); |
| 51 | + |
| 52 | + const auto& field_to_columns = allocator.GetFieldToColumns(); |
| 53 | + ASSERT_EQ((std::set<int32_t>{0}), field_to_columns.at(0)); |
| 54 | + ASSERT_EQ((std::set<int32_t>{1}), field_to_columns.at(1)); |
| 55 | + ASSERT_EQ((std::set<int32_t>{2}), field_to_columns.at(2)); |
| 56 | + ASSERT_EQ((std::set<int32_t>{2}), field_to_columns.at(3)); |
| 57 | + ASSERT_EQ((std::set<int32_t>{0}), field_to_columns.at(4)); |
| 58 | + ASSERT_EQ((std::set<int32_t>{1}), field_to_columns.at(5)); |
| 59 | + ASSERT_EQ((std::set<int32_t>{0}), allocator.GetOverflowFieldSet()); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(LruMapSharedShreddingColumnAllocatorTest, HandlesEmptyRows) { |
| 63 | + LruMapSharedShreddingColumnAllocator allocator(2); |
| 64 | + |
| 65 | + RowAllocation empty_row = allocator.AllocateRow({}); |
| 66 | + ExpectAllocation(empty_row, {-1, -1}, {}); |
| 67 | + ASSERT_EQ(0, allocator.GetMaxRowWidth()); |
| 68 | + |
| 69 | + RowAllocation row = allocator.AllocateRow({7}); |
| 70 | + ExpectAllocation(row, {7, -1}, {}); |
| 71 | + ASSERT_EQ(1, allocator.GetMaxRowWidth()); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace paimon::test |
0 commit comments