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

statistics: fix wrong point range in crossValidationSelectivity #33357

Merged
merged 3 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions statistics/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,22 @@ func TestNotLoadedStatsOnAllNULLCol(t *testing.T) {
" └─TableReader(Probe) 4.00 root data:TableFullScan",
" └─TableFullScan 4.00 cop[tikv] table:t1 keep order:false"))
}

func TestCrossValidationSelectivity(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
tk := testkit.NewTestKit(t, store)
h := dom.StatsHandle()
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("set @@tidb_analyze_version = 1")
tk.MustExec("create table t (a int, b int, c int, primary key (a, b) clustered)")
require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh()))
tk.MustExec("insert into t values (1,2,3), (1,4,5)")
require.NoError(t, h.DumpStatsDeltaToKV(handle.DumpAll))
tk.MustExec("analyze table t")
tk.MustQuery("explain format = 'brief' select * from t where a = 1 and b > 0 and b < 1000 and c > 1000").Check(testkit.Rows(
"TableReader 0.00 root data:Selection",
"└─Selection 0.00 cop[tikv] gt(test.t.c, 1000)",
" └─TableRangeScan 2.00 cop[tikv] table:t range:(1 0,1 1000), keep order:false"))
}
18 changes: 4 additions & 14 deletions statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,23 +591,13 @@ func (coll *HistColl) crossValidationSelectivity(sctx sessionctx.Context, idx *I
if col.IsInvalid(sctx, coll.Pseudo) {
continue
}
lowExclude := idxPointRange.LowExclude
highExclude := idxPointRange.HighExclude
// Consider this case:
// create table t(a int, b int, c int, primary key(a,b,c));
// insert into t values(1,1,1),(2,2,3);
// explain select * from t where (a,b) in ((1,1),(2,2)) and c > 2;
// For column a, we will get range: (1, 1], (2, 2], but GetColumnRowCount() with rang = (2, 2] will return 0.
// And the result of the explain statement will output estRow 0.0. So we change it to [2, 2].
if lowExclude != highExclude && i < usedColsLen {
lowExclude = false
highExclude = false
}
// Since the column range is point range(LowVal is equal to HighVal), we need to set both LowExclude and HighExclude to false.
// Otherwise we would get 0.0 estRow from GetColumnRowCount.
rang := ranger.Range{
LowVal: []types.Datum{idxPointRange.LowVal[i]},
LowExclude: lowExclude,
LowExclude: false,
HighVal: []types.Datum{idxPointRange.HighVal[i]},
HighExclude: highExclude,
HighExclude: false,
Collators: []collate.Collator{idxPointRange.Collators[i]},
}

Expand Down