From f8abffa25293eca37d300bee88ead233420022e9 Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Sat, 12 Oct 2024 10:08:21 +0800 Subject: [PATCH 01/15] fix 56535 --- pkg/ddl/index.go | 6 +++++- pkg/ddl/ingest/integration_test.go | 24 ++++++++++++++++++++++++ pkg/ddl/reorg.go | 4 ++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index a13178994b65d..508152888c311 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2549,7 +2549,11 @@ func getNextPartitionInfo(reorg *reorgInfo, t table.PartitionedTable, currPhysic if len(pi.AddingDefinitions) == 0 { // case 1 // Simply AddIndex, without any partitions added or dropped! - pid, err = findNextPartitionID(currPhysicalTableID, pi.Definitions) + if reorg.mergingTmpIdx && currPhysicalTableID == t.Meta().ID { + pid = pi.Definitions[0].ID + } else { + pid, err = findNextPartitionID(currPhysicalTableID, pi.Definitions) + } } else { // case 3 (or if not found AddingDefinitions; 4) // check if recreating Global Index (during Reorg Partition) diff --git a/pkg/ddl/ingest/integration_test.go b/pkg/ddl/ingest/integration_test.go index d495808937cbc..66c9021d9a896 100644 --- a/pkg/ddl/ingest/integration_test.go +++ b/pkg/ddl/ingest/integration_test.go @@ -182,6 +182,30 @@ func TestAddIndexIngestCancel(t *testing.T) { require.True(t, ok) } +func TestAddGlobalIndexInIngest(t *testing.T) { + store, _ := realtikvtest.CreateMockStoreAndDomainAndSetup(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + defer ingesttestutil.InjectMockBackendMgr(t, store)() + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a int, b int) partition by hash(a) partitions 5;") + tk.MustExec("insert into t (a, b) values (1, 1), (2, 2), (3, 3);") + var i atomic.Int32 + i.Store(3) + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec", func(bool) { + tk2 := testkit.NewTestKit(t, store) + tmp := i.Add(1) + _, err := tk2.Exec(fmt.Sprintf("insert into test.t values (%d, %d)", tmp, tmp)) + assert.Nil(t, err) + }) + tk.MustExec("alter table t add unique index idx(b) global;") + testfailpoint.Disable(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec") + rs1 := tk.MustQuery("select * from t use index(idx)").Sort().String() + rs2 := tk.MustQuery("select * from t use index()").Sort().String() + require.Equal(t, rs1, rs2) +} + func TestIngestPartitionRowCount(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/pkg/ddl/reorg.go b/pkg/ddl/reorg.go index d29a39e5053dc..c654e4bc31565 100644 --- a/pkg/ddl/reorg.go +++ b/pkg/ddl/reorg.go @@ -882,6 +882,10 @@ func getReorgInfo(ctx *ReorgContext, jobCtx *jobContext, rh *reorgHandler, job * tb = tbl.(table.PhysicalTable) } if mergingTmpIdx { + idxInfo := model.FindIndexInfoByID(tblInfo.Indices, elements[0].ID) + if idxInfo.Global { + pid = tblInfo.ID + } firstElemTempID := tablecodec.TempIndexPrefix | elements[0].ID lastElemTempID := tablecodec.TempIndexPrefix | elements[len(elements)-1].ID start = tablecodec.EncodeIndexSeekKey(pid, firstElemTempID, nil) From 840a23f28fb49e2b8b44fadb62fc5fccd66570c6 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Sat, 12 Oct 2024 15:24:41 +0800 Subject: [PATCH 02/15] update --- pkg/ddl/index.go | 11 +++++--- pkg/ddl/ingest/integration_test.go | 24 ----------------- pkg/ddl/reorg.go | 14 +++++++--- .../realtikvtest/addindextest3/ingest_test.go | 26 +++++++++++++++++++ 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 508152888c311..9fad50cdd7cee 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2220,9 +2220,14 @@ func (w *worker) addTableIndex(t table.Table, reorgInfo *reorgInfo) error { if tbl, ok := t.(table.PartitionedTable); ok { var finish bool for !finish { - p := tbl.GetPartition(reorgInfo.PhysicalTableID) - if p == nil { - return dbterror.ErrCancelledDDLJob.GenWithStack("Can not find partition id %d for table %d", reorgInfo.PhysicalTableID, t.Meta().ID) + var p table.PhysicalTable + if tbl.Meta().ID == reorgInfo.PhysicalTableID { + p = t.(table.PhysicalTable) + } else { + p = tbl.GetPartition(reorgInfo.PhysicalTableID) + if p == nil { + return dbterror.ErrCancelledDDLJob.GenWithStack("Can not find partition id %d for table %d", reorgInfo.PhysicalTableID, t.Meta().ID) + } } err = w.addPhysicalTableIndex(p, reorgInfo) if err != nil { diff --git a/pkg/ddl/ingest/integration_test.go b/pkg/ddl/ingest/integration_test.go index 66c9021d9a896..d495808937cbc 100644 --- a/pkg/ddl/ingest/integration_test.go +++ b/pkg/ddl/ingest/integration_test.go @@ -182,30 +182,6 @@ func TestAddIndexIngestCancel(t *testing.T) { require.True(t, ok) } -func TestAddGlobalIndexInIngest(t *testing.T) { - store, _ := realtikvtest.CreateMockStoreAndDomainAndSetup(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - defer ingesttestutil.InjectMockBackendMgr(t, store)() - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int) partition by hash(a) partitions 5;") - tk.MustExec("insert into t (a, b) values (1, 1), (2, 2), (3, 3);") - var i atomic.Int32 - i.Store(3) - testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec", func(bool) { - tk2 := testkit.NewTestKit(t, store) - tmp := i.Add(1) - _, err := tk2.Exec(fmt.Sprintf("insert into test.t values (%d, %d)", tmp, tmp)) - assert.Nil(t, err) - }) - tk.MustExec("alter table t add unique index idx(b) global;") - testfailpoint.Disable(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec") - rs1 := tk.MustQuery("select * from t use index(idx)").Sort().String() - rs2 := tk.MustQuery("select * from t use index()").Sort().String() - require.Equal(t, rs1, rs2) -} - func TestIngestPartitionRowCount(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/pkg/ddl/reorg.go b/pkg/ddl/reorg.go index c654e4bc31565..a183b0c815a66 100644 --- a/pkg/ddl/reorg.go +++ b/pkg/ddl/reorg.go @@ -15,6 +15,7 @@ package ddl import ( + "bytes" "context" "encoding/hex" "fmt" @@ -882,9 +883,16 @@ func getReorgInfo(ctx *ReorgContext, jobCtx *jobContext, rh *reorgHandler, job * tb = tbl.(table.PhysicalTable) } if mergingTmpIdx { - idxInfo := model.FindIndexInfoByID(tblInfo.Indices, elements[0].ID) - if idxInfo.Global { - pid = tblInfo.ID + for _, element := range elements { + if !bytes.Equal(element.TypeKey, meta.IndexElementKey) { + continue + } + // If has a global index in elements, need start process at `tblInfo.ID` + // because there are some temporary global indexes prefixed with table ID. + idxInfo := model.FindIndexInfoByID(tblInfo.Indices, element.ID) + if idxInfo.Global { + pid = tblInfo.ID + } } firstElemTempID := tablecodec.TempIndexPrefix | elements[0].ID lastElemTempID := tablecodec.TempIndexPrefix | elements[len(elements)-1].ID diff --git a/tests/realtikvtest/addindextest3/ingest_test.go b/tests/realtikvtest/addindextest3/ingest_test.go index 36020ff7c01b3..8c1c3bfac4e57 100644 --- a/tests/realtikvtest/addindextest3/ingest_test.go +++ b/tests/realtikvtest/addindextest3/ingest_test.go @@ -700,3 +700,29 @@ func TestIssue55808(t *testing.T) { err := tk.ExecToErr("alter table t add index idx(a);") require.ErrorContains(t, err, "injected error") } + +func TestAddGlobalIndexInIngest(t *testing.T) { + store, _ := realtikvtest.CreateMockStoreAndDomainAndSetup(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a int, b int) partition by hash(a) partitions 5;") + tk.MustExec("insert into t (a, b) values (1, 1), (2, 2), (3, 3);") + var i atomic.Int32 + i.Store(3) + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec", func(bool) { + tk2 := testkit.NewTestKit(t, store) + tmp := i.Add(1) + _, err := tk2.Exec(fmt.Sprintf("insert into test.t values (%d, %d)", tmp, tmp)) + assert.Nil(t, err) + }) + tk.MustExec("alter table t add index idx_1(b), add unique index idx_2(b) global;") + testfailpoint.Disable(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec") + rs1 := tk.MustQuery("select * from t use index(idx_2)").Sort() + rs2 := tk.MustQuery("select * from t use index()").Sort() + rs3 := tk.MustQuery("select * from t use index(idx_1)").Sort() + require.Greater(t, len(rs1.Rows()), 3) + require.Equal(t, rs1.String(), rs2.String()) + require.Equal(t, rs1.String(), rs3.String()) +} From e2f7c91cc8878c90aea102a5ea4cc3dee467645b Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Sat, 12 Oct 2024 15:42:54 +0800 Subject: [PATCH 03/15] update --- pkg/ddl/index.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 9fad50cdd7cee..c26cd789ef662 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2222,6 +2222,7 @@ func (w *worker) addTableIndex(t table.Table, reorgInfo *reorgInfo) error { for !finish { var p table.PhysicalTable if tbl.Meta().ID == reorgInfo.PhysicalTableID { + //nolint:forcetypeassert p = t.(table.PhysicalTable) } else { p = tbl.GetPartition(reorgInfo.PhysicalTableID) From 8d74b01e65ddfad0300ff55f6e67a1cf5d361199 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Sat, 12 Oct 2024 16:45:40 +0800 Subject: [PATCH 04/15] move test --- pkg/ddl/ingest/integration_test.go | 27 +++++++++++++++++++ .../realtikvtest/addindextest3/ingest_test.go | 26 ------------------ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/pkg/ddl/ingest/integration_test.go b/pkg/ddl/ingest/integration_test.go index d495808937cbc..d99c349a6bfde 100644 --- a/pkg/ddl/ingest/integration_test.go +++ b/pkg/ddl/ingest/integration_test.go @@ -424,3 +424,30 @@ func TestAddIndexIngestPartitionCheckpoint(t *testing.T) { require.Equal(t, 20, int(rowCnt.Load())) tk.MustExec("admin check table t;") } + +func TestAddGlobalIndexInIngest(t *testing.T) { + store := realtikvtest.CreateMockStoreAndSetup(t) + defer ingesttestutil.InjectMockBackendMgr(t, store)() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a int, b int) partition by hash(a) partitions 5;") + tk.MustExec("insert into t (a, b) values (1, 1), (2, 2), (3, 3);") + var i atomic.Int32 + i.Store(3) + testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec", func(bool) { + tk2 := testkit.NewTestKit(t, store) + tmp := i.Add(1) + _, err := tk2.Exec(fmt.Sprintf("insert into test.t values (%d, %d)", tmp, tmp)) + assert.Nil(t, err) + }) + tk.MustExec("alter table t add index idx_1(b), add unique index idx_2(b) global;") + rs1 := tk.MustQuery("select * from t use index(idx_2)").Sort() + rs2 := tk.MustQuery("select * from t use index()").Sort() + rs3 := tk.MustQuery("select * from t use index(idx_1)").Sort() + require.Greater(t, len(rs1.Rows()), 3) + require.Equal(t, rs1.String(), rs2.String()) + require.Equal(t, rs1.String(), rs3.String()) +} diff --git a/tests/realtikvtest/addindextest3/ingest_test.go b/tests/realtikvtest/addindextest3/ingest_test.go index 8c1c3bfac4e57..36020ff7c01b3 100644 --- a/tests/realtikvtest/addindextest3/ingest_test.go +++ b/tests/realtikvtest/addindextest3/ingest_test.go @@ -700,29 +700,3 @@ func TestIssue55808(t *testing.T) { err := tk.ExecToErr("alter table t add index idx(a);") require.ErrorContains(t, err, "injected error") } - -func TestAddGlobalIndexInIngest(t *testing.T) { - store, _ := realtikvtest.CreateMockStoreAndDomainAndSetup(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int) partition by hash(a) partitions 5;") - tk.MustExec("insert into t (a, b) values (1, 1), (2, 2), (3, 3);") - var i atomic.Int32 - i.Store(3) - testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec", func(bool) { - tk2 := testkit.NewTestKit(t, store) - tmp := i.Add(1) - _, err := tk2.Exec(fmt.Sprintf("insert into test.t values (%d, %d)", tmp, tmp)) - assert.Nil(t, err) - }) - tk.MustExec("alter table t add index idx_1(b), add unique index idx_2(b) global;") - testfailpoint.Disable(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec") - rs1 := tk.MustQuery("select * from t use index(idx_2)").Sort() - rs2 := tk.MustQuery("select * from t use index()").Sort() - rs3 := tk.MustQuery("select * from t use index(idx_1)").Sort() - require.Greater(t, len(rs1.Rows()), 3) - require.Equal(t, rs1.String(), rs2.String()) - require.Equal(t, rs1.String(), rs3.String()) -} From 3364169f8bf48ba1e405499d3bde6c7494dc5ced Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Sat, 12 Oct 2024 16:47:35 +0800 Subject: [PATCH 05/15] Update pkg/ddl/ingest/integration_test.go Co-authored-by: tangenta --- pkg/ddl/ingest/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ddl/ingest/integration_test.go b/pkg/ddl/ingest/integration_test.go index d99c349a6bfde..107e44f715e61 100644 --- a/pkg/ddl/ingest/integration_test.go +++ b/pkg/ddl/ingest/integration_test.go @@ -426,7 +426,7 @@ func TestAddIndexIngestPartitionCheckpoint(t *testing.T) { } func TestAddGlobalIndexInIngest(t *testing.T) { - store := realtikvtest.CreateMockStoreAndSetup(t) + store := testkit.CreateMockStore(t) defer ingesttestutil.InjectMockBackendMgr(t, store)() tk := testkit.NewTestKit(t, store) From d4441b0888ab2c00218cbe7ecd6def89ee606b84 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Sat, 12 Oct 2024 16:59:55 +0800 Subject: [PATCH 06/15] update bazel --- pkg/ddl/ingest/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ddl/ingest/BUILD.bazel b/pkg/ddl/ingest/BUILD.bazel index 5777ffe58e69e..933176bbf4f98 100644 --- a/pkg/ddl/ingest/BUILD.bazel +++ b/pkg/ddl/ingest/BUILD.bazel @@ -72,7 +72,7 @@ go_test( embed = [":ingest"], flaky = True, race = "on", - shard_count = 20, + shard_count = 21, deps = [ "//pkg/config", "//pkg/ddl/ingest/testutil", From 922f1745c9e19fa21b2ffd2f125f590b1c0420aa Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Sat, 12 Oct 2024 17:34:57 +0800 Subject: [PATCH 07/15] add comments --- pkg/ddl/index.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index c26cd789ef662..dbabab92350b1 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2556,6 +2556,8 @@ func getNextPartitionInfo(reorg *reorgInfo, t table.PartitionedTable, currPhysic // case 1 // Simply AddIndex, without any partitions added or dropped! if reorg.mergingTmpIdx && currPhysicalTableID == t.Meta().ID { + // If the current Physical id is the table id, + // the next Physical id should be the first partition id. pid = pi.Definitions[0].ID } else { pid, err = findNextPartitionID(currPhysicalTableID, pi.Definitions) From 59b8e3420fb37f4de03847606c42dfec39436570 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Mon, 14 Oct 2024 10:21:27 +0800 Subject: [PATCH 08/15] Update pkg/ddl/index.go Co-authored-by: tangenta --- pkg/ddl/index.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index dbabab92350b1..2dd54999ae869 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2223,7 +2223,7 @@ func (w *worker) addTableIndex(t table.Table, reorgInfo *reorgInfo) error { var p table.PhysicalTable if tbl.Meta().ID == reorgInfo.PhysicalTableID { //nolint:forcetypeassert - p = t.(table.PhysicalTable) + p = t.(table.PhysicalTable) // global index } else { p = tbl.GetPartition(reorgInfo.PhysicalTableID) if p == nil { From 4ec368c43fff4d3043284f6d0d04e5d82a93db5a Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Mon, 14 Oct 2024 15:28:16 +0800 Subject: [PATCH 09/15] fix GetPartition function --- pkg/table/tables/partition.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/table/tables/partition.go b/pkg/table/tables/partition.go index 43944c5528b5c..d4aa1c206f724 100644 --- a/pkg/table/tables/partition.go +++ b/pkg/table/tables/partition.go @@ -1510,7 +1510,14 @@ func (t *partitionedTable) locateHashPartition(ctx expression.EvalContext, partE // GetPartition returns a Table, which is actually a partition. func (t *partitionedTable) GetPartition(pid int64) table.PhysicalTable { - return t.getPartition(pid) + part := t.getPartition(pid) + + // Explicitly check if the partition is nil, and return a nil interface if it is + if part == nil { + return nil // Return a truly nil interface instead of an interface holding a nil pointer + } + + return part } // getPartition returns a Table, which is actually a partition. From 29b074b434439a238fd4fd68d5d3d8693791c6d0 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Mon, 14 Oct 2024 16:52:58 +0800 Subject: [PATCH 10/15] follow comments --- pkg/ddl/index.go | 8 +++++--- pkg/ddl/ingest/integration_test.go | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 2dd54999ae869..6da34e050549a 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2218,12 +2218,14 @@ func (w *worker) addTableIndex(t table.Table, reorgInfo *reorgInfo) error { var err error if tbl, ok := t.(table.PartitionedTable); ok { - var finish bool + var finish, ok bool for !finish { var p table.PhysicalTable if tbl.Meta().ID == reorgInfo.PhysicalTableID { - //nolint:forcetypeassert - p = t.(table.PhysicalTable) // global index + p, ok = t.(table.PhysicalTable) // global index + if !ok { + return errors.New(fmt.Sprintf("unexpected error, can't cast %T to table.PhysicalTable", t)) + } } else { p = tbl.GetPartition(reorgInfo.PhysicalTableID) if p == nil { diff --git a/pkg/ddl/ingest/integration_test.go b/pkg/ddl/ingest/integration_test.go index 107e44f715e61..14278054ea023 100644 --- a/pkg/ddl/ingest/integration_test.go +++ b/pkg/ddl/ingest/integration_test.go @@ -430,11 +430,11 @@ func TestAddGlobalIndexInIngest(t *testing.T) { defer ingesttestutil.InjectMockBackendMgr(t, store)() tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") + tk.MustExec("use test") tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int) partition by hash(a) partitions 5;") - tk.MustExec("insert into t (a, b) values (1, 1), (2, 2), (3, 3);") + tk.MustExec("create table t (a int, b int) partition by hash(a) partitions 5") + tk.MustExec("insert into t (a, b) values (1, 1), (2, 2), (3, 3)") var i atomic.Int32 i.Store(3) testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/writeLocalExec", func(bool) { @@ -443,7 +443,7 @@ func TestAddGlobalIndexInIngest(t *testing.T) { _, err := tk2.Exec(fmt.Sprintf("insert into test.t values (%d, %d)", tmp, tmp)) assert.Nil(t, err) }) - tk.MustExec("alter table t add index idx_1(b), add unique index idx_2(b) global;") + tk.MustExec("alter table t add index idx_1(b), add unique index idx_2(b) global") rs1 := tk.MustQuery("select * from t use index(idx_2)").Sort() rs2 := tk.MustQuery("select * from t use index()").Sort() rs3 := tk.MustQuery("select * from t use index(idx_1)").Sort() From b92dd5f9538bdc51ac7849675bf7d6114108dda6 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Mon, 14 Oct 2024 17:02:02 +0800 Subject: [PATCH 11/15] lint --- pkg/ddl/index.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 6da34e050549a..26d41184d5b44 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2224,7 +2224,7 @@ func (w *worker) addTableIndex(t table.Table, reorgInfo *reorgInfo) error { if tbl.Meta().ID == reorgInfo.PhysicalTableID { p, ok = t.(table.PhysicalTable) // global index if !ok { - return errors.New(fmt.Sprintf("unexpected error, can't cast %T to table.PhysicalTable", t)) + return fmt.Errorf("unexpected error, can't cast %T to table.PhysicalTable", t) } } else { p = tbl.GetPartition(reorgInfo.PhysicalTableID) From 9fc2b615d7610e23ea5803f5be92465f7884bb49 Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Mon, 14 Oct 2024 17:31:22 +0800 Subject: [PATCH 12/15] update test cases --- pkg/ddl/ingest/integration_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/ddl/ingest/integration_test.go b/pkg/ddl/ingest/integration_test.go index 14278054ea023..846568380aff7 100644 --- a/pkg/ddl/ingest/integration_test.go +++ b/pkg/ddl/ingest/integration_test.go @@ -447,7 +447,17 @@ func TestAddGlobalIndexInIngest(t *testing.T) { rs1 := tk.MustQuery("select * from t use index(idx_2)").Sort() rs2 := tk.MustQuery("select * from t use index()").Sort() rs3 := tk.MustQuery("select * from t use index(idx_1)").Sort() - require.Greater(t, len(rs1.Rows()), 3) + num := len(rs1.Rows()) + require.Greater(t, num, 3) + require.Equal(t, rs1.String(), rs2.String()) + require.Equal(t, rs1.String(), rs3.String()) + + // for indexes have different columns + tk.MustExec("alter table t add index idx_3(a), add unique index idx_4(b) global") + rs1 = tk.MustQuery("select * from t use index(idx_4)").Sort() + rs2 = tk.MustQuery("select * from t use index()").Sort() + rs3 = tk.MustQuery("select * from t use index(idx_3)").Sort() + require.Greater(t, len(rs1.Rows()), num) require.Equal(t, rs1.String(), rs2.String()) require.Equal(t, rs1.String(), rs3.String()) } From 50ba8397b9f2fd063b5c963edb1192070ea17dca Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Mon, 14 Oct 2024 17:32:31 +0800 Subject: [PATCH 13/15] update test cases --- pkg/ddl/ingest/integration_test.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/ddl/ingest/integration_test.go b/pkg/ddl/ingest/integration_test.go index 846568380aff7..64ffc99e02920 100644 --- a/pkg/ddl/ingest/integration_test.go +++ b/pkg/ddl/ingest/integration_test.go @@ -444,20 +444,20 @@ func TestAddGlobalIndexInIngest(t *testing.T) { assert.Nil(t, err) }) tk.MustExec("alter table t add index idx_1(b), add unique index idx_2(b) global") - rs1 := tk.MustQuery("select * from t use index(idx_2)").Sort() - rs2 := tk.MustQuery("select * from t use index()").Sort() - rs3 := tk.MustQuery("select * from t use index(idx_1)").Sort() - num := len(rs1.Rows()) + rsGlobalIndex := tk.MustQuery("select * from t use index(idx_2)").Sort() + rsTable := tk.MustQuery("select * from t use index()").Sort() + rsNormalIndex := tk.MustQuery("select * from t use index(idx_1)").Sort() + num := len(rsGlobalIndex.Rows()) require.Greater(t, num, 3) - require.Equal(t, rs1.String(), rs2.String()) - require.Equal(t, rs1.String(), rs3.String()) + require.Equal(t, rsGlobalIndex.String(), rsTable.String()) + require.Equal(t, rsGlobalIndex.String(), rsNormalIndex.String()) // for indexes have different columns tk.MustExec("alter table t add index idx_3(a), add unique index idx_4(b) global") - rs1 = tk.MustQuery("select * from t use index(idx_4)").Sort() - rs2 = tk.MustQuery("select * from t use index()").Sort() - rs3 = tk.MustQuery("select * from t use index(idx_3)").Sort() - require.Greater(t, len(rs1.Rows()), num) - require.Equal(t, rs1.String(), rs2.String()) - require.Equal(t, rs1.String(), rs3.String()) + rsGlobalIndex = tk.MustQuery("select * from t use index(idx_4)").Sort() + rsTable = tk.MustQuery("select * from t use index()").Sort() + rsNormalIndex = tk.MustQuery("select * from t use index(idx_3)").Sort() + require.Greater(t, len(rsGlobalIndex.Rows()), num) + require.Equal(t, rsGlobalIndex.String(), rsTable.String()) + require.Equal(t, rsGlobalIndex.String(), rsNormalIndex.String()) } From d130d619e688be0e3cdf2be96518163e25646c8b Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Mon, 14 Oct 2024 18:23:41 +0800 Subject: [PATCH 14/15] update --- pkg/ddl/index.go | 18 +++++++++++++++++- pkg/ddl/ingest/integration_test.go | 9 +++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 26d41184d5b44..9dac392feefc9 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2560,7 +2560,23 @@ func getNextPartitionInfo(reorg *reorgInfo, t table.PartitionedTable, currPhysic if reorg.mergingTmpIdx && currPhysicalTableID == t.Meta().ID { // If the current Physical id is the table id, // the next Physical id should be the first partition id. - pid = pi.Definitions[0].ID + allGlobal := true + for _, element := range reorg.elements { + if !bytes.Equal(element.TypeKey, meta.IndexElementKey) { + allGlobal = false + break + } + idxInfo := model.FindIndexInfoByID(t.Meta().Indices, element.ID) + if !idxInfo.Global { + allGlobal = false + break + } + } + if allGlobal { + pid = 0 + } else { + pid = pi.Definitions[0].ID + } } else { pid, err = findNextPartitionID(currPhysicalTableID, pi.Definitions) } diff --git a/pkg/ddl/ingest/integration_test.go b/pkg/ddl/ingest/integration_test.go index 64ffc99e02920..1d9c30878b1b9 100644 --- a/pkg/ddl/ingest/integration_test.go +++ b/pkg/ddl/ingest/integration_test.go @@ -460,4 +460,13 @@ func TestAddGlobalIndexInIngest(t *testing.T) { require.Greater(t, len(rsGlobalIndex.Rows()), num) require.Equal(t, rsGlobalIndex.String(), rsTable.String()) require.Equal(t, rsGlobalIndex.String(), rsNormalIndex.String()) + + // for all global indexes + tk.MustExec("alter table t add unique index idx_5(b) global, add unique index idx_6(b) global") + rsGlobalIndex1 := tk.MustQuery("select * from t use index(idx_6)").Sort() + rsTable = tk.MustQuery("select * from t use index()").Sort() + rsGlobalIndex2 := tk.MustQuery("select * from t use index(idx_5)").Sort() + require.Greater(t, len(rsGlobalIndex1.Rows()), len(rsGlobalIndex.Rows())) + require.Equal(t, rsGlobalIndex1.String(), rsTable.String()) + require.Equal(t, rsGlobalIndex1.String(), rsGlobalIndex2.String()) } From 8a87e3564860a8b976c3ca03c6ac74a1a572a24e Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Mon, 14 Oct 2024 18:34:05 +0800 Subject: [PATCH 15/15] update comments --- pkg/ddl/index.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 9dac392feefc9..26ebe094d7b9a 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2559,7 +2559,8 @@ func getNextPartitionInfo(reorg *reorgInfo, t table.PartitionedTable, currPhysic // Simply AddIndex, without any partitions added or dropped! if reorg.mergingTmpIdx && currPhysicalTableID == t.Meta().ID { // If the current Physical id is the table id, - // the next Physical id should be the first partition id. + // 1. All indexes are global index, the next Physical id should be the first partition id. + // 2. Not all indexes are global index, return 0. allGlobal := true for _, element := range reorg.elements { if !bytes.Equal(element.TypeKey, meta.IndexElementKey) {