Inconsistent results for equivalent AVG expressions under join expansion (floating-point precision issue)
- 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 t1;
CREATE TABLE t1 (
c0 BLOB NOT NULL
);
DROP TABLE IF EXISTS t2;
CREATE TABLE t2 (
c0 TINYBLOB NOT NULL
);
INSERT INTO t2 (c0) VALUES (0x302E36373033313336383532353434373535);
INSERT INTO t2 (c0) VALUES (0x55755F6CE9BC9031733F);
INSERT INTO t2 (c0) VALUES ('');
INSERT INTO t2 (c0) VALUES ('');
INSERT INTO t2 (c0) VALUES ('');
INSERT INTO t1 (c0) VALUES (0x32303639303530303736);
-- Query 1
SELECT AVG((t1.c0) * (t1.c0))
FROM t1;
-- Query 2
SELECT AVG((t1.c0) * (t1.c0))
FROM t2, t1;
### 2. What did you expect to see? (Required)
The two queries are semantically equivalent.
The second query only introduces a Cartesian product that duplicates identical values, which should not affect the result of the AVG aggregation.
Mathematically:
AVG(x²) ≡ AVG(x², x², …, x²)
Therefore, both queries are expected to return exactly the same result.
### 3. What did you see instead (Required)
The two queries return different results:
Query 1:
4280968216995606000
Query 2:
4280968216995605500
The difference is small but non-zero (Δ = 500).
This indicates that the result depends on the execution plan (specifically, whether a Cartesian product is introduced), which violates expected deterministic behavior for equivalent queries.
### 4. What is your TiDB version? (Required)
-- Database version: 8.0.11-TiDB-v8.5.5
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.