forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoiner_test.go
100 lines (88 loc) · 2.87 KB
/
joiner_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
// Copyright 2019 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"math/rand"
. "github.com/pingcap/check"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)
var _ = Suite(&testSuiteJoiner{})
type testSuiteJoiner struct{}
func (s *testSuiteJoiner) SetUpSuite(c *C) {
}
func (s *testSuiteJoiner) TestRequiredRows(c *C) {
joinTypes := []core.JoinType{core.InnerJoin, core.LeftOuterJoin, core.RightOuterJoin}
lTypes := [][]byte{
{mysql.TypeLong},
{mysql.TypeFloat},
{mysql.TypeLong, mysql.TypeFloat},
}
rTypes := lTypes
convertTypes := func(mysqlTypes []byte) []*types.FieldType {
fieldTypes := make([]*types.FieldType, 0, len(mysqlTypes))
for _, t := range mysqlTypes {
fieldTypes = append(fieldTypes, types.NewFieldType(t))
}
return fieldTypes
}
for _, joinType := range joinTypes {
for _, ltype := range lTypes {
for _, rtype := range rTypes {
maxChunkSize := defaultCtx().GetSessionVars().MaxChunkSize
lfields := convertTypes(ltype)
rfields := convertTypes(rtype)
outerRow := genTestChunk(maxChunkSize, 1, lfields).GetRow(0)
innerChk := genTestChunk(maxChunkSize, maxChunkSize, rfields)
var defaultInner []types.Datum
for i, f := range rfields {
defaultInner = append(defaultInner, innerChk.GetRow(0).GetDatum(i, f))
}
joiner := newJoiner(defaultCtx(), joinType, false, defaultInner, nil, lfields, rfields)
fields := make([]*types.FieldType, 0, len(lfields)+len(rfields))
fields = append(fields, rfields...)
fields = append(fields, lfields...)
result := chunk.New(fields, maxChunkSize, maxChunkSize)
for i := 0; i < 10; i++ {
required := rand.Int()%maxChunkSize + 1
result.SetRequiredRows(required, maxChunkSize)
result.Reset()
it := chunk.NewIterator4Chunk(innerChk)
it.Begin()
_, _, err := joiner.tryToMatchInners(outerRow, it, result)
c.Assert(err, IsNil)
c.Assert(result.NumRows(), Equals, required)
}
}
}
}
}
func genTestChunk(maxChunkSize int, numRows int, fields []*types.FieldType) *chunk.Chunk {
chk := chunk.New(fields, maxChunkSize, maxChunkSize)
for numRows > 0 {
numRows--
for col, field := range fields {
switch field.Tp {
case mysql.TypeLong:
chk.AppendInt64(col, 0)
case mysql.TypeFloat:
chk.AppendFloat32(col, 0)
default:
panic("not support")
}
}
}
return chk
}