HappenLee opened a new pull request, #68312:
URL: https://github.com/apache/doris/pull/68312
### What problem does this PR solve?
Problem Summary:
A query that has already computed one aggregate state per key still needs an
aggregate operator to obtain its final values through `<agg>_merge`. This
repeats aggregation for the finest grouping when states are also reused for
coarser rollups.
Add the scalar `<agg>_finalize(state)` combinator. It returns one result for
each input state, using the existing aggregate implementation and
serialized-state representation. For example:
```sql
SELECT k, avg_finalize(s)
FROM (SELECT k, avg_combine(v) AS s FROM t GROUP BY k) partial;
```
The same implementation supports aggregates such as `count`, `sum`, `min`,
`max`, and `array_agg`. FE validates that the state's canonical aggregate name
matches the finalizer, derives its result type, and treats the function as
scalar. BE handles the underlying serialized column type, skips outer NULL
payloads, and releases temporary state after each row. Constant inputs use the
ordinary scalar constant path. Existing empty-state semantics and serialized
formats are unchanged.
This adds the scalar building block only; it does not change optimizer
rollup rewrites.
### Release note
Add `<aggregate>_finalize(AGG_STATE)` scalar functions to retrieve each
aggregate state's result without merging rows.
### Check List (For Author)
- Test
- [x] Regression test: test_agg_state_finalize on a fresh local ASAN BE
+ FE cluster; output generated by the standard runner, checked against direct
original aggregates, and verified by a normal comparison run.
- [x] Unit Test: 33 FE tests passed across FinalizeCombinatorTest,
StateCombinatorTest, CombineCombinatorTest, and FunctionRegistryTest. All 7
FunctionAggStateFinalizeTest cases passed under ASAN.
- [x] Manual test: compare grouped and empty AVG/COUNT/SUM/MIN/MAX,
decimal AVG and ARRAY_AGG with the original aggregates; verify rollup AVG is
14/3 and distinguish a missing outer-join state from COUNT's empty state.
- [ ] No need to test or manual test. Explain why:
- Behavior changed:
- [ ] No.
- [x] Yes. Add a family of scalar finalization functions; existing
aggregate/state semantics are unchanged.
- Does this need documentation?
- [ ] No.
- [x] Yes. Usage and semantics are included in the function-combinator
README.
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label
### Additional validation
- Standard ASAN BE + FE build, FE Checkstyle, clang-format 16, build hygiene
and source whitespace checks passed.
- clang-tidy was attempted with the repository script and the
production/test translation units. New-code style and added
cognitive-complexity warnings were fixed. A fully clean run remains blocked by
existing header diagnostics and analyzer issues (including a test-helper
array-bound path that does not connect its asserted unary arity to the input
array); the local tool's crashing `modernize-use-scoped-lock` check was
disabled only for the supplemental analysis. No repository analysis
configuration was changed.
--
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]