diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index 3a4e6bed22a28..e186a566ce197 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -1207,6 +1207,13 @@ func (ds *DataSource) convertToPartialIndexScan(prop *property.PhysicalProperty, is := ds.getOriginalPhysicalIndexScan(prop, path, false, false) // TODO: Consider using isIndexCoveringColumns() to avoid another TableRead indexConds := path.IndexFilters + if matchProp { + if is.Table.GetPartitionInfo() != nil && !is.Index.Global && is.SCtx().GetSessionVars().StmtCtx.UseDynamicPartitionPrune() { + is.Columns, is.schema, _ = AddExtraPhysTblIDColumn(is.SCtx(), is.Columns, is.schema) + } + // Add sort items for index scan for merge-sort operation between partitions. + is.ByItems = byItems + } if len(indexConds) > 0 { var selectivity float64 if path.CountAfterAccess > 0 { @@ -1247,6 +1254,12 @@ func (ds *DataSource) convertToPartialTableScan(prop *property.PhysicalProperty, } } ts.filterCondition = newFilterConds + if matchProp { + if ts.Table.GetPartitionInfo() != nil && ts.SCtx().GetSessionVars().StmtCtx.UseDynamicPartitionPrune() { + ts.Columns, ts.schema, _ = AddExtraPhysTblIDColumn(ts.SCtx(), ts.Columns, ts.schema) + } + ts.ByItems = byItems + } if len(ts.filterCondition) > 0 { selectivity, _, err := ds.tableStats.HistColl.Selectivity(ds.ctx, ts.filterCondition, nil) if err != nil { diff --git a/planner/core/issuetest/planner_issue_test.go b/planner/core/issuetest/planner_issue_test.go index e43e17d95ad2d..9656af1c3153f 100644 --- a/planner/core/issuetest/planner_issue_test.go +++ b/planner/core/issuetest/planner_issue_test.go @@ -110,6 +110,20 @@ func TestIssue45036(t *testing.T) { " └─TableRangeScan_8 10000.00 cop[tikv] table:s range:[1,100000], keep order:false, stats:pseudo")) } +func TestIssue46005(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table tbl_39(col_239 year(4) not null default '2009', primary key(col_239), unique key idx_223(col_239), key idx_224(col_239))") + tk.MustExec("insert into tbl_39 values (1994),(1995),(1996),(1997)") + tk.HasPlan( + "select /*+ use_index_merge( tbl_39) */ col_239 from tbl_39 where not( tbl_39.col_239 not in ( '1994' ) ) and tbl_39.col_239 not in ( '2004' , '2010' , '2010' ) or not( tbl_39.col_239 <= '1996' ) and not( tbl_39.col_239 between '2026' and '2011' ) order by tbl_39.col_239 limit 382", + "IndexMerge", + ) + rs := tk.MustQuery("select /*+ use_index_merge( tbl_39) */ col_239 from tbl_39 where not( tbl_39.col_239 not in ( '1994' ) ) and tbl_39.col_239 not in ( '2004' , '2010' , '2010' ) or not( tbl_39.col_239 <= '1996' ) and not( tbl_39.col_239 between '2026' and '2011' ) order by tbl_39.col_239 limit 382") + rs.Check(testkit.Rows("1994", "1997")) +} + func TestIssue46083(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -506,3 +520,4 @@ func TestIssue52687(t *testing.T) { where (t_kg74.c_obnq8s7_s2 = case when (t_kg74.c_a1tv2 is NULL) then t_kg74.c_g else t_kg74.c_obnq8s7_s2 end );`) } +