From b353681098fcdbfc1d2c4abca23950057196ebd0 Mon Sep 17 00:00:00 2001 From: AilinKid <314806019@qq.com> Date: Mon, 20 Feb 2023 17:26:59 +0800 Subject: [PATCH] add a switch to avoid plan generation fail before tiflash side merged Signed-off-by: AilinKid <314806019@qq.com> --- planner/core/casetest/enforce_mpp_test.go | 7 +++++-- planner/core/task.go | 2 +- sessionctx/variable/session.go | 3 +++ sessionctx/variable/sysvar.go | 4 ++++ sessionctx/variable/tidb_vars.go | 4 ++++ 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/planner/core/casetest/enforce_mpp_test.go b/planner/core/casetest/enforce_mpp_test.go index 043256b25c5dd..10c49e9091726 100644 --- a/planner/core/casetest/enforce_mpp_test.go +++ b/planner/core/casetest/enforce_mpp_test.go @@ -20,8 +20,10 @@ import ( "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/parser/model" + "github.com/pingcap/tidb/planner/core/internal" "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/external" "github.com/pingcap/tidb/testkit/testdata" "github.com/pingcap/tidb/util/collate" "github.com/stretchr/testify/require" @@ -492,7 +494,7 @@ func TestMPPSingleDistinct3Stage(t *testing.T) { // // since it doesn't change the schema out (index ref is still the right), so by now it's fine. SEE case: EXPLAIN select count(distinct a), count(distinct b), sum(c) from t. func TestMPPMultiDistinct3Stage(t *testing.T) { - store := testkit.CreateMockStore(t, withMockTiFlash(2)) + store := testkit.CreateMockStore(t, internal.WithMockTiFlash(2)) tk := testkit.NewTestKit(t, store) // test table @@ -503,6 +505,7 @@ func TestMPPMultiDistinct3Stage(t *testing.T) { tb := external.GetTableByName(t, tk, "test", "t") err := domain.GetDomain(tk.Session()).DDL().UpdateTableReplicaInfo(tk.Session(), tb.Meta().ID, true) require.NoError(t, err) + tk.MustExec("set @@session.tidb_opt_enable_three_stage_multi_distinct_agg=1") tk.MustExec("set @@session.tidb_isolation_read_engines=\"tiflash\";") tk.MustExec("set @@session.tidb_enforce_mpp=1") tk.MustExec("set @@session.tidb_allow_mpp=ON;") @@ -525,7 +528,7 @@ func TestMPPMultiDistinct3Stage(t *testing.T) { Plan []string Warn []string } - enforceMPPSuiteData := plannercore.GetEnforceMPPSuiteData() + enforceMPPSuiteData := GetEnforceMPPSuiteData() enforceMPPSuiteData.LoadTestCases(t, &input, &output) for i, tt := range input { testdata.OnRecord(func() { diff --git a/planner/core/task.go b/planner/core/task.go index b999721adf49f..5d1af579df163 100644 --- a/planner/core/task.go +++ b/planner/core/task.go @@ -1887,7 +1887,7 @@ func (p *basePhysicalAgg) scale3StageForDistinctAgg() (bool, expression.Grouping // canUse3Stage4MultiDistinctAgg returns true if this agg can use 3 stage for multi distinct aggregation func (p *basePhysicalAgg) canUse3Stage4MultiDistinctAgg() (can bool, gss expression.GroupingSets) { - if !p.ctx.GetSessionVars().Enable3StageDistinctAgg || len(p.GroupByItems) > 0 { + if !p.ctx.GetSessionVars().Enable3StageDistinctAgg || !p.ctx.GetSessionVars().Enable3StageMultiDistinctAgg || len(p.GroupByItems) > 0 { return false, nil } defer func() { diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index c3babeb0e908f..3faa2fab07358 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -778,6 +778,9 @@ type SessionVars struct { // Enable3StageDistinctAgg indicates whether to allow 3 stage distinct aggregate Enable3StageDistinctAgg bool + // Enable3StageMultiDistinctAgg indicates whether to allow 3 stage multi distinct aggregate + Enable3StageMultiDistinctAgg bool + // MultiStatementMode permits incorrect client library usage. Not recommended to be turned on. MultiStatementMode int diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index f3ccd7dd534b4..a6a50c7c2ffdc 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -181,6 +181,10 @@ var defaultSysVars = []*SysVar{ s.Enable3StageDistinctAgg = TiDBOptOn(val) return nil }}, + {Scope: ScopeGlobal | ScopeSession, Name: TiDBOptEnable3StageMultiDistinctAgg, Value: BoolToOnOff(DefTiDB3StageMultiDistinctAgg), Type: TypeBool, SetSession: func(s *SessionVars, val string) error { + s.Enable3StageMultiDistinctAgg = TiDBOptOn(val) + return nil + }}, {Scope: ScopeSession, Name: TiDBOptWriteRowID, Value: BoolToOnOff(DefOptWriteRowID), Type: TypeBool, SetSession: func(s *SessionVars, val string) error { s.AllowWriteRowID = TiDBOptOn(val) return nil diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index ccb9aa13af366..04698c4e3d0ed 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -63,6 +63,9 @@ const ( // TiDBOpt3StageDistinctAgg is used to indicate whether to plan and execute the distinct agg in 3 stages TiDBOpt3StageDistinctAgg = "tidb_opt_three_stage_distinct_agg" + // TiDBOptEnable3StageMultiDistinctAgg is used to indicate whether to plan and execute the multi distinct agg in 3 stages + TiDBOptEnable3StageMultiDistinctAgg = "tidb_opt_enable_three_stage_multi_distinct_agg" + // TiDBBCJThresholdSize is used to limit the size of small table for mpp broadcast join. // Its unit is bytes, if the size of small table is larger than it, we will not use bcj. TiDBBCJThresholdSize = "tidb_broadcast_join_threshold_size" @@ -1106,6 +1109,7 @@ const ( DefTiDBRemoveOrderbyInSubquery = false DefTiDBSkewDistinctAgg = false DefTiDB3StageDistinctAgg = true + DefTiDB3StageMultiDistinctAgg = false DefTiDBReadStaleness = 0 DefTiDBGCMaxWaitTime = 24 * 60 * 60 DefMaxAllowedPacket uint64 = 67108864