-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.go
605 lines (529 loc) · 15.6 KB
/
join.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
// Copyright 2016 The Cockroach Authors.
//
// 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.
//
// Author: Raphael 'kena' Poss (knz@cockroachlabs.com)
// Author: Irfan Sharif (irfansharif@cockroachlabs.com)
package sql
import (
"fmt"
"unsafe"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
)
type joinType int
const (
joinTypeInner joinType = iota
joinTypeLeftOuter
joinTypeRightOuter
joinTypeFullOuter
)
// bucket here is the set of rows for a given group key (comprised of
// columns specified by the join constraints), 'seen' is used to determine if
// there was a matching row in the opposite stream.
type bucket struct {
rows []parser.Datums
seen []bool
}
func (b *bucket) Seen(i int) bool {
return b.seen[i]
}
func (b *bucket) Rows() []parser.Datums {
return b.rows
}
func (b *bucket) MarkSeen(i int) {
b.seen[i] = true
}
func (b *bucket) AddRow(row parser.Datums) {
b.rows = append(b.rows, row)
}
type buckets struct {
buckets map[string]*bucket
rowContainer *sqlbase.RowContainer
}
func (b *buckets) Buckets() map[string]*bucket {
return b.buckets
}
func (b *buckets) AddRow(
ctx context.Context, acc WrappedMemoryAccount, encoding []byte, row parser.Datums,
) error {
bk, ok := b.buckets[string(encoding)]
if !ok {
bk = &bucket{}
}
rowCopy, err := b.rowContainer.AddRow(ctx, row)
if err != nil {
return err
}
if err := acc.Grow(ctx, sqlbase.SizeOfDatums); err != nil {
return err
}
bk.AddRow(rowCopy)
if !ok {
b.buckets[string(encoding)] = bk
}
return nil
}
const sizeOfBoolSlice = unsafe.Sizeof([]bool{})
const sizeOfBool = unsafe.Sizeof(true)
// InitSeen initializes the seen array for each of the buckets. It must be run
// before the buckets' seen state is used.
func (b *buckets) InitSeen(ctx context.Context, acc WrappedMemoryAccount) error {
for _, bucket := range b.buckets {
if err := acc.Grow(
ctx, int64(sizeOfBoolSlice+uintptr(len(bucket.rows))*sizeOfBool),
); err != nil {
return err
}
bucket.seen = make([]bool, len(bucket.rows))
}
return nil
}
func (b *buckets) Close(ctx context.Context) {
b.rowContainer.Close(ctx)
b.rowContainer = nil
b.buckets = nil
}
func (b *buckets) Fetch(encoding []byte) (*bucket, bool) {
bk, ok := b.buckets[string(encoding)]
return bk, ok
}
// joinNode is a planNode whose rows are the result of an inner or
// left/right outer join.
type joinNode struct {
planner *planner
joinType joinType
// The data sources.
left planDataSource
right planDataSource
// pred represents the join predicate.
pred *joinPredicate
// columns contains the metadata for the results of this node.
columns sqlbase.ResultColumns
// output contains the last generated row of results from this node.
output parser.Datums
// buffer is our intermediate row store where we effectively 'stash' a batch
// of results at once, this is then used for subsequent calls to Next() and
// Values().
buffer *RowBuffer
buckets buckets
bucketsMemAcc WrappableMemoryAccount
// emptyRight contain tuples of NULL values to use on the right for left and
// full outer joins when the on condition fails.
emptyRight parser.Datums
// emptyLeft contains tuples of NULL values to use on the left for right and
// full outer joins when the on condition fails.
emptyLeft parser.Datums
// explain indicates whether this node is running on behalf of
// EXPLAIN(DEBUG).
explain explainMode
// doneReadingRight is used by debugNext() and DebugValues() when
// explain == explainDebug.
doneReadingRight bool
// finishedOutput indicates that we've finished writing all of the rows for
// this join and that we can quit as soon as our buffer is empty.
finishedOutput bool
}
// commonColumns returns the names of columns common on the
// right and left sides, for use by NATURAL JOIN.
func commonColumns(left, right *dataSourceInfo) parser.NameList {
var res parser.NameList
for _, cLeft := range left.sourceColumns {
if cLeft.Hidden {
continue
}
for _, cRight := range right.sourceColumns {
if cRight.Hidden {
continue
}
if parser.ReNormalizeName(cLeft.Name) == parser.ReNormalizeName(cRight.Name) {
res = append(res, parser.Name(cLeft.Name))
}
}
}
return res
}
// makeJoin constructs a planDataSource for a JOIN node.
// The tableInfo field from the left node is taken over (overwritten)
// by the new node.
func (p *planner) makeJoin(
ctx context.Context,
astJoinType string,
left planDataSource,
right planDataSource,
cond parser.JoinCond,
) (planDataSource, error) {
var typ joinType
switch astJoinType {
case "JOIN", "INNER JOIN", "CROSS JOIN":
typ = joinTypeInner
case "LEFT JOIN":
typ = joinTypeLeftOuter
case "RIGHT JOIN":
typ = joinTypeRightOuter
case "FULL JOIN":
typ = joinTypeFullOuter
default:
return planDataSource{}, errors.Errorf("unsupported JOIN type %T", astJoinType)
}
leftInfo, rightInfo := left.info, right.info
// Check that the same table name is not used on both sides.
for _, alias := range rightInfo.sourceAliases {
if _, ok := leftInfo.sourceAliases.srcIdx(alias.name); ok {
t := alias.name.Table()
if t == "" {
// Allow joins of sources that define columns with no
// associated table name. At worst, the USING/NATURAL
// detection code or expression analysis for ON will detect an
// ambiguity later.
continue
}
return planDataSource{}, fmt.Errorf(
"cannot join columns from the same source name %q (missing AS clause)", t)
}
}
var (
info *dataSourceInfo
pred *joinPredicate
err error
)
if cond == nil {
pred, info, err = makeCrossPredicate(leftInfo, rightInfo)
} else {
switch t := cond.(type) {
case *parser.OnJoinCond:
pred, info, err = p.makeOnPredicate(ctx, leftInfo, rightInfo, t.Expr)
case parser.NaturalJoinCond:
cols := commonColumns(leftInfo, rightInfo)
pred, info, err = makeUsingPredicate(leftInfo, rightInfo, cols)
case *parser.UsingJoinCond:
pred, info, err = makeUsingPredicate(leftInfo, rightInfo, t.Cols)
}
}
if err != nil {
return planDataSource{}, err
}
n := &joinNode{
planner: p,
left: left,
right: right,
joinType: typ,
pred: pred,
columns: info.sourceColumns,
}
n.buffer = &RowBuffer{
RowContainer: sqlbase.NewRowContainer(
p.session.TxnState.makeBoundAccount(), sqlbase.ColTypeInfoFromResCols(n.Columns()), 0,
),
}
n.bucketsMemAcc = p.session.TxnState.OpenAccount()
n.buckets = buckets{
buckets: make(map[string]*bucket),
rowContainer: sqlbase.NewRowContainer(
p.session.TxnState.makeBoundAccount(),
sqlbase.ColTypeInfoFromResCols(n.right.plan.Columns()),
0,
),
}
return planDataSource{
info: info,
plan: n,
}, nil
}
// Columns implements the planNode interface.
func (n *joinNode) Columns() sqlbase.ResultColumns { return n.columns }
// Ordering implements the planNode interface.
func (n *joinNode) Ordering() orderingInfo { return orderingInfo{} }
// MarkDebug implements the planNode interface.
func (n *joinNode) MarkDebug(mode explainMode) {
if mode != explainDebug {
panic(fmt.Sprintf("unknown debug mode %d", mode))
}
n.explain = mode
n.left.plan.MarkDebug(mode)
n.right.plan.MarkDebug(mode)
}
func (n *joinNode) Spans(ctx context.Context) (reads, writes roachpb.Spans, err error) {
leftReads, leftWrites, err := n.left.plan.Spans(ctx)
if err != nil {
return nil, nil, err
}
rightReads, rightWrites, err := n.right.plan.Spans(ctx)
if err != nil {
return nil, nil, err
}
return append(leftReads, rightReads...), append(leftWrites, rightWrites...), nil
}
// Start implements the planNode interface.
func (n *joinNode) Start(ctx context.Context) error {
if err := n.left.plan.Start(ctx); err != nil {
return err
}
if err := n.right.plan.Start(ctx); err != nil {
return err
}
if n.explain != explainDebug {
if err := n.hashJoinStart(ctx); err != nil {
return err
}
}
// Pre-allocate the space for output rows.
n.output = make(parser.Datums, len(n.columns))
// If needed, pre-allocate left and right rows of NULL tuples for when the
// join predicate fails to match.
if n.joinType == joinTypeLeftOuter || n.joinType == joinTypeFullOuter {
n.emptyRight = make(parser.Datums, len(n.right.plan.Columns()))
for i := range n.emptyRight {
n.emptyRight[i] = parser.DNull
}
}
if n.joinType == joinTypeRightOuter || n.joinType == joinTypeFullOuter {
n.emptyLeft = make(parser.Datums, len(n.left.plan.Columns()))
for i := range n.emptyLeft {
n.emptyLeft[i] = parser.DNull
}
}
return nil
}
func (n *joinNode) hashJoinStart(ctx context.Context) error {
var scratch []byte
// Load all the rows from the right side and build our hashmap.
acc := n.bucketsMemAcc.Wtxn(n.planner.session)
for {
hasRow, err := n.right.plan.Next(ctx)
if err != nil {
return err
}
if !hasRow {
break
}
row := n.right.plan.Values()
encoding, _, err := n.pred.encode(scratch, row, n.pred.rightEqualityIndices)
if err != nil {
return err
}
if err := n.buckets.AddRow(ctx, acc, encoding, row); err != nil {
return err
}
scratch = encoding[:0]
}
if n.joinType == joinTypeFullOuter || n.joinType == joinTypeRightOuter {
return n.buckets.InitSeen(ctx, acc)
}
return nil
}
func (n *joinNode) debugNext(ctx context.Context) (bool, error) {
if !n.doneReadingRight {
hasRightRow, err := n.right.plan.Next(ctx)
if err != nil {
return false, err
}
if hasRightRow {
return true, nil
}
n.doneReadingRight = true
}
return n.left.plan.Next(ctx)
}
// Next implements the planNode interface.
func (n *joinNode) Next(ctx context.Context) (res bool, err error) {
if n.explain == explainDebug {
return n.debugNext(ctx)
}
// If results available from from previously computed results, we just
// return true.
if n.buffer.Next() {
return true, nil
}
// If the buffer is empty and we've finished outputting, we're done.
if n.finishedOutput {
return false, nil
}
wantUnmatchedLeft := n.joinType == joinTypeLeftOuter || n.joinType == joinTypeFullOuter
wantUnmatchedRight := n.joinType == joinTypeRightOuter || n.joinType == joinTypeFullOuter
if len(n.buckets.Buckets()) == 0 {
if !wantUnmatchedLeft {
// No rows on right; don't even try.
return false, nil
}
}
// Compute next batch of matching rows.
var scratch []byte
for {
leftHasRow, err := n.left.plan.Next(ctx)
if err != nil {
return false, nil
}
if !leftHasRow {
break
}
lrow := n.left.plan.Values()
encoding, containsNull, err := n.pred.encode(scratch, lrow, n.pred.leftEqualityIndices)
if err != nil {
return false, err
}
// We make the explicit check for whether or not lrow contained a NULL
// tuple. The reasoning here is because of the way we expect NULL
// equality checks to behave (i.e. NULL != NULL) and the fact that we
// use the encoding of any given row as key into our bucket. Thus if we
// encountered a NULL row when building the hashmap we have to store in
// order to use it for RIGHT OUTER joins but if we encounter another
// NULL row when going through the left stream (probing phase), matching
// this with the first NULL row would be incorrect.
//
// If we have have the following:
// CREATE TABLE t(x INT); INSERT INTO t(x) VALUES (NULL);
// | x |
// ------
// | NULL |
//
// For the following query:
// SELECT * FROM t AS a FULL OUTER JOIN t AS b USING(x);
//
// We expect:
// | x |
// ------
// | NULL |
// | NULL |
//
// The following examples illustrates the behaviour when joining on two
// or more columns, and only one of them contains NULL.
// If we have have the following:
// CREATE TABLE t(x INT, y INT);
// INSERT INTO t(x, y) VALUES (44,51), (NULL,52);
// | x | y |
// ------
// | 44 | 51 |
// | NULL | 52 |
//
// For the following query:
// SELECT * FROM t AS a FULL OUTER JOIN t AS b USING(x, y);
//
// We expect:
// | x | y |
// ------
// | 44 | 51 |
// | NULL | 52 |
// | NULL | 52 |
if containsNull {
if !wantUnmatchedLeft {
scratch = encoding[:0]
// Failed to match -- no matching row, nothing to do.
continue
}
// We append an empty right row to the left row, adding the result
// to our buffer for the subsequent call to Next().
n.pred.prepareRow(n.output, lrow, n.emptyRight)
if _, err := n.buffer.AddRow(ctx, n.output); err != nil {
return false, err
}
return n.buffer.Next(), nil
}
b, ok := n.buckets.Fetch(encoding)
if !ok {
if !wantUnmatchedLeft {
scratch = encoding[:0]
continue
}
// Left or full outer join: unmatched rows are padded with NULLs.
// Given that we did not find a matching right row we append an
// empty right row to the left row, adding the result to our buffer
// for the subsequent call to Next().
n.pred.prepareRow(n.output, lrow, n.emptyRight)
if _, err := n.buffer.AddRow(ctx, n.output); err != nil {
return false, err
}
return n.buffer.Next(), nil
}
// We iterate through all the rows in the bucket attempting to match the
// on condition, if the on condition passes we add it to the buffer.
foundMatch := false
for idx, rrow := range b.Rows() {
passesOnCond, err := n.pred.eval(&n.planner.evalCtx, n.output, lrow, rrow)
if err != nil {
return false, err
}
if !passesOnCond {
continue
}
foundMatch = true
n.pred.prepareRow(n.output, lrow, rrow)
if wantUnmatchedRight {
// Mark the row as seen if we need to retrieve the rows
// without matches for right or full joins later.
b.MarkSeen(idx)
}
if _, err := n.buffer.AddRow(ctx, n.output); err != nil {
return false, err
}
}
if !foundMatch && wantUnmatchedLeft {
// If none of the rows matched the on condition and we are computing a
// left or full outer join, we need to add a row with an empty
// right side.
n.pred.prepareRow(n.output, lrow, n.emptyRight)
if _, err := n.buffer.AddRow(ctx, n.output); err != nil {
return false, err
}
}
if n.buffer.Next() {
return true, nil
}
scratch = encoding[:0]
}
// no more lrows, we go through the unmatched rows in the internal hashmap.
if !wantUnmatchedRight {
return false, nil
}
for _, b := range n.buckets.Buckets() {
for idx, rrow := range b.Rows() {
if !b.Seen(idx) {
n.pred.prepareRow(n.output, n.emptyLeft, rrow)
if _, err := n.buffer.AddRow(ctx, n.output); err != nil {
return false, err
}
}
}
}
n.finishedOutput = true
return n.buffer.Next(), nil
}
// Values implements the planNode interface.
func (n *joinNode) Values() parser.Datums {
return n.buffer.Values()
}
// DebugValues implements the planNode interface.
func (n *joinNode) DebugValues() debugValues {
var res debugValues
if !n.doneReadingRight {
res = n.right.plan.DebugValues()
} else {
res = n.left.plan.DebugValues()
}
if res.output == debugValueRow {
res.output = debugValueBuffered
}
return res
}
// Close implements the planNode interface.
func (n *joinNode) Close(ctx context.Context) {
n.buffer.Close(ctx)
n.buffer = nil
n.buckets.Close(ctx)
n.bucketsMemAcc.Wtxn(n.planner.session).Close(ctx)
n.right.plan.Close(ctx)
n.left.plan.Close(ctx)
}