-
Notifications
You must be signed in to change notification settings - Fork 273
/
sample.go
241 lines (213 loc) · 6.6 KB
/
sample.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2021 Matrix Origin
//
// 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 sample
import (
"bytes"
"fmt"
"github.com/matrixorigin/matrixone/pkg/common/hashmap"
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/common/mpool"
"github.com/matrixorigin/matrixone/pkg/container/batch"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/container/vector"
"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/sql/colexec"
"github.com/matrixorigin/matrixone/pkg/vm"
"github.com/matrixorigin/matrixone/pkg/vm/process"
"math/rand"
)
func (arg *Argument) String(buf *bytes.Buffer) {
switch arg.Type {
case mergeSampleByRow:
buf.WriteString(fmt.Sprintf("merge sample %d rows ", arg.Rows))
case sampleByRow:
buf.WriteString(fmt.Sprintf(" sample %d rows ", arg.Rows))
case sampleByPercent:
buf.WriteString(fmt.Sprintf(" sample %.2f percent ", arg.Percents))
default:
buf.WriteString("unknown sample type")
}
}
func (arg *Argument) Prepare(proc *process.Process) (err error) {
arg.ctr = &container{
isGroupBy: len(arg.GroupExprs) != 0,
isMultiSample: len(arg.SampleExprs) > 1,
tempBatch1: make([]*batch.Batch, 1),
sampleVectors: make([]*vector.Vector, len(arg.SampleExprs)),
}
switch arg.Type {
case sampleByRow:
arg.ctr.samplePool = newSamplePoolByRows(proc, arg.Rows, len(arg.SampleExprs))
case sampleByPercent:
arg.ctr.samplePool = newSamplePoolByPercent(proc, arg.Percents, len(arg.SampleExprs))
case mergeSampleByRow:
arg.ctr.samplePool = newSamplePoolByRowsForMerge(proc, arg.Rows, len(arg.SampleExprs))
default:
return moerr.NewInternalErrorNoCtx(fmt.Sprintf("unknown sample type %d", arg.Type))
}
arg.ctr.samplePool.setPerfFields(arg.ctr.isGroupBy)
// sample column related.
arg.ctr.sampleExecutors, err = colexec.NewExpressionExecutorsFromPlanExpressions(proc, arg.SampleExprs)
if err != nil {
return err
}
// group by columns related.
arg.ctr.groupVectorsNullable = false
if arg.ctr.isGroupBy {
arg.ctr.groupExecutors = make([]colexec.ExpressionExecutor, len(arg.GroupExprs))
for i := range arg.GroupExprs {
arg.ctr.groupExecutors[i], err = colexec.NewExpressionExecutor(proc, arg.GroupExprs[i])
if err != nil {
return err
}
}
arg.ctr.groupVectors = make([]*vector.Vector, len(arg.GroupExprs))
keyWidth, groupKeyNullable := getGroupKeyWidth(arg.GroupExprs)
arg.ctr.useIntHashMap = keyWidth <= 8
arg.ctr.groupVectorsNullable = groupKeyNullable
}
return nil
}
func (arg *Argument) Call(proc *process.Process) (vm.CallResult, error) {
if err, isCancel := vm.CancelCheck(proc); isCancel {
return vm.CancelResult, err
}
// duplicate code from other operators.
result, lastErr := arg.children[0].Call(proc)
if lastErr != nil {
return result, lastErr
}
anal := proc.GetAnalyze(arg.info.Idx, arg.info.ParallelIdx, arg.info.ParallelMajor)
anal.Start()
defer anal.Stop()
if arg.buf != nil {
proc.PutBatch(arg.buf)
arg.buf = nil
}
// real work starts here.
bat := result.Batch
ctr := arg.ctr
if ctr.workDone {
result.Batch = nil
return result, nil
}
if bat == nil {
result.Batch, lastErr = ctr.samplePool.Output(true)
anal.Output(result.Batch, arg.info.IsLast)
arg.buf = result.Batch
result.Status = vm.ExecStop
ctr.workDone = true
return result, lastErr
}
var err error
if !bat.IsEmpty() {
anal.Input(bat, arg.info.IsFirst)
if err = ctr.evaluateSampleAndGroupByColumns(proc, bat); err != nil {
return result, err
}
if ctr.isGroupBy {
err = ctr.hashAndSample(bat, arg.IBucket, arg.NBucket, proc.Mp())
} else {
err = ctr.samplePool.Sample(1, ctr.sampleVectors, nil, bat)
}
if err != nil {
return result, err
}
}
// if the pool is full, there is a 50 % chance that we can stop the query for performance.
if ctr.samplePool.IsFull() && rand.Intn(2) == 0 {
result.Batch, err = ctr.samplePool.Output(true)
result.Status = vm.ExecStop
ctr.workDone = true
} else {
result.Batch, err = ctr.samplePool.Output(false)
}
anal.Output(result.Batch, arg.info.IsLast)
arg.buf = result.Batch
return result, err
}
func getGroupKeyWidth(exprList []*plan.Expr) (keyWidth int, groupKeyNullable bool) {
keyWidth = 0
groupKeyNullable = false
for _, expr := range exprList {
width := types.T(expr.Typ.Id).TypeLen()
groupKeyNullable = groupKeyNullable || (!expr.Typ.NotNullable)
if types.T(expr.Typ.Id).FixedLength() < 0 {
width = 128
if expr.Typ.Width != 0 {
width = int(expr.Typ.Width)
}
}
if groupKeyNullable {
width++
}
keyWidth += width
}
return keyWidth, groupKeyNullable
}
func (ctr *container) evaluateSampleAndGroupByColumns(proc *process.Process, bat *batch.Batch) (err error) {
ctr.tempBatch1[0] = bat
// evaluate the sample columns.
for i, executor := range ctr.sampleExecutors {
ctr.sampleVectors[i], err = executor.Eval(proc, ctr.tempBatch1)
if err != nil {
return err
}
}
// evaluate the group by columns.
for i, executor := range ctr.groupExecutors {
ctr.groupVectors[i], err = executor.Eval(proc, ctr.tempBatch1)
if err != nil {
return err
}
}
return nil
}
func (ctr *container) hashAndSample(bat *batch.Batch, ib, nb int, mp *mpool.MPool) (err error) {
var iterator hashmap.Iterator
var groupList []uint64
count := bat.RowCount()
if ctr.useIntHashMap {
if ctr.intHashMap == nil {
ctr.intHashMap, err = hashmap.NewIntHashMap(ctr.groupVectorsNullable, uint64(ib), uint64(nb), mp)
if err != nil {
return err
}
}
iterator = ctr.intHashMap.NewIterator()
} else {
if ctr.strHashMap == nil {
ctr.strHashMap, err = hashmap.NewStrMap(ctr.groupVectorsNullable, uint64(ib), uint64(nb), mp)
if err != nil {
return err
}
}
iterator = ctr.strHashMap.NewIterator()
}
for i := 0; i < count; i += hashmap.UnitLimit {
n := count - i
if n > hashmap.UnitLimit {
n = hashmap.UnitLimit
}
groupList, _, err = iterator.Insert(i, n, ctr.groupVectors)
if err != nil {
return err
}
err = ctr.samplePool.BatchSample(i, n, groupList, ctr.sampleVectors, ctr.groupVectors, bat)
if err != nil {
return err
}
}
return
}