mrhhsg opened a new pull request, #71192: URL: https://github.com/apache/doris/pull/71192
### Background Aggregate functions keep per-group state in arenas and mutable columns. Distinct string keys are stored in an arena, while list, array, map, and ordered aggregates append values to column buffers. ### What problem does this PR solve? Issue Number: None Related PR: None PR Type: Performance Problem Summary: Repeated string keys could be copied into a shared arena before deduplication, retaining bytes that never become part of the result. A small group could also acquire batch-sized column buffers on its first append. This change retains only new distinct keys and gives small states capacity based on the data they actually append, without changing aggregate results or state serialization. ### Details - **Change map:** Five BE aggregate headers contain 246 added and 34 removed production lines. Four BE test files add 723 lines, and one SQL regression suite plus its generated expected output add 73 lines. There are no FE, protocol, configuration, or documentation changes. - **Why it was slow:** Duplicate collect_set and generic DISTINCT keys occupied arena space despite being rejected by their sets. First writes to small per-group states could use large default buffers; reserving whole string or recursive columns can also retain storage for absent chars or child rows. - **Optimization:** Check borrowed string keys before copying new keys into the destination arena. Serialize multi-column DISTINCT tuples into one contiguous allocation and roll that allocation back on duplicates, including tuples that cross arena chunks. Use smaller initial list buckets and reserve the actual final size for live merges. For first appends in list, array, map, and ordered states, reserve only safe physical leaves and actual row counts; string-backed columns, including JSONB, reserve offsets and nullable bits but not unused chars. Avoid recursive hints for complex, VARIANT, and AGG_STATE layouts. - **Results:** In the pre-change state-level reproduction, 32 repeated inserts of one 1,024-byte collect_set key increased arena used bytes from 1,024 to 33,792; the new unit test requires no growth for duplicates. One BIGINT collect_list value previously allocated a 4,096-byte PODArray buffer; its new initial bucket is 64 bytes. Focused tests also verify that empty strings and NULL JSONB values do not retain chars buffers. These are state-capacity results, not query-level RSS or throughput measurements. - **Applicability and cost:** Benefits workloads with repeated string keys or many small aggregate groups. A new unique generic string key incurs a lookup before insertion, and small buffers can grow more often for larger groups. Result ordering, limits, NULL handling, ownership, and serialized formats remain unchanged. ### Testing - Local ASAN BE unit tests: `./run-be-ut.sh --run --filter='VAggCollectTest.*:AggregateFunctionCollectTest.*:AggregateFunctionDistinctGenericMemoryTest.*:*DistinctNumericMergeTest*.*:*DistinctMergeDispatchTest*.*:AggregateFunctionArrayAggTest.*:AggregateFunctionSortDataTest.*:AggregateFunctionMapAggV2Test.*' -j 48` — 72/72 passed. The pre-change reproduction failed on duplicate arena usage and first-value capacity; the final focused suite passed. - Local build: `./build.sh --be --fe -j 48` — passed, including FE Checkstyle. - Local isolated-cluster SQL regression: `./run-regression-test.sh --conf output/local-regression/regression-conf-16300.groovy --run -d query_p0/aggregate -s <suite>` — `test_aggregate_state_memory`, `agg_group_concat`, `array_agg`, and `map_agg` each passed (1 suite, 0 failures). - `build-support/clang-format.sh`, `build-support/check-format.sh`, `build-support/check-build-hygiene.sh`, and `git diff --check` — passed. - Query-level RSS/throughput benchmarks and mixed-version multi-BE tests were not run. ### Release note Reduce memory retained by aggregate states with duplicate keys and small groups. ### Check List (For Author) - Behavior changed: No - Does this need documentation: No - Pre-submission status: Partially verified; focused local build, unit tests, and SQL regression passed, but query-level memory and throughput were not measured. ### AI Disclosure - Tool: Codex - Generated: Code, tests, and this description. - Human review: Pending. -- 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]
