branch-4.1: [fix](fd) Guard uniform aggregate inference by participation #67881 - #68175
Merged
Merged
Conversation
## Problem
When an aggregate groups by a unique, non-null key, each group contains
one row. The planner used that fact to mark every `COUNT` and `NDV`
output as uniform. That is not true for nullable arguments: `COUNT(v)`
and `NDV(v)` return `0` for a null value and `1` for a non-null value.
An outer aggregation can consequently remove such an output from its
group keys and merge rows that must remain separate.
## Root cause
The logical and physical aggregate trait derivations classified an
output as uniform solely from the aggregate function class. They did not
distinguish `COUNT(*)` from argument-based aggregates or check whether
the complete argument expressions always participate in the aggregate.
## Reproduction
Create a unique-key table containing two rows whose nullable value
differs:
```sql
create table uniform_agg_witness (
pk int not null,
b int not null,
v int null
) unique key(pk)
distributed by hash(pk) buckets 1
properties("replication_num"="1");
insert into uniform_agg_witness values (1, 7, null), (2, 7, 9);
select b, c, count(*) as n, sum(h) as sh
from (
select pk, b, count(v) as c, ndv(v) as h
from uniform_agg_witness
group by pk, b
) s
group by b, c
order by b, c;
```
The invalid uniform trait removed `c` from the outer group keys and
produced one merged row. The correct result has separate `(7, 0)` and
`(7, 1)` groups.
## Fix
- Share one uniform-aggregate proof between logical and physical
aggregate plans.
- Keep `COUNT(*)` uniform for a single-row group.
- Treat argument-based `COUNT` and `NDV` as uniform only when every
complete argument expression is definitely non-null.
- Default all other cases to non-uniform. This conservatively rejects
nullable arguments, nullable conditional expressions, narrowing and try
casts whose result may be null, multi-argument counts with any nullable
argument, and null-extended outer-join outputs.
- Preserve the safe optimization for non-null arguments.
## Tests
- `./run-fe-ut.sh --run org.apache.doris.nereids.properties.UniformTest`
(12 tests passed)
- `./build.sh --fe`
- `./run-regression-test.sh --run -f
regression-test/suites/nereids_rules_p0/eliminate_gby_key/eliminate_group_by_key_by_uniform.groovy
... -forceGenOut`
- Re-ran the same regression suite normally against the generated
expected output.
The regression asserts both results and plan group keys: nullable
`COUNT`/`NDV` remain in the outer grouping, while non-null `COUNT` still
permits safe group-key elimination.
Contributor
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
Contributor
|
run buildall |
yiguolei
approved these changes
Sep 19, 2026
Contributor
Author
|
PR approved by anyone and no changes requested. |
Contributor
Author
|
PR approved by at least one committer and no changes requested. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cherry-picked from #67881