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

Wrong results with correlated subquery #32089

Closed
ramanich1 opened this issue Feb 2, 2022 · 2 comments · Fixed by #32370
Closed

Wrong results with correlated subquery #32089

ramanich1 opened this issue Feb 2, 2022 · 2 comments · Fixed by #32370
Assignees
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. severity/critical sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@ramanich1
Copy link
Collaborator

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

drop table if exists t1,t2;
CREATE TABLE t1(a INT PRIMARY KEY);
CREATE TABLE t2(b INT,c INT);
INSERT INTO t1 VALUES (1), (2);
INSERT INTO t2 VALUES (1,2), (2,3);
SELECT (SELECT 1 FROM t1 WHERE a=b AND c=1) FROM t2;

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

mysql> SELECT (SELECT 1 FROM t1 WHERE a=b AND c=1) FROM t2;
+--------------------------------------+
| (SELECT 1 FROM t1 WHERE a=b AND c=1) |
+--------------------------------------+
|                                 NULL |
|                                 NULL |
+--------------------------------------+
2 rows in set (0.00 sec)

3. What did you see instead (Required)

mysql> SELECT (SELECT 1 FROM t1 WHERE a=b AND c=1) FROM t2;
+--------------------------------------+
| (SELECT 1 FROM t1 WHERE a=b AND c=1) |
+--------------------------------------+
|                                    1 |
|                                    1 |
+--------------------------------------+
2 rows in set (0.00 sec)

4. What is your TiDB version? (Required)

| Release Version: v5.5.0-alpha-168-g23f7e51ae
Edition: Community
Git Commit Hash: 23f7e51ae01287fa7f811e1462d8987e4d7727a4
Git Branch: master
UTC Build Time: 2022-01-27 17:39:21
GoVersion: go1.17.2
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
@ramanich1 ramanich1 added the type/bug The issue is confirmed as a bug. label Feb 2, 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. labels Feb 2, 2022
@morgo morgo changed the title TiDB nested select results not consistent with Mysql. Wrong results with correlated subquery Feb 2, 2022
@morgo
Copy link
Contributor

morgo commented Feb 2, 2022

Verified as described. This looks to be a bug in the 'correlated' part of the subquery:

tidb> SELECT (SELECT 1 FROM t1 WHERE c=1) FROM t2;
+------------------------------+
| (SELECT 1 FROM t1 WHERE c=1) |
+------------------------------+
|                         NULL |
|                         NULL |
+------------------------------+
2 rows in set (0.00 sec)

tidb> SELECT (SELECT 1 FROM t1 WHERE a=b AND c=1) FROM t2;
+--------------------------------------+
| (SELECT 1 FROM t1 WHERE a=b AND c=1) |
+--------------------------------------+
|                                    1 |
|                                    1 |
+--------------------------------------+
2 rows in set (0.00 sec)

@morgo morgo added the sig/planner SIG: Planner label Feb 2, 2022
@jebter jebter added 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. labels Feb 8, 2022
@ti-chi-bot ti-chi-bot 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. may-affects-5.0 This bug maybe affects 5.0.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. labels Feb 8, 2022
@AilinKid
Copy link
Contributor

AilinKid commented Feb 10, 2022

Verified as described. This looks to be a bug in the 'correlated' part of the subquery:

tidb> SELECT (SELECT 1 FROM t1 WHERE c=1) FROM t2;
+------------------------------+
| (SELECT 1 FROM t1 WHERE c=1) |
+------------------------------+
|                         NULL |
|                         NULL |
+------------------------------+
2 rows in set (0.00 sec)

tidb> SELECT (SELECT 1 FROM t1 WHERE a=b AND c=1) FROM t2;
+--------------------------------------+
| (SELECT 1 FROM t1 WHERE a=b AND c=1) |
+--------------------------------------+
|                                    1 |
|                                    1 |
+--------------------------------------+
2 rows in set (0.00 sec)

seems "SELECT (SELECT a FROM t1 WHERE a=b AND c=1) FROM t2;" can make it.
the number 1 should be explained as position parameter rather than constant value

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. severity/critical sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants