Ruikai Shi created FLINK-41813:
----------------------------------

             Summary: VAR_POP / STDDEV_POP in OVER windows return wrong results 
for large-magnitude DOUBLE inputs (raw-moment expansion inherited from Calcite; 
FLINK-38740 fixed only GROUP BY)
                 Key: FLINK-41813
                 URL: https://issues.apache.org/jira/browse/FLINK-41813
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
    Affects Versions: 2.3.1
            Reporter: Ruikai Shi


h3. Problem

FLINK-38740 fixed the precision problem of {{STDDEV_*}} / {{VAR_*}} for regular 
GROUP BY aggregation by introducing a Welford-style M2 aggregate in 
{{AggregateReduceFunctionsRule}}. OVER-window aggregation takes a different 
path and is still affected.

For {{VAR_POP(x) OVER (...)}} (and {{VAR_SAMP}} / {{STDDEV_POP}} / 
{{STDDEV_SAMP}}), {{FlinkConvertletTable}} delegates to Calcite's 
{{StandardConvertletTable}}. That rewrites the aggregate into raw moments:

{noformat}
VAR_POP(x) = (SUM(x * x) - SUM(x) * SUM(x) / COUNT(x)) / COUNT(x)
{noformat}

For DOUBLE inputs with a large magnitude relative to their spread, such as 
epoch timestamps, counters, or prices in minor units, this loses all 
significant digits. The result can be 0, off by an order of magnitude, or 
negative (which gives NaN for {{STDDEV_*}}). The same problem has been reported 
to Calcite as CALCITE-7986.

h3. Reproduction 1 (batch mode): result is about 10x too large

{code:sql}
SET 'execution.runtime-mode' = 'batch';
SET 'parallelism.default' = '1';

WITH s(t, x) AS (VALUES
  (0, CAST(1609459228990 AS DOUBLE)), (1, CAST(1609459228991 AS DOUBLE)),
  (2, CAST(1609459228992 AS DOUBLE)), (3, CAST(1609459228993 AS DOUBLE)),
  (4, CAST(1609459228994 AS DOUBLE)), (5, CAST(1609459228995 AS DOUBLE)),
  (6, CAST(1609459228996 AS DOUBLE)), (7, CAST(1609459228997 AS DOUBLE)),
  (8, CAST(1609459228998 AS DOUBLE)), (9, CAST(1609459228999 AS DOUBLE)),
  (10, CAST(1609459229000 AS DOUBLE)), (11, CAST(1609459236000 AS DOUBLE)),
  (12, CAST(1609459243000 AS DOUBLE)), (13, CAST(1609459244000 AS DOUBLE)),
  (14, CAST(1609459247000 AS DOUBLE)))
SELECT VAR_POP(x) OVER w AS var_pop, STDDEV_POP(x) OVER w AS stddev_pop
FROM s WINDOW w AS (ORDER BY t ROWS BETWEEN 4 PRECEDING AND CURRENT ROW)
ORDER BY t DESC LIMIT 1;
{code}

* The last window is {{[1609459229000, 1609459236000, 1609459243000, 
1609459244000, 1609459247000]}} (epoch milliseconds).
* Expected (exact): {{VAR_POP = 42160000}}, {{STDDEV_POP ≈ 6493.0732}}
* Actual (Flink 2.3.1): {{VAR_POP = 429496729.6}}, {{STDDEV_POP ≈ 20724.3029}}

h3. Reproduction 2 (batch mode): result is 0

{code:sql}
WITH s(t, x) AS (VALUES
  (0, CAST(1445191307978 AS DOUBLE)),
  (1, CAST(1445191308963 AS DOUBLE)),
  (2, CAST(1445191308963 AS DOUBLE)),
  (3, CAST(1445191309228 AS DOUBLE)),
  (4, CAST(1445191310353 AS DOUBLE)))
SELECT VAR_POP(x) OVER w AS var_pop, STDDEV_POP(x) OVER w AS stddev_pop
FROM s WINDOW w AS (ORDER BY t ROWS BETWEEN 4 PRECEDING AND CURRENT ROW)
ORDER BY t DESC LIMIT 1;
{code}

* Expected (exact): {{VAR_POP = 576554}}, {{STDDEV_POP ≈ 759.3115}}
* Actual (Flink 2.3.1): {{VAR_POP = 0.0}}, {{STDDEV_POP = 0.0}}

Both results are bit-identical across repeated runs. Standalone Calcite 1.41.0 
produces the same values, and evaluating the expanded formula in IEEE double 
arithmetic reproduces them bit for bit. In streaming mode, bounded OVER windows 
(event time, {{ROWS ... PRECEDING}}) showed the same errors in our tests. On 
public log data with epoch timestamps, we also observed negative variances, and 
therefore NaN standard deviations, on windows whose true variance is positive.

h3. Where

* {{FlinkConvertletTable}} delegates {{VAR_*}} / {{STDDEV_*}} in OVER 
aggregation to Calcite's 
{{StandardConvertletTable.AvgVarianceConvertlet.expandVariance}} (Calcite 
1.41.0 is the dependency on current master).
* The FLINK-38740 change (PR #27325) modified {{AggregateReduceFunctionsRule}} 
and added {{WelfordM2AggFunction}}. It did not touch the OVER conversion path.

h3. Possible directions

# Route {{VAR_*}} / {{STDDEV_*}} in OVER aggregation to a numerically stable 
accumulator, for example reusing the M2 approach from FLINK-38740. Sliding OVER 
windows would need retraction support.
# Alternatively, fix this upstream in Calcite (CALCITE-7986) and pick it up 
with a Calcite upgrade. Since Flink already maintains its own stable M2 
aggregate, a Flink-side fix may be quicker.

Related: CALCITE-7986 (same root cause in Calcite), FLINK-38740 (GROUP BY fix), 
FLINK-41179 (a different variance issue with mini-batch two-phase GROUP BY).




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

Reply via email to