Skip to content

Commit

Permalink
planner: apply rule_partition_pruning when optimizing CTE under stati…
Browse files Browse the repository at this point in the history
…c mode (#51903) (#52058)

close #51873
  • Loading branch information
ti-chi-bot authored Apr 1, 2024
1 parent 65dbe03 commit feabbc9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions executor/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
31 changes: 31 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7026,6 +7026,37 @@ func TestTiFlashFineGrainedShuffle(t *testing.T) {
}
}

func TestIssue51873(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
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)
Expand Down
3 changes: 3 additions & 0 deletions planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,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
Expand Down

0 comments on commit feabbc9

Please sign in to comment.