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

Query returns wrong result if tidb_opt_derive_topn is on #42321

Closed
windtalker opened this issue Mar 16, 2023 · 0 comments · Fixed by #42334
Closed

Query returns wrong result if tidb_opt_derive_topn is on #42321

windtalker opened this issue Mar 16, 2023 · 0 comments · Fixed by #42334
Labels
severity/major type/bug The issue is confirmed as a bug.

Comments

@windtalker
Copy link
Contributor

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

mysql> show create table t;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                        |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t     | CREATE TABLE `t` (
  `id1` int(11) NOT NULL,
  `id2` int(11) NOT NULL,
  `value1` int(11) DEFAULT NULL,
  `value2` int(11) DEFAULT NULL,
  PRIMARY KEY (`id1`,`id2`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from t;
+-----+-----+--------+--------+
| id1 | id2 | value1 | value2 |
+-----+-----+--------+--------+
|   1 |   1 |      1 |      1 |
|   1 |   2 |      1 |      1 |
|   1 |   3 |      1 |      1 |
|   1 |   4 |      1 |      1 |
|   2 |   1 |      1 |      1 |
|   2 |   2 |      1 |      1 |
|   2 |   3 |      1 |      1 |
|   2 |   4 |      1 |      1 |
+-----+-----+--------+--------+
8 rows in set (0.00 sec)

mysql> set tidb_opt_derive_topn=off;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from (select *, row_number() over (partition by id1) as rownumber from t) DT where rownumber <= 3;
+-----+-----+--------+--------+-----------+
| id1 | id2 | value1 | value2 | rownumber |
+-----+-----+--------+--------+-----------+
|   1 |   1 |      1 |      1 |         1 |
|   1 |   2 |      1 |      1 |         2 |
|   1 |   3 |      1 |      1 |         3 |
|   2 |   1 |      1 |      1 |         1 |
|   2 |   2 |      1 |      1 |         2 |
|   2 |   3 |      1 |      1 |         3 |
+-----+-----+--------+--------+-----------+
6 rows in set (0.01 sec)

mysql> set tidb_opt_derive_topn=on;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from (select *, row_number() over (partition by id1) as rownumber from t) DT where rownumber <= 3;
+-----+-----+--------+--------+-----------+
| id1 | id2 | value1 | value2 | rownumber |
+-----+-----+--------+--------+-----------+
|   1 |   1 |      1 |      1 |         1 |
|   1 |   2 |      1 |      1 |         2 |
|   1 |   3 |      1 |      1 |         3 |
+-----+-----+--------+--------+-----------+
3 rows in set (0.00 sec)

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

When set tidb_opt_derive_topn to on or off, the query result should be the same

3. What did you see instead (Required)

When set tidb_opt_derive_topn to on, query result is wrong.

4. What is your TiDB version? (Required)

master@ fa28db1

@windtalker windtalker added the type/bug The issue is confirmed as a bug. label Mar 16, 2023
@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.1 may-affects-6.5 labels Mar 16, 2023
@windtalker windtalker removed may-affects-4.0 This bug maybe affects 4.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-5.0 This bug maybe affects 5.0.x versions. may-affects-6.1 may-affects-6.5 labels Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity/major type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants