forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_test.go
151 lines (144 loc) · 4.85 KB
/
stats_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package core_test
import (
"context"
"fmt"
"testing"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/testkit/testdata"
"github.com/pingcap/tidb/util/hint"
"github.com/stretchr/testify/require"
)
func TestGroupNDVs(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int not null, b int not null, key(a,b))")
tk.MustExec("insert into t1 values(1,1),(1,2),(2,1),(2,2),(1,1)")
tk.MustExec("create table t2(a int not null, b int not null, key(a,b))")
tk.MustExec("insert into t2 values(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3),(1,1)")
tk.MustExec("analyze table t1")
tk.MustExec("analyze table t2")
ctx := context.Background()
p := parser.New()
var input []string
var output []struct {
SQL string
AggInput string
JoinInput string
}
statsSuiteData := core.GetStatsSuiteData()
statsSuiteData.GetTestCases(t, &input, &output)
for i, tt := range input {
comment := fmt.Sprintf("case:%v sql: %s", i, tt)
stmt, err := p.ParseOneStmt(tt, "", "")
require.NoError(t, err, comment)
ret := &core.PreprocessorReturn{}
err = core.Preprocess(tk.Session(), stmt, core.WithPreprocessorReturn(ret))
require.NoError(t, err)
tk.Session().GetSessionVars().PlanColumnID = 0
builder, _ := core.NewPlanBuilder().Init(tk.Session(), ret.InfoSchema, &hint.BlockHintProcessor{})
p, err := builder.Build(ctx, stmt)
require.NoError(t, err, comment)
p, err = core.LogicalOptimize(ctx, builder.GetOptFlag(), p.(core.LogicalPlan))
require.NoError(t, err, comment)
lp := p.(core.LogicalPlan)
_, err = core.RecursiveDeriveStats4Test(lp)
require.NoError(t, err, comment)
var agg *core.LogicalAggregation
var join *core.LogicalJoin
stack := make([]core.LogicalPlan, 0, 2)
traversed := false
for !traversed {
switch v := lp.(type) {
case *core.LogicalAggregation:
agg = v
lp = lp.Children()[0]
case *core.LogicalJoin:
join = v
lp = v.Children()[0]
stack = append(stack, v.Children()[1])
case *core.LogicalApply:
lp = lp.Children()[0]
stack = append(stack, v.Children()[1])
case *core.LogicalUnionAll:
lp = lp.Children()[0]
for i := 1; i < len(v.Children()); i++ {
stack = append(stack, v.Children()[i])
}
case *core.DataSource:
if len(stack) == 0 {
traversed = true
} else {
lp = stack[0]
stack = stack[1:]
}
default:
lp = lp.Children()[0]
}
}
aggInput := ""
joinInput := ""
if agg != nil {
s := core.GetStats4Test(agg.Children()[0])
aggInput = property.ToString(s.GroupNDVs)
}
if join != nil {
l := core.GetStats4Test(join.Children()[0])
r := core.GetStats4Test(join.Children()[1])
joinInput = property.ToString(l.GroupNDVs) + ";" + property.ToString(r.GroupNDVs)
}
testdata.OnRecord(func() {
output[i].SQL = tt
output[i].AggInput = aggInput
output[i].JoinInput = joinInput
})
require.Equal(t, output[i].AggInput, aggInput, comment)
require.Equal(t, output[i].JoinInput, joinInput, comment)
}
}
func TestNDVGroupCols(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int not null, b int not null, key(a,b))")
tk.MustExec("insert into t1 values(1,1),(1,2),(2,1),(2,2)")
tk.MustExec("create table t2(a int not null, b int not null, key(a,b))")
tk.MustExec("insert into t2 values(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)")
tk.MustExec("analyze table t1")
tk.MustExec("analyze table t2")
var input []string
var output []struct {
SQL string
Plan []string
}
statsSuiteData := core.GetStatsSuiteData()
statsSuiteData.GetTestCases(t, &input, &output)
for i, tt := range input {
testdata.OnRecord(func() {
output[i].SQL = tt
output[i].Plan = testdata.ConvertRowsToStrings(tk.MustQuery("explain format = 'brief' " + tt).Rows())
})
// The test point is the row count estimation for aggregations and joins.
tk.MustQuery("explain format = 'brief' " + tt).Check(testkit.Rows(output[i].Plan...))
}
}