[
https://issues.apache.org/jira/browse/CALCITE-7970?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
dylanhz updated CALCITE-7970:
-----------------------------
Description:
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.
was:
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.
h3. Interpreter regression coverage
The fix should also preserve execution when the logical plan is passed directly
to {{Interpreter}}. Its optimization program runs
{{AGGREGATE_REDUCE_FUNCTIONS}} without a DISTINCT expansion step. Simply
skipping reduction for DISTINCT variance leaves the interpreter with a
{{VAR_POP}} call it cannot execute.
{code:sql}
SELECT VAR_POP(DISTINCT x)
FROM (VALUES (CAST(1 AS DOUBLE)), (2)) AS t(x)
{code}
Direct interpretation should return {{0.25}}. This case uses two positive
values so it checks that the query remains executable independently of the
opposite-sign example above.
> 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
> Priority: Major
>
> 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)