Incorrect result for variance-equivalent aggregation expression with constant input
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
### 1. Minimal reproduce step (Required)
drop table if exists t0,t1;
create table t0
(
c0 text null
);
create table t1
(
c0 text null
);
INSERT INTO database0.t0 (c0)
VALUES ('-2012790846');
INSERT INTO database0.t1 (c0)
VALUES ('TE\'~{v');
INSERT INTO database0.t1 (c0)
VALUES ('/');
INSERT INTO database0.t1 (c0)
VALUES ('');
INSERT INTO database0.t1 (c0)
VALUES ('!gA');
INSERT INTO database0.t1 (c0)
VALUES ('Gt*J');
INSERT INTO database0.t1 (c0)
VALUES ('0.3573013800560694');
SELECT (SUM((t0.c0) * (t0.c0)) - SUM((t0.c0)) * SUM((t0.c0)) / COUNT((t0.c0))) / NULLIF(COUNT((t0.c0)), 0)
FROM t1
STRAIGHT_JOIN t0;
### 2. What did you expect to see? (Required)
The query is mathematically equivalent to the population variance formula:
Var(X) = (Σx² - (Σx)² / n) / n
In this case:
• All values of t0.c0 are identical (-2012790846)
• The join produces 6 identical rows
So:
x₁ = x₂ = ... = xₙ
⇒ Var(X) = 0
Expected result:
0
### 3. What did you see instead (Required)
682.6666666666666
### 4. What is your TiDB version? (Required)
Release Version: v8.5.5
Edition: Community
Git Commit Hash: 1fa258b833ff113883beeba40bc130be7ce66610
Git Branch: HEAD
UTC Build Time: 2026-01-14 22:19:19
GoVersion: go1.25.5
Race Enabled: false
Check Table Before Drop: false
Store: tikv
Contributor guide
Assessment
This issue has not been assessed yet.