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

   ## Problem
   
   Nereids can infer that a grouped aggregate output is unique solely because 
the aggregate input slot is unique. A later `GROUP BY` may then be removed even 
when different input groups produce the same aggregate value, resulting in 
duplicate rows or incorrect counts.
   
   ## Root cause
   
   The aggregate injectivity check recognized only the aggregate function 
class. It did not validate the complete argument expression or the conversion 
from the argument type to the aggregate result type. For example, 
`SUM(ABS(pk))` maps both `-1` and `1` to `1`, while `AVG` converts `BIGINT` to 
`DOUBLE` and can collapse adjacent values above the exact integer range of 
`DOUBLE`.
   
   ## Reproduction
   
   Create a unique-key table containing `-1`, `1`, `9007199254740992`, and 
`9007199254740993`. Group by the unique key in a subquery and expose either 
`SUM(ABS(pk))` or `AVG(pk)`, then group by that aggregate result in an outer 
query. The unsafe uniqueness inference removes the outer aggregation and 
returns two rows instead of one row with count `2`.
   
   ## Fix
   
   Use the existing conservative type-level injective-cast proof for aggregate 
uniqueness inference. The output is considered unique only when the function is 
`SUM`, `AVG`, `MIN`, or `MAX`, its argument reduces to a bare slot through 
injective cast steps, and the single-row aggregate result conversion is also 
injective. Any expression or conversion that cannot be proven injective no 
longer participates in this optimization. The shared helper covers both logical 
and physical aggregate traits.
   
   ## Tests
   
   - `./run-fe-ut.sh --run org.apache.doris.nereids.properties.UniqueTest`
   - `./build.sh --fe`
   - `./run-regression-test.sh --run -f 
regression-test/suites/nereids_rules_p0/eliminate_gby_key/eliminate_group_by.groovy`
   
   All tests passed. The regression suite covers both a non-injective argument 
expression and a lossy aggregate result conversion.
   


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