Skip to content

STDDEV_POP / VAR_* over BIGINT UNSIGNED near 2^64 wrong on UNION ALL #70400

Description

@wanteatfruit

Bug Report

On a base table (or a materialized copy of a UNION ALL), TiDB correctly returns 0.0. Over a ≥3-way UNION ALL of the same rows (e.g. MOD(sh,4)=0/1/2/3 partitions), the same aggregates return a nonzero value (STDDEV_POP443.405). A 2-way UNION ALL still returns 0.0. MySQL 9.7.2 and MariaDB 11.4 return 0.0 for the 4-way form.

1. Minimal reproduce step (Required)

CREATE TABLE u (sh BIGINT UNSIGNED);
INSERT INTO u VALUES
  (18446744073709551614),
  (18446744073709551613),
  (18446744073709551612),
  (18446744073709551611),
  (18446744073709551610),
  (18446744073709551609),
  (18446744073709551608),
  (18446744073709551607);

SELECT STDDEV_POP(sh) FROM u;
-- 0.0

SELECT STDDEV_POP(sh) FROM (
  SELECT sh FROM u WHERE MOD(sh, 4) = 0
  UNION ALL SELECT sh FROM u WHERE MOD(sh, 4) = 1
  UNION ALL SELECT sh FROM u WHERE MOD(sh, 4) = 2
  UNION ALL SELECT sh FROM u WHERE MOD(sh, 4) = 3
) AS x;
-- 443.40500673763256

2. What did you expect to see? (Required)

3. What did you see instead (Required)

4. What is your TiDB version? (Required)

tidb 8.0.11-TiDB-v8.5.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions