hello-stephen commented on code in PR #70774:
URL: https://github.com/apache/doris/pull/70774#discussion_r4795009613


##########
be/test/core/value/bitmap_value_test.cpp:
##########
@@ -758,6 +767,170 @@ TEST(BitmapValueTest, write_read) {
     config::enable_set_in_bitmap_value = old_enable_set;
 }
 
+TEST(BitmapValueTest, serialize_current_representation_and_optimize_on_demand) 
{
+    BitmapValue bitmap;
+    for (uint64_t i = 0; i < 1024; ++i) {
+        bitmap.add(i);
+        bitmap.add((uint64_t {1} << 40) + i);
+    }
+    const auto* backing = bitmap._bitmap.get();
+    auto optimized = *backing;
+    optimized.runOptimize();
+    optimized.shrinkToFit();
+
+    auto serialize_with_write_to = [](const BitmapValue& value, std::string& 
dst) {
+        dst.resize(value.getSizeInBytes());
+        const size_t written = value.write_to(dst.data());
+        EXPECT_LE(written, dst.size());
+        dst.resize(written);
+    };
+
+    const auto old_version = config::bitmap_serialize_version;
+    for (int16_t version : {int16_t {1}, int16_t {2}}) {
+        config::bitmap_serialize_version = version;
+        const auto source_size = backing->getSizeInBytes(version);
+        std::string expected(optimized.getSizeInBytes(version), '\0');
+        optimized.write(expected.data(), version);
+        EXPECT_LT(expected.size(), source_size);
+
+        std::string bytes;
+        serialize_with_write_to(bitmap, bytes);
+        EXPECT_EQ(bytes, expected);
+        bitmap.write(bytes);
+        EXPECT_EQ(bytes, expected);
+        EXPECT_EQ(bitmap.getSizeInBytes(), source_size);
+        EXPECT_EQ(bitmap._bitmap.get(), backing);
+        std::string buffer(bitmap.getSizeInBytes(), '\0');
+        EXPECT_EQ(bitmap.write_to(buffer.data()), expected.size());
+        buffer.resize(expected.size());
+        EXPECT_EQ(buffer, expected);
+        EXPECT_EQ(bitmap._bitmap.get(), backing);
+
+        BitmapValue alias(bitmap);
+        alias.optimize();
+        EXPECT_NE(alias._bitmap.get(), backing);
+        serialize_with_write_to(alias, bytes);
+        EXPECT_EQ(bytes, expected);
+        EXPECT_EQ(alias.getSizeInBytes(), expected.size());
+        BitmapValue shared(alias);
+        EXPECT_EQ(shared._bitmap.get(), alias._bitmap.get());
+
+        shared.add(2048);
+        EXPECT_NE(shared._bitmap.get(), alias._bitmap.get());
+        EXPECT_FALSE(alias.contains(2048));
+        shared.optimize();
+
+        EXPECT_EQ(shared.deserialize_with_size(expected.data()), 
expected.size());
+        EXPECT_EQ(shared.getSizeInBytes(), expected.size());
+        serialize_with_write_to(bitmap, bytes);
+        EXPECT_EQ(bytes, expected);
+
+        BitmapValue solo;
+        for (uint64_t i = 0; i < 1024; ++i) {
+            solo.add(i);
+            solo.add((uint64_t {1} << 40) + i);
+        }
+        const auto* solo_backing = solo._bitmap.get();
+        ASSERT_EQ(solo._bitmap.use_count(), 1);
+        EXPECT_EQ(solo.getSizeInBytes(), source_size);
+        solo.optimize();
+        EXPECT_EQ(solo._bitmap.get(), solo_backing);
+        EXPECT_LT(solo.getSizeInBytes(), source_size);
+        EXPECT_EQ(solo.getSizeInBytes(), expected.size());
+    }
+    config::bitmap_serialize_version = old_version;
+}
+
+TEST(BitmapValueTest, copy_and_mutate_optimized_bitmap) {
+    BitmapValue source;
+    for (uint64_t i = 0; i < 1024; ++i) {
+        source.add(i);
+    }
+    source.optimize();
+    const auto* backing = source._bitmap.get();
+    BitmapValue copied(source), assigned, unioned, xored;
+    assigned = source;
+    unioned |= source;
+    xored ^= source;
+    BitmapValue moved(std::move(copied));
+    copied = std::move(assigned);
+    for (auto* value : {&moved, &copied, &unioned, &xored}) {
+        value->optimize();
+        EXPECT_NE(value->_bitmap.get(), backing);
+    }
+    moved.remove(0);
+    copied |= source;
+    unioned &= source;
+    xored ^= BitmapValue(uint64_t {0});
+    for (const auto* value : {&moved, &copied, &unioned, &xored}) {
+        EXPECT_NE(value->_bitmap.get(), backing);
+    }
+    EXPECT_EQ(source._bitmap.get(), backing);
+    EXPECT_EQ(source.cardinality(), 1024);
+}
+
+TEST(BitmapValueTest, roaring_copy_on_write) {
+    BitmapValue source;
+    for (uint64_t i = 0; i < 64; ++i) {
+        source.add(i);
+    }
+
+    const auto* backing = source._bitmap.get();
+    ASSERT_NE(backing, nullptr);
+    BitmapValue copied(source);
+    BitmapValue assigned;
+    BitmapValue unioned;
+    BitmapValue xored;
+    assigned = source;
+    unioned |= source;
+    xored ^= source;
+
+    for (auto* alias : {&copied, &assigned, &unioned, &xored}) {
+        EXPECT_EQ(alias->_bitmap.get(), backing);
+        alias->remove(0);
+        EXPECT_NE(alias->_bitmap.get(), backing);
+        EXPECT_FALSE(alias->contains(0));
+        EXPECT_TRUE(source.contains(0));
+    }
+
+    source.add(64);
+    EXPECT_EQ(source._bitmap.get(), backing);
+    EXPECT_TRUE(source.contains(64));
+    EXPECT_FALSE(copied.contains(64));
+}
+
+TEST(BitmapValueTest, intersect_count_decodes_consecutive_bitmaps) {
+    detail::Roaring64Map first;
+    for (uint32_t i = 0; i < 1024; ++i) {
+        first.add(i);
+    }
+    // Include a sparse high group as well as the unoptimized low group.
+    first.add(uint64_t {1} << 40);
+    const BitmapValue second(uint64_t {42});
+    auto check = [&]<typename Key>(const Key& a, const Key& b) {
+        using StoredKey =
+                std::conditional_t<std::is_same_v<Key, std::string>, 
std::string_view, Key>;
+        for (int version : {1, 2}) {
+            std::string state(sizeof(int32_t) + 
detail::Helper::serialize_size(a) +
+                                      first.getSizeInBytes(version) +
+                                      detail::Helper::serialize_size(b) + 
second.getSizeInBytes(),
+                              '\0');
+            char* writer = detail::Helper::write_to(int32_t {2}, state.data());
+            writer = detail::Helper::write_to(a, writer);

Review Comment:
   [P3] Exercise the intersect_count state writer
   
   This test hand-writes the keys and bitmap payloads, so neither 
specialization calls the changed `BitmapIntersect::serialize()` path. A 
regression that leaves padding before the next key could pass. Add a 
serialize-to-deserialize round trip for numeric and string keys with two 
compressible bitmap entries so the test checks advancement after a shortened 
payload regardless of key iteration order.



##########
be/src/core/data_type/data_type_bitmap.cpp:
##########
@@ -61,17 +61,12 @@ char* DataTypeBitMap::serialize(const IColumn& column, 
char* buf, int be_exec_ve
     const auto& data_column = assert_cast<const ColumnBitmap&>(*bitmap_column);
     // serialize the bitmap size array
     auto* meta_ptr = reinterpret_cast<size_t*>(buf);
-    for (size_t i = 0; i < real_need_copy_num; ++i) {
-        auto& bitmap = data_column.get_element(i);
-        unaligned_store<size_t>(&meta_ptr[i], bitmap.getSizeInBytes());
-    }
-
-    // serialize each bitmap
     char* data_ptr = buf + sizeof(size_t) * real_need_copy_num;
     for (size_t i = 0; i < real_need_copy_num; ++i) {
         auto& bitmap = data_column.get_element(i);
-        bitmap.write_to(data_ptr);
-        data_ptr += unaligned_load<size_t>(&meta_ptr[i]);
+        const auto size = bitmap.write_to(data_ptr);
+        unaligned_store<size_t>(&meta_ptr[i], size);
+        data_ptr += size;

Review Comment:
   [P3] Report the actual uncompressed Block size
   
   This now advances by the compact payload length. `Block::serialize()` still 
returns the prepass reservation in `uncompressed_bytes`. Exchange uses that 
value for `UncompressedRowBatchSize`, and Arrow Flight uses it for 
`UncompressedBytes`. A large dense bitmap batch can show hundreds of MiB of 
uncompressed data even when the compressor receives under 1 MiB. Populate the 
reported byte count from the emitted size already used for 
`PBlock.uncompressed_size()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to