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

   ### Background
   
   String `collect_set` and the `multi_distinct_<fn>` states behind aggregates 
such as `group_concat(DISTINCT s)` keep their values in a hash set of 
`StringRef`s whose bytes live in the aggregation's `Arena`. The arena frees 
nothing until it is destroyed. A DISTINCT state with several arguments 
serializes each row into one arena key.
   
   ### What problem does this PR solve?
   
   Issue Number: close #70383
   Related PR: #24966 (introduced the bug)
   PR Type: Bugfix
   
   Problem Summary:
   
   These states copy every input string into the arena before the set lookup, 
so each duplicate row leaves a dead copy. Memory grows with rows × value size, 
not with the distinct values: #70383 measured a 1.5 GB peak for `SELECT 
size(collect_set(s)) FROM t` over 5M rows holding 10 distinct 200-byte strings. 
This PR looks up the input bytes first and copies only a new value; a duplicate 
multi-argument key is rolled back off the arena. Results do not change.
   
   ### Details
   
   - **Change map:** production code +40 −19 in two headers; BE unit tests +355 
(90% of the added lines).
   - **Frequency:** Always, on add and merge of string `collect_set` / 
`group_uniq_array` (`max_size` counts only distinct values, so it does not 
help) and of `multi_distinct_<fn>` with a non-numeric or multi-column argument.
   - **Root cause:** The five methods listed in #70383 call `arena.insert` (or 
serialize the key) before inserting into the set, and keep the copy when the 
key is already there. #24966 replaced `ArenaKeyHolder` / `SerializedKeyHolder`, 
which copied only on insert, with this.
   - **Why existing tests missed it:** The unit tests in `agg_collect_test.cpp` 
and `aggregate_function_distinct_test.cpp`, and regression suites such as 
`test_aggregate_collect` and `agg_group_concat`, check results, which were 
always right; none checks what the arena holds.
   - **Fix:** `insert_borrowed` probes with the borrowed bytes through phmap 
`lazy_emplace` and copies into the arena only in the constructor callback, so a 
new key still costs one probe. The multi-argument add calls `arena.rollback` on 
a duplicate, as `AggregateFunctionUniqVariant::add` does.
   - **Other paths and branches:** `map_agg`, `AggregateFunctionUniqVariant` 
and `hash_map_context.h` already copy only on insert. `branch-4.1`, 
`branch-4.0` and `branch-3.1` have the same code (4.0 and 3.1 under 
`be/src/vec/aggregate_functions/`).
   
   ### Testing
   
   - New BE unit tests `CollectSetStringArenaTest`, `DistinctGenericArenaTest` 
and `DistinctGenericArenaMergeTest` assert one arena copy per distinct value 
after add, add with `max_size`, merge and deserialize-and-merge, plus rollback 
across an arena chunk, first-insertion order, and results after the input is 
overwritten. **Not built or run yet** (no local BE build); the BE UT pipeline 
runs them.
   
   <details>
   <summary>Stand-alone check against phmap 1.3.8 (not the Doris 
build)</summary>
   
   The three changed insert paths, copied into a program with an arena that 
mirrors `be/src/core/arena.h`, compiled with clang++ 
`-fsanitize=address,undefined` against phmap 1.3.8 (the version Doris pins). 
The arena holds 2,010 bytes instead of 4,020,000 after 20,000 rows over 10 
distinct 201-byte strings, and 2,010 instead of 2,010,000 after merging the 
same 10-value state 1,000 times. Ordered states kept first-insertion order, and 
rollback held when a duplicate key crossed into a new chunk.
   
   </details>
   
   ### Notes for Reviewers
   
   - Start at `insert_borrowed` in `aggregate_function_distinct.h`. The 
decision to review: `lazy_emplace` instead of `contains` + `emplace`, which 
hashes a new key twice. If the arena throws inside the callback, the reserved 
slot stays unconstructed; the query fails and the state is only destroyed 
(`StringRef` is trivially destructible), the same exposure as `lazy_emplace` in 
`aggregate_function_map_combinator.cpp`.
   - The ordered map reads its index before `lazy_emplace`, because phmap 
counts the new slot before the callback runs.
   
   ### Release note
   
   Fix string `collect_set` / `group_uniq_array` and DISTINCT aggregates such 
as `group_concat(DISTINCT s)` using memory proportional to input rows instead 
of distinct values.
   
   ### Check List (For Author)
   
   - Behavior changed: No
   - Does this need documentation: No
   - Pre-submission status: Partially verified: the new BE unit tests have not 
been built or run, and query peak memory has not been measured on a Doris build.
   
   ### AI Disclosure
   
   - Tool: Claude Code (Claude Opus 5.5)
   - Generated: the fix, the unit tests, and this description
   - Human review: not stated
   - Prompts: Asked the agent to open a draft PR that fixes #70383.
   


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