From b1e0241e99c241f2efd9fc44901f46c4d03fa1e1 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 18 Apr 2024 21:36:08 +0800 Subject: [PATCH] planner: apply rule_partition_pruning when optimizing CTE under static mode (#51903) (#52269) close pingcap/tidb#51873 --- executor/sample_test.go | 1 + planner/core/integration_test.go | 34 +++++++++++++++++++++++++++++++- planner/core/optimizer.go | 3 +++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/executor/sample_test.go b/executor/sample_test.go index 2db04f1a8a92e..6c96cb1681c9d 100644 --- a/executor/sample_test.go +++ b/executor/sample_test.go @@ -178,6 +178,7 @@ func TestTableSampleWithTiDBRowID(t *testing.T) { func TestTableSampleWithPartition(t *testing.T) { store := testkit.CreateMockStore(t) tk := createSampleTestkit(t, store) + tk.MustExec(`set @@tidb_opt_fix_control = "44262:ON"`) tk.MustExec("create table t (a int, b varchar(255), primary key (a)) partition by hash(a) partitions 2;") tk.MustExec("insert into t values (1, '1'), (2, '2'), (3, '3');") rows := tk.MustQuery("select * from t tablesample regions();").Rows() diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index df0d04d1e6381..39a6baef1376b 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -3843,6 +3843,38 @@ func TestAggPushToCopForCachedTable(t *testing.T) { tk.MustExec("drop table if exists t31202") } +func TestIssue51873(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec(`create database if not exists test1`) + tk.MustExec(`use test1`) + tk.MustExec(`CREATE TABLE h1 ( + id bigint(20) NOT NULL AUTO_INCREMENT, + position_date date NOT NULL, + asset_id varchar(32) DEFAULT NULL, + portfolio_code varchar(50) DEFAULT NULL, + PRIMARY KEY (id,position_date) /*T![clustered_index] NONCLUSTERED */, + UNIQUE KEY uidx_posi_asset_balance_key (position_date,portfolio_code,asset_id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=30002 +PARTITION BY RANGE COLUMNS(position_date) +(PARTITION p202401 VALUES LESS THAN ('2024-02-01'))`) + tk.MustExec(`create table h2 like h1`) + tk.MustExec(`insert into h1 values(1,'2024-01-01',1,1)`) + tk.MustExec(`insert into h2 values(1,'2024-01-01',1,1)`) + tk.MustExec(`analyze table h1`) + tk.MustQuery(`with assetBalance AS + (SELECT asset_id, portfolio_code FROM h1 pab WHERE pab.position_date = '2024-01-01' ), +cashBalance AS (SELECT portfolio_code, asset_id + FROM h2 pcb WHERE pcb.position_date = '2024-01-01' ), +assetIdList AS (SELECT DISTINCT asset_id AS assetId + FROM assetBalance ) +SELECT main.portfolioCode +FROM (SELECT DISTINCT balance.portfolio_code AS portfolioCode + FROM assetBalance balance + LEFT JOIN assetIdList + ON balance.asset_id = assetIdList.assetId ) main`).Check(testkit.Rows("1")) +} + func TestTiFlashFineGrainedShuffleWithMaxTiFlashThreads(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -5342,7 +5374,7 @@ func TestIssue41458(t *testing.T) { tk.MustExec("use test") tk.MustExec(`create table t (a int, b int, c int, index ia(a));`) tk.MustExec("select * from t t1 join t t2 on t1.b = t2.b join t t3 on t2.b=t3.b join t t4 on t3.b=t4.b where t3.a=1 and t2.a=2;") - rawRows := tk.MustQuery("select plan from information_schema.statements_summary where SCHEMA_NAME = 'test' and STMT_TYPE = 'Select';").Sort().Rows() + rawRows := tk.MustQuery("select plan from information_schema.statements_summary where SCHEMA_NAME = 'test' and STMT_TYPE = 'Select' and DIGEST_TEXT LIKE '%t3%';").Sort().Rows() plan := rawRows[0][0].(string) rows := strings.Split(plan, "\n") rows = rows[1:] diff --git a/planner/core/optimizer.go b/planner/core/optimizer.go index 44a68a0c3f66f..528100d2bdb35 100644 --- a/planner/core/optimizer.go +++ b/planner/core/optimizer.go @@ -301,6 +301,9 @@ func DoOptimize(ctx context.Context, sctx sessionctx.Context, flag uint64, logic } flag |= flagCollectPredicateColumnsPoint flag |= flagSyncWaitStatsLoadPoint + if !logic.SCtx().GetSessionVars().StmtCtx.UseDynamicPruneMode { + flag |= flagPartitionProcessor // apply partition pruning under static mode + } logic, err := logicalOptimize(ctx, flag, logic) if err != nil { return nil, 0, err