-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_join_hash.go
366 lines (280 loc) · 10.5 KB
/
op_join_hash.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
// Copyright (c) 2019 Couchbase, 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 n1k1
import (
"encoding/binary" // <== genCompiler:hide
"strings"
"github.com/couchbase/rhmap/store" // <== genCompiler:hide
"github.com/couchbase/n1k1/base"
)
// OpJoinHash implements...
// o.Kind: info tracked in probe map values: yieldsUnjoined:
// joinHash-inner [ leftVals ] f
// joinHash-leftOuter [ joinCount leftVals ] t
// intersect-all [ joinCount leftCount ] f
// intersect-distinct [ joinCount ] f
// except-all [ joinCount leftCount ] t
// except-distinct [ joinCount ] t
func OpJoinHash(o *base.Op, lzVars *base.Vars, lzYieldVals base.YieldVals,
lzYieldErr base.YieldErr, path, pathNext string) {
kindParts := strings.Split(o.Kind, "-")
opIntersect := kindParts[0] == "intersect"
var exprLeft, exprRight []interface{}
// Analyze the Op's config according to the above table of flags.
var canonical, joinCount, leftCount, leftVals, yieldsUnjoined bool
if kindParts[0] == "joinHash" {
exprLeft = o.Params[0].([]interface{})
exprRight = o.Params[1].([]interface{})
canonical = false
if kindParts[1] == "leftOuter" {
joinCount, yieldsUnjoined = true, true
}
leftVals = true
} else {
// INTERSECT & EXCEPT canonicalize their incoming vals so
// they're usable as map lookup keys.
exprLeft = []interface{}{"valsEncodeCanonical"}
exprRight = []interface{}{"valsEncodeCanonical"}
canonical = true
joinCount = true
if kindParts[1] == "all" {
leftCount = true
}
yieldsUnjoined = kindParts[0] == "except"
}
// ---------------------------------------------------------------
if LzScope {
var lzZero16 [16]byte
// As the left side is visited to fill or build the probe map,
// any left vals, if they are needed for a join, are stored as
// a chain of entries in the lzChunks.
//
// TODO: Configurable initial size for chunks, and reusable chunks.
// TODO: Reuse backing bytes for chunks.
lzChunks, lzErr := lzVars.Ctx.AllocChunks()
if lzErr != nil {
lzYieldErr(lzErr)
}
var lzMap *store.RHStore // The probe map.
if lzErr == nil {
// Every chain of left vals ends at offset 0, size 0.
lzChunks.BytesAppend(lzZero16[:])
// TODO: Configurable initial size for RHStore, and reusable RHStore.
// TODO: Reuse backing bytes for lzMap.
lzMap, lzErr = lzVars.Ctx.AllocMap()
if lzErr != nil {
lzYieldErr(lzErr)
}
}
var lzVal, lzValOut base.Val
var lzValsOut base.Vals
var lzProbeValNew, lzLeftBytes []byte
var lzJoinCount uint64
_, _, _ = lzValOut, lzLeftBytes, lzJoinCount
exprLeftFunc :=
MakeExprFunc(lzVars, o.Children[0].Labels, exprLeft, pathNext, "JHL") // !lz
exprRightFunc :=
MakeExprFunc(lzVars, o.Children[1].Labels, exprRight, pathNext, "JHR") // !lz
EmitPush(pathNext, "JHF") // !lz
lzYieldValsOrig := lzYieldVals
// Callback for left side, which fills the probe map.
lzYieldVals = func(lzVals base.Vals) {
var lzErr error
// Prepare the probe key.
lzVal = exprLeftFunc(lzVals, lzYieldErr) // <== emitCaptured: pathNext "JHL"
lzProbeKey := lzVal
if !canonical { // !lz
lzProbeKey, lzErr = lzVars.Ctx.ValComparer.CanonicalJSON(lzProbeKey, lzValOut[:0])
lzValOut = lzProbeKey[:0]
} // !lz
// The probe key must be valued.
if lzErr == nil && base.ValHasValue(lzProbeKey) {
// Check if we have an entry for the probe key.
lzProbeVal, lzProbeKeyFound := lzMap.Get([]byte(lzProbeKey))
if !lzProbeKeyFound {
// Initialze a brand new probe val to insert into
// the probe map.
lzProbeValNew = lzProbeValNew[:0]
if joinCount { // !lz
// Alloc space for joinCount for later RHS probing.
lzProbeValNew = append(lzProbeValNew, lzZero16[:8]...)
} // !lz
if leftCount { // !lz
lzProbeValNew = base.BinaryAppendUint64(lzProbeValNew, 1)
} // !lz
if leftVals { // !lz
// End chain has offset/size of 0/0.
lzLeftBytes = append(lzLeftBytes[:0], lzZero16[:16]...)
lzLeftBytes = base.ValsEncode(lzVals, lzLeftBytes)
lzOffset, lzSize, lzErr := lzChunks.BytesAppend(lzLeftBytes)
if lzErr != nil {
lzYieldErr(lzErr)
}
lzProbeValNew = base.BinaryAppendUint64(lzProbeValNew, lzOffset)
lzProbeValNew = base.BinaryAppendUint64(lzProbeValNew, lzSize)
} // !lz
lzMap.Set(store.Key(lzProbeKey), lzProbeValNew)
} else {
// Not the first time that we're seeing this probe
// key, so increment its leftCount, append to its
// leftVals chain, etc.
lzProbeValNew = lzProbeValNew[:0]
lzProbeValOld := lzProbeVal
if joinCount { // !lz
// Alloc space for joinCount for later RHS probing.
lzProbeValNew = append(lzProbeValNew, lzZero16[:8]...)
lzProbeValOld = lzProbeValOld[8:]
} // !lz
if leftCount { // !lz
lzLeftCount := binary.LittleEndian.Uint64(lzProbeValOld[:8]) + 1
lzProbeValNew = base.BinaryAppendUint64(lzProbeValNew, lzLeftCount)
lzProbeValOld = lzProbeValOld[8:]
} // !lz
if leftVals { // !lz
// Copy previous offset/size to extend the chain.
lzLeftBytes = append(lzLeftBytes[:0], lzProbeValOld[:16]...)
lzLeftBytes = base.ValsEncode(lzVals, lzLeftBytes)
lzOffset, lzSize, lzErr := lzChunks.BytesAppend(lzLeftBytes)
if lzErr != nil {
lzYieldErr(lzErr)
}
lzProbeValNew = base.BinaryAppendUint64(lzProbeValNew, lzOffset)
lzProbeValNew = base.BinaryAppendUint64(lzProbeValNew, lzSize)
} // !lz
// The updated probe val has the same size as the
// existing probe val, so optimize by in-place
// overwriting the existing probe val.
copy(lzProbeVal, lzProbeValNew)
}
}
}
lzYieldErrOrig := lzYieldErr
lzYieldErr = func(lzErrIn error) {
if lzErrIn != nil {
lzErr = lzErrIn
lzYieldErrOrig(lzErrIn)
}
}
EmitPop(pathNext, "JHF") // !lz
if lzErr == nil {
// Run the left side to fill the probe map.
ExecOp(o.Children[0], lzVars, lzYieldVals, lzYieldErr, pathNext, "JHL") // !lz
}
// -----------------------------------------------------------
if lzErr == nil {
// No error, so at this point the probe map has been
// filled with left-side probe entries, and next we will
// visit the right-side.
EmitPush(pathNext, "JHP") // !lz
// Callback for right side, which probes the probe map.
lzYieldVals = func(lzVals base.Vals) {
var lzErr error
// Prepare the probe key.
lzVal = exprRightFunc(lzVals, lzYieldErr) // <== emitCaptured: pathNext "JHR"
lzProbeKey := lzVal
if !canonical { // !lz
lzProbeKey, lzErr = lzVars.Ctx.ValComparer.CanonicalJSON(lzProbeKey, lzValOut[:0])
lzValOut = lzProbeKey[:0]
} // !lz
// The probe key must be valued.
if lzErr == nil && base.ValHasValue(lzProbeKey) {
lzProbeVal, lzProbeKeyFound := lzMap.Get([]byte(lzProbeKey))
if lzProbeKeyFound {
if joinCount { // !lz
// Increment join count on this probe key
// as both LHS & RHS have the probe key.
lzJoinCount = binary.LittleEndian.Uint64(lzProbeVal[:8]) + 1
binary.LittleEndian.PutUint64(lzProbeVal[:8], lzJoinCount)
if lzJoinCount == 1 {
if opIntersect && !leftCount { // !lz
// Ex: intersect-distinct.
lzValsOut = base.ValsDecode(lzProbeKey, lzValsOut[:0])
lzYieldValsOrig(lzValsOut)
} // !lz
}
lzProbeVal = lzProbeVal[8:]
} // !lz
if leftCount { // !lz
if opIntersect { // !lz
// Ex: intersect-all.
lzLeftCount := binary.LittleEndian.Uint64(lzProbeVal[:8])
if lzLeftCount >= lzJoinCount {
lzValsOut = base.ValsDecode(lzProbeKey, lzValsOut[:0])
lzYieldValsOrig(lzValsOut)
}
} // !lz
lzProbeVal = lzProbeVal[8:]
} // !lz
if leftVals { // !lz
// Ex: joinHash-inner, joinHash-leftOuter.
lzValsOut, lzErr = base.YieldChainedVals(lzYieldValsOrig,
lzVals, lzChunks, lzProbeVal, lzValsOut)
if lzErr != nil {
lzYieldErr(lzErr)
}
} // !lz
}
}
}
lzYieldErr = func(lzErrIn error) {
if lzErrIn == nil {
// No error, so yield items if needed for
// joinHash-leftOuter and for except.
if joinCount && yieldsUnjoined { // !lz
rightLabelsLen := len(o.Children[1].Labels) // !lz
_ = rightLabelsLen // !lz
lzRightSuffix := make(base.Vals, rightLabelsLen)
_ = lzRightSuffix
// Callback for entries in the probe map.
lzMapVisitor := func(lzProbeKey store.Key, lzProbeVal store.Val) bool {
lzJoinCount := binary.LittleEndian.Uint64(lzProbeVal[:8])
if lzJoinCount == 0 { // Entry was not visited by RHS.
lzProbeVal = lzProbeVal[8:]
if leftCount { // !lz
// Ex: except-all.
lzLeftCount := binary.LittleEndian.Uint64(lzProbeVal[:8])
lzValsOut = base.ValsDecode(lzProbeKey, lzValsOut[:0])
for lzI := uint64(0); lzI < lzLeftCount; lzI++ {
lzYieldValsOrig(lzValsOut)
}
lzProbeVal = lzProbeVal[8:]
} // !lz
if leftVals { // !lz
// Ex: joinHash-leftOuter.
lzValsOut, lzErr = base.YieldChainedVals(lzYieldValsOrig,
lzRightSuffix, lzChunks, lzProbeVal, lzValsOut)
if lzErr != nil {
lzYieldErrOrig(lzErr)
}
} // !lz
if !leftCount && !leftVals { // !lz
// Ex: except-distinct.
lzValsOut = base.ValsDecode(lzProbeKey, lzValsOut[:0])
lzYieldValsOrig(lzValsOut)
} // !lz
}
return true
}
lzMap.Visit(lzMapVisitor)
} // !lz
}
lzYieldErrOrig(lzErrIn)
}
EmitPop(pathNext, "JHP") // !lz
if lzErr == nil {
// Run the right side to probe the probe map.
ExecOp(o.Children[1], lzVars, lzYieldVals, lzYieldErr, pathNext, "JHR") // !lz
}
}
lzVars.Ctx.RecycleMap(lzMap)
lzVars.Ctx.RecycleChunks(lzChunks)
}
}