diff --git a/pkg/util/ranger/detacher.go b/pkg/util/ranger/detacher.go index bb2b74a2ee4bd..da1aadb10eed5 100644 --- a/pkg/util/ranger/detacher.go +++ b/pkg/util/ranger/detacher.go @@ -779,7 +779,10 @@ func (d *rangeDetacher) detachDNFCondAndBuildRangeForIndex(condition *expression hasResidual = true } points := rb.build(item, newTpSlice[0], d.lengths[0], d.convertToSortKey) - tmpNewTp := convertStringFTToBinaryCollate(newTpSlice[0]) + tmpNewTp := newTpSlice[0] + if d.convertToSortKey { + tmpNewTp = convertStringFTToBinaryCollate(tmpNewTp) + } // TODO: restrict the mem usage of ranges ranges, rangeFallback, err := points2Ranges(d.sctx, points, tmpNewTp, d.rangeMaxSize) if err != nil { diff --git a/pkg/util/ranger/ranger.go b/pkg/util/ranger/ranger.go index ef8d30789f8ec..c59b0982ed8c7 100644 --- a/pkg/util/ranger/ranger.go +++ b/pkg/util/ranger/ranger.go @@ -486,7 +486,10 @@ func (d *rangeDetacher) buildRangeOnColsByCNFCond(newTp []*types.FieldType, eqAn if rb.err != nil { return nil, nil, nil, errors.Trace(rb.err) } - tmpNewTp := convertStringFTToBinaryCollate(newTp[i]) + tmpNewTp := newTp[i] + if d.convertToSortKey { + tmpNewTp = convertStringFTToBinaryCollate(tmpNewTp) + } if i == 0 { ranges, rangeFallback, err = points2Ranges(d.sctx, point, tmpNewTp, d.rangeMaxSize) } else { diff --git a/tests/integrationtest/r/planner/core/casetest/partition/integration_partition.result b/tests/integrationtest/r/planner/core/casetest/partition/integration_partition.result index dd34b8ef6e571..44ef24389322b 100644 --- a/tests/integrationtest/r/planner/core/casetest/partition/integration_partition.result +++ b/tests/integrationtest/r/planner/core/casetest/partition/integration_partition.result @@ -738,3 +738,15 @@ Projection 10.00 root list_partition_pruning.thash.a └─Limit 10.00 cop[tikv] offset:0, count:10 └─Selection 10.00 cop[tikv] gt(list_partition_pruning.thash.a, 10) └─TableFullScan 30.00 cop[tikv] table:thash, partition:p3 keep order:true, stats:pseudo +drop table if exists t; +create table t(col varchar(32) COLLATE utf8mb4_general_ci DEFAULT NULL) PARTITION BY KEY (`col`) PARTITIONS 7; +explain format = brief select * from t where col = 'linpin'; +id estRows task access object operator info +TableReader 10.00 root data:Selection +└─Selection 10.00 cop[tikv] eq(list_partition_pruning.t.col, "linpin") + └─TableFullScan 10000.00 cop[tikv] table:t, partition:p4 keep order:false, stats:pseudo +explain format = brief select * from t where col = 'LINPIN'; +id estRows task access object operator info +TableReader 10.00 root data:Selection +└─Selection 10.00 cop[tikv] eq(list_partition_pruning.t.col, "LINPIN") + └─TableFullScan 10000.00 cop[tikv] table:t, partition:p4 keep order:false, stats:pseudo diff --git a/tests/integrationtest/t/planner/core/casetest/partition/integration_partition.test b/tests/integrationtest/t/planner/core/casetest/partition/integration_partition.test index b54787a65910a..a19c49b78cfbe 100644 --- a/tests/integrationtest/t/planner/core/casetest/partition/integration_partition.test +++ b/tests/integrationtest/t/planner/core/casetest/partition/integration_partition.test @@ -180,3 +180,8 @@ explain format='brief' select a from trange use index () where a > 10 order by b explain format='brief' select a from tlist use index () where a > 10 order by b limit 10; explain format='brief' select a from thash use index () where a > 10 order by b limit 10; +# TestIssue51316 +drop table if exists t; +create table t(col varchar(32) COLLATE utf8mb4_general_ci DEFAULT NULL) PARTITION BY KEY (`col`) PARTITIONS 7; +explain format = brief select * from t where col = 'linpin'; +explain format = brief select * from t where col = 'LINPIN';