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

Incorrect query result caused by LEFT/RIGHT JOIN #51361

Closed
Syang111 opened this issue Feb 27, 2024 · 4 comments
Closed

Incorrect query result caused by LEFT/RIGHT JOIN #51361

Syang111 opened this issue Feb 27, 2024 · 4 comments
Assignees
Labels
affects-8.1 This bug affects the 8.1.x(LTS) versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.1 may-affects-6.5 may-affects-7.1 may-affects-7.5 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@Syang111
Copy link

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

CREATE TABLE t0(c0 FLOAT);
CREATE VIEW v0(c0) AS SELECT t0.c0 FROM t0;
INSERT INTO t0(c0) VALUES (NULL);

SELECT t0.c0 FROM v0 LEFT JOIN t0 ON 1 WHERE (TIMEDIFF( '2003-07-13', '2007-06-25') AND true);
SELECT c0 FROM (SELECT t0.c0, (TIMEDIFF('2003-07-13', '2007-06-25') AND true) IS TRUE AS flag FROM v0 LEFT JOIN t0 ON 1) AS t WHERE flag=1;

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

The following two sqls should return the same result

SELECT t0.c0 FROM v0 LEFT JOIN t0 ON 1 WHERE (TIMEDIFF( '2003-07-13', '2007-06-25') AND true);
SELECT c0 FROM (SELECT t0.c0, (TIMEDIFF('2003-07-13', '2007-06-25') AND true) IS TRUE AS flag FROM v0 LEFT JOIN t0 ON 1) AS t WHERE flag=1;

3. What did you see instead (Required)

These two sqls return different results. If change LEFT JOIN to RIGHT JOIN, you also get the same result as the one below.

mysql> SELECT t0.c0 FROM v0 LEFT JOIN t0 ON 1 WHERE (TIMEDIFF( '2003-07-13', '2007-06-25') AND true);
+------+
| c0   |
+------+
| NULL |
+------+
1 row in set, 2 warnings (0.01 sec)

mysql> SELECT c0 FROM (SELECT t0.c0, (TIMEDIFF('2003-07-13', '2007-06-25') AND true) IS TRUE AS flag FROM v0 LEFT JOIN t0 ON 1) AS t WHERE flag=1;
+------------+
| c0         |
+------------+
| 1.4013e-45 |
+------------+
1 row in set, 4 warnings (0.01 sec)

4. What is your TiDB version? (Required)

TiDB-v7.6.0

@Syang111 Syang111 added the type/bug The issue is confirmed as a bug. label Feb 27, 2024
@aytrack
Copy link
Contributor

aytrack commented Feb 28, 2024

[16:14:44]TiDB root:test> SELECT * FROM (SELECT t0.c0, (TIMEDIFF('2003-07-13', '2007-06-25') AND true) IS TRUE AS flag FROM v0 LEFT JOIN t0 ON 1) AS t  where flag = 1;
+------------+------+
| c0         | flag |
+------------+------+
| 1.4013e-45 | 1    |
+------------+------+
1 row in set
Time: 0.004s
[16:14:52]TiDB root:test> SELECT * FROM (SELECT t0.c0, (TIMEDIFF('2003-07-13', '2007-06-25') AND true) IS TRUE AS flag FROM v0 LEFT JOIN t0 ON 1) AS t ;
+--------+------+
| c0     | flag |
+--------+------+
| <null> | 1    |
+--------+------+
1 row in set
Time: 0.004s
[16:15:00]TiDB root:test> explain SELECT * FROM (SELECT t0.c0, (TIMEDIFF('2003-07-13', '2007-06-25') AND true) IS TRUE AS flag FROM v0 LEFT JOIN t0 ON 1) AS t  where flag = 1;
+--------------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
| id                       | estRows | task      | access object | operator info                                                                                                 |
+--------------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
| Projection_12            | 2.56    | root      |               | test.t0.c0, istrue(and(istrue_with_null(cast(timediff(2003-07-13, 2007-06-25), double BINARY)), 1))->Column#5 |
| └─HashJoin_14            | 2.56    | root      |               | CARTESIAN left outer join                                                                                     |
|   ├─Selection_15(Build)  | 1.60    | root      |               | eq(istrue(and(istrue_with_null(cast(timediff("2003-07-13", "2007-06-25"), double BINARY)), 1)), 1)            |
|   │ └─TableReader_18     | 2.00    | root      |               | data:TableFullScan_17                                                                                         |
|   │   └─TableFullScan_17 | 2.00    | cop[tikv] | table:t0      | keep order:false                                                                                              |
|   └─Selection_19(Probe)  | 1.60    | root      |               | eq(istrue(and(istrue_with_null(cast(timediff("2003-07-13", "2007-06-25"), double BINARY)), 1)), 1)            |
|     └─TableReader_21     | 2.00    | root      |               | data:TableFullScan_20                                                                                         |
|       └─TableFullScan_20 | 2.00    | cop[tikv] | table:t0      | keep order:false                                                                                              |
+--------------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
8 rows in set
Time: 0.004s
[16:15:10]TiDB root:test> explain SELECT * FROM (SELECT t0.c0, (TIMEDIFF('2003-07-13', '2007-06-25') AND true) IS TRUE AS flag FROM v0 LEFT JOIN t0 ON 1) AS t ;
+---------------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
| id                        | estRows | task      | access object | operator info                                                                                                 |
+---------------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
| Projection_8              | 4.00    | root      |               | test.t0.c0, istrue(and(istrue_with_null(cast(timediff(2003-07-13, 2007-06-25), double BINARY)), 1))->Column#5 |
| └─HashJoin_10             | 4.00    | root      |               | CARTESIAN left outer join                                                                                     |
|   ├─Projection_11(Build)  | 2.00    | root      |               | 1->Column#8                                                                                                   |
|   │ └─TableReader_13      | 2.00    | root      |               | data:TableFullScan_12                                                                                         |
|   │   └─TableFullScan_12  | 2.00    | cop[tikv] | table:t0      | keep order:false                                                                                              |
|   └─TableReader_15(Probe) | 2.00    | root      |               | data:TableFullScan_14                                                                                         |
|     └─TableFullScan_14    | 2.00    | cop[tikv] | table:t0      | keep order:false                                                                                              |
+---------------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+

@winoros
Copy link
Member

winoros commented Feb 28, 2024

The result of the select_15 and select_19 is correct, but the join is wrong.
image

@windtalker
Copy link
Contributor

The root cause is the same as #51290

@windtalker windtalker added sig/planner SIG: Planner and removed sig/execution SIG execution labels Mar 5, 2024
@ti-chi-bot ti-chi-bot added the affects-8.1 This bug affects the 8.1.x(LTS) versions. label Apr 9, 2024
@hawkingrei hawkingrei self-assigned this Jun 23, 2024
@hawkingrei
Copy link
Member

it has been fixed by #53235

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-8.1 This bug affects the 8.1.x(LTS) versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.1 may-affects-6.5 may-affects-7.1 may-affects-7.5 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

No branches or pull requests

7 participants