tidb icon indicating copy to clipboard operation
tidb copied to clipboard

Shouldn't push down the topN when the result for the order by item is uncertain

Open Reminiscent opened this issue 3 years ago • 1 comments

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

drop table if exists t3;
CREATE TABLE t3(c0 INT, primary key(c0));
insert into t3 values(1), (2), (3), (4), (5);
SELECT v2.c0 FROM (select rand() as c0 from t3) v2 order by v2.c0 limit 100;

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

The result should be ordered.

mysql> SELECT v2.c0 FROM (select rand() as c0 from t3) v2 order by v2.c0 limit 100;
+---------------------+
| c0                  |
+---------------------+
| 0.10052517065827285 |
|  0.7717371729870673 |
|  0.8531838886935114 |
|   0.883147331777166 |
|  0.9643439622263833 |
+---------------------+
5 rows in set (0.00 sec)

3. What did you see instead (Required)

The result is not ordered.

mysql> SELECT v2.c0 FROM (select rand() as c0 from t3) v2 order by v2.c0 limit 100;
+--------------------+
| c0                 |
+--------------------+
| 0.3638526968321322 |
| 0.3650706674578326 |
| 0.7337952775264115 |
| 0.5737644159922045 |
| 0.6674362781154329 |
+--------------------+
5 rows in set (0.00 sec)
mysql> explain SELECT v2.c0 FROM (select rand() as c0 from t3) v2 order by v2.c0 limit 2;
+--------------------------------+----------+-----------+---------------+--------------------------------+
| id                             | estRows  | task      | access object | operator info                  |
+--------------------------------+----------+-----------+---------------+--------------------------------+
| Projection_7                   | 2.00     | root      |               | rand()->Column#2               |
| └─Projection_13                | 2.00     | root      |               | test.t3.c0                     |
|   └─TopN_8                     | 2.00     | root      |               | Column#3, offset:0, count:2    |
|     └─Projection_14            | 10000.00 | root      |               | test.t3.c0, rand()->Column#3   |
|       └─TableReader_12         | 10000.00 | root      |               | data:TableFullScan_11          |
|         └─TableFullScan_11     | 10000.00 | cop[tikv] | table:t3      | keep order:false, stats:pseudo |
+--------------------------------+----------+-----------+---------------+--------------------------------+
6 rows in set (0.00 sec)

4. What is your TiDB version? (Required)

latest master

Reminiscent avatar Sep 20 '22 07:09 Reminiscent

Though affect the version5.0-5.2. But we don't need to cherry-pick to version5.0-5.2. Just add the label to record.

Reminiscent avatar Sep 22 '22 07:09 Reminiscent