forked from araddon/qlbridge
/
groupby.go
482 lines (425 loc) · 11.2 KB
/
groupby.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package exec
import (
"database/sql/driver"
"encoding/gob"
"fmt"
"strings"
"time"
u "github.com/araddon/gou"
"github.com/araddon/qlbridge/datasource"
"github.com/araddon/qlbridge/expr"
"github.com/araddon/qlbridge/plan"
"github.com/araddon/qlbridge/rel"
"github.com/araddon/qlbridge/value"
"github.com/araddon/qlbridge/vm"
)
var (
_ = u.EMPTY
// Ensure that we implement the Task Runner interface
_ TaskRunner = (*GroupBy)(nil)
)
func init() {
gob.Register(AggPartial{})
}
// Group by a Sql Group By task which creates a hashable key from row
// commposed of key = {each,value,of,column,in,groupby}
//
// A very stupid naive parallel groupby holds values in memory. This
// is a toy implementation that is only useful for small cardinality
// group-bys, small number of rows.
type GroupBy struct {
*TaskBase
closed bool
p *plan.GroupBy
}
func NewGroupBy(ctx *plan.Context, p *plan.GroupBy) *GroupBy {
m := &GroupBy{
TaskBase: NewTaskBase(ctx),
p: p,
}
return m
}
// GroupByFinal a Sql Group By Operator finalizer for partials. IE, if group
// by is a distributed task, then this is the reducer for sub-tasks.
type GroupByFinal struct {
*TaskBase
p *plan.GroupBy
complete chan bool
closed bool
isComplete bool
}
// NewGroupByFinal creates the group-by-finalizer task.
func NewGroupByFinal(ctx *plan.Context, p *plan.GroupBy) *GroupByFinal {
m := &GroupByFinal{
TaskBase: NewTaskBase(ctx),
p: p,
complete: make(chan bool),
}
return m
}
// Run runs this group by tasks, standard task interface.
func (m *GroupBy) Run() error {
defer m.Ctx.Recover()
defer close(m.msgOutCh)
outCh := m.MessageOut()
inCh := m.MessageIn()
columns := m.p.Stmt.Columns
colIndex := m.p.Stmt.ColIndexes()
aggs, err := buildAggs(m.p)
if err != nil {
u.Warnf("Group By statement not supported? %v", err)
return err
}
// are are going to hold entire row in memory while we are calculating
// so obviously not scalable.
gb := make(map[string][]*datasource.SqlDriverMessageMap)
msgReadLoop:
for {
select {
case <-m.SigChan():
return nil
case msg, ok := <-inCh:
if !ok {
break msgReadLoop
} else {
var sdm *datasource.SqlDriverMessageMap
switch mt := msg.(type) {
case *datasource.SqlDriverMessageMap:
sdm = mt
default:
msgReader, isContextReader := msg.(expr.ContextReader)
if !isContextReader {
err := fmt.Errorf("To use Join must use SqlDriverMessageMap but got %T", msg)
u.Errorf("unrecognized msg %T", msg)
close(m.TaskBase.sigCh)
return err
}
sdm = datasource.NewSqlDriverMessageMapCtx(msg.Id(), msgReader, colIndex)
}
// We are going to use VM Engine to create a value for each statement in group by
// then join each value together to create a unique key.
keys := make([]string, len(m.p.Stmt.GroupBy))
for i, col := range m.p.Stmt.GroupBy {
if key, ok := vm.Eval(sdm, col.Expr); ok {
keys[i] = key.ToString()
}
}
key := strings.Join(keys, ",")
gb[key] = append(gb[key], sdm)
}
}
}
i := uint64(0)
for key, v := range gb {
//u.Debugf("got %s:%v msgs", k, len(v))
for _, mm := range v {
for i, col := range columns {
//u.Debugf("col: idx:%v sidx: %v pidx:%v key:%v %s", col.Index, col.SourceIndex, col.ParentIndex, col.Key(), col.Expr)
if col.Expr == nil {
u.Warnf("wat? nil col expr? %#v", col)
} else {
v, ok := vm.Eval(mm, col.Expr)
//u.Infof("mt: %T mm %#v", mm, mm)
if !ok || v == nil {
//u.Debugf("evaled nil? key=%v val=%v expr:%s", col.Key(), v, col.Expr.String())
//u.Infof("mt: %T mm %#v", mm, mm)
aggs[i].Do(value.NewNilValue())
} else {
//u.Debugf("evaled: key=%v val=%v", col.Key(), v.Value())
aggs[i].Do(v)
}
}
}
}
row := make([]driver.Value, len(columns))
for i, agg := range aggs {
row[i] = driver.Value(agg.Result())
agg.Reset()
//u.Debugf("agg result: %#v %v", row[i], row[i])
}
if m.p.Partial {
// Partial results, append key at end? shouldn't be able to be fit in message itself?
row = append(row, key)
//u.Debugf("GroupBy output row? key:%s %#v", key, row)
}
//u.Debugf("row: %v cols:%v", row, colIndex)
outCh <- datasource.NewSqlDriverMessageMap(i, row, colIndex)
i++
}
return nil
}
// Run group-by-final Runs standard task interface.
func (m *GroupByFinal) Run() error {
defer m.Ctx.Recover()
defer close(m.msgOutCh)
outCh := m.MessageOut()
inCh := m.MessageIn()
columns := m.p.Stmt.Columns
colIndex := m.p.Stmt.ColIndexes()
m.p.Partial = false
aggs, err := buildAggs(m.p)
if err != nil {
return err
}
gb := make(map[string][][]driver.Value)
msgReadLoop:
for {
select {
case <-m.SigChan():
u.Warnf("got signal quit")
return nil
case msg, ok := <-inCh:
if !ok {
//u.Debugf("GroupByFinal, got closed channel shutdown")
break msgReadLoop
} else {
//u.Infof("got gbfinal message %#v", msg)
switch mt := msg.(type) {
case *datasource.SqlDriverMessageMap:
if len(mt.Vals) != len(columns)+1 {
u.Warnf("Wrong number of values? %#v", mt)
}
key, ok := mt.Vals[len(mt.Vals)-1].(string)
if !ok {
u.Warnf("expected key? %#v", mt.Vals)
}
vals := mt.Vals[0 : len(mt.Vals)-1]
//u.Infof("found key:%s for %#v", key, mt.Vals)
gb[key] = append(gb[key], vals)
default:
err := fmt.Errorf("To use Join must use SqlDriverMessageMap but got %T", msg)
u.Errorf("unrecognized msg %T", msg)
close(m.TaskBase.sigCh)
return err
}
}
}
}
i := uint64(0)
for _, vals := range gb {
//u.Debugf("got %s:%v msgs", key, vals)
for _, dv := range vals {
for i, col := range columns {
//u.Debugf("col: idx:%v sidx: %v pidx:%v key:%v %s", col.Index, col.SourceIndex, col.ParentIndex, col.Key(), col.Expr)
if i-1 >= len(dv) {
u.Errorf("what??? %v dv: %d %#v", i, len(dv), dv)
}
if col.Expr == nil {
u.Warnf("wat? nil col expr? %#v", col)
} else {
v := dv[i]
switch vt := v.(type) {
case *AggPartial:
//u.Debugf("evaled: key=%v val=%v", col.Key(), v.Value())
aggs[i].Merge(vt)
case AggPartial:
aggs[i].Merge(&vt)
case int64:
aggs[i].Merge(&AggPartial{Ct: vt})
case string:
aggs[i] = &groupByFunc{vt}
default:
u.Warnf("unhandled type: %#v", v)
}
}
}
}
row := make([]driver.Value, len(columns))
for i, agg := range aggs {
row[i] = driver.Value(agg.Result())
agg.Reset()
//u.Debugf("agg result: %#v %v", row[i], row[i])
}
//u.Debugf("GroupBy output row? %v", row)
outCh <- datasource.NewSqlDriverMessageMap(i, row, colIndex)
i++
}
m.isComplete = true
close(m.complete)
return nil
}
// Close the task, channels, cleanup.
func (m *GroupBy) Close() error {
m.Lock()
if m.closed {
m.Unlock()
return nil
}
m.closed = true
m.Unlock()
return m.TaskBase.Close()
}
// Close the task and cleanup. Trys to wait for the downstream
// reducer tasks to complete after flushing messages.
func (m *GroupByFinal) Close() error {
m.Lock()
if m.closed {
m.Unlock()
return nil
}
m.closed = true
m.Unlock()
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
//u.Infof("%p group by final Close() waiting for complete", m)
select {
case <-ticker.C:
u.Warnf("timeout???? ")
case <-m.complete:
//u.Warnf("%p got groupbyfinal complete", m)
}
return m.TaskBase.Close()
}
// AggPartial is a struct to represent the partial aggregation
// that will be reduced on finalizer. IE, for consistent-hash based
// group-bys calculated across multiple nodes this holds info that
// needs to be further calculated it only represents this hash.
type AggPartial struct {
Ct int64
N float64
}
type AggFunc func(v value.Value)
type resultFunc func() interface{}
type Aggregator interface {
Do(v value.Value)
Result() interface{}
Reset()
Merge(*AggPartial)
}
type agg struct {
do AggFunc
result resultFunc
}
type groupByFunc struct {
last interface{}
}
func (m *groupByFunc) Do(v value.Value) { m.last = v.Value() }
func (m *groupByFunc) Result() interface{} { return m.last }
func (m *groupByFunc) Reset() { m.last = nil }
func (m *groupByFunc) Merge(a *AggPartial) {}
func NewGroupByValue(col *rel.Column) Aggregator {
return &groupByFunc{}
}
type sum struct {
partial bool
ct int64
n float64
}
func (m *sum) Do(v value.Value) {
m.ct++
switch vt := v.(type) {
case value.IntValue:
m.n += vt.Float()
case value.NumberValue:
m.n += vt.Val()
}
}
func (m *sum) Result() interface{} {
if !m.partial {
return m.n
}
return &AggPartial{
m.ct,
m.n,
}
}
func (m *sum) Reset() { m.n = 0 }
func (m *sum) Merge(a *AggPartial) {
m.ct += a.Ct
m.n += a.N
}
func NewSum(col *rel.Column, partial bool) Aggregator {
return &sum{partial: partial}
}
type avg struct {
partial bool
ct int64
n float64
}
func (m *avg) Do(v value.Value) {
m.ct++
switch vt := v.(type) {
case value.IntValue:
m.n += vt.Float()
case value.NumberValue:
m.n += vt.Val()
}
}
func (m *avg) Result() interface{} {
if !m.partial {
return m.n / float64(m.ct)
}
return &AggPartial{
m.ct,
m.n,
}
}
func (m *avg) Reset() { m.n = 0; m.ct = 0 }
func (m *avg) Merge(a *AggPartial) {
m.ct += a.Ct
m.n += a.N
}
func NewAvg(col *rel.Column, partial bool) Aggregator {
return &avg{partial: partial}
}
type count struct {
n int64
}
func (m *count) Do(v value.Value) {
if v == nil || v.Nil() {
return
}
m.n++
}
func (m *count) Result() interface{} {
return m.n
}
func (m *count) Reset() { m.n = 0 }
func (m *count) Merge(a *AggPartial) {
m.n += a.Ct
}
func NewCount(col *rel.Column) Aggregator {
return &count{}
}
func buildAggs(p *plan.GroupBy) ([]Aggregator, error) {
aggs := make([]Aggregator, len(p.Stmt.Columns))
colLoop:
for colIdx, col := range p.Stmt.Columns {
for _, gb := range p.Stmt.GroupBy {
if gb.As == col.As || (col.Expr != nil && col.Expr.Equal(gb.Expr)) {
// simple Non Aggregate Value gb.As == col.AS
// SELECT domain, count(*) FROM users GROUP BY domain;
// aliased column
// SELECT `users`.`name` AS usernames FROM `users` GROUP BY `users`.`name`
// gb.String() == "`users`.`name`" && col.Expr.String() == "`users`.`name`"
aggs[colIdx] = NewGroupByValue(col)
continue colLoop
}
}
// Since we made it here, it is an aggregate func
// move to a registry of some kind to allow extension
switch n := col.Expr.(type) {
case *expr.FuncNode:
// TODO: extract to a UDF Registry Similar to builtins
switch strings.ToLower(n.Name) {
case "avg":
aggs[colIdx] = NewAvg(col, p.Partial)
case "count":
aggs[colIdx] = NewCount(col)
case "sum":
aggs[colIdx] = NewSum(col, p.Partial)
default:
return nil, fmt.Errorf("Not implemented groupby for function: %s", col.Expr)
}
case *expr.BinaryNode:
// expression logic?
return nil, fmt.Errorf("Not implemented groupby for expression column: %s", col.Expr)
case *expr.IdentityNode:
// We can have a naked group by which basically means distinct? should have been caught above
return nil, fmt.Errorf("Not implemented groupby for identity column %s", col.Expr)
default:
return nil, fmt.Errorf("Not implemented groupby for %T column: %s", col.Expr, col.Expr)
}
}
return aggs, nil
}