Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TiDB (with tikv) produces different results for SELECT statements when using transaction #36903

Closed
JZuming opened this issue Aug 4, 2022 · 2 comments · Fixed by #36961
Closed
Labels
affects-4.0 This bug affects 4.0.x versions. affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 severity/major type/bug The issue is confirmed as a bug.

Comments

@JZuming
Copy link

JZuming commented Aug 4, 2022

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

Setup the environment:

tiup playground &
mysql -h "127.0.0.1" -u root -P 4000
MySQL> create database testdb;
MySQL> \q
mysql -h "127.0.0.1" -u root -P 4000 -D testdb < mysql_bk.sql

mysql_bk.sql: mysql_bk.txt

Testcase 1

mysql -h "127.0.0.1" -u root -P 4000 -D testdb

mysql> insert into t_vwvgdc (wkey, pkey, c_rdsfbc) values (155, 228000, 99.50);
mysql> select
                  ref_4.pkey as c0
                from
                  t_vwvgdc as ref_4
                where 0 <> 0
              union
              select
                  ref_5.pkey as c0
                from
                  t_vwvgdc as ref_5;

Testcase 2

mysql -h "127.0.0.1" -u root -P 4000 -D testdb

mysql> BEGIN OPTIMISTIC;
mysql> insert into t_vwvgdc (wkey, pkey, c_rdsfbc) values (155, 228000, 99.50);
mysql> select
                  ref_4.pkey as c0
                from
                  t_vwvgdc as ref_4
                where 0 <> 0
              union
              select
                  ref_5.pkey as c0
                from
                  t_vwvgdc as ref_5;
mysql> commit;

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

The execution result of SELECT statements in Testcase 1 and Testcase 2 are same.

3. What did you see instead (Required)

Output of SELECT statement in Testcase 1

+--------+
| c0     |
+--------+
|  17000 |
|  15000 |
|  66000 |
|    ... |
| 228000 |
+--------+
30 rows in set (0.00 sec)

Output of SELECT statement in Testcase 2

Empty set (0.00 sec)

Their results are different.

4. What is your TiDB version? (Required)

Release Version: v6.1.0
Edition: Community
Git Commit Hash: 1a89decdb192cbdce6a7b0020d71128bc964d30f
Git Branch: HEAD
UTC Build Time: 2022-08-01 09:18:07
GoVersion: go1.18
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
@JZuming JZuming added the type/bug The issue is confirmed as a bug. label Aug 4, 2022
@tiancaiamao
Copy link
Contributor

Do you mean there is a UnionScan executor? if so, that's expected.
Inside transaction, we use a UnionScan to combine the data from TiKV snapshot and transaction's in-memory buffer.

All the changes are buffered in the transaction before it commit. Without UnionScan, you can't read what you write, so the plan is different. @JZuming

@tiancaiamao tiancaiamao added type/question The issue belongs to a question. and removed type/bug The issue is confirmed as a bug. labels Aug 5, 2022
@JZuming
Copy link
Author

JZuming commented Aug 5, 2022

Quite strange. Here is another test case

BEGIN OPTIMISTIC;

insert into t_vwvgdc (wkey, pkey, c_rdsfbc) values (155, 228000, 99.50);

select pkey from t_vwvgdc where 0 <> 0;   --- output empty set

select pkey from t_vwvgdc;   --- output 30 rows

select pkey from t_vwvgdc where 0 <> 0
union
select pkey from t_vwvgdc;   --- output empty set

commit;

The first SELECT outputs an empty set, and the second SELECT outputs a set with 30 rows. It is quite strange that the third SELECT (first SELECT union second SELECT) outputs an empty set.

@tiancaiamao tiancaiamao added type/bug The issue is confirmed as a bug. severity/major and removed type/question The issue belongs to a question. labels Aug 8, 2022
@ti-chi-bot ti-chi-bot added may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.0 This bug maybe affects 5.0.x versions. may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.0 may-affects-6.1 may-affects-6.2 labels Aug 8, 2022
@tiancaiamao tiancaiamao added affects-5.0 This bug affects 5.0.x versions. affects-4.0 This bug affects 4.0.x versions. and removed may-affects-5.0 This bug maybe affects 5.0.x versions. labels Aug 8, 2022
@ti-chi-bot ti-chi-bot removed the may-affects-4.0 This bug maybe affects 4.0.x versions. label Aug 8, 2022
@tiancaiamao tiancaiamao removed may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. labels Aug 8, 2022
@tiancaiamao tiancaiamao added affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. and removed may-affects-5.4 This bug maybe affects 5.4.x versions. labels Aug 8, 2022
ti-chi-bot pushed a commit that referenced this issue Aug 24, 2022
ti-chi-bot pushed a commit that referenced this issue Sep 28, 2022
ti-chi-bot pushed a commit that referenced this issue Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-4.0 This bug affects 4.0.x versions. affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 severity/major type/bug The issue is confirmed as a bug.
Projects
None yet
3 participants