From 961159c12f64d2ec5a11ff8c24d273d704b02f9b Mon Sep 17 00:00:00 2001 From: Zhou Kunqin <25057648+time-and-fate@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:57:30 +0800 Subject: [PATCH] util/ranger: do not convert to binary collate for string values when `convertToSortKey` is `false` (#51363) close pingcap/tidb#51316 --- pkg/util/ranger/detacher.go | 5 ++++- pkg/util/ranger/ranger.go | 5 ++++- .../casetest/partition/integration_partition.result | 12 ++++++++++++ .../casetest/partition/integration_partition.test | 5 +++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/util/ranger/detacher.go b/pkg/util/ranger/detacher.go index f77796277569b..ec58b986c223a 100644 --- a/pkg/util/ranger/detacher.go +++ b/pkg/util/ranger/detacher.go @@ -778,7 +778,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 02498a340afc7..011616c933614 100644 --- a/pkg/util/ranger/ranger.go +++ b/pkg/util/ranger/ranger.go @@ -490,7 +490,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';