BiteTheDDDDt opened a new pull request, #62797:
URL: https://github.com/apache/doris/pull/62797

   ### What problem does this PR solve?
   
   Issue Number: close #xxx
   
   Related PR: N/A
   
   Problem Summary:
   
   `HllToBase64::vector()` in `be/src/exprs/function/function_hll.cpp` 
pre-allocates the output `chars` buffer using 
`HyperLogLog::max_serialized_size()` (for SPARSE/FULL HLL the upper bound is `1 
+ HLL_REGISTERS_COUNT = 16385` bytes), but the inner loop only advances 
`encoded_offset` by the **actual** base64-encoded length of each row's 
serialized HLL. After the loop, `offsets.back() == encoded_offset` reflects the 
real bytes written, while `chars.size()` is still the over-allocated upper 
bound.
   
   This violates `ColumnString`'s invariant `chars.size() == offsets.back()`, 
which is asserted by `ColumnString::sanity_check_simple()` (see 
`be/src/core/column/column_string.cpp:153`). In debug / ASAN builds this throws 
`Internal error: chars.size(): X, offset[N-1]: Y`. In release builds it leaks 
tail bytes into memory accounting and any downstream consumer that relies on 
`chars.size()`.
   
   The bug is most easily reproducible with SPARSE HLLs: 
`max_serialized_size()` returns 16385 but the real serialized form is `1 + 4 + 
3 * num_non_zero_registers`, so the gap between allocated and used bytes is 
large. The existing tests only covered EXPLICIT HLL (`hll_hash('abc')`) and 
EMPTY HLL where `max == real`, so they did not exercise this path.
   
   Fix: shrink `chars` to the real `encoded_offset` after the encoding loop 
completes.
   
   ### Release note
   
   Fix a `ColumnString` invariant violation in `hll_to_base64` when the HLL is 
in SPARSE / FULL representation.
   
   ### Check List (For Author)
   
   - Test:
       - [x] Regression Test: added two cases in 
`regression-test/suites/query_p0/sql_functions/hll_functions/test_hll_functions.groovy`
 that build a SPARSE HLL (500 distinct hashes) and exercise `hll_to_base64` 
round-trip and length, covering the previously untested SPARSE path.
       - [x] Manual test: rebuilt BE and reran the regression suite — passes.
   
   - Behavior changed: No
   
   - Does this need documentation: No
   


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