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

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


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