Skip to content

Commit

Permalink
planner: missed order by item for index merge sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Aug 31, 2023
1 parent 5fbe25e commit e1f3a13
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
26 changes: 13 additions & 13 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,13 @@ func (ds *DataSource) convertToPartialIndexScan(prop *property.PhysicalProperty,
is := ds.getOriginalPhysicalIndexScan(prop, path, matchProp, 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 {
Expand All @@ -1366,13 +1373,6 @@ func (ds *DataSource) convertToPartialIndexScan(prop *property.PhysicalProperty,
indexPlan.SetChildren(is)
return indexPlan
}
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
}
indexPlan = is
return indexPlan
}
Expand All @@ -1398,6 +1398,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 := cardinality.Selectivity(ds.SCtx(), ds.tableStats.HistColl, ts.filterCondition, nil)
if err != nil {
Expand All @@ -1408,12 +1414,6 @@ func (ds *DataSource) convertToPartialTableScan(prop *property.PhysicalProperty,
tablePlan.SetChildren(ts)
return tablePlan
}
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
}
tablePlan = ts
return tablePlan
}
Expand Down
2 changes: 1 addition & 1 deletion planner/core/issuetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ go_test(
data = glob(["testdata/**"]),
flaky = True,
race = "on",
shard_count = 9,
shard_count = 10,
deps = [
"//parser",
"//planner",
Expand Down
14 changes: 14 additions & 0 deletions planner/core/issuetest/planner_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,17 @@ func TestIssue46083(t *testing.T) {
tk.MustExec("CREATE TEMPORARY TABLE v0(v1 int)")
tk.MustExec("INSERT INTO v0 WITH ta2 AS (TABLE v0) TABLE ta2 FOR UPDATE OF ta2;")
}

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"))
}

0 comments on commit e1f3a13

Please sign in to comment.