/
indexed_join.go
executable file
·273 lines (234 loc) · 6.76 KB
/
indexed_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
// Copyright 2020-2021 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 plan
import (
"io"
"reflect"
"github.com/opentracing/opentracing-go"
"github.com/Rock-liyi/p2pdb-store/sql"
)
// An IndexedJoin is a join that uses index lookups for the secondary table.
type IndexedJoin struct {
// The primary and secondary table nodes. The normal meanings of Left and
// Right in BinaryNode aren't necessarily meaningful here -- the Left node is always the primary table, and the Right
// node is always the secondary. These may or may not correspond to the left and right tables in the written query.
BinaryNode
// The join condition.
Cond sql.Expression
// The type of join. Left and right refer to the lexical position in the written query, not primary / secondary. In
// the case of a right join, the right table will always be the primary.
joinType JoinType
scopeLen int
}
// JoinType returns the join type for this indexed join
func (ij *IndexedJoin) JoinType() JoinType {
return ij.joinType
}
func NewIndexedJoin(left, right sql.Node, joinType JoinType, cond sql.Expression, scopeLen int) *IndexedJoin {
return &IndexedJoin{
BinaryNode: BinaryNode{left, right},
joinType: joinType,
Cond: cond,
scopeLen: scopeLen,
}
}
func (ij *IndexedJoin) String() string {
pr := sql.NewTreePrinter()
joinType := ""
switch ij.joinType {
case JoinTypeLeft:
joinType = "Left"
case JoinTypeRight:
joinType = "Right"
}
_ = pr.WriteNode("%sIndexedJoin%s", joinType, ij.Cond)
_ = pr.WriteChildren(ij.left.String(), ij.right.String())
return pr.String()
}
func (ij *IndexedJoin) DebugString() string {
pr := sql.NewTreePrinter()
joinType := ""
switch ij.joinType {
case JoinTypeLeft:
joinType = "Left"
case JoinTypeRight:
joinType = "Right"
}
_ = pr.WriteNode("%sIndexedJoin%s", joinType, sql.DebugString(ij.Cond))
_ = pr.WriteChildren(sql.DebugString(ij.left), sql.DebugString(ij.right))
return pr.String()
}
func (ij *IndexedJoin) Schema() sql.Schema {
return append(ij.left.Schema(), ij.right.Schema()...)
}
func (ij *IndexedJoin) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
return indexedJoinRowIter(ctx, row, ij.left, ij.right, ij.Cond, ij.joinType, ij.scopeLen)
}
func (ij *IndexedJoin) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(ij, len(children), 2)
}
return NewIndexedJoin(children[0], children[1], ij.joinType, ij.Cond, ij.scopeLen), nil
}
func indexedJoinRowIter(
ctx *sql.Context,
parentRow sql.Row,
left sql.Node,
right sql.Node,
cond sql.Expression,
joinType JoinType,
scopeLen int,
) (sql.RowIter, error) {
var leftName, rightName string
if leftTable, ok := left.(sql.Nameable); ok {
leftName = leftTable.Name()
} else {
leftName = reflect.TypeOf(left).String()
}
if rightTable, ok := right.(sql.Nameable); ok {
rightName = rightTable.Name()
} else {
rightName = reflect.TypeOf(right).String()
}
span, ctx := ctx.Span("plan.indexedJoin", opentracing.Tags{
"left": leftName,
"right": rightName,
})
l, err := left.RowIter(ctx, parentRow)
if err != nil {
span.Finish()
return nil, err
}
return sql.NewSpanIter(span, &indexedJoinIter{
parentRow: parentRow,
primary: l,
secondaryProvider: right,
cond: cond,
joinType: joinType,
rowSize: len(parentRow) + len(left.Schema()) + len(right.Schema()),
scopeLen: scopeLen,
}), nil
}
// indexedJoinIter is an iterator that iterates over every row in the primary table and performs an index lookup in
// the secondary table for each value
type indexedJoinIter struct {
parentRow sql.Row
primary sql.RowIter
primaryRow sql.Row
secondaryProvider sql.Node
secondary sql.RowIter
cond sql.Expression
joinType JoinType
foundMatch bool
rowSize int
scopeLen int
}
func (i *indexedJoinIter) loadPrimary(ctx *sql.Context) error {
if i.primaryRow == nil {
r, err := i.primary.Next(ctx)
if err != nil {
return err
}
i.primaryRow = i.parentRow.Append(r)
i.foundMatch = false
}
return nil
}
func (i *indexedJoinIter) loadSecondary(ctx *sql.Context) (sql.Row, error) {
if i.secondary == nil {
rowIter, err := i.secondaryProvider.RowIter(ctx, i.primaryRow)
if err != nil {
return nil, err
}
i.secondary = rowIter
}
secondaryRow, err := i.secondary.Next(ctx)
if err != nil {
if err == io.EOF {
err = i.secondary.Close(ctx)
i.secondary = nil
if err != nil {
return nil, err
}
i.primaryRow = nil
return nil, io.EOF
}
return nil, err
}
return secondaryRow, nil
}
func (i *indexedJoinIter) Next(ctx *sql.Context) (sql.Row, error) {
for {
if err := i.loadPrimary(ctx); err != nil {
return nil, err
}
primary := i.primaryRow
secondary, err := i.loadSecondary(ctx)
if err != nil {
if err == io.EOF {
if !i.foundMatch && (i.joinType == JoinTypeLeft || i.joinType == JoinTypeRight) {
row := i.buildRow(primary, nil)
return i.removeParentRow(row), nil
}
continue
}
return nil, err
}
row := i.buildRow(primary, secondary)
matches, err := conditionIsTrue(ctx, row, i.cond)
if err != nil {
return nil, err
}
if !matches {
continue
}
i.foundMatch = true
return i.removeParentRow(row), nil
}
}
func (i *indexedJoinIter) removeParentRow(r sql.Row) sql.Row {
copy(r[i.scopeLen:], r[len(i.parentRow):])
r = r[:len(r)-len(i.parentRow)+i.scopeLen]
return r
}
func conditionIsTrue(ctx *sql.Context, row sql.Row, cond sql.Expression) (bool, error) {
v, err := cond.Eval(ctx, row)
if err != nil {
return false, err
}
// Expressions containing nil evaluate to nil, not false
return v == true, nil
}
// buildRow builds the result set row using the rows from the primary and secondary tables
func (i *indexedJoinIter) buildRow(primary, secondary sql.Row) sql.Row {
row := make(sql.Row, i.rowSize)
copy(row, primary)
copy(row[len(primary):], secondary)
return row
}
func (i *indexedJoinIter) Close(ctx *sql.Context) (err error) {
if i.primary != nil {
if err = i.primary.Close(ctx); err != nil {
if i.secondary != nil {
_ = i.secondary.Close(ctx)
}
return err
}
}
if i.secondary != nil {
err = i.secondary.Close(ctx)
i.secondary = nil
}
return err
}