[ 
https://issues.apache.org/jira/browse/CALCITE-7970?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18151777#comment-18151777
 ] 

Mihai Budiu commented on CALCITE-7970:
--------------------------------------

Turns out that COVER_* and REGR_* have the same problem. I will file a separate 
PR for these under the same issue.

> AggregateReduceFunctionsRule produces incorrect results for DISTINCT variance 
> and standard deviation
> ----------------------------------------------------------------------------------------------------
>
>                 Key: CALCITE-7970
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7970
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: dylanhz
>            Assignee: dylanhz
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 1.43.0
>
>
> When AggregateReduceFunctionsRule runs before DISTINCT aggregates are 
> expanded, VAR_POP(DISTINCT x) and VAR_SAMP(DISTINCT x) can return incorrect 
> results for inputs that contain both a value and its negation. The same 
> rewrite is used for STDDEV_POP and STDDEV_SAMP.
> h3. Reproduction
> Run the following query with a HepPlanner program containing 
> {{{}CoreRules.AGGREGATE_REDUCE_FUNCTIONS{}}}, then pass the resulting plan to 
> {{Programs.standard()}} for execution. The rule order matters because 
> expanding DISTINCT first avoids the faulty rewrite.
> {code:sql}
> SELECT VAR_POP(DISTINCT x) AS vp, VAR_SAMP(DISTINCT x) AS vs
> FROM (VALUES (CAST(-1 AS DOUBLE)), (1)) AS t(x)
> {code}
> ||Result||VP||VS||
> |Expected|1.0|2.0|
> |Actual|0.5|1.0|
> h3. Cause
> The distinct input values are -1 and 1. Their mean is 0, and each contributes 
> 1 to the sum of squares, so the sum of squares is 2. The rewrite generates 
> {{{}SUM(DISTINCT x * x){}}}, which sees two identical squares and counts only 
> one. The error occurs even with exact arithmetic.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to