Skip to content

branch-4.2: [fix](serde) Serialize exact HLL byte lengths in DataTypeHLLSerDe (#70908) - #71015

Open
linrrzqqq wants to merge 1 commit into
apache:branch-4.2from
linrrzqqq:pick-70908-branch-4.2
Open

linrrzqqq wants to merge 1 commit into
apache:branch-4.2from
linrrzqqq:pick-70908-branch-4.2

Conversation

@linrrzqqq

Copy link
Copy Markdown
Collaborator

pick: #70908

…ache#70908)

### Background

Doris `HyperLogLog` encodes values as `EMPTY` (1 byte), `EXPLICIT` (up to 160 hashes), `SPARSE` (161 to 4,096 non-zero registers, 5 to 12,293 bytes), or `FULL` (16,385 bytes). `max_serialized_size()` returns the 16,385-byte upper bound for both sparse and full states, whereas `serialize()` returns the exact byte count written and `is_valid()` rejects trailing bytes.

### What problem does this PR solve?

Issue Number: None
Related PR: apache#18627 (Protobuf HLL SerDe), apache#19091 (Arrow HLL SerDe), apache#20928 (MySQL binary HLL output), apache#24812 (ORC HLL SerDe)
PR Type: Bugfix

Problem Summary:

Four `DataTypeHLLSerDe` writers emitted `max_serialized_size()` (`16,385` bytes) instead of the byte count returned by `HyperLogLog::serialize()`. For sparse HLLs, MySQL binary output, ORC `INTO OUTFILE`, Arrow, and Protobuf appended trailing padding or uninitialized heap bytes, causing `HyperLogLog::deserialize()` (such as `hll_from_base64`) to fail. This PR emits the exact byte length returned by `serialize()` across all four writers.

### Details

- **Change map:** Production +17/-17 in `data_type_hll_serde.cpp`; tests +315/-6 across `data_type_serde_hll_test.cpp` (+186/-1), `test_hll_sparse_return_binary.groovy` (+97/-0, new), and `data_type_hll_test.cpp` (+32/-5).
- **Frequency:** Every `HLL_DATA_SPARSE` value (`161..4096` non-zero registers) written through PB, Arrow, MySQL binary, or ORC SerDe.
- **Root cause:** Those four writers allocated or bounded buffers by `max_serialized_size()` and discarded the actual byte count returned by `serialize()`.
- **Why existing tests missed it:** Existing HLL SerDe and binary-output tests used at most `100` hashes (`EXPLICIT`), where `serialize()` equals `max_serialized_size()`.
- **Fix:** Capture `actual_size` from `serialize()` in PB, Arrow, MySQL text, and ORC writers, and advance ORC batch length and offset by `actual_size` without extra temporary buffering.
- **Other paths and branches:** Block shuffle (`DataTypeHLL`) and storage (`HllColumnWriter`) already write exact sizes; `branch-3.0` through `branch-4.2` carry the same SerDe writers.

### Testing

Sparse HLL fixture (`200` distinct registers, cardinality `201`, compact serialized length `1 + 4 + 3 * 200 = 605` bytes):

```sql
SELECT
    hll_cardinality(h) AS orig_card,
    OCTET_LENGTH(FROM_BASE64(hll_to_base64(h))) AS expected_bytes
FROM (
    SELECT hll_union(hll_hash(CAST(number AS VARCHAR))) AS h
    FROM numbers("number" = "200")
) t;
```

```text
+-----------+----------------+
| orig_card | expected_bytes |
+-----------+----------------+
|       201 |            605 |
+-----------+----------------+
```

**1. MySQL binary output (`write_column_to_mysql_text`) and `hll_from_base64` round-trip**

```sql
SET return_object_data_as_binary = true;
SELECT hll_union(hll_hash(CAST(number AS VARCHAR)))
FROM numbers("number" = "200");
```

Before the fix (`6a6c7529c1e`), the client received `16,385` bytes (`605` valid sparse bytes + `15,780` uninitialized tail bytes), and passing the Base64 payload back to `hll_from_base64` failed:

```text
received_bytes=16385, hll_type=2, num_registers=200, actual_bytes=605, tail_bytes=15780

ERROR 1105 (HY000) at line 1: errCode = 2, detailMessage = (127.0.0.1)[RUNTIME_ERROR]hll_from_base64 decode failed: base64: AsgAAAB0AAO8AAVVAQJ3AQSjAQTvAQINAgFCAgGLAgWOAgJWAwHWAwHuAwErBAFYBAF2BAS5BANMBQKJBQGhBQOzBQICBgELBgFDBgFWBgGZBwStBwHnBwLwBwdcCAJiCAJsCAGfCAOOCQHpCQILCgEUCgEKCwGPDAGaDAEcDQM1DQFTDQGVDQLCDQHbDQJUDgFcDgaMDgE4DwFEDwHzDwH2DwQvEANkEAHcEAEkEQF4EQGrEQEMEgE6EgJBEgGNEgGQEgPmEgVrEwHBEwLCEwFNFAGxFAIJFQIfFQFDFQFVFQR1FQIWFgcnFgGBFgEXFwFQFwF2FwHDFwHGFwHpGAEcGQFQGQK0GgFjGwaoGwUPHAGKHAWOHAQOHQFnHQEVHgUaHgGRHgFeHwN6HwKIHwGOHwG6HwThH
```

With the fix, the client receives `605` bytes (`tail_bytes=0`) and `SELECT hll_cardinality(hll_from_base64('$PAYLOAD_B64')) AS card` succeeds:

```text
received_bytes=605, hll_type=2, num_registers=200, actual_bytes=605, tail_bytes=0

+------+
| card |
+------+
|  201 |
+------+
```

**2. ORC `INTO OUTFILE` (`write_column_to_orc`)**

```sql
SELECT hll_union(hll_hash(CAST(number AS VARCHAR))) AS h
FROM numbers("number" = "200")
INTO OUTFILE "file://$OUT_DIR/orc_"
FORMAT AS ORC;
```

`orc-statistics` on Column 1 (`h`) before vs. with the fix:

```text
# Before the fix (6a6c752)          # With the fix
*** Column 1 ***                        *** Column 1 ***
Data type: Binary                       Data type: Binary
Values: 1                               Values: 1
Has null: no                            Has null: no
Total length: 16385                     Total length: 605
```

**3. Automated tests**

- BE UT (`./run-be-ut.sh --run --filter="HLLSerdeTest.*:*DataTypeHLL*:*SerDePb*" -j50 --coverage`, ASAN): 48/48 passed (`84.56%` line coverage and `87.21%` region coverage on `data_type_hll_serde.cpp`; `100.00%` on `data_type_hll_serde.h`), covering `EMPTY`/`EXPLICIT`/`SPARSE`/`FULL` serialization across PB, Arrow, MySQL, and ORC.
- Regression P0 (`./run-regression-test.sh`): `datatype_p0/hll/test_hll_sparse_return_binary.groovy` (1/1 passed; `SELECT COUNT(*)` verified 4 retained rows) and `query_p0/sql_functions/hll_functions/test_hll_functions.groovy` (1/1 passed).

### Release note

`DataTypeHLLSerDe` now writes exact serialized byte lengths for sparse `HLL` values across MySQL binary output, ORC export, Arrow, and Protobuf.

### Check List (For Author)

- Behavior changed: No
- Does this need documentation: No
- Pre-submission status: Ready

### AI Disclosure

- Tool: Codex (gpt-6.2-sol)
- Generated: BE fix, unit tests, regression test, and this description
- Human review: Author reviewed earlier revisions and directed reverting `read_column_from_pb()` and simplifying buffer allocations in ORC, Arrow, and Protobuf writers; the final commit and this description await author review.
- Prompts: Requested fixing HLL SerDe upper-bound serialization, reverting `read_column_from_pb()`, restoring single-allocation ORC serialization without outer loop buffers, pushing without opening a PR, and updating the PR description with concrete examples.

(cherry picked from commit eedce64)
@linrrzqqq
linrrzqqq requested a review from yiguolei as a code owner November 25, 2026 07:41
@linrrzqqq linrrzqqq changed the title branch-4.2: [fix](serde) Serialize exact HLL byte lengths in DataTypeHLLSerDe (#7… branch-4.2: [fix](serde) Serialize exact HLL byte lengths in DataTypeHLLSerDe (#70908) Nov 25, 2026
@linrrzqqq

Copy link
Copy Markdown
Collaborator Author

run buildall

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant