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


##########
be/test/core/value/bitmap_value_test.cpp:
##########
@@ -232,15 +235,17 @@ TEST(BitmapValueTest, Roaring64Map_write_read) {
 
     roaring64_map.write(buffer.get(), 1);
 
-    bitmap_read = detail::Roaring64Map::read(buffer.get());
+    bitmap_read = detail::Roaring64Map::read(buffer.get(), &consumed);
+    EXPECT_EQ(consumed, bytes);

Review Comment:
   [P2] Update the test oracles for upper-bound sizing
   
   This added equality fails for the test's `{0,1,3,4,5,7,8}` bitmap: its 
14-byte array ties the run representation, so it stays an array. The v1 bound 
is 31 bytes, while the new fixed-12347 header produces 24 consumed bytes. 
Capture `write()`'s return value and compare consumption with that, checking it 
is within the bound. Please also update `convert_bitmap_to_string()` and 
`encode_bitmap_to_base64()` to retain only actual written bytes, and reconcile 
the old 12346 base64 literals in `function_bitmap_to_base64`; the added `raw64` 
expectation currently encodes seven trailing zero bytes that production 
`.write()` removes. These assertions/oracles otherwise fail under the intended 
new contract.



##########
be/src/core/value/bitmap_value.h:
##########
@@ -1035,6 +1041,311 @@ class Roaring64Map {
         *bytes_read = (is_v1 ? 0 : 1) + readbytes;
         return r;
     }
+
+    // From CRoaring `ra_portable_header_size()` (`roaring_array.c`): header 
size with run_bitset.
+    static size_t portable_header_size(int32_t size) {
+        if (size <= 0) {
+            return sizeof(uint32_t) * 2;
+        }
+        const size_t n = static_cast<size_t>(size);
+        // Always include run_bitset since cookie is fixed to SERIAL_COOKIE 
(12347).
+        const size_t run_bitset_bytes = (n + 7) / 8;
+        const size_t base = sizeof(uint32_t) + run_bitset_bytes + n * 
sizeof(uint32_t);
+        return (size < roaring::internal::NO_OFFSET_THRESHOLD) ? base
+                                                               : (base + n * 
sizeof(uint32_t));
+    }
+
+    // Upper bound for fixed-12347 serialization (+ ceil(n/8)-4 bytes when 
n>32 without RUN yet).
+    static size_t max_serialized_size(const roaring::Roaring& src, bool is_v1) 
{
+        const auto& ra = src.roaring.high_low_container;
+        if (ra.size <= 0 || (ra.flags & ROARING_FLAG_FROZEN) != 0) {
+            return src.getSizeInBytes(is_v1);
+        }
+        size_t portable_max = src.getSizeInBytes(true);
+        if (ra.size > 32 && !roaring::internal::ra_has_run_container(&ra)) {
+            portable_max += ((static_cast<size_t>(ra.size) + 7) / 8) - 
sizeof(uint32_t);
+        }
+        if (is_v1) {
+            return portable_max;
+        }
+        const uint64_t card = 
roaring::api::roaring_bitmap_get_cardinality(&src.roaring);
+        const size_t flat_size =
+                1 + sizeof(uint32_t) + static_cast<size_t>(card) * 
sizeof(uint32_t);
+        if (card * sizeof(uint32_t) + sizeof(uint32_t) <=
+            portable_header_size(ra.size) + 2 * static_cast<size_t>(ra.size)) {
+            return flat_size;
+        }
+        return std::max(1 + portable_max, flat_size);

Review Comment:
   [P1] Avoid reserving the flat-array size for dense v2 bitmaps
   
   With `bitmap_serialize_version=2`, this `max` makes even an already compact 
bitmap reserve at least four bytes per member. For IDs `[0, 2^20)` stored in 16 
run containers, `getSizeInBytes()` now returns 4,194,310 although `write_to()` 
emits just 232 bytes. `BitmapValue::write()`, Block serialization, storage 
batches and JSONB/ORC Arenas allocate this bound, so batches of compact bitmaps 
can exhaust query memory. In the non-early-flat branch, the fallback is chosen 
only when the flat representation fits within the portable bytes already 
written; reserving the full flat size is unnecessary. Keep the bound tied to 
that write strategy and add a dense v2 reservation regression case.



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