forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memo.go
745 lines (672 loc) · 17.3 KB
/
memo.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
// Copyright 2022 Dolthub, 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 analyzer
import (
"fmt"
"strings"
"github.com/gabereiser/go-mysql-server/optgen/cmd/support"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/plan"
)
//go:generate go run ../../optgen/cmd/optgen/main.go -out memo.og.go -pkg analyzer memo memo.go
type GroupId uint16
type TableId uint16
// Memo collects a forest of query plans structured by logical and
// physical equivalency. Logically equivalent plans, represented by
// an exprGroup, produce the same rows (possibly unordered) and schema.
// Physical plans are stored in a linked list within an expression group.
type Memo struct {
cnt uint16
root *exprGroup
orderHint *joinOrderDeps
c Coster
s Carder
statsRw sql.StatsReadWriter
ctx *sql.Context
scope *Scope
scopeLen int
tableProps *tableProps
}
func NewMemo(ctx *sql.Context, stats sql.StatsReadWriter, s *Scope, cost Coster, card Carder) *Memo {
return &Memo{
ctx: ctx,
c: cost,
s: card,
statsRw: stats,
scope: s,
scopeLen: len(s.Schema()),
tableProps: newTableProps(),
}
}
// memoize creates a new logical expression group to encapsulate the
// action of a SQL clause.
// TODO: this is supposed to deduplicate logically equivalent table scans
// and scalar expressions, replacing references with a pointer. Currently
// a hacky format to quickly support memoizing join trees.
func (m *Memo) memoize(rel relExpr) *exprGroup {
m.cnt++
id := GroupId(m.cnt)
grp := newExprGroup(m, id, rel)
if s, ok := rel.(sourceRel); ok {
m.tableProps.addTable(s.name(), id)
}
return grp
}
// optimizeRoot finds the implementation for the root expression
// that has the lowest cost.
func (m *Memo) optimizeRoot() error {
return m.optimizeMemoGroup(m.root)
}
// optimizeMemoGroup recursively builds the lowest cost plan for memo
// group expressions. We optimize expressions groups independently, walking
// the linked list of execution plans for a particular group only after
// optimizing all subgroups. All plans within a group by definition share
// the same subgroup dependencies. After finding the best implementation
// for a particular group, we fix the best plan for that group and recurse
// into its parents.
// TODO: we should not have to cost every plan, sometimes there is a provably
// best case implementation
func (m *Memo) optimizeMemoGroup(grp *exprGroup) error {
if grp.done {
return nil
}
n := grp.first
for n != nil {
var cost float64
for _, g := range n.children() {
err := m.optimizeMemoGroup(g)
if err != nil {
return err
}
cost += g.cost
}
relCost, err := m.c.EstimateCost(m.ctx, n, m.statsRw)
if err != nil {
return err
}
n.setCost(relCost)
cost += relCost
m.updateBest(grp, n, cost)
n = n.next()
}
grp.done = true
var err error
grp.relProps.card, err = m.s.EstimateCard(m.ctx, grp.best, m.statsRw)
if err != nil {
return err
}
return nil
}
// updateBest is given a logical expression group, a physical implementation
// in the same group, and the estimated cost for the operator. We update best
// plans for an expression group cognizant of join ordering; we pick either
// the best hinted plan or the best overall plan if the hint corresponds to
// no valid plan.
func (m *Memo) updateBest(grp *exprGroup, n relExpr, cost float64) {
if m.orderHint != nil {
if m.orderHint.obeysOrder(n) {
if !grp.orderSatisfied {
grp.best = n
grp.cost = cost
grp.orderSatisfied = true
return
}
grp.updateBest(n, cost)
} else if grp.best == nil || !grp.orderSatisfied {
grp.updateBest(n, cost)
}
return
}
grp.updateBest(n, cost)
}
func (m *Memo) bestRootPlan() (sql.Node, error) {
b := NewExecBuilder()
return buildBestJoinPlan(b, m.root, nil)
}
// buildBestJoinPlan converts group's lowest cost implementation into a
// tree node with a recursive DFS.
func buildBestJoinPlan(b *ExecBuilder, grp *exprGroup, input sql.Schema) (sql.Node, error) {
if !grp.done {
panic("expected expression group plans to be fixed")
}
n := grp.best
var err error
children := make([]sql.Node, len(n.children()))
for i, g := range n.children() {
children[i], err = buildBestJoinPlan(b, g, input)
if err != nil {
return nil, err
}
input = append(input, g.relProps.OutputCols()...)
}
return b.buildRel(n, input, children...)
}
func (m *Memo) WithJoinOrder(hint JoinOrderHint) {
// order maps groupId ->
order := make(map[GroupId]uint64)
for i, t := range hint.tables {
id, ok := m.tableProps.getId(t)
if !ok {
return
}
order[id] = uint64(i)
}
m.orderHint = newJoinOrderDeps(order)
m.orderHint.build(m.root)
if !m.orderHint.isValid() {
// bad hint, fallback to default costing
m.orderHint = nil
}
}
func (m *Memo) String() string {
exprs := make([]string, m.cnt)
groups := make([]*exprGroup, 0)
if m.root != nil {
r := m.root.first
for r != nil {
groups = append(groups, r.group())
groups = append(groups, r.children()...)
r = r.next()
}
}
for len(groups) > 0 {
newGroups := make([]*exprGroup, 0)
for _, g := range groups {
if exprs[int(g.id)-1] != "" {
continue
}
exprs[int(g.id)-1] = g.String()
newGroups = append(newGroups, g.children()...)
}
groups = newGroups
}
b := strings.Builder{}
b.WriteString("memo:\n")
beg := "├──"
for i, g := range exprs {
if i == len(exprs)-1 {
beg = "└──"
}
b.WriteString(fmt.Sprintf("%s G%d: %s\n", beg, i+1, g))
}
return b.String()
}
// relProps are relational attributes shared by all plans in an expression
// group (see: exprGroup).
type relProps struct {
grp *exprGroup
outputCols sql.Schema
inputTables sql.FastIntSet
outputTables sql.FastIntSet
card float64
}
func newRelProps(rel relExpr) *relProps {
p := &relProps{
grp: rel.group(),
}
if r, ok := rel.(sourceRel); ok {
p.outputCols = r.outputCols()
}
p.populateOutputTables()
p.populateInputTables()
return p
}
// populateOutputTables initializes the bitmap indicating which tables'
// attributes are available outputs from the exprGroup
func (p *relProps) populateOutputTables() {
switch n := p.grp.first.(type) {
case sourceRel:
p.outputTables = sql.NewFastIntSet(int(n.tableId()))
case *antiJoin:
p.outputTables = n.left.relProps.OutputTables()
case *semiJoin:
p.outputTables = n.left.relProps.OutputTables()
case *distinct:
p.outputTables = n.child.relProps.OutputTables()
case *project:
p.outputTables = n.child.relProps.OutputTables()
case joinRel:
p.outputTables = n.joinPrivate().left.relProps.OutputTables().Union(n.joinPrivate().right.relProps.OutputTables())
default:
panic(fmt.Sprintf("unhandled type: %T", n))
}
}
// populateInputTables initializes the bitmap indicating which tables
// are input into this exprGroup. This is used to enforce join order
// hinting for semi joins.
func (p *relProps) populateInputTables() {
switch n := p.grp.first.(type) {
case sourceRel:
p.inputTables = sql.NewFastIntSet(int(n.tableId()))
case *distinct:
p.inputTables = n.child.relProps.InputTables()
case *project:
p.inputTables = n.child.relProps.InputTables()
case joinRel:
p.inputTables = n.joinPrivate().left.relProps.InputTables().Union(n.joinPrivate().right.relProps.InputTables())
default:
panic(fmt.Sprintf("unhandled type: %T", n))
}
}
func (p *relProps) populateOutputCols() {
p.outputCols = p.outputColsForRel(p.grp.best)
}
func (p *relProps) outputColsForRel(r relExpr) sql.Schema {
switch r := r.(type) {
case *semiJoin:
return r.left.relProps.OutputCols()
case *antiJoin:
return r.left.relProps.OutputCols()
case *lookupJoin:
if r.op.IsRightPartial() {
return r.right.relProps.OutputCols()
} else if r.op.IsPartial() {
return r.left.relProps.OutputCols()
} else {
return append(r.joinPrivate().left.relProps.OutputCols(), r.joinPrivate().right.relProps.OutputCols()...)
}
case joinRel:
return append(r.joinPrivate().left.relProps.OutputCols(), r.joinPrivate().right.relProps.OutputCols()...)
case *distinct:
return r.child.relProps.OutputCols()
case *project:
return r.outputCols()
case sourceRel:
return r.outputCols()
default:
panic("unknown type")
}
return nil
}
// OutputCols returns the output schema of a node
func (p *relProps) OutputCols() sql.Schema {
if p.outputCols == nil {
if p.grp.best == nil {
return p.outputColsForRel(p.grp.first)
}
p.populateOutputCols()
}
return p.outputCols
}
// OutputTables returns a bitmap of tables in the output schema of this node.
func (p *relProps) OutputTables() sql.FastIntSet {
return p.outputTables
}
// InputTables returns a bitmap of tables input into this node.
func (p *relProps) InputTables() sql.FastIntSet {
return p.inputTables
}
type tableProps struct {
grpToName map[GroupId]string
nameToGrp map[string]GroupId
}
func newTableProps() *tableProps {
return &tableProps{
grpToName: make(map[GroupId]string),
nameToGrp: make(map[string]GroupId),
}
}
func (p *tableProps) addTable(n string, id GroupId) {
p.grpToName[id] = n
p.nameToGrp[n] = id
}
func (p *tableProps) getTable(id GroupId) (string, bool) {
n, ok := p.grpToName[id]
return n, ok
}
func (p *tableProps) getId(n string) (GroupId, bool) {
id, ok := p.nameToGrp[n]
return id, ok
}
func (p *tableProps) getTableNames(f sql.FastIntSet) []string {
var names []string
for idx, ok := f.Next(0); ok; idx, ok = f.Next(idx + 1) {
if ok {
groupId := GroupId(idx + 1)
table, ok := p.getTable(groupId)
if !ok {
panic(fmt.Sprintf("table not found for group %d", groupId))
}
names = append(names, table)
}
}
return names
}
// exprGroup is a linked list of plans that return the same result set
// defined by row count and schema.
type exprGroup struct {
m *Memo
relProps *relProps
first relExpr
best relExpr
id GroupId
cost float64
done bool
orderSatisfied bool
opHint plan.JoinType
}
func newExprGroup(m *Memo, id GroupId, rel relExpr) *exprGroup {
// bit of circularity: |grp| references |ref|, |rel| references |grp|,
// and |relProps| references |rel| and |grp| info.
grp := &exprGroup{
m: m,
id: id,
first: rel,
}
rel.setGroup(grp)
grp.relProps = newRelProps(rel)
return grp
}
func (e *exprGroup) obeysOpHint(n relExpr) bool {
switch n := n.(type) {
case joinRel:
return n.joinPrivate().op == e.opHint
case *distinct:
return e.obeysOpHint(n.child.best)
case *project:
return e.obeysOpHint(n.child.best)
default:
return true
}
}
// prepend adds a new plan to an expression group at the beginning of
// the list, to avoid recursive exploration steps (like adding indexed joins).
func (e *exprGroup) prepend(rel relExpr) {
first := e.first
e.first = rel
rel.setNext(first)
}
func (e *exprGroup) children() []*exprGroup {
n := e.first
children := make([]*exprGroup, 0)
for n != nil {
children = append(children, n.children()...)
n = n.next()
}
return children
}
func (e *exprGroup) updateBest(n relExpr, grpCost float64) {
if e.best == nil || grpCost <= e.cost || (!e.obeysOpHint(e.best) && e.obeysOpHint(n)) {
e.best = n
e.cost = grpCost
}
}
func (e *exprGroup) String() string {
b := strings.Builder{}
n := e.first
sep := ""
for n != nil {
b.WriteString(sep)
b.WriteString(fmt.Sprintf("(%s", formatRelExpr(n)))
if e.best != nil {
b.WriteString(fmt.Sprintf(" %.1f", n.cost()))
childCost := 0.0
for _, c := range n.children() {
childCost += c.cost
}
if e.cost == n.cost()+childCost {
b.WriteString(")*")
} else {
b.WriteString(")")
}
} else {
b.WriteString(")")
}
sep = " "
n = n.next()
}
return b.String()
}
// Coster types can estimate the CPU and memory cost of physical execution
// operators.
type Coster interface {
// EstimateCost cost returns the incremental CPU and memory cost for an
// operator, or an error. Cost is dependent on physical operator type,
// and the cardinality of inputs.
EstimateCost(*sql.Context, relExpr, sql.StatsReader) (float64, error)
}
// Carder types can estimate the cardinality (row count) of relational
// expressions.
type Carder interface {
// EstimateCard returns the estimate row count outputs for a relational
// expression. Cardinality is an expression group property.
EstimateCard(*sql.Context, relExpr, sql.StatsReader) (float64, error)
}
// relExpr wraps a sql.Node for use as a exprGroup linked list node.
// TODO: we need relExprs for every sql.Node and sql.Expression
type relExpr interface {
fmt.Stringer
group() *exprGroup
next() relExpr
setNext(relExpr)
children() []*exprGroup
setGroup(g *exprGroup)
setCost(c float64)
cost() float64
}
type relBase struct {
// g is this relation's expression group
g *exprGroup
// n is the next relExpr in the exprGroup linked list
n relExpr
// c is this relation's cost while costing and plan reify are separate
c float64
// cnt is this relations output row count
cnt float64
}
// relKEy is a quick identifier for avoiding duplicate work on the same
// relExpr.
// TODO: the key should be a formalized hash of 1) the operator type, and 2)
// hashes of the relExpr and scalarExpr children.
func relKey(r relExpr) uint64 {
key := int(r.group().id)
i := 1<<16 - 1
for _, c := range r.children() {
key += i * int(c.id)
i *= 1<<16 - 1
}
return uint64(key)
}
func (r *relBase) group() *exprGroup {
return r.g
}
func (r *relBase) setGroup(g *exprGroup) {
r.g = g
}
func (r *relBase) next() relExpr {
return r.n
}
func (r *relBase) setNext(rel relExpr) {
r.n = rel
}
func (r *relBase) setCost(c float64) {
r.c = c
}
func (r *relBase) cost() float64 {
return r.c
}
func tableIdForSource(id GroupId) TableId {
return TableId(id - 1)
}
// sourceRel represents a data source, like a tableScan, subqueryAlias,
// or list of values.
type sourceRel interface {
relExpr
// outputCols retuns the output schema of this data source.
// TODO: this is more useful as a relExpr property, but we need
// this to fix up expression indexes currently
outputCols() sql.Schema
name() string
tableId() TableId
}
// joinRel represents a plan.JoinNode or plan.CrossJoin. See plan.JoinType
// for the full list.
type joinRel interface {
relExpr
joinPrivate() *joinBase
group() *exprGroup
}
var _ joinRel = (*antiJoin)(nil)
var _ joinRel = (*concatJoin)(nil)
var _ joinRel = (*crossJoin)(nil)
var _ joinRel = (*leftJoin)(nil)
var _ joinRel = (*fullOuterJoin)(nil)
var _ joinRel = (*hashJoin)(nil)
var _ joinRel = (*innerJoin)(nil)
var _ joinRel = (*lookupJoin)(nil)
var _ joinRel = (*semiJoin)(nil)
type joinBase struct {
*relBase
op plan.JoinType
filter []sql.Expression
left *exprGroup
right *exprGroup
}
func (r *joinBase) children() []*exprGroup {
return []*exprGroup{r.left, r.right}
}
func (r *joinBase) joinPrivate() *joinBase {
return r
}
// copy creates a joinBase with the same underlying join expression.
// note: it is important to copy the base node to avoid cyclical
// relExpr references in the exprGroup linked list.
func (r *joinBase) copy() *joinBase {
return &joinBase{
relBase: &relBase{
g: r.g,
n: r.n,
c: r.c,
},
op: r.op,
filter: r.filter,
left: r.left,
right: r.right,
}
}
type lookup struct {
source string
index sql.Index
keyExprs []sql.Expression
nullmask []bool
parent *joinBase
}
type indexScan struct {
source string
idx sql.Index
parent *joinBase
}
var ExprDefs support.GenDefs = []support.MemoDef{ // alphabetically sorted
{
Name: "crossJoin",
IsJoin: true,
},
{
Name: "innerJoin",
IsJoin: true,
},
{
Name: "leftJoin",
IsJoin: true,
},
{
Name: "semiJoin",
IsJoin: true,
},
{
Name: "antiJoin",
IsJoin: true,
},
{
Name: "lookupJoin",
IsJoin: true,
Attrs: [][2]string{
{"lookup", "*lookup"},
},
},
{
Name: "concatJoin",
IsJoin: true,
Attrs: [][2]string{
{"concat", "[]*lookup"},
},
},
{
Name: "hashJoin",
IsJoin: true,
Attrs: [][2]string{
{"innerAttrs", "[]sql.Expression"},
{"outerAttrs", "[]sql.Expression"},
},
},
{
Name: "mergeJoin",
IsJoin: true,
Attrs: [][2]string{
{"innerScan", "*indexScan"},
{"outerScan", "*indexScan"},
},
},
{
Name: "fullOuterJoin",
IsJoin: true,
},
{
Name: "tableScan",
SourceType: "*plan.ResolvedTable",
},
{
Name: "values",
SourceType: "*plan.ValueDerivedTable",
},
{
Name: "tableAlias",
SourceType: "*plan.TableAlias",
},
{
Name: "recursiveTable",
SourceType: "*plan.RecursiveTable",
},
{
Name: "recursiveCte",
SourceType: "*plan.RecursiveCte",
},
{
Name: "subqueryAlias",
SourceType: "*plan.SubqueryAlias",
},
{
Name: "max1Row",
SourceType: "sql.NameableNode",
},
{
Name: "tableFunc",
SourceType: "sql.TableFunction",
},
{
Name: "selectSingleRel",
SourceType: "*plan.SelectSingleRel",
},
{
Name: "project",
IsUnary: true,
Attrs: [][2]string{
{"projections", "[]sql.Expression"},
},
},
{
Name: "distinct",
IsUnary: true,
},
}