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

Partition Pruning gives wrong partition for RANGE COLUMNS with multiple columns and utf8mb4_0900_ai_ci collation #57261

Closed
mjonss opened this issue Nov 9, 2024 · 2 comments · Fixed by #57344
Assignees
Labels
affects-7.5 This bug affects the 7.5.x(LTS) versions. affects-8.1 This bug affects the 8.1.x(LTS) versions. affects-8.5 This bug affects the 8.5.x(LTS) versions. severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.

Comments

@mjonss
Copy link
Contributor

mjonss commented Nov 9, 2024

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

CREATE TABLE `t` (
 `a` varchar(255) COLLATE utf8mb4_0900_ai_ci NOT NULL,
 `b` varchar(255) COLLATE utf8mb4_0900_ai_ci NOT NULL
)
PARTITION BY RANGE COLUMNS(`a`,`b`)
(PARTITION `pLTi` VALUES LESS THAN ('i',''),
 PARTITION `pMax` VALUES LESS THAN (MAXVALUE,''));

insert into t values ("Q","Q");

-- Wrong partition, pLTi instead of pMax
explain select * from t where a like 'Q%';
select * from t where a like 'Q%';
-- Wrong partition, pLTi instead of pMax
explain select * from t where a = 'Q';
select * from t where a = 'Q';
-- OK, but with partition:all !
explain select * from t where a >= 'Q';
select * from t where a >= 'Q';

-- Wrong partition, pLTi instead of pMax
explain select * from t where a like 'q%';
select * from t where a like 'q%';
-- Wrong partition, pLTi instead of pMax
explain select * from t where a = 'q';
select * from t where a = 'q';
-- OK, but with partition:all !
explain select * from t where a >= 'q';
select * from t where a >= 'q';

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

Use of partition pMax instead of pLTi

3. What did you see instead (Required)

tidb> CREATE TABLE `t` (
    ->  `a` varchar(255) COLLATE utf8mb4_0900_ai_ci NOT NULL,
    ->  `b` varchar(255) COLLATE utf8mb4_0900_ai_ci NOT NULL
    -> )
    -> PARTITION BY RANGE COLUMNS(`a`,`b`)
    -> (PARTITION `pLTi` VALUES LESS THAN ('i',''),
    ->  PARTITION `pMax` VALUES LESS THAN (MAXVALUE,''));
Query OK, 0 rows affected (0.03 sec)

tidb> 
tidb> insert into t values ("Q","Q");
Query OK, 1 row affected (0.03 sec)

tidb> 
tidb> -- Wrong partition, pLTi instead of pMax
Query OK, 1 row affected (0.00 sec)

tidb> explain select * from t where a like 'Q%';
+-------------------------+----------+-----------+----------------+--------------------------------+
| id                      | estRows  | task      | access object  | operator info                  |
+-------------------------+----------+-----------+----------------+--------------------------------+
| TableReader_7           | 250.00   | root      | partition:pLTi | data:Selection_6               |
| └─Selection_6       | 250.00   | cop[tikv] |                | like(test.t.a, "Q%", 92)       |
|   └─TableFullScan_5 | 10000.00 | cop[tikv] | table:t        | keep order:false, stats:pseudo |
+-------------------------+----------+-----------+----------------+--------------------------------+
3 rows in set (0.00 sec)

tidb> select * from t where a like 'Q%';
Empty set (0.00 sec)

tidb> -- Wrong partition, pLTi instead of pMax
Query OK, 0 rows affected (0.00 sec)

tidb> explain select * from t where a = 'Q';
+-------------------------+----------+-----------+----------------+--------------------------------+
| id                      | estRows  | task      | access object  | operator info                  |
+-------------------------+----------+-----------+----------------+--------------------------------+
| TableReader_7           | 10.00    | root      | partition:pLTi | data:Selection_6               |
| └─Selection_6       | 10.00    | cop[tikv] |                | eq(test.t.a, "Q")              |
|   └─TableFullScan_5 | 10000.00 | cop[tikv] | table:t        | keep order:false, stats:pseudo |
+-------------------------+----------+-----------+----------------+--------------------------------+
3 rows in set (0.00 sec)

tidb> select * from t where a = 'Q';
Empty set (0.00 sec)

tidb> -- OK, but with partition:all !
Query OK, 0 rows affected (0.00 sec)

tidb> explain select * from t where a >= 'Q';
+-------------------------+----------+-----------+---------------+--------------------------------+
| id                      | estRows  | task      | access object | operator info                  |
+-------------------------+----------+-----------+---------------+--------------------------------+
| TableReader_7           | 3333.33  | root      | partition:all | data:Selection_6               |
| └─Selection_6       | 3333.33  | cop[tikv] |               | ge(test.t.a, "Q")              |
|   └─TableFullScan_5 | 10000.00 | cop[tikv] | table:t       | keep order:false, stats:pseudo |
+-------------------------+----------+-----------+---------------+--------------------------------+
3 rows in set (0.00 sec)

