|
19 | 19 | #include <utility> |
20 | 20 |
|
21 | 21 | #include "gtest/gtest.h" |
| 22 | +#include "paimon/global_index/bitmap_global_index_result.h" |
| 23 | +#include "paimon/global_index/bitmap_topk_global_index_result.h" |
22 | 24 | #include "paimon/testing/utils/testharness.h" |
23 | 25 |
|
24 | 26 | namespace paimon::test { |
@@ -75,4 +77,60 @@ TEST_F(GlobalIndexResultTest, TestSimple) { |
75 | 77 | ASSERT_OK_AND_ASSIGN(auto or_result, result1->Or(result2)); |
76 | 78 | ASSERT_EQ(or_result->ToString(), "{1,3,4,5,100,200}"); |
77 | 79 | } |
| 80 | + |
| 81 | +TEST_F(GlobalIndexResultTest, TestSerializeAndDeserializeSimple) { |
| 82 | + auto pool = GetDefaultPool(); |
| 83 | + std::vector<uint8_t> byte_buffer = { |
| 84 | + 0, 0, 0, 1, 0, 0, 0, 69, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, |
| 85 | + 48, 3, 0, 5, 0, 0, 5, 0, 255, 127, 0, 0, 0, 128, 2, 0, 245, 133, 0, 0, 37, |
| 86 | + 0, 0, 0, 47, 0, 0, 0, 49, 0, 0, 0, 55, 0, 0, 0, 2, 0, 1, 0, 4, 0, |
| 87 | + 10, 0, 0, 0, 255, 255, 1, 0, 0, 0, 2, 0, 255, 224, 0, 0, 0, 0}; |
| 88 | + ASSERT_OK_AND_ASSIGN( |
| 89 | + std::shared_ptr<GlobalIndexResult> index_result, |
| 90 | + GlobalIndexResult::Deserialize((char*)byte_buffer.data(), byte_buffer.size(), pool)); |
| 91 | + auto typed_result = std::dynamic_pointer_cast<BitmapGlobalIndexResult>(index_result); |
| 92 | + ASSERT_TRUE(typed_result); |
| 93 | + |
| 94 | + auto bitmap = RoaringBitmap64::From( |
| 95 | + {1l, 2l, 3l, 4l, 5l, 10l, 2247483647l, 2147483647l, 2147483648l, 2147483649l, 2147483650l}); |
| 96 | + auto expected_result = std::make_shared<BitmapGlobalIndexResult>( |
| 97 | + [bitmap]() -> Result<RoaringBitmap64> { return bitmap; }); |
| 98 | + ASSERT_EQ(expected_result->ToString(), typed_result->ToString()); |
| 99 | + ASSERT_OK_AND_ASSIGN(auto serialize_bytes, GlobalIndexResult::Serialize(index_result, pool)); |
| 100 | + ASSERT_EQ(byte_buffer, std::vector<uint8_t>(serialize_bytes->data(), |
| 101 | + serialize_bytes->data() + serialize_bytes->size())); |
| 102 | +} |
| 103 | + |
| 104 | +TEST_F(GlobalIndexResultTest, TestSerializeAndDeserializeWithScore) { |
| 105 | + auto pool = GetDefaultPool(); |
| 106 | + std::vector<uint8_t> byte_buffer = { |
| 107 | + 0, 0, 0, 1, 0, 0, 0, 64, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 108 | + 58, 48, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 255, 127, 0, 0, 0, 128, 2, 0, |
| 109 | + 245, 133, 0, 0, 40, 0, 0, 0, 42, 0, 0, 0, 44, 0, 0, 0, 50, 0, 0, 0, |
| 110 | + 10, 0, 255, 255, 1, 0, 3, 0, 5, 0, 255, 224, 0, 0, 0, 6, 63, 129, 71, 174, |
| 111 | + 191, 168, 245, 195, 64, 135, 92, 41, 66, 74, 245, 195, 194, 200, 128, 0, 64, 6, 102, 102}; |
| 112 | + ASSERT_OK_AND_ASSIGN( |
| 113 | + std::shared_ptr<GlobalIndexResult> index_result, |
| 114 | + GlobalIndexResult::Deserialize((char*)byte_buffer.data(), byte_buffer.size(), pool)); |
| 115 | + auto typed_result = std::dynamic_pointer_cast<BitmapTopKGlobalIndexResult>(index_result); |
| 116 | + ASSERT_TRUE(typed_result); |
| 117 | + |
| 118 | + auto bitmap = RoaringBitmap64::From( |
| 119 | + {10l, 2147483647l, 2147483649l, 2147483651l, 2147483653l, 2247483647l}); |
| 120 | + std::vector<float> scores = {1.01f, -1.32f, 4.23f, 50.74f, -100.25f, 2.10f}; |
| 121 | + auto expected_result = |
| 122 | + std::make_shared<BitmapTopKGlobalIndexResult>(std::move(bitmap), std::move(scores)); |
| 123 | + ASSERT_EQ(expected_result->ToString(), typed_result->ToString()); |
| 124 | + ASSERT_OK_AND_ASSIGN(auto serialize_bytes, GlobalIndexResult::Serialize(index_result, pool)); |
| 125 | + ASSERT_EQ(byte_buffer, std::vector<uint8_t>(serialize_bytes->data(), |
| 126 | + serialize_bytes->data() + serialize_bytes->size())); |
| 127 | +} |
| 128 | + |
| 129 | +TEST_F(GlobalIndexResultTest, TestInvalidSerialize) { |
| 130 | + auto pool = GetDefaultPool(); |
| 131 | + auto result = std::make_shared<FakeGlobalIndexResult>(std::vector<int64_t>({1, 3, 5, 100})); |
| 132 | + ASSERT_NOK_WITH_MSG(GlobalIndexResult::Serialize(result, pool), |
| 133 | + "invalid GlobalIndexResult, must be BitmapGlobalIndexResult or " |
| 134 | + "BitmapTopkGlobalIndexResult"); |
| 135 | +} |
78 | 136 | } // namespace paimon::test |
0 commit comments