Skip to content

Pushdown TopN into TiKV with cast function meets incorrect result #55677

@Defined2014

Description

@Defined2014

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; 

Another test case

CREATE TABLE `test` (
  `id` bigint NOT NULL,
  `update_user` varchar(32) CHARACTER SET gbk COLLATE gbk_chinese_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) 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) 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)

Second case

mysql> select * from test order by cast(update_user as char) desc , id limit 10;
+----+-------------+
| id | update_user |
+----+-------------+
|  2 | 李四        |
|  4 | 李四        |
|  6 | 李四        |
|  8 | 李四        |
| 10 | 李四        |
| 12 | 李四        |
| 14 | 李四        |
|  1 | 张三        |
|  3 | 张三        |
|  5 | 张三        |
+----+-------------+
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)

Second case

mysql> select * from test order by cast(update_user as char) desc , id limit 10;
+----+-------------+
| id | update_user |
+----+-------------+
|  2 | 李四        |
|  4 | 李四        |
|  6 | 李四        |
|  1 | 张三        |
|  3 | 张三        |
|  5 | 张三        |
|  7 | 张三        |
|  9 | 张三        |
| 11 | 张三        |
| 13 | 张三        |
+----+-------------+
10 rows in set (25.76 sec)

4. What is your TiDB version? (Required)

v7.5.1, nightly(v8.3.0+) and more

Metadata

Metadata

Assignees

Labels

affects-5.4This bug affects the 5.4.x(LTS) versions.affects-6.1This bug affects the 6.1.x(LTS) versions.affects-6.5This bug affects the 6.5.x(LTS) versions.affects-7.1This bug affects the 7.1.x(LTS) versions.affects-7.5This bug affects the 7.5.x(LTS) versions.affects-8.1This bug affects the 8.1.x(LTS) versions.component/charsetreport/customerCustomers have encountered this bug.severity/majorsig/sql-infraSIG: SQL Infratype/bugThe issue is confirmed as a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions