HappenLee commented on code in PR #70774:
URL: https://github.com/apache/doris/pull/70774#discussion_r4693247521


##########
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_optimized_to(data_ptr);

Review Comment:
   Agreed that `get_uncompressed_serialized_bytes()` may return an upper bound. 
The P1 here is that the compact writer can **exceed** that bound.
   
   I checked the sizes with the CRoaring version used by Doris (2.1.2). For the 
same set of IDs in v1, I measured the following before and after 
`runOptimize()` and `shrinkToFit()`.
   
   | Bytes in one bitmap value | Original | Optimized copy |
   | --- | ---: | ---: |
   | Roaring container data | 1024 | 1022 |
   | Roaring header | 520 | 524 |
   | Doris type byte | 1 | 1 |
   | **Total** | **1545** | **1547** |
   
   Introducing the first run container saves 2 bytes in the container data. The 
run-container flags increase the overall header by 4 bytes net. The two bitmaps 
contain exactly the same IDs.
   
   On this head, the Block size prepass counts **1545** bytes for the value. 
`DataTypeBitMap::serialize()` then calls `write_optimized_to(data_ptr)`, which 
needs **1547**. A batch without spare space from other rows can therefore write 
past its reserved output. The Segment writer makes the same assumption in 
`StorageLayout<BITMAP>::column_to_storage()`. Recording the actual size after 
the write is too late. I verified the encoding lengths independently of BE 
execution.
   
   Could we fix this before merging? A small change in `write_optimized_to()` 
would cover both callers: compare the two encoded sizes **before writing** and 
emit the original representation if the optimized copy is larger. Please add a 
regression test for both v1 and v2 that checks the upper bound and verifies 
that the chosen representation round-trips to the same set.



-- 
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