tidb> select * from t where a >= 'Q';
+---+---+
| a | b |
+---+---+
| Q | Q |
+---+---+
1 row in set (0.00 sec)

tidb> 
tidb> -- Wrong partition, pLTi instead of pMax
Query OK, 0 rows affected (0.00 sec)

tidb> explain select * from t where a like 'q%';
+-------------------------+----------+-----------+----------------+--------------------------------+
| id                      | estRows  | task      | access object  | operator info                  |
+-------------------------+----------+-----------+----------------+--------------------------------+
| TableReader_7           | 250.00   | root      | partition:pLTi | data:Selection_6               |
| └─Selection_6       | 250.00   | cop[tikv] |                | like(test.t.a, "q%", 92)       |
|   └─TableFullScan_5 | 10000.00 | cop[tikv] | table:t        | keep order:false, stats:pseudo |
+-------------------------+----------+-----------+----------------+--------------------------------+
3 rows in set (0.00 sec)

tidb> select * from t where a like 'q%';
Empty set (0.00 sec)

tidb> -- Wrong partition, pLTi instead of pMax
Query OK, 0 rows affected (0.00 sec)

tidb> explain select * from t where a = 'q';
+-------------------------+----------+-----------+----------------+--------------------------------+
| id                      | estRows  | task      | access object  | operator info                  |
+-------------------------+----------+-----------+----------------+--------------------------------+
| TableReader_7           | 10.00    | root      | partition:pLTi | data:Selection_6               |
| └─Selection_6       | 10.00    | cop[tikv] |                | eq(test.t.a, "q")              |
|   └─TableFullScan_5 | 10000.00 | cop[tikv] | table:t        | keep order:false, stats:pseudo |
+-------------------------+----------+-----------+----------------+--------------------------------+
3 rows in set (0.00 sec)

tidb> select * from t where a = 'q';
Empty set (0.00 sec)

tidb> -- OK, but with partition:all !
Query OK, 0 rows affected (0.00 sec)

tidb> explain select * from t where a >= 'q';
+-------------------------+----------+-----------+---------------+--------------------------------+
| id                      | estRows  | task      | access object | operator info                  |
+-------------------------+----------+-----------+---------------+--------------------------------+
| TableReader_7           | 3333.33  | root      | partition:all | data:Selection_6               |
| └─Selection_6       | 3333.33  | cop[tikv] |               | ge(test.t.a, "q")              |
|   └─TableFullScan_5 | 10000.00 | cop[tikv] | table:t       | keep order:false, stats:pseudo |
+-------------------------+----------+-----------+---------------+--------------------------------+
3 rows in set (0.00 sec)

tidb> select * from t where a >= 'q';
+---+---+
| a | b |
+---+---+
| Q | Q |
+---+---+
1 row in set (0.00 sec)

4. What is your TiDB version? (Required)

tidb_version(): Release Version: v8.5.0-alpha-60-gb522e98080
Edition: Community
Git Commit Hash: b522e98080057cbaa451b207d42a8f37399082d7
Git Branch: HEAD
UTC Build Time: 2024-11-06 16:17:23
GoVersion: go1.23.2
Race Enabled: false
Check Table Before Drop: false
Store: tikv
@mjonss mjonss added type/bug The issue is confirmed as a bug. severity/major affects-8.5 This bug affects the 8.5.x(LTS) versions. labels Nov 9, 2024
@mjonss mjonss added the affects-8.1 This bug affects the 8.1.x(LTS) versions. label Nov 9, 2024
@mjonss mjonss added affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-7.5 This bug affects the 7.5.x(LTS) versions. and removed may-affects-7.1 labels Nov 9, 2024
@mjonss mjonss removed may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.1 may-affects-6.5 affects-7.1 This bug affects the 7.1.x(LTS) versions. labels Nov 9, 2024
@mjonss mjonss changed the title Partition Pruning gives wrong partition for RANGE COLUMNS with multiple columns and non-binary collation Partition Pruning gives wrong partition for RANGE COLUMNS with multiple columns and utf8mb4_0900_ai_ci collation Nov 9, 2024
@mjonss
Copy link
Contributor Author

mjonss commented Nov 9, 2024

Does not affect utf8mb4_general_ci or utf8mb4_unicode_ci collations, but when creating tests, better to include them as well.

@jebter jebter added the sig/sql-infra SIG: SQL Infra label Nov 12, 2024
@Defined2014 Defined2014 self-assigned this Nov 13, 2024
@Defined2014
Copy link
Contributor

explain select * from t where a = 'x';

Execute sql above will panic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-7.5 This bug affects the 7.5.x(LTS) versions. affects-8.1 This bug affects the 8.1.x(LTS) versions. affects-8.5 This bug affects the 8.5.x(LTS) versions. severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.
Projects
None yet
3 participants