From 9df37638d4ecb15db76a750ca989f0dccdfe3210 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 20 Nov 2024 10:45:02 +0800 Subject: [PATCH] br: fix br work with global index (#57493) (#57524) close pingcap/tidb#57469 --- br/tests/br_table_partition/prepare.sh | 18 ++++++++++++++---- pkg/distsql/request_builder.go | 14 +++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/br/tests/br_table_partition/prepare.sh b/br/tests/br_table_partition/prepare.sh index 9a89b313ea2ff..6f553d5f11790 100755 --- a/br/tests/br_table_partition/prepare.sh +++ b/br/tests/br_table_partition/prepare.sh @@ -58,16 +58,17 @@ run_sql "CREATE TABLE IF NOT EXISTS $DB.${TABLE}_List ($TABLE_COLUMNS) PARTITION wait +insertRecords $DB.${TABLE}_Hash 1 $ROW_COUNT & +insertRecords $DB.${TABLE}_List 1 $ROW_COUNT & + for i in $(seq $TABLE_COUNT); do for j in $(seq $CONCURRENCY); do insertRecords $DB.$TABLE${i} $(expr $ROW_COUNT / $CONCURRENCY \* $(expr $j - 1) + 1) $(expr $ROW_COUNT / $CONCURRENCY \* $j) & done - insertRecords $DB.${TABLE}_Hash 1 $ROW_COUNT & - insertRecords $DB.${TABLE}_List 1 $ROW_COUNT & - if ! ((i % 4)); then + if [ $((i % 4)) -eq 0 ]; then run_sql "ALTER TABLE $DB.$TABLE${i} REMOVE PARTITIONING" fi - if ! ((i % 2)); then + if [ $((i % 2)) -eq 0 ]; then run_sql "ALTER TABLE $DB.$TABLE${i} \ PARTITION BY RANGE(c1) ( \ PARTITION p0 VALUES LESS THAN (0), \ @@ -77,3 +78,12 @@ for i in $(seq $TABLE_COUNT); do fi done wait + +run_sql "ALTER TABLE $DB.${TABLE}_Hash ADD UNIQUE INDEX idx(c1) GLOBAL" & +run_sql "ALTER TABLE $DB.${TABLE}_List ADD UNIQUE INDEX idx(c1) GLOBAL" & + +for i in $(seq $TABLE_COUNT); do + run_sql "ALTER TABLE $DB.$TABLE${i} ADD UNIQUE INDEX idx(c1) GLOBAL" & +done +wait + diff --git a/pkg/distsql/request_builder.go b/pkg/distsql/request_builder.go index c9f63b74f48a3..672543d329af1 100644 --- a/pkg/distsql/request_builder.go +++ b/pkg/distsql/request_builder.go @@ -828,6 +828,18 @@ func BuildTableRanges(tbl *model.TableInfo) ([]kv.KeyRange, error) { } ranges := make([]kv.KeyRange, 0, len(pis.Definitions)*(len(tbl.Indices)+1)+1) + // Handle global index ranges + for _, idx := range tbl.Indices { + if idx.State != model.StatePublic || !idx.Global { + continue + } + idxRanges, err := IndexRangesToKVRanges(nil, tbl.ID, idx.ID, ranger.FullRange()) + if err != nil { + return nil, err + } + ranges = idxRanges.AppendSelfTo(ranges) + } + for _, def := range pis.Definitions { rgs, err := appendRanges(tbl, def.ID) if err != nil { @@ -854,7 +866,7 @@ func appendRanges(tbl *model.TableInfo, tblID int64) ([]kv.KeyRange, error) { retRanges = kvRanges.AppendSelfTo(retRanges) for _, index := range tbl.Indices { - if index.State != model.StatePublic { + if index.State != model.StatePublic || index.Global { continue } ranges = ranger.FullRange()