forked from influxdata/flux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
from.go
266 lines (215 loc) · 7.21 KB
/
from.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
package bigtable
import (
"context"
"fmt"
"github.com/InfluxCommunity/flux"
"github.com/InfluxCommunity/flux/dependencies/bigtable"
"github.com/InfluxCommunity/flux/execute"
"github.com/InfluxCommunity/flux/plan"
"github.com/InfluxCommunity/flux/runtime"
"github.com/InfluxCommunity/flux/stdlib/universe"
"github.com/InfluxCommunity/flux/values"
"google.golang.org/api/option"
)
const FromBigtableKind = "fromBigtable"
type FromBigtableOpSpec struct {
Token string `json:"token,omitempty"`
Project string `json:"project,omitempty"`
Instance string `json:"instance,omitempty"`
Table string `json:"table,omitempty"`
}
func init() {
fromBigtableSignature := runtime.MustLookupBuiltinType("experimental/bigtable", "from")
runtime.RegisterPackageValue("experimental/bigtable", "from", flux.MustValue(flux.FunctionValue(FromBigtableKind, createFromBigtableOpSpec, fromBigtableSignature)))
plan.RegisterProcedureSpec(FromBigtableKind, newFromBigtableProcedure, FromBigtableKind)
plan.RegisterPhysicalRules(BigtableFilterRewriteRule{}, BigtableLimitRewriteRule{})
execute.RegisterSource(FromBigtableKind, createFromBigtableSource)
}
func createFromBigtableOpSpec(args flux.Arguments, administration *flux.Administration) (flux.OperationSpec, error) {
spec := new(FromBigtableOpSpec)
if token, err := args.GetRequiredString("token"); err != nil {
return nil, err
} else {
spec.Token = token
}
if project, err := args.GetRequiredString("project"); err != nil {
return nil, err
} else {
spec.Project = project
}
if instance, err := args.GetRequiredString("instance"); err != nil {
return nil, err
} else {
spec.Instance = instance
}
if table, err := args.GetRequiredString("table"); err != nil {
return nil, err
} else {
spec.Table = table
}
return spec, nil
}
func (s *FromBigtableOpSpec) Kind() flux.OperationKind {
return FromBigtableKind
}
type FromBigtableProcedureSpec struct {
plan.DefaultCost
Token string
Project string
Instance string
Table string
// Used by BigtableFilterRewriteRule
RowSet bigtable.RowSet
Filter bigtable.Filter
ReadOptions []bigtable.ReadOption
}
func newFromBigtableProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*FromBigtableOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
return &FromBigtableProcedureSpec{
Token: spec.Token,
Project: spec.Project,
Instance: spec.Instance,
Table: spec.Table,
Filter: bigtable.PassAllFilter(),
ReadOptions: make([]bigtable.ReadOption, 0),
}, nil
}
func (s *FromBigtableProcedureSpec) Kind() plan.ProcedureKind {
return FromBigtableKind
}
func (s *FromBigtableProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(FromBigtableProcedureSpec)
ns.Token = s.Token
ns.Project = s.Project
ns.Instance = s.Instance
ns.Table = s.Table
ns.RowSet = s.RowSet
ns.Filter = s.Filter
ns.ReadOptions = make([]bigtable.ReadOption, 0)
ns.ReadOptions = append(ns.ReadOptions, s.ReadOptions...)
return ns
}
func createFromBigtableSource(prSpec plan.ProcedureSpec, dsid execute.DatasetID, a execute.Administration) (execute.Source, error) {
spec, ok := prSpec.(*FromBigtableProcedureSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", prSpec)
}
BigtableDecoder := BigtableDecoder{id: dsid, administration: a, spec: spec}
return execute.CreateSourceFromDecoder(&BigtableDecoder, dsid, a)
}
type FamilyRow []bigtable.ReadItem
type BigtableDecoder struct {
id execute.DatasetID
administration execute.Administration
spec *FromBigtableProcedureSpec
client *bigtable.Client
tbl *bigtable.Table
reader *execute.RowReader
}
func NewBigtableDecoder(reader *execute.RowReader, administration execute.Administration) *BigtableDecoder {
return &BigtableDecoder{reader: reader, administration: administration}
}
var _ execute.SourceDecoder = (*BigtableDecoder)(nil)
func (c *BigtableDecoder) Connect(ctx context.Context) error {
provider := bigtable.GetProvider(ctx)
client, err := provider.NewClient(ctx, c.spec.Project, c.spec.Instance, option.WithCredentialsJSON([]byte(c.spec.Token)))
if err != nil {
return err
}
c.client = client
c.tbl = client.Open(c.spec.Table)
return nil
}
func (c *BigtableDecoder) Fetch(ctx context.Context) (bool, error) {
// On the first Fetch, get all the data
if c.reader == nil {
r, err := NewBigtableRowReader(ctx, c)
if err != nil {
return false, err
}
c.reader = &r
}
// Every time we call Fetch, change which family we want to look at
return (*c.reader).(*BigtableRowReader).nextFamily()
}
func (c *BigtableDecoder) Decode(ctx context.Context) (flux.Table, error) {
familyCol := flux.ColMeta{Label: "family", Type: flux.TString}
var groupKey flux.GroupKey
if bigtableReader, ok := (*c.reader).(*BigtableRowReader); ok {
groupKey = execute.NewGroupKey([]flux.ColMeta{familyCol}, []values.Value{values.NewString(bigtableReader.currentFamily())})
} else {
groupKey = execute.NewGroupKey(nil, nil)
}
builder := execute.NewColListTableBuilder(groupKey, c.administration.Allocator())
// Every table will have a row key, timestamp, and family
rowKeyIdx, err := builder.AddCol(flux.ColMeta{Label: "rowKey", Type: flux.TString})
if err != nil {
return nil, err
}
timeIdx, err := builder.AddCol(flux.ColMeta{Label: execute.DefaultTimeColLabel, Type: flux.TTime})
if err != nil {
return nil, err
}
familyIdx, err := builder.AddCol(familyCol)
if err != nil {
return nil, err
}
columns := map[string]int{"rowKey": rowKeyIdx, execute.DefaultTimeColLabel: timeIdx, "family": familyIdx}
rowIndex := 0
reader := *c.reader
for reader.Next() {
rowValues, err := reader.GetNextRow()
if err != nil {
return nil, err
}
for j, val := range rowValues {
if j >= len(columns) {
label := reader.ColumnNames()[j]
idx, err := builder.AddCol(flux.ColMeta{Label: label, Type: flux.TString})
if err != nil {
return nil, err
}
columns[label] = idx
if err := builder.SetValue(rowIndex, j, val); err != nil {
return nil, err
}
} else {
if err := builder.AppendValue(j, val); err != nil {
return nil, err
}
}
}
rowIndex++
}
return builder.Table()
}
func (c *BigtableDecoder) Close() error {
return c.client.Close()
}
type BigtableFilterRewriteRule struct{}
func (r BigtableFilterRewriteRule) Name() string {
return "BigtableFilterRewriteRule"
}
func (r BigtableFilterRewriteRule) Pattern() plan.Pattern {
return plan.MultiSuccessor(universe.FilterKind, plan.SingleSuccessor(FromBigtableKind))
}
func (r BigtableFilterRewriteRule) Rewrite(ctx context.Context, filter plan.Node) (plan.Node, bool, error) {
query := filter.Predecessors()[0]
node, changed := AddFilterToNode(query, filter)
return node, changed, nil
}
type BigtableLimitRewriteRule struct{}
func (r BigtableLimitRewriteRule) Name() string {
return "BigtableLimitRewriteRule"
}
func (r BigtableLimitRewriteRule) Pattern() plan.Pattern {
return plan.MultiSuccessor(universe.LimitKind, plan.SingleSuccessor(FromBigtableKind))
}
func (r BigtableLimitRewriteRule) Rewrite(ctx context.Context, limit plan.Node) (plan.Node, bool, error) {
query := limit.Predecessors()[0]
node, changed := AddLimitToNode(query, limit)
return node, changed, nil
}