tidb icon indicating copy to clipboard operation
tidb copied to clipboard

The query results provided by TiDB are unstable

Open sjyango opened this issue 10 months ago • 3 comments

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

CREATE TABLE t0 (
  c0 decimal(10, 0) NOT NULL,
  c1 float unsigned zerofill NOT NULL,
  PRIMARY KEY (c0, c1)
);

INSERT INTO t0 VALUES (-2068985011, 0.75245386), (-668435082, 0.19411194), (-500731198, 0.39079505), (0, 0), (0, 0.9938275), (12196703, 970789000), (919009011, 0.28699672), (1069380201, 0.2576304);

SELECT t0.c0 FROM t0 GROUP BY t0.c0, CAST(t0.c1 AS DATE), t0.c0 OR '' HAVING NOT (t0.c0);

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

+----+
| c0 |
+----+
|  0 |
+----+

3. What did you see instead (Required)

The query results provided by TiDB are unstable, and there is a small probability of different results(Result 1 and Result 2) occurring when the same query is executed multiple times.

Result 1:
MySQL> SELECT t0.c0 FROM t0 GROUP BY t0.c0, CAST(t0.c1 AS DATE), t0.c0 OR '' HAVING NOT (t0.c0);
+----+
| c0 |
+----+
|  0 |
+----+
1 row in set (0.002 sec)

Result 2:
MySQL> SELECT t0.c0 FROM t0 GROUP BY t0.c0, CAST(t0.c1 AS DATE), t0.c0 OR '' HAVING NOT (t0.c0);
+----+
| c0 |
+----+
|  0 |
|  0 |
+----+
2 rows in set, 1 warning (0.005 sec)

4. What is your TiDB version? (Required)

MySQL> select tidb_version();
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                               |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v8.0.0
Edition: Community
Git Commit Hash: 8ba1fa452b1ccdbfb85879ea94b9254aabba2916
Git Branch: HEAD
UTC Build Time: 2024-03-28 14:22:04
GoVersion: go1.21.6
Race Enabled: false
Check Table Before Drop: false
Store: tikv |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.002 sec)

sjyango avatar Apr 27 '24 06:04 sjyango

The root cause is tikv's cast decimal as date is not consistent with tidb. If planner generates two-phase agg, then tikv will execute the cast decimal as date, otherwise, tidb will.

yibin87 avatar Apr 29 '24 06:04 yibin87

/label affects-8.1

yibin87 avatar Apr 29 '24 06:04 yibin87

/severity major

yibin87 avatar Apr 29 '24 06:04 yibin87

The bug is not just that TiKV's cast_real_as_time is inconsistent with TiDB, but that the TiDB and TiKV's cast_real_as_time are inconsistent with TiDB. MySQL 8.3.0

mysql> select c1, cast(c1 as date) from t0 order by c1;
+--------------+------------------+
| c1           | cast(c1 as date) |
+--------------+------------------+
| 000000000000 | NULL             |
| 00000.194112 | NULL             |
| 000000.25763 | NULL             |
| 00000.286997 | NULL             |
| 00000.390795 | NULL             |
| 00000.752454 | NULL             |
| 00000.993828 | NULL             |
| 000970789000 | NULL             |
+--------------+------------------+
8 rows in set, 8 warnings (0.02 sec)

TiDB

mysql> select c1, cast(c1 as date) from t0 order by c1;
+------------+---------------------+
| c1         | cast(c1 as date)    |
+------------+---------------------+
|          0 | 0000-00-00 00:00:00 |
| 0.19411194 | 0000-00-00          |
|  0.2576304 | 0000-00-00          |
| 0.28699672 | 0000-00-00          |
| 0.39079505 | 0000-00-00          |
| 0.75245386 | NULL                |
|  0.9938275 | NULL                |
|  970789000 | NULL                |
+------------+---------------------+
8 rows in set, 3 warnings (0.01 sec)

TiKV

mysql> select c1, cast(c1 as date) from t0 order by c1;
+------------+------------------+
| c1         | cast(c1 as date) |
+------------+------------------+
|          0 | 0000-00-00       |
| 0.19411194 | 0000-00-00       |
|  0.2576304 | 0000-00-00       |
| 0.28699672 | 0000-00-00       |
| 0.39079505 | 0000-00-00       |
| 0.75245386 | 0000-00-00       |
|  0.9938275 | 0000-00-00       |
|  970789000 | NULL             |
+------------+------------------+
8 rows in set, 1 warning (0.01 sec)

gengliqi avatar Aug 22 '24 16:08 gengliqi