forked from c4pt0r/kvql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projection_plan.go
159 lines (148 loc) · 3.18 KB
/
projection_plan.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
package kvql
import (
"fmt"
"strings"
)
type ProjectionPlan struct {
Txn Txn
ChildPlan Plan
AllFields bool
FieldNames []string
FieldTypes []Type
Fields []Expression
}
func (p *ProjectionPlan) Init() error {
return p.ChildPlan.Init()
}
func (p *ProjectionPlan) FieldNameList() []string {
if p.AllFields {
return []string{"KEY", "VALUE"}
}
return p.FieldNames
}
func (p *ProjectionPlan) FieldTypeList() []Type {
if p.AllFields {
return []Type{TSTR, TSTR}
}
return p.FieldTypes
}
func (p *ProjectionPlan) Next(ctx *ExecuteCtx) ([]Column, error) {
ctx.Clear()
k, v, err := p.ChildPlan.Next(ctx)
if err != nil {
return nil, err
}
if k == nil && v == nil && err == nil {
return nil, nil
}
if p.AllFields {
return []Column{k, v}, nil
}
return p.processProjection(NewKVP(k, v), ctx)
}
func (p *ProjectionPlan) Batch(ctx *ExecuteCtx) ([][]Column, error) {
ctx.Clear()
kvps, err := p.ChildPlan.Batch(ctx)
if err != nil {
return nil, err
}
if len(kvps) == 0 {
return nil, nil
}
if p.AllFields {
ret := make([][]Column, 0, len(kvps))
for _, kvp := range kvps {
ret = append(ret, []Column{kvp.Key, kvp.Value})
}
return ret, nil
}
return p.processProjectionBatch(kvps, ctx)
}
func (p *ProjectionPlan) processProjectionBatch(chunk []KVPair, ctx *ExecuteCtx) ([][]Column, error) {
var (
nFields = len(p.Fields)
ret = make([][]Column, len(chunk))
cols = make([][]any, nFields)
err error
have bool
)
for i := 0; i < nFields; i++ {
have = false
if ctx != nil {
fname := p.FieldNames[i]
cols[i], have = ctx.GetChunkFieldFinalResult(fname)
}
if !have {
cols[i], err = p.Fields[i].ExecuteBatch(chunk, ctx)
} else {
ctx.UpdateHit()
}
if err != nil {
return nil, err
}
}
for i := 0; i < len(chunk); i++ {
row := make([]Column, nFields)
for j := 0; j < nFields; j++ {
row[j] = cols[j][i]
}
ret[i] = row
}
return ret, nil
}
func (p *ProjectionPlan) processProjection(kvp KVPair, ctx *ExecuteCtx) ([]Column, error) {
nFields := len(p.Fields)
ret := make([]Column, nFields)
var (
result any
err error
)
for i := 0; i < nFields; i++ {
have := false
if ctx != nil {
fname := p.FieldNames[i]
result, have = ctx.GetFieldResult(fname)
}
if !have {
result, err = p.Fields[i].Execute(kvp, ctx)
} else {
ctx.UpdateHit()
}
if err != nil {
return nil, err
}
switch value := result.(type) {
case bool, []byte, string,
int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64,
JSON, map[string]any, []any:
ret[i] = value
default:
if value == nil {
ret[i] = nil
break
}
return nil, NewExecuteError(p.Fields[i].GetPos(), "Expression result type not support")
}
}
return ret, nil
}
func (p *ProjectionPlan) String() string {
fields := []string{}
if p.AllFields {
fields = append(fields, "*")
} else {
for _, f := range p.Fields {
fields = append(fields, f.String())
}
}
return fmt.Sprintf("ProjectionPlan{Fields = <%s>}", strings.Join(fields, ", "))
}
func (p *ProjectionPlan) Explain() []string {
ret := []string{p.String()}
for _, plan := range p.ChildPlan.Explain() {
ret = append(ret, plan)
}
return ret
}