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

   ### 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/EliminateGroupBy.java`
 treats sample and
   population dispersion functions identically:
   
   ```java
   // lines 71-72
   private static final ImmutableSet<Class<? extends Expression>> 
supportedDevLikeFunctions
           = ImmutableSet.of(Stddev.class, StddevSamp.class, Variance.class, 
VarianceSamp.class);
   
   // lines 133-136
   } else if (supportedDevLikeFunctions.contains(f.getClass())) {
       Expression expr = ifNullElse(f.child(0), new 
NullLiteral(DoubleType.INSTANCE),
               new DoubleLiteral(0));
       newOutput.add((NamedExpression) ne.withChildren(expr));
   }
   ```
   
   When grouping by a unique, non-null key each group has exactly `n = 1` row. 
Population variance/stddev
   (`VAR_POP` / `STDDEV_POP`) divide by `n = 1` and indeed return `0.0`. 
**Sample** variance/stddev
   (`VAR_SAMP` / `STDDEV_SAMP`) divide by `n - 1 = 0` and must return `NULL`; 
the rewrite silently returns `0.0`.
   
   ### What You Expected?
   
   - Expected: `(1, NULL, NULL)`, `(2, NULL, NULL)` (each group is a single 
row).
   - Actual: `(1, 0.0, 0.0)`, `(2, 0.0, 0.0)`.
   
   (`STDDEV` / `VARIANCE` — i.e. population — correctly return `0.0` and must 
keep doing so.)
   
   ### How to Reproduce?
   
   ```sql
   CREATE TABLE t (k INT, x INT) DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) 
BUCKETS 1;
   INSERT INTO t VALUES (1, 5), (2, 7);
   
   SELECT k, STDDEV_SAMP(x), VAR_SAMP(x) FROM t GROUP BY k;
   ```
   
   ### Anything Else?
   
   ### Root cause
   
   `supportedDevLikeFunctions` conflates population and sample variants. The 
empty-group/one-row compensation
   `ifNullElse(arg, NULL, 0.0)` is only valid for the population functions.
   
   ### Suggested fix
   
   Split the set: keep `Stddev` / `Variance` in `supportedDevLikeFunctions`, 
and handle `StddevSamp` /
   `VarianceSamp` separately by rewriting to `NULL` (not `0.0`) when the group 
is a single row — or simply
   exclude the sample variants from `canRewrite` and keep the aggregate.
   
   ### 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