Skip to content

CAST which only changes the length of the string type column will cause full scan instead of index seek #45199

@pcqz

Description

@pcqz

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

create table t1(a varchar(100) primary key);
create table t2(a varchar(10) primary key);
create view v as select a from t1 union select a from t2;
explain select * from v where a='test';

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

+----------------------+---------+------+--------------------------------------+-------------------------------------------------------+
| id                   | estRows | task | access object                        | operator info                                         |
+----------------------+---------+------+--------------------------------------+-------------------------------------------------------+
| HashAgg_13           | 16.00   | root |                                      | group by:Column#3, funcs:firstrow(Column#3)->Column#3 |
| └─Union_14           | 20.00   | root |                                      |                                                       |
|   ├─Point_Get_16     | 1.00    | root | table:t1, clustered index:PRIMARY(a) |                                                       |
|   └─Point_Get_18     | 1.00    | root | table:t2, clustered index:PRIMARY(a) |                                                       |
+----------------------+---------+------+--------------------------------------+-------------------------------------------------------+

3. What did you see instead (Required)

+--------------------------------+----------+-----------+--------------------------------------+-------------------------------------------------------------------------------------+
| id                             | estRows  | task      | access object                        | operator info                                                                       |
+--------------------------------+----------+-----------+--------------------------------------+-------------------------------------------------------------------------------------+
| HashAgg_13                     | 6408.00  | root      |                                      | group by:Column#3, funcs:firstrow(Column#3)->Column#3                               |
| └─Union_14                     | 8010.00  | root      |                                      |                                                                                     |
|   ├─Point_Get_16               | 1.00     | root      | table:t1, clustered index:PRIMARY(a) |                                                                                     |
|   └─Projection_17              | 8000.00  | root      |                                      | cast(test.t2.a, varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin)->Column#3   |
|     └─TableReader_20           | 8000.00  | root      |                                      | data:Selection_19                                                                   |
|       └─Selection_19           | 8000.00  | cop[tikv] |                                      | eq(cast(test.t2.a, varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin), "test") |
|         └─TableFullScan_18     | 10000.00 | cop[tikv] | table:t2                             | keep order:false, stats:pseudo                                                      |
+--------------------------------+----------+-----------+--------------------------------------+-------------------------------------------------------------------------------------+

4. What is your TiDB version? (Required)

master

Metadata

Metadata

Assignees

Labels

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.severity/moderatesig/plannerSIG: Plannertype/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