Skip to content

Commit

Permalink
Merge branch 'fk-show-bug' of https://github.com/crazycs520/tidb into…
Browse files Browse the repository at this point in the history
… fk-show-bug
  • Loading branch information
crazycs520 committed Nov 14, 2022
2 parents 083baa6 + 0273d42 commit 12665f2
Show file tree
Hide file tree
Showing 22 changed files with 518 additions and 417 deletions.
4 changes: 3 additions & 1 deletion executor/executor_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func TestIssue24210(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=1")

// for ProjectionExec
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/executor/mockProjectionExecBaseExecutorOpenReturnedError", `return(true)`))
Expand Down Expand Up @@ -579,6 +580,7 @@ func TestIssue30382(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("set @@session.tidb_enable_list_partition = ON;")
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (c_int int, c_str varchar(40), c_decimal decimal(12, 6), primary key (c_int) , key(c_str(2)) , key(c_decimal) ) partition by list (c_int) ( partition p0 values IN (1, 5, 9, 13, 17, 21, 25, 29, 33, 37), partition p1 values IN (2, 6, 10, 14, 18, 22, 26, 30, 34, 38), partition p2 values IN (3, 7, 11, 15, 19, 23, 27, 31, 35, 39), partition p3 values IN (4, 8, 12, 16, 20, 24, 28, 32, 36, 40)) ;")
Expand All @@ -596,7 +598,7 @@ func TestIssue30382(t *testing.T) {
"SelectLock 6400.00 root for update 0",
"└─HashJoin 6400.00 root CARTESIAN inner join, other cond:or(gt(Column#8, 1), or(ne(test.t1.c_str, Column#7), if(ne(Column#9, 0), NULL, 0)))",
" ├─Selection(Build) 0.80 root ne(Column#10, 0)",
" │ └─StreamAgg 1.00 root funcs:max(Column#17)->Column#7, funcs:count(distinct Column#18)->Column#8, funcs:sum(Column#19)->Column#9, funcs:count(1)->Column#10",
" │ └─HashAgg 1.00 root funcs:max(Column#17)->Column#7, funcs:count(distinct Column#18)->Column#8, funcs:sum(Column#19)->Column#9, funcs:count(1)->Column#10",
" │ └─Projection 3323.33 root test.t2.c_str, test.t2.c_str, cast(isnull(test.t2.c_str), decimal(20,0) BINARY)->Column#19",
" │ └─TableReader 3323.33 root partition:all data:Selection",
" │ └─Selection 3323.33 cop[tikv] lt(test.t2.c_decimal, 5)",
Expand Down
3 changes: 2 additions & 1 deletion executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ func TestIndexNestedLoopHashJoin(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("set @@tidb_init_chunk_size=2")
tk.MustExec("set @@tidb_index_join_batch_size=10")
tk.MustExec("DROP TABLE IF EXISTS t, s")
Expand Down Expand Up @@ -1478,7 +1479,7 @@ func TestIndexNestedLoopHashJoin(t *testing.T) {
" └─TableRowIDScan 27.00 cop[tikv] table:l2 keep order:false"))
tk.MustQuery("select * from t l1 where exists ( select * from t l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey <> l1.l_suppkey )order by `l_orderkey`,`l_linenumber`;").Check(testkit.Rows("0 0 0 0", "0 1 0 1", "0 2 0 0", "1 0 1 0", "1 1 1 1", "1 2 1 0", "2 0 0 0", "2 1 0 1", "2 2 0 0"))
tk.MustQuery("desc format = 'brief' select count(*) from t l1 where exists ( select * from t l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey <> l1.l_suppkey );").Check(testkit.Rows(
"StreamAgg 1.00 root funcs:count(1)->Column#11",
"HashAgg 1.00 root funcs:count(1)->Column#11",
"└─IndexHashJoin 7.20 root semi join, inner:IndexLookUp, outer key:test.t.l_orderkey, inner key:test.t.l_orderkey, equal cond:eq(test.t.l_orderkey, test.t.l_orderkey), other cond:ne(test.t.l_suppkey, test.t.l_suppkey)",
" ├─TableReader(Build) 9.00 root data:Selection",
" │ └─Selection 9.00 cop[tikv] not(isnull(test.t.l_suppkey))",
Expand Down
1 change: 1 addition & 0 deletions executor/oomtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_test(
timeout = "short",
srcs = ["oom_test.go"],
flaky = True,
race = "on",
deps = [
"//testkit",
"//testkit/testsetup",
Expand Down
1 change: 1 addition & 0 deletions executor/oomtest/oom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func TestMemTracker4DeleteExec(t *testing.T) {

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=1")
tk.MustExec("create table MemTracker4DeleteExec1 (id int, a int, b int, index idx_a(`a`))")
tk.MustExec("create table MemTracker4DeleteExec2 (id int, a int, b int, index idx_a(`a`))")

Expand Down
25 changes: 13 additions & 12 deletions executor/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,7 @@ func TestParallelApply(t *testing.T) {

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_cost_model_version=2")
tk.MustExec("create database test_parallel_apply")
tk.MustExec("use test_parallel_apply")
tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'")
Expand Down Expand Up @@ -2149,9 +2150,9 @@ func TestParallelApply(t *testing.T) {
`└─Apply 10000.00 root CARTESIAN inner join, other cond:gt(cast(test_parallel_apply.touter.a, decimal(10,0) BINARY), Column#7)`,
` ├─TableReader(Build) 10000.00 root data:TableFullScan`,
` │ └─TableFullScan 10000.00 cop[tikv] table:touter keep order:false, stats:pseudo`,
` └─StreamAgg(Probe) 10000.00 root funcs:sum(Column#9)->Column#7`,
` └─IndexReader 10000.00 root partition:all index:StreamAgg`, // IndexReader is a inner child of Apply
` └─StreamAgg 10000.00 cop[tikv] funcs:sum(test_parallel_apply.thash.a)->Column#9`,
` └─HashAgg(Probe) 10000.00 root funcs:sum(Column#8)->Column#7`,
` └─IndexReader 10000.00 root partition:all index:HashAgg`, // IndexReader is a inner child of Apply
` └─HashAgg 10000.00 cop[tikv] funcs:sum(test_parallel_apply.thash.a)->Column#8`,
` └─Selection 80000000.00 cop[tikv] gt(test_parallel_apply.thash.a, test_parallel_apply.touter.b)`,
` └─IndexFullScan 100000000.00 cop[tikv] table:thash, index:a(a) keep order:false, stats:pseudo`))
tk.MustQuery(`select * from touter where touter.a > (select sum(thash.a) from thash use index(a) where thash.a>touter.b)`).Sort().Check(
Expand All @@ -2163,9 +2164,9 @@ func TestParallelApply(t *testing.T) {
`└─Apply 10000.00 root CARTESIAN inner join, other cond:gt(cast(test_parallel_apply.touter.a, decimal(10,0) BINARY), Column#7)`,
` ├─TableReader(Build) 10000.00 root data:TableFullScan`,
` │ └─TableFullScan 10000.00 cop[tikv] table:touter keep order:false, stats:pseudo`,
` └─StreamAgg(Probe) 10000.00 root funcs:sum(Column#9)->Column#7`,
` └─TableReader 10000.00 root partition:all data:StreamAgg`, // TableReader is a inner child of Apply
` └─StreamAgg 10000.00 cop[tikv] funcs:sum(test_parallel_apply.thash.b)->Column#9`,
` └─HashAgg(Probe) 10000.00 root funcs:sum(Column#8)->Column#7`,
` └─TableReader 10000.00 root partition:all data:HashAgg`, // TableReader is a inner child of Apply
` └─HashAgg 10000.00 cop[tikv] funcs:sum(test_parallel_apply.thash.b)->Column#8`,
` └─Selection 80000000.00 cop[tikv] gt(test_parallel_apply.thash.a, test_parallel_apply.touter.b)`,
` └─TableFullScan 100000000.00 cop[tikv] table:thash keep order:false, stats:pseudo`))
tk.MustQuery(`select * from touter where touter.a > (select sum(thash.b) from thash ignore index(a) where thash.a>touter.b)`).Sort().Check(
Expand All @@ -2192,9 +2193,9 @@ func TestParallelApply(t *testing.T) {
`└─Apply 10000.00 root CARTESIAN inner join, other cond:gt(cast(test_parallel_apply.touter.a, decimal(10,0) BINARY), Column#7)`,
` ├─TableReader(Build) 10000.00 root data:TableFullScan`,
` │ └─TableFullScan 10000.00 cop[tikv] table:touter keep order:false, stats:pseudo`,
` └─StreamAgg(Probe) 10000.00 root funcs:sum(Column#9)->Column#7`,
` └─IndexReader 10000.00 root partition:all index:StreamAgg`, // IndexReader is a inner child of Apply
` └─StreamAgg 10000.00 cop[tikv] funcs:sum(test_parallel_apply.trange.a)->Column#9`,
` └─HashAgg(Probe) 10000.00 root funcs:sum(Column#8)->Column#7`,
` └─IndexReader 10000.00 root partition:all index:HashAgg`, // IndexReader is a inner child of Apply
` └─HashAgg 10000.00 cop[tikv] funcs:sum(test_parallel_apply.trange.a)->Column#8`,
` └─Selection 80000000.00 cop[tikv] gt(test_parallel_apply.trange.a, test_parallel_apply.touter.b)`,
` └─IndexFullScan 100000000.00 cop[tikv] table:trange, index:a(a) keep order:false, stats:pseudo`))
tk.MustQuery(`select * from touter where touter.a > (select sum(trange.a) from trange use index(a) where trange.a>touter.b)`).Sort().Check(
Expand All @@ -2206,9 +2207,9 @@ func TestParallelApply(t *testing.T) {
`└─Apply 10000.00 root CARTESIAN inner join, other cond:gt(cast(test_parallel_apply.touter.a, decimal(10,0) BINARY), Column#7)`,
` ├─TableReader(Build) 10000.00 root data:TableFullScan`,
` │ └─TableFullScan 10000.00 cop[tikv] table:touter keep order:false, stats:pseudo`,
` └─StreamAgg(Probe) 10000.00 root funcs:sum(Column#9)->Column#7`,
` └─TableReader 10000.00 root partition:all data:StreamAgg`, // TableReader is a inner child of Apply
` └─StreamAgg 10000.00 cop[tikv] funcs:sum(test_parallel_apply.trange.b)->Column#9`,
` └─HashAgg(Probe) 10000.00 root funcs:sum(Column#8)->Column#7`,
` └─TableReader 10000.00 root partition:all data:HashAgg`, // TableReader is a inner child of Apply
` └─HashAgg 10000.00 cop[tikv] funcs:sum(test_parallel_apply.trange.b)->Column#8`,
` └─Selection 80000000.00 cop[tikv] gt(test_parallel_apply.trange.a, test_parallel_apply.touter.b)`,
` └─TableFullScan 100000000.00 cop[tikv] table:trange keep order:false, stats:pseudo`))
tk.MustQuery(`select * from touter where touter.a > (select sum(trange.b) from trange ignore index(a) where trange.a>touter.b)`).Sort().Check(
Expand Down
1 change: 1 addition & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ func TestPlanCacheWithDifferentVariableTypes(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`set tidb_enable_prepared_plan_cache=1`)
tk.MustExec("set tidb_cost_model_version=1")
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("set @@tidb_enable_collect_execution_info=0;")
Expand Down
1 change: 1 addition & 0 deletions executor/seqtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_test(
"seq_executor_test.go",
],
flaky = True,
race = "on",
shard_count = 39,
deps = [
"//config",
Expand Down
20 changes: 15 additions & 5 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -1776,14 +1776,24 @@ func (e *SimpleExec) executeAlterInstance(s *ast.AlterInstanceStmt) error {
func (e *SimpleExec) executeDropStats(s *ast.DropStatsStmt) (err error) {
h := domain.GetDomain(e.ctx).StatsHandle()
var statsIDs []int64
// TODO: GLOBAL option will be deprecated. Also remove this condition when the syntax is removed
if s.IsGlobalStats {
statsIDs = []int64{s.Table.TableInfo.ID}
statsIDs = []int64{s.Tables[0].TableInfo.ID}
} else {
if statsIDs, _, err = core.GetPhysicalIDsAndPartitionNames(s.Table.TableInfo, s.PartitionNames); err != nil {
return err
}
if len(s.PartitionNames) == 0 {
statsIDs = append(statsIDs, s.Table.TableInfo.ID)
for _, table := range s.Tables {
partitionStatIds, _, err := core.GetPhysicalIDsAndPartitionNames(table.TableInfo, nil)
if err != nil {
return err
}
statsIDs = append(statsIDs, partitionStatIds...)
statsIDs = append(statsIDs, table.TableInfo.ID)
}
} else {
// TODO: drop stats for specific partition is deprecated. Also remove this condition when the syntax is removed
if statsIDs, _, err = core.GetPhysicalIDsAndPartitionNames(s.Tables[0].TableInfo, s.PartitionNames); err != nil {
return err
}
}
}
if err := h.DeleteTableStatsFromKV(statsIDs); err != nil {
Expand Down
49 changes: 49 additions & 0 deletions executor/simpletest/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@ partition by range (a) (
checkPartitionStats("global", "p0", "p1", "global")

tk.MustExec("drop stats test_drop_gstats partition p0")
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1681|'DROP STATS ... PARTITION ...' is deprecated and will be removed in a future release."))
checkPartitionStats("global", "p1", "global")

err := tk.ExecToErr("drop stats test_drop_gstats partition abcde")
Expand All @@ -1010,6 +1011,7 @@ partition by range (a) (
checkPartitionStats("global", "p1")

tk.MustExec("drop stats test_drop_gstats global")
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1287|'DROP STATS ... GLOBAL' is deprecated and will be removed in a future release. Please use DROP STATS ... instead"))
checkPartitionStats("p1")

tk.MustExec("analyze table test_drop_gstats")
Expand Down Expand Up @@ -1056,3 +1058,50 @@ func TestDropStats(t *testing.T) {
require.True(t, statsTbl.Pseudo)
h.SetLease(0)
}

func TestDropStatsForMultipleTable(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
testKit := testkit.NewTestKit(t, store)
testKit.MustExec("use test")
testKit.MustExec("create table t1 (c1 int, c2 int)")
testKit.MustExec("create table t2 (c1 int, c2 int)")

is := dom.InfoSchema()
tbl1, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
require.NoError(t, err)
tableInfo1 := tbl1.Meta()

tbl2, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t2"))
require.NoError(t, err)
tableInfo2 := tbl2.Meta()

h := dom.StatsHandle()
h.Clear()
testKit.MustExec("analyze table t1, t2")
statsTbl1 := h.GetTableStats(tableInfo1)
require.False(t, statsTbl1.Pseudo)
statsTbl2 := h.GetTableStats(tableInfo2)
require.False(t, statsTbl2.Pseudo)

testKit.MustExec("drop stats t1, t2")
require.Nil(t, h.Update(is))
statsTbl1 = h.GetTableStats(tableInfo1)
require.True(t, statsTbl1.Pseudo)
statsTbl2 = h.GetTableStats(tableInfo2)
require.True(t, statsTbl2.Pseudo)

testKit.MustExec("analyze table t1, t2")
statsTbl1 = h.GetTableStats(tableInfo1)
require.False(t, statsTbl1.Pseudo)
statsTbl2 = h.GetTableStats(tableInfo2)
require.False(t, statsTbl2.Pseudo)

h.SetLease(1)
testKit.MustExec("drop stats t1, t2")
require.Nil(t, h.Update(is))
statsTbl1 = h.GetTableStats(tableInfo1)
require.True(t, statsTbl1.Pseudo)
statsTbl2 = h.GetTableStats(tableInfo2)
require.True(t, statsTbl2.Pseudo)
h.SetLease(0)
}
1 change: 1 addition & 0 deletions executor/splittest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_test(
"split_table_test.go",
],
flaky = True,
race = "on",
shard_count = 5,
deps = [
"//ddl",
Expand Down
1 change: 1 addition & 0 deletions expression/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ go_test(
data = glob(["testdata/**"]),
embed = [":expression"],
flaky = True,
race = "on",
shard_count = 50,
deps = [
"//config",
Expand Down
6 changes: 5 additions & 1 deletion parser/ast/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ func TestMiscVisitorCover(t *testing.T) {
&ast.PrivElem{},
&ast.VariableAssignment{Value: valueExpr},
&ast.KillStmt{},
&ast.DropStatsStmt{Table: &ast.TableName{}},
&ast.DropStatsStmt{
Tables: []*ast.TableName{
{},
},
},
&ast.ShutdownStmt{},
}

Expand Down
23 changes: 16 additions & 7 deletions parser/ast/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,26 @@ func (n *AnalyzeTableStmt) Accept(v Visitor) (Node, bool) {
}

// DropStatsStmt is used to drop table statistics.
// if the PartitionNames is not empty, or IsGlobalStats is true, it will contain exactly one table
type DropStatsStmt struct {
stmtNode

Table *TableName
Tables []*TableName
PartitionNames []model.CIStr
IsGlobalStats bool
}

// Restore implements Node interface.
func (n *DropStatsStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("DROP STATS ")
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while add table")

for index, table := range n.Tables {
if index != 0 {
ctx.WritePlain(", ")
}
if err := table.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore DropStatsStmt.Tables[%d]", index)
}
}

if n.IsGlobalStats {
Expand All @@ -228,11 +235,13 @@ func (n *DropStatsStmt) Accept(v Visitor) (Node, bool) {
return v.Leave(newNode)
}
n = newNode.(*DropStatsStmt)
node, ok := n.Table.Accept(v)
if !ok {
return n, false
for i, val := range n.Tables {
node, ok := val.Accept(v)
if !ok {
return n, false
}
n.Tables[i] = node.(*TableName)
}
n.Table = node.(*TableName)
return v.Leave(n)
}

Expand Down
Loading

0 comments on commit 12665f2

Please sign in to comment.