From 4442d49213ee72bd69f84eec1df9fb1e0deae881 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 20 Nov 2024 21:41:38 +0800 Subject: [PATCH] executor: fix nil pointer for query on tikv_region_status with non-exist table id (#57534) close pingcap/tidb#57530 --- pkg/executor/infoschema_reader.go | 8 ++++++-- tests/realtikvtest/sessiontest/session_fail_test.go | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/executor/infoschema_reader.go b/pkg/executor/infoschema_reader.go index 8e61832ce8c76..0d7b7e8f0570c 100644 --- a/pkg/executor/infoschema_reader.go +++ b/pkg/executor/infoschema_reader.go @@ -1962,7 +1962,7 @@ func (e *memtableRetriever) setDataForTiKVRegionStatus(ctx context.Context, sctx for _, tableID := range extractorTableIDs { regionsInfo, err := e.getRegionsInfoForTable(ctx, tikvHelper, is, tableID) if err != nil { - if errors.ErrorEqual(err, infoschema.ErrTableExists) { + if errors.ErrorEqual(err, infoschema.ErrTableNotExists) { continue } return err @@ -1982,6 +1982,10 @@ func (e *memtableRetriever) setDataForTiKVRegionStatus(ctx context.Context, sctx return err } } + if allRegionsInfo == nil { + return nil + } + tableInfos := tikvHelper.GetRegionsTableInfo(allRegionsInfo, is, nil) for i := range allRegionsInfo.Regions { regionTableList := tableInfos[allRegionsInfo.Regions[i].ID] @@ -2006,7 +2010,7 @@ func (e *memtableRetriever) setDataForTiKVRegionStatus(ctx context.Context, sctx func (e *memtableRetriever) getRegionsInfoForTable(ctx context.Context, h *helper.Helper, is infoschema.InfoSchema, tableID int64) (*pd.RegionsInfo, error) { tbl, _ := is.TableByID(ctx, tableID) if tbl == nil { - return nil, infoschema.ErrTableExists.GenWithStackByArgs(tableID) + return nil, infoschema.ErrTableNotExists.GenWithStackByArgs(tableID) } pt := tbl.Meta().GetPartitionInfo() diff --git a/tests/realtikvtest/sessiontest/session_fail_test.go b/tests/realtikvtest/sessiontest/session_fail_test.go index 4637136707bb5..6aa93bf456c27 100644 --- a/tests/realtikvtest/sessiontest/session_fail_test.go +++ b/tests/realtikvtest/sessiontest/session_fail_test.go @@ -325,3 +325,10 @@ func TestTiKVClientReadTimeout(t *testing.T) { explain = fmt.Sprintf("%v", rows[0]) require.Regexp(t, ".*TableReader.* root time:.*, loops:.* cop_task: {num: 1, .*num_rpc:(3|4|5).*", explain) } + +func TestIssue57530(t *testing.T) { + store := realtikvtest.CreateMockStoreAndSetup(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use information_schema") + tk.MustQuery("select * from TIKV_REGION_STATUS where table_id = 81920").Check(testkit.Rows()) +}