tidb
tidb copied to clipboard
Pushdown TopN into TiKV with `cast` function meets incorrect result
Bug Report
Please answer these questions before submitting your issue. Thanks!
1. Minimal reproduce step (Required)
CREATE TABLE `test` (
`id` bigint(20) NOT NULL,
`update_user` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
insert into test values(1,'å¼ ä¸');
insert into test values(2,'æå');
insert into test values(3,'å¼ ä¸');
insert into test values(4,'æå');
insert into test values(5,'å¼ ä¸');
insert into test values(6,'æå');
insert into test values(7,'å¼ ä¸');
insert into test values(8,'æå');
insert into test values(9,'å¼ ä¸');
insert into test values(10,'æå');
insert into test values(11,'å¼ ä¸');
insert into test values(12,'æå');
insert into test values(13,'å¼ ä¸');
insert into test values(14,'æå');
select * from test order by cast(update_user as char character set gbk) desc , id limit 10;
2. What did you expect to see? (Required)
MySQL [test]> select * from test order by cast(update_user as char character set gbk) desc , id limit 10;
+----+-------------+
| id | update_user |
+----+-------------+
| 1 | å¼ ä¸ |
| 3 | å¼ ä¸ |
| 5 | å¼ ä¸ |
| 7 | å¼ ä¸ |
| 9 | å¼ ä¸ |
| 11 | å¼ ä¸ |
| 13 | å¼ ä¸ |
| 2 | æå |
| 4 | æå |
| 6 | æå |
+----+-------------+
10 rows in set (0.00 sec)
3. What did you see instead (Required)
MySQL [test]> select * from test order by cast(update_user as char character set gbk) desc, id limit 10;
+----+-------------+
| id | update_user |
+----+-------------+
| 1 | å¼ ä¸ |
| 3 | å¼ ä¸ |
| 5 | å¼ ä¸ |
| 2 | æå |
| 4 | æå |
| 6 | æå |
| 8 | æå |
| 10 | æå |
| 12 | æå |
| 14 | æå |
+----+-------------+
10 rows in set (0.00 sec)
4. What is your TiDB version? (Required)
v7.5.1, nightly(v8.3.0+) and more
The typeFiled of cast function is
{
charset: "gbk",
collation: "utf8bm4_bin"
}
It's set by https://github.com/pingcap/tidb/blob/74404a41cad60352ef087aa4ec21ead74fce6c65/pkg/expression/expr_to_pb.go#L291