tidb icon indicating copy to clipboard operation
tidb copied to clipboard

Pushdown TopN into TiKV with `cast` function meets incorrect result

Open Defined2014 opened this issue 1 year ago • 1 comments

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

Defined2014 avatar Aug 27 '24 06:08 Defined2014

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

Defined2014 avatar Aug 28 '24 08:08 Defined2014