From 2a6bd468c617380190d3a7ab5b399dbfeceefa74 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 13 Dec 2024 15:05:03 +0800 Subject: [PATCH] executor: skip execution when build query for VIEW in I_S (#58203) (#58232) close pingcap/tidb#58184 --- pkg/executor/infoschema_reader.go | 2 +- pkg/executor/infoschema_reader_test.go | 15 +++++++++------ pkg/planner/core/logical_plan_builder.go | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/executor/infoschema_reader.go b/pkg/executor/infoschema_reader.go index 40e9b3c68fa6f..6586413cbcd41 100644 --- a/pkg/executor/infoschema_reader.go +++ b/pkg/executor/infoschema_reader.go @@ -867,7 +867,7 @@ func (e *hugeMemTableRetriever) dataForColumnsInTable(ctx context.Context, sctx // Build plan is not thread safe, there will be concurrency on sessionctx. if err := runWithSystemSession(internalCtx, sctx, func(s sessionctx.Context) error { is := sessiontxn.GetTxnManager(s).GetTxnInfoSchema() - planBuilder, _ := plannercore.NewPlanBuilder().Init(s.GetPlanCtx(), is, hint.NewQBHintHandler(nil)) + planBuilder, _ := plannercore.NewPlanBuilder(plannercore.PlanBuilderOptNoExecution{}).Init(s.GetPlanCtx(), is, hint.NewQBHintHandler(nil)) var err error viewLogicalPlan, err = planBuilder.BuildDataSourceFromView(ctx, schema.Name, tbl, nil, nil) return errors.Trace(err) diff --git a/pkg/executor/infoschema_reader_test.go b/pkg/executor/infoschema_reader_test.go index 598f573b91ac7..632b71dd28616 100644 --- a/pkg/executor/infoschema_reader_test.go +++ b/pkg/executor/infoschema_reader_test.go @@ -31,6 +31,7 @@ import ( "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/store/mockstore" "github.com/pingcap/tidb/pkg/testkit" + "github.com/pingcap/tidb/pkg/testkit/testfailpoint" "github.com/pingcap/tidb/pkg/util/stringutil" "github.com/stretchr/testify/require" "github.com/tikv/client-go/v2/tikv" @@ -552,21 +553,23 @@ func TestShowColumnsWithSubQueryView(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") - if tk.MustQuery("select @@tidb_schema_cache_size > 0").Equal(testkit.Rows("1")) { - // infoschema v2 requires network, so it cannot be tested this way. - t.Skip() - } + tk.MustExec("set @@global.tidb_schema_cache_size = 0;") + t.Cleanup(func() { + tk.MustExec("set @@global.tidb_schema_cache_size = default;") + }) tk.MustExec("CREATE TABLE added (`id` int(11), `name` text, `some_date` timestamp);") tk.MustExec("CREATE TABLE incremental (`id` int(11), `name`text, `some_date` timestamp);") tk.MustExec("create view temp_view as (select * from `added` where id > (select max(id) from `incremental`));") // Show columns should not send coprocessor request to the storage. - require.NoError(t, failpoint.Enable("tikvclient/tikvStoreSendReqResult", `return("timeout")`)) + testfailpoint.Enable(t, "tikvclient/tikvStoreSendReqResult", `return("timeout")`) + testfailpoint.Enable(t, "github.com/pingcap/tidb/pkg/planner/core/BuildDataSourceFailed", "panic") + tk.MustQuery("show columns from temp_view;").Check(testkit.Rows( "id int(11) YES ", "name text YES ", "some_date timestamp YES ")) - require.NoError(t, failpoint.Disable("tikvclient/tikvStoreSendReqResult")) + tk.MustQuery("select COLUMN_NAME from information_schema.columns where table_name = 'temp_view';").Check(testkit.Rows("id", "name", "some_date")) } // Code below are helper utilities for the test cases. diff --git a/pkg/planner/core/logical_plan_builder.go b/pkg/planner/core/logical_plan_builder.go index efec0862b0b7c..489a58c62efe4 100644 --- a/pkg/planner/core/logical_plan_builder.go +++ b/pkg/planner/core/logical_plan_builder.go @@ -5456,6 +5456,7 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model. terror.ErrorNotEqual(err, plannererrors.ErrNotSupportedYet) { err = plannererrors.ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O) } + failpoint.Inject("BuildDataSourceFailed", func() {}) return nil, err } pm := privilege.GetPrivilegeManager(b.ctx)