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

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

Closed
Defined2014 opened this issue Aug 27, 2024 · 1 comment · Fixed by #55724
Closed

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

Defined2014 opened this issue Aug 27, 2024 · 1 comment · Fixed by #55724
Assignees
Labels
affects-5.4 This bug affects 5.4.x versions. affects-6.1 affects-6.5 affects-7.1 affects-7.5 affects-8.1 component/charset report/customer Customers have encountered this bug. severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.

Comments

@Defined2014
Copy link
Contributor

Defined2014 commented Aug 27, 2024

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

@Defined2014 Defined2014 added type/bug The issue is confirmed as a bug. component/charset sig/sql-infra SIG: SQL Infra labels Aug 27, 2024
@Defined2014 Defined2014 changed the title Push down TopN into TiKV with cast function meets incorrect result Pushdown TopN into TiKV with cast function meets incorrect result Aug 27, 2024
@Defined2014 Defined2014 self-assigned this Aug 28, 2024
@Defined2014
Copy link
Contributor Author

The typeFiled of cast function is

{
    charset: "gbk",
    collation: "utf8bm4_bin"
}

It's set by

tp.SetCollate(str1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.4 This bug affects 5.4.x versions. affects-6.1 affects-6.5 affects-7.1 affects-7.5 affects-8.1 component/charset report/customer Customers have encountered this bug. severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant