Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: missed order by item for index merge sometimes (#46576) #46604

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,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 {
Expand Down Expand Up @@ -1249,6 +1256,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 {
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 @@ -5,6 +5,6 @@ go_test(
timeout = "short",
srcs = ["planner_issue_test.go"],
flaky = True,
shard_count = 5,
shard_count = 6,
deps = ["//testkit"],
)
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 @@ -107,3 +107,17 @@ func TestIssue45036(t *testing.T) {
" └─TableReader_9 10000.00 root partition:all data:TableRangeScan_8",
" └─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"))
}