linrrzqqq opened a new pull request, #70908:
URL: https://github.com/apache/doris/pull/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: #18627 (Protobuf HLL SerDe), #19091 (Arrow HLL SerDe), #20928 
(MySQL binary HLL output), #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()` to fail. Meanwhile, `read_column_from_pb()` 
ignored `deserialize()` failures and skipped `null_map` size checks. This PR 
emits exact serialized lengths and rejects malformed or length-mismatched 
Protobuf HLLs.
   
   ### Details
   
   - **Change map:** Production +46/-41 in `data_type_hll_serde.cpp`; tests 
+368/-6 across `data_type_serde_hll_test.cpp`, `data_type_hll_test.cpp`, and 
`test_hll_sparse_return_binary.groovy`.
   - **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 discarded the actual size returned by 
`serialize()`; `read_column_from_pb()` ignored `deserialize()` returning 
`false` and unchecked `null_map` lengths.
   - **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:** Emit the byte count returned by `serialize()` in all four 
writers, and validate `null_map_size()` and `deserialize()` with rollback in 
`read_column_from_pb()`.
   - **Other paths and branches:** Block shuffle (`DataTypeHLL`) and storage 
(`HllColumnWriter`) already use exact sizes; `branch-3.0` through `branch-4.2` 
carry the same SerDe code.
   - **Behavior change:** `read_column_from_pb()` now returns `InvalidArgument` 
on malformed HLL bytes or mismatched `null_map` lengths instead of inserting 
empty HLLs.
   
   ### 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 (6a6c7529c1e)          # 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): 
49/49 passed (`87.74%` line coverage on `data_type_hll_serde.cpp`), covering 
`EMPTY`/`EXPLICIT`/`SPARSE`/`FULL` across PB, Arrow, MySQL, and ORC, plus 
malformed PB rollback and `null_map` mismatches.
   - Regression P0 (`./run-regression-test.sh`): 
`datatype_p0/hll/test_hll_sparse_return_binary.groovy` (1/1 passed) 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, and rejects 
malformed Protobuf `HLL` payloads or mismatched `null_map` lengths.
   
   ### Check List (For Author)
   
   - Behavior changed: Yes (see Details)
   - 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 an earlier revision and identified the 
`null_map` check and regression table retention; the final edit and this 
description await author review.
   - Prompts: Requested fixing HLL SerDe upper-bound serialization, rejecting 
malformed protobuf inputs and mismatched `null_map` lengths, keeping the 
regression test table, pushing without opening a PR, and trimming `Details` 
while showing concrete before/after reproduction outputs.
   
   <!-- Recommended: develop the change with the develop-with-evidence skill 
and write this description with the doris-pr-description skill, both from 
https://github.com/apache/doris-skills (the doris-skills submodule of this 
repository). -->
   
   ### Background
   
   <!-- How the feature or module this PR touches works today, in two to four 
plain sentences, for a reviewer who doesn't know it. Link the user doc if there 
is one. Leave the problem and the fix to Problem Summary. -->
   
   ### What problem does this PR solve?
   
   <!-- Issue Number: "close #N" if this PR resolves the issue, "#N" if it only 
relates to it, "None" if there is none. A Feature needs an issue. -->
   <!-- Related PR: "#N (relation)" for each PR a reviewer should know about, 
such as "#123 (introduced the bug)", separated by commas. Write "None" if no 
related PR is known. -->
   <!-- PR Type: Bugfix, Performance, Feature, Enhancement, Refactor, Test, 
Build/CI/Chore, Revert, Release/Dependency, or Other. -->
   
   Issue Number:
   Related PR:
   PR Type:
   
   Problem Summary:
   
   <!-- Two to five sentences: the problem or need and who is affected; the 
approach in one sentence; when the diff is larger than that suggests, what else 
it had to handle, or that most of it is tests; and what existing users see 
after an upgrade. -->
   
   ### Details
   
   <!--
   For a diff of more than about 300 changed lines, start with "- **Change 
map:**": how many lines are production code, tests, generated files, and docs, 
then the code in parts, each with the problem it handles and its size.
   Then answer each field of your PR type on its own line, as "- **Field:** 
answer", in this order:
     Bugfix: Frequency, Root cause, Why existing tests missed it, Fix, Other 
paths and branches
     Performance: Why it was slow, Optimization, Results, Applicability and cost
     Feature: Scope, Design, Usage, Default behavior, Compatibility
     Enhancement: Before and after, Configs and defaults, Impact on existing 
users
     Refactor: Moves vs. logic changes, Behavior change, Compatibility, 
Performance impact, Rollback
     Test, adding tests: Scenarios, What existing tests miss, Fails when broken
     Test, fixing a test: Failure, Cause, Stability evidence
     Build/CI/Chore: Affected, Developer and release impact, License and 
third-party, Rollback
     Revert: Why not fix forward, Original author notified, Follow-up
     Release/Dependency: Versions, Upstream changes, Compatibility, Rollback
     Other: Why Other
   "None" is a valid answer when it is true. Don't restate the diff.
   -->
   
   ### Testing
   
   <!-- The tests, suites, or commands that ran, what each shows, and where it 
ran (locally or in CI). For a fix, the test that fails before it and passes 
after. -->
   
   ### Notes for Reviewers
   
   <!-- Optional; delete this section if there is nothing to say. Why each 
non-obvious edit was made, what you are unsure about, and where to start 
reading if Details has no Change map. -->
   
   ### Release note
   
   <!-- One paragraph on what users will notice, or None. -->
   
   ### Check List (For Author)
   
   <!-- Behavior changed: "No", or "Yes (see Details)" with a Behavior change 
field in Details. -->
   <!-- Does this need documentation: "No", or "Yes" with the 
apache/doris-website PR or the plan. -->
   <!-- Pre-submission status: "Ready"; "Partially verified: " and what is not 
verified; or "Not ready: " and why, for a Draft PR. -->
   
   - Behavior changed:
   - Does this need documentation:
   - Pre-submission status:
   
   ### AI Disclosure
   
   <!-- Tool: the agent and model, or None. Generated: the parts it wrote 
(code, tests, this description), or None. Human review: what you reviewed of 
the generated parts, or "Not applicable" without AI tools. You may add "- 
Prompts:" to summarize how you directed the agent. -->
   
   - Tool:
   - Generated:
   - Human review:
   


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