linrrzqqq opened a new pull request, #71169:
URL: https://github.com/apache/doris/pull/71169
### Background
64-bit bitmaps (`BITMAP64` / `BITMAP64_V2`) aggregate states concatenate
multiple bitmaps without per-element byte lengths, so decoding entry `0` must
advance the buffer pointer to entry `1`:
```text
BITMAP64 / V2 : [ type | count | hi32_0 | roaring0 | hi32_1 | roaring1 |
... ]
```
On write, a 32-bit roaring with `n >= 4` array containers and `card`
elements picks the shorter wire format:
```text
v1 (portable): cookie=12346 (no RUN) : 8 + 8*n + 2*card
bytes
cookie=12347 (has RUN) : 4 + ceil(n/8) + 8*n + payload
bytes
v2 (native) : tag=1 (flat uint32_t[]) : 1 + 4 + 4*card
bytes
tag=2 (portable) : 1 + v1_size
bytes
```
On read, `roaring::Roaring::read()` **only restores the integer set in
memory and discards the wire header** (`12346`/`12347` or `tag=1`/`tag=2`); it
does not return how many input bytes were consumed.
### What problem does this PR solve?
Issue Number: None
Related PR:
PR Type: Bugfix
Problem Summary:
To advance past each decoded entry, Doris called `getSizeInBytes()` on the
**in-memory object**. When the wire format differs from what the decoded bitmap
would re-encode to, the cursor jumps by the wrong offset and fails or corrupts
subsequent groups. This PR returns consumed bytes directly during
deserialization.
For `n = 80, card = 324` with `{1, 2, 3}` in container `79` (`1` run saves
`0 B` payload, while `12347` adds `ceil(80/8) - 4 = 6 B` header):
| Format | Formula (`n = 80, card = 324`) | Wire size | Role |
|---|---|---|---|
| `tag=2` (no RUN, `12346` header) | `1 + 8 + 8*80 + 2*324` | `1297 B` |
**Old reader** re-estimates here (`-4 B` short) |
| `tag=1` (flat `uint32_t[]`) | `1 + 4 + 4*324` | `1301 B` | **Writer**
emits this (`< 1303 B`) |
| `tag=2` (with RUN, `12347` header) | `1 + (4 + 10) + 8*80 + 2*324` | `1303
B` | `runOptimize()` exceeds `tag=1` |
```text
Wire bytes (v2, tag=1 flat): [ tag=1 | card=324 | 324 uint32s (1301B) ][
hi32=9 | roaring1 ]
^
real boundary (+1301)
Old reader: read() loads 324 ints into arrays (no RUN) ->
getSizeInBytes(false) picks tag=2 (1297B)
buf += 1297 --> lands 4 bytes EARLY inside roaring0 and
misreads hi32=9!
```
Reproduction under `bitmap_serialize_version = 2` (`4294967296 = 2^32`,
`65536 = 2^16`):
```sql
WITH src AS (
SELECT
CASE
WHEN number < 312 THEN 4294967296 + (number DIV 4) * 65536 +
(number % 4) * 10 -- hi32=1, containers 0..77 (4 sparse ints each)
WHEN number < 321 THEN 4294967296 + 78 * 65536 + (number - 312)
* 10 -- hi32=1, container 78 (9 sparse ints)
WHEN number < 324 THEN 4294967296 + 79 * 65536 + (number - 320)
-- hi32=1, container 79 ({1,2,3} -> 1 RUN, 0B saved)
ELSE 9 * 4294967296 + 10
-- hi32=9: second group (9 << 32) | 10
END AS v
FROM numbers("number" = "325")
),
bm AS (
SELECT bitmap_union(to_bitmap(v)) AS b FROM src
)
SELECT
bitmap_count(b) AS orig_cnt,
bitmap_count(bitmap_from_base64(bitmap_to_base64(b))) AS roundtrip_cnt,
bitmap_contains(bitmap_from_base64(bitmap_to_base64(b)), 9 * 4294967296
+ 10) AS has_hi9
FROM bm;
```
| Before the fix (`master` `e1ed017a6c2`) | With the fix |
|---|---|
| `ERROR 1105 (HY000) at line 1: errCode = 2, detailMessage =
(10.26.20.3)[RUNTIME_ERROR]bitmap_from_base64 decode failed: base64: DQIB...` |
`orig_cnt: 325`, `roundtrip_cnt: 325`, `has_hi9: 1` |
### Details
- **Change map:** `+78 -12` production code
(`be/src/core/value/bitmap_value.h`, `be/src/util/bitmap_intersect.h`) and
`+266 -0` unit tests (`be/test/core/value/bitmap_value_test.cpp`).
- **Frequency:** Triggers whenever an unframed `BITMAP64`, `BITMAP64_V2`, or
`BitmapIntersect` stream contains a bitmap whose wire size differs from its
post-decode `getSizeInBytes()` (e.g., v2 `tag=1` vs `tag=2`, or v1 `12346`
before vs `12347` after `runOptimize()`).
- **Root cause:** `detail::Roaring64Map::read` and
`BitmapIntersect::deserialize` discarded wire-format framing during decode and
inferred consumed bytes via `read_var.getSizeInBytes(is_v1)` /
`bitmap.getSizeInBytes()`.
- **Why existing tests missed it:** Existing unit tests
(`BitmapValueTest.write_read`, `bitmap_serde`,
`intersect_count_decodes_consecutive_bitmaps`) and regression suites
(`test_bitmap_serialize.groovy`, `test_bitmap_function.groovy`) only use small
bitmaps that never hit the `n > 32` container `12346`/`12347` or v2
`tag=1`/`tag=2` crossover.
- **Fix:** Added single-pass `Roaring64Map::read_roaring`
(`ra_portable_deserialize(..., &readbytes)` for portable/v2-`tag=2`, O(1)
header size for v2-`tag=1`) and `BitmapValue::deserialize_with_size`, updating
`Roaring64Map::read` and `BitmapIntersect::deserialize` to advance by actual
consumed bytes.
- **Other paths and branches:** Other `BitmapValue::deserialize` callers in
`be/src/` (columns, SerDe, storage, `AggOrthBitmapBaseData`, base64) have
explicit length framing or a single bitmap and are unaffected. Release branches
(`branch-4.0`, `branch-4.1`, etc.) were not fetched or checked.
### Testing
- E2E SQL query above against an isolated Release FE+BE cluster
(`bitmap_serialize_version = 2`): fails with `ERROR 1105 ... bitmap_from_base64
decode failed` on `master` (`e1ed017a6c2`), and returns `325 | 325 | 1` with
the current branch build.
- `bash ./run-be-ut.sh --run --filter="BitmapValueTest.*:AggBitmapTest.*" -j
24` (ASAN_UT on `37d6ad5b725`): 50/50 tests passed, including the new
`BitmapValueTest.deserialize_consumes_actual_input_size` covering v1 (`12346`
vs `12347`), v2 (`tag=1` vs `tag=2`), `BitmapIntersect`, and
`EMPTY`/`SINGLE`/`SET`/`SET_V2`.
- After the latest comment and `[[unlikely]]` edit in
`be/src/core/value/bitmap_value.h`: verified with the Release BE E2E query
above and `clang++ -fsyntax-only` on `be/test/core/value/bitmap_value_test.cpp`.
### Release note
Fix cursor misalignment when deserializing concatenated `BITMAP64`,
`BITMAP64_V2`, and `BitmapIntersect` payloads whose wire size differs from the
decoded bitmap's re-encoded size.
### Check List (For Author)
- Behavior changed: No
- Does this need documentation: No
- Pre-submission status: Partially verified: E2E Release SQL and syntax
check passed on the latest edit; ASAN BE unit tests (50/50) ran on
`37d6ad5b725` before the comment and `[[unlikely]]` edit
### AI Disclosure
- Tool: Antigravity
- Generated: the fix, unit tests, and this description
- Human review: not stated
--
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]