/
plan_iter.go
561 lines (466 loc) · 11 KB
/
plan_iter.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
//
// Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Universal Permissive License v 1.0 as shown at
// https://oss.oracle.com/licenses/upl/
//
package nosqldb
import (
"fmt"
"strconv"
"strings"
"github.com/oracle/nosql-go-sdk/nosqldb/internal/proto"
"github.com/oracle/nosql-go-sdk/nosqldb/nosqlerr"
"github.com/oracle/nosql-go-sdk/nosqldb/types"
)
// planIterKind represents the kind of plan iterators.
type planIterKind int
const (
constRef planIterKind = 0
varRef planIterKind = 1
extVarRef planIterKind = 2
arithOp planIterKind = 8
fieldStep planIterKind = 11
sfw planIterKind = 14
recv planIterKind = 17
sumFunc planIterKind = 39
minMaxFunc planIterKind = 41
sorting planIterKind = 47
group planIterKind = 65
sorting2 planIterKind = 66
)
func (itk planIterKind) String() string {
switch itk {
case constRef:
return "CONST"
case varRef:
return "VAR_REF"
case extVarRef:
return "EXTERNAL_VAR_REF"
case arithOp:
return "ARITH_OP"
case fieldStep:
return "FIELD_STEP"
case sfw:
return "SFW"
case recv:
return "RECV"
case sumFunc:
return "FN_SUM"
case minMaxFunc:
return "FN_MIN_MAX"
case sorting:
return "SORT"
case group:
return "GROUP"
case sorting2:
return "SORT2"
default:
return "UNKNOWN"
}
}
// funcCode represents a function code that used for the built-in function.
type funcCode int
const (
// opAddSub represents the function code for addition/subtraction operations.
opAddSub funcCode = 14
// opMultDiv represents the function code for multiplication/division operations.
opMultDiv funcCode = 15
fnCountStar funcCode = 42
fnCount funcCode = 43
fnCountNumbers funcCode = 44
fnSum funcCode = 45
// fnMin represents the function code for the min() function.
fnMin funcCode = 47
// fnMax represents the function code for the max() function.
fnMax funcCode = 48
)
func (fc funcCode) String() string {
switch fc {
case opAddSub:
return "OP_ADD_SUB"
case opMultDiv:
return "OP_MULT_DIV"
case fnMin:
return "FN_MIN"
case fnMax:
return "FN_MAX"
case fnCountStar:
return "FN_COUNT_STAR"
case fnCount:
return "FN_COUNT"
case fnCountNumbers:
return "FN_COUNT_NUMBERS"
case fnSum:
return "FN_SUM"
default:
return "UNKNOWN"
}
}
type planIter interface {
open(rcb *runtimeControlBlock) error
next(rcb *runtimeControlBlock) (bool, error)
close(rcb *runtimeControlBlock) error
reset(rcb *runtimeControlBlock) error
getResult(rcb *runtimeControlBlock) types.FieldValue
setResult(rcb *runtimeControlBlock, val types.FieldValue)
getKind() planIterKind
getState(rcb *runtimeControlBlock) planIterState
getPlan() string
displayContent(sb *strings.Builder, f *planFormatter)
}
type funcPlanIter interface {
planIter
getFuncCode() funcCode
}
type aggrPlanIter interface {
planIter
getAggrValue(rcb *runtimeControlBlock, reset bool) (v types.FieldValue, err error)
}
func readStringArray(r proto.Reader) ([]string, error) {
size, err := r.ReadPackedInt()
if err != nil {
return nil, err
}
if size < -1 {
return nil, fmt.Errorf("invalid length: %d", size)
}
if size == -1 {
return nil, nil
}
strs := make([]string, 0, size)
for i := 0; i < size; i++ {
p, err := r.ReadString()
if err != nil {
return nil, err
}
if p != nil {
strs = append(strs, *p)
}
}
return strs, nil
}
func readSequenceLength(r proto.Reader) (n int, err error) {
n, err = r.ReadPackedInt()
if err != nil {
return
}
if n < -1 {
return n, fmt.Errorf("invalid sequence length: %d", n)
}
return
}
func readSortSpecs(r proto.Reader) ([]*sortSpec, error) {
n, err := readSequenceLength(r)
if err != nil {
return nil, err
}
if n == -1 {
return nil, nil
}
specs := make([]*sortSpec, n)
for i := 0; i < n; i++ {
specs[i], err = newSortSpec(r)
if err != nil {
return nil, err
}
}
return specs, nil
}
func deserializePlanIters(r proto.Reader) ([]planIter, error) {
n, err := readSequenceLength(r)
if err != nil {
return nil, err
}
if n == -1 {
return nil, nil
}
iters := make([]planIter, n)
for i := 0; i < n; i++ {
iters[i], err = deserializePlanIter(r)
if err != nil {
return nil, err
}
}
return iters, nil
}
func deserializePlanIter(r proto.Reader) (planIter, error) {
b, err := r.ReadByte()
if err != nil {
return nil, err
}
v := int8(b)
if v == -1 {
return nil, nil
}
kind := planIterKind(int(b))
switch kind {
case constRef:
return newConstIter(r)
case varRef:
return newVarRefIter(r)
case extVarRef:
return newExtVarRefIter(r)
case arithOp:
return newArithOpIter(r)
case fieldStep:
return newFieldStepIter(r)
case sumFunc:
return newFuncSumIter(r)
case minMaxFunc:
return newFuncMinMaxIter(r)
case sorting, sorting2:
return newSortIter(r, kind)
case sfw:
return newSFWIter(r)
case recv:
return newReceiveIter(r)
case group:
return newGroupIter(r)
default:
return nil, fmt.Errorf("unknown query plan iterator of kind: %d", kind)
}
}
type planIterDelegate struct {
kind planIterKind
resultReg int
statePos int
loc location
}
func newPlanIterDelegate(r proto.Reader, k planIterKind) (d *planIterDelegate, err error) {
resultReg, err := readIntAndCheck(r, -1)
if err != nil {
return
}
statePos, err := readIntAndCheck(r, 0)
if err != nil {
return
}
startLine, err := readIntAndCheck(r, 0)
if err != nil {
return
}
startColumn, err := readIntAndCheck(r, 0)
if err != nil {
return
}
endLine, err := readIntAndCheck(r, 0)
if err != nil {
return
}
endColumn, err := readIntAndCheck(r, 0)
if err != nil {
return
}
d = &planIterDelegate{
kind: k,
resultReg: resultReg,
statePos: statePos,
loc: location{
startLine: startLine,
startColumn: startColumn,
endLine: endLine,
endColumn: endColumn,
},
}
return d, nil
}
func readIntAndCheck(r proto.Reader, min int) (i int, err error) {
i, err = r.ReadInt()
if err != nil {
return
}
if i >= min {
return
}
return 0, nosqlerr.NewIllegalArgument("invalid integer value %d, "+
"must be greater than or equal to %d", i, min)
}
// isDone returns whether the iterator is in the DONE or CLOSED state.
// CLOSED is included because, in the order of states, a CLOSED iterator is also DONE.
func (d *planIterDelegate) isDone(rcb runtimeControlBlock) bool {
state := rcb.getState(d.statePos)
if state == nil {
return false
}
return state.isDone() || state.isClosed()
}
// getKind returns the kind of plan iterator.
func (d *planIterDelegate) getKind() planIterKind {
return d.kind
}
func (d *planIterDelegate) getState(rcb *runtimeControlBlock) planIterState {
return rcb.getState(d.statePos)
}
func (d *planIterDelegate) open(rcb *runtimeControlBlock) error {
return rcb.openIter(d.statePos)
}
func (d *planIterDelegate) close(rcb *runtimeControlBlock) error {
return rcb.closeIter(d.statePos)
}
func (d *planIterDelegate) reset(rcb *runtimeControlBlock) error {
state := rcb.getState(d.statePos)
return state.reset()
}
// next moves the plan iterator to the next iteration.
func (d *planIterDelegate) next(rcb *runtimeControlBlock) (bool, error) {
state := rcb.getState(d.statePos)
if state.isDone() {
return false, nil
}
state.done()
return true, nil
}
func (d *planIterDelegate) setResult(rcb *runtimeControlBlock, val types.FieldValue) {
rcb.setRegValue(d.resultReg, val)
}
// getResult returns the result stored in register that managed by runtime control block.
func (d *planIterDelegate) getResult(rcb *runtimeControlBlock) types.FieldValue {
return rcb.getRegValue(d.resultReg)
}
func (d *planIterDelegate) getExecPlan(iter planIter) string {
sb := &strings.Builder{}
f := newPlanFormatter()
d.displayPlan(iter, sb, f)
return sb.String()
}
func (d *planIterDelegate) displayPlan(iter planIter, sb *strings.Builder, f *planFormatter) {
f.printIndent(sb)
d.displayName(iter, sb)
d.displayReg(sb)
sb.WriteString("\n")
f.printIndent(sb)
sb.WriteString("[\n")
f.incIndent()
iter.displayContent(sb, f)
f.decIndent()
sb.WriteString("\n")
f.printIndent(sb)
sb.WriteString("]")
}
func (d *planIterDelegate) displayReg(sb *strings.Builder) {
fmt.Fprintf(sb, "([%d])", d.resultReg)
}
func (d *planIterDelegate) displayName(it planIter, sb *strings.Builder) {
if fnIt, ok := it.(funcPlanIter); ok {
fmt.Fprintf(sb, "%v", fnIt.getFuncCode())
} else {
fmt.Fprintf(sb, "%v", it.getKind())
}
}
func (d *planIterDelegate) displayContent(sb *strings.Builder, f *planFormatter) {
// no-op
}
type location struct {
startLine int
startColumn int
endLine int
endColumn int
}
func (loc location) String() string {
return fmt.Sprintf("[%d:%d, %d:%d]", loc.startLine, loc.startColumn,
loc.endLine, loc.endColumn)
}
type planFormatter struct {
indentIncrement int
indent int
}
func newPlanFormatter() *planFormatter {
return &planFormatter{
indentIncrement: 2,
}
}
func (f *planFormatter) printIndent(b *strings.Builder) {
if f.indent <= 0 {
return
}
format := "%" + strconv.Itoa(f.indent) + "s"
fmt.Fprintf(b, format, " ")
}
func (f *planFormatter) incIndent() {
f.indent += f.indentIncrement
}
func (f *planFormatter) decIndent() {
f.indent -= f.indentIncrement
}
type planIterState interface {
isOpen() bool
isDone() bool
isClosed() bool
open() error
done() error
reset() error
close() error
setState(newState iterState) error
}
var _ planIterState = (*iterState)(nil)
// iterState represents dynamic state for an iterator.
type iterState int
const (
open iterState = iota // 0
running // 1
done // 2
closed // 3
)
func newIterState() *iterState {
state := open
return &state
}
func (st iterState) String() string {
switch st {
case open:
return "OPEN"
case running:
return "RUNNING"
case done:
return "DONE"
case closed:
return "CLOSED"
default:
return "UNKNOWN"
}
}
func (st iterState) isOpen() bool {
return st == open
}
func (st iterState) isDone() bool {
return st == done
}
func (st iterState) isClosed() bool {
return st == closed
}
func (st *iterState) open() error {
return st.setState(open)
}
func (st *iterState) reset() error {
return st.setState(open)
}
func (st *iterState) done() error {
return st.setState(done)
}
func (st *iterState) close() error {
return st.setState(closed)
}
func (st *iterState) setState(newState iterState) error {
switch *st {
case open, running:
// "open --> done" transition is allowed for iterators that are "done"
// on the 1st next() call after an open() or reset() call. In this case,
// rather than setting the state to "running" on entrance to the next()
// call and then setting the state again to "done" before returning from
// the same next() call, we allow a direct transition from "open" to "done".
*st = newState
return nil
case done:
if newState == open || newState == closed {
*st = newState
return nil
}
case closed:
if newState == closed {
return nil
}
}
return nosqlerr.NewIllegalState("wrong state transition for plan iterator. "+
"Current state: %v, New state: %v", *st, newState)
}