wjjc1017 opened a new issue, #69491:
URL: https://github.com/apache/doris/issues/69491

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   
   ### Version
   
   Doris Nereids `master`
   
   ### What's Wrong?
   
   
`fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushCountIntoUnionAll.java`
 accepts a compound
   expression containing `COUNT` but only rewrites a *direct* 
`Alias(Count(...))`:
   
   - `checkAgg` (lines 163-165) collects `Count` **recursively** from the 
output expressions, so
     `Alias(Add(Count(k), 10), "res")` passes the guard.
   - The upper-aggregate `Sum0` rewrite (lines 127-133) and the per-child 
rewrite (lines 142-155) only match
     `expr instanceof Alias && ((Alias) expr).child() instanceof Count`.
   
   For `COUNT(k) + 10` neither match fires. Each `UNION ALL` branch is 
aggregated by `k` (so at most one row per
   `k`), and the upper `LogicalAggregate` re-executes `COUNT(k) + 10` on the 
already-deduplicated `k` column instead
   of `SUM0` over the per-branch counts.
   
   ### What You Expected?
   
   - Expected: `COUNT(k) = 6`, so `res = 16`.
   - Actual: `res = 12` (each branch counts at most one row per `k`: `1 + 1 + 
10`).
   
   With plain `COUNT(k)` (no `+ 10`) the same query returns `6` correctly, 
confirming the failure is specific to the
   compound expression.
   
   ### How to Reproduce?
   
   ```sql
   CREATE TABLE a (k INT) DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1;
   CREATE TABLE b (k INT) DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1;
   INSERT INTO a VALUES (10), (10), (10);
   INSERT INTO b VALUES (10), (10), (10);
   
   SELECT k, COUNT(k) + 10 AS res
   FROM (SELECT k FROM a UNION ALL SELECT k FROM b) t
   GROUP BY k;
   ```
   
   ### Anything Else?
   
   ### Root cause
   
   The guard and the rewrite disagree on the expression shape. `checkAgg` 
allows nested `Count`, while
   `doPush` only replaces `Alias(Count(...))`; the residual `COUNT(k)` is then 
evaluated *above* the grouping.
   
   ### Suggested fix
   
   Either (a) tighten `checkAgg` to require every output expression to be a 
bare `Alias(Count(...))` (reject
   compound expressions), or (b) make the rewrite recursive: replace **every** 
`Count` subtree inside the output
   expression with a reference to the corresponding per-branch `Sum0` output 
(and mirror the same transformation in
   `replaceExpressionByUnionAll`). Option (a) is the minimal safe fix.
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


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