forked from influxdata/flux
/
to.go
424 lines (368 loc) · 12.5 KB
/
to.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
package sql
import (
"database/sql"
"fmt"
"strings"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
)
const (
ToSQLKind = "toSQL"
DefaultBatchSize = 10000 //TODO: decide if this should be kept low enough for the lowest (SQLite), or not.
)
type ToSQLOpSpec struct {
DriverName string `json:"driverName,omitempty"`
DataSourceName string `json:"dataSourcename,omitempty"`
Table string `json:"table,omitempty"`
BatchSize int `json:"batchSize,omitempty"`
}
func init() {
toSQLSignature := runtime.MustLookupBuiltinType("sql", "to")
runtime.RegisterPackageValue("sql", "to", flux.MustValue(flux.FunctionValueWithSideEffect(ToSQLKind, createToSQLOpSpec, toSQLSignature)))
flux.RegisterOpSpec(ToSQLKind, func() flux.OperationSpec { return &ToSQLOpSpec{} })
plan.RegisterProcedureSpecWithSideEffect(ToSQLKind, newToSQLProcedure, ToSQLKind)
execute.RegisterTransformation(ToSQLKind, createToSQLTransformation)
}
func (o *ToSQLOpSpec) ReadArgs(args flux.Arguments) error {
var err error
o.DriverName, err = args.GetRequiredString("driverName")
if err != nil {
return err
}
if len(o.DriverName) == 0 {
return errors.New(codes.Invalid, "invalid driver name")
}
o.DataSourceName, err = args.GetRequiredString("dataSourceName")
if err != nil {
return err
}
if len(o.DataSourceName) == 0 {
return errors.New(codes.Invalid, "invalid data source name")
}
o.Table, err = args.GetRequiredString("table")
if err != nil {
return err
}
if len(o.Table) == 0 {
return errors.New(codes.Invalid, "invalid table name")
}
b, _, err := args.GetInt("batchSize")
if err != nil {
return err
}
if b <= 0 {
// set default as argument we not supplied
o.BatchSize = DefaultBatchSize
} else {
o.BatchSize = int(b)
}
return err
}
func createToSQLOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
s := new(ToSQLOpSpec)
if err := s.ReadArgs(args); err != nil {
return nil, err
}
return s, nil
}
func (ToSQLOpSpec) Kind() flux.OperationKind {
return ToSQLKind
}
type ToSQLProcedureSpec struct {
plan.DefaultCost
Spec *ToSQLOpSpec
}
func (o *ToSQLProcedureSpec) Kind() plan.ProcedureKind {
return ToSQLKind
}
func (o *ToSQLProcedureSpec) Copy() plan.ProcedureSpec {
s := o.Spec
res := &ToSQLProcedureSpec{
Spec: &ToSQLOpSpec{
DriverName: s.DriverName,
DataSourceName: s.DataSourceName,
Table: s.Table,
BatchSize: s.BatchSize,
},
}
return res
}
func newToSQLProcedure(qs flux.OperationSpec, a plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*ToSQLOpSpec)
if !ok && spec != nil {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &ToSQLProcedureSpec{Spec: spec}, nil
}
func createToSQLTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*ToSQLProcedureSpec)
if !ok {
return nil, nil, errors.Newf(codes.Internal, "invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
deps := flux.GetDependencies(a.Context())
t, err := NewToSQLTransformation(d, deps, cache, s)
if err != nil {
return nil, nil, err
}
return t, d, nil
}
type ToSQLTransformation struct {
d execute.Dataset
cache execute.TableBuilderCache
spec *ToSQLProcedureSpec
db *sql.DB
tx *sql.Tx
}
func (t *ToSQLTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
func NewToSQLTransformation(d execute.Dataset, deps flux.Dependencies, cache execute.TableBuilderCache, spec *ToSQLProcedureSpec) (*ToSQLTransformation, error) {
validator, err := deps.URLValidator()
if err != nil {
return nil, err
}
if err := validateDataSource(validator, spec.Spec.DriverName, spec.Spec.DataSourceName); err != nil {
return nil, err
}
// validate the data driver name and source name.
db, err := sql.Open(spec.Spec.DriverName, spec.Spec.DataSourceName)
if err != nil {
return nil, err
}
var tx *sql.Tx
if spec.Spec.DriverName != "sqlmock" {
tx, err = db.Begin()
if err != nil {
return nil, err
}
}
return &ToSQLTransformation{
d: d,
cache: cache,
spec: spec,
db: db,
tx: tx,
}, nil
}
type idxType struct {
Idx int
Type flux.ColType
}
func (t *ToSQLTransformation) Process(id execute.DatasetID, tbl flux.Table) (err error) {
colNames, valStrings, valArgs, err := CreateInsertComponents(t, tbl)
if err != nil {
return err
}
for i := range valStrings {
if err := ExecuteQueries(t.tx, t.spec.Spec, colNames, &valStrings[i], &valArgs[i]); err != nil {
return err
}
}
return err
}
func (t *ToSQLTransformation) UpdateWatermark(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateWatermark(pt)
}
func (t *ToSQLTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *ToSQLTransformation) Finish(id execute.DatasetID, err error) {
if t.spec.Spec.DriverName != "sqlmock" {
var txErr error
if err == nil {
txErr = t.tx.Commit()
} else {
txErr = t.tx.Rollback()
}
if txErr != nil {
err = errors.Wrap(err, codes.Inherit, txErr)
}
if dbErr := t.db.Close(); dbErr != nil {
err = errors.Wrap(err, codes.Inherit, dbErr)
}
}
t.d.Finish(err)
}
type translationFunc func(f flux.ColType, colname string) (string, error)
func correctBatchSize(batchSize, numberCols int) int {
/*
BatchSize for the DB is the number of parameters that can be queued within each call to Exec.
As each row you send has a parameter count equal to the number of columns (i.e. the number of "?" used in the insert statement), and some DBs
have a default limit on the number of parameters which can be queued before calling Exec; SQLite, for example, has a default of 999 (can only be changed
at compile time).
So if the row width is 10 columns, the maximum Batchsize would be:
(999 - row_width) / row_width = 98 rows. (with 0.9 of a row unused)
and,
(1000 - row_width) / row_width = 99 rows. (no remainder)
NOTE: Given a statement like:
INSERT INTO data_table (now,values,difference) VALUES(?,?,?)
each iteration of EXEC() would add 3 new values (one for each of the '?' placeholders) - but the final "parameter count" includes the initial 3 column names.
this is why the calculation subracts an initial "column width" from the supplied Batchsize.
Sending more would result in the call to Exec returning a "too many SQL variables" error, and the transaction would be rolled-back / aborted
*/
if batchSize < numberCols {
// if this is because the width of a single row is very large, pass to DB driver, and if this exceeds the number of allowed parameters
// this will be fed back to the user to handle - possibly by reducing the row width
return numberCols
}
return (batchSize - numberCols) / numberCols
}
func getTranslationFunc(driverName string) (func() translationFunc, error) {
// simply return the translationFunc that corresponds to the driver type
switch driverName {
case "sqlite3":
return SqliteColumnTranslateFunc, nil
case "postgres", "sqlmock":
return PostgresColumnTranslateFunc, nil
case "mysql":
return MysqlColumnTranslateFunc, nil
default:
return nil, errors.Newf(codes.Internal, "invalid driverName: %s", driverName)
}
}
func CreateInsertComponents(t *ToSQLTransformation, tbl flux.Table) (colNames []string, valStringArray [][]string, valArgsArray [][]interface{}, err error) {
cols := tbl.Cols()
batchSize := correctBatchSize(t.spec.Spec.BatchSize, len(cols))
labels := make(map[string]idxType, len(cols))
var questionMarks, newSQLTableCols []string
for i, col := range cols {
labels[col.Label] = idxType{Idx: i, Type: col.Type}
questionMarks = append(questionMarks, "?")
colNames = append(colNames, col.Label)
driverName := t.spec.Spec.DriverName
// the following allows driver-specific type errors (of which there can be MANY) to be returned, rather than the default of invalid type
translateColumn, err := getTranslationFunc(driverName)
if err != nil {
return nil, nil, nil, err
}
switch col.Type {
case flux.TFloat, flux.TInt, flux.TUInt, flux.TString, flux.TBool, flux.TTime:
// each type is handled within the function - precise mapping is handled within each driver's implementation
v, err := translateColumn()(col.Type, col.Label)
if err != nil {
return nil, nil, nil, err
}
newSQLTableCols = append(newSQLTableCols, v)
default:
return nil, nil, nil, errors.Newf(codes.Internal, "invalid type for column %s", col.Label)
}
}
// Creates the placeholders for values in the query
// eg: (?,?)
valuePlaceHolders := fmt.Sprintf("(%s)", strings.Join(questionMarks, ","))
builder, new := t.cache.TableBuilder(tbl.Key())
if new {
if err := execute.AddTableCols(tbl, builder); err != nil {
return nil, nil, nil, err
}
}
if err := tbl.Do(func(er flux.ColReader) error {
l := er.Len()
// valueStrings is an array of valuePlaceHolders, which will be joined later
valueStrings := make([]string, 0, l)
// valueArgs holds all the values to pass into the query
valueArgs := make([]interface{}, 0, l*len(cols))
if t.spec.Spec.DriverName != "sqlmock" {
q := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s)", t.spec.Spec.Table, strings.Join(newSQLTableCols, ","))
_, err = t.tx.Exec(q)
if err != nil {
return err
}
}
for i := 0; i < l; i++ {
valueStrings = append(valueStrings, valuePlaceHolders)
for j, col := range er.Cols() {
switch col.Type {
case flux.TFloat:
if er.Floats(j).IsNull(i) {
valueArgs = append(valueArgs, nil)
break
}
valueArgs = append(valueArgs, er.Floats(j).Value(i))
case flux.TInt:
if er.Ints(j).IsNull(i) {
valueArgs = append(valueArgs, nil)
break
}
valueArgs = append(valueArgs, er.Ints(j).Value(i))
case flux.TUInt:
if er.UInts(j).IsNull(i) {
valueArgs = append(valueArgs, nil)
break
}
valueArgs = append(valueArgs, er.UInts(j).Value(i))
case flux.TString:
if er.Strings(j).IsNull(i) {
valueArgs = append(valueArgs, nil)
break
}
valueArgs = append(valueArgs, er.Strings(j).ValueString(i))
case flux.TTime:
if er.Times(j).IsNull(i) {
valueArgs = append(valueArgs, nil)
break
}
valueArgs = append(valueArgs, values.Time(er.Times(j).Value(i)).Time())
case flux.TBool:
if er.Bools(j).IsNull(i) {
valueArgs = append(valueArgs, nil)
break
}
valueArgs = append(valueArgs, er.Bools(j).Value(i))
default:
return errors.Newf(codes.FailedPrecondition, "invalid type for column %s", col.Label)
}
}
if err := execute.AppendRecord(i, er, builder); err != nil {
return err
}
if i != 0 && i%batchSize == 0 {
// create "mini batches" of values - each one represents a single db.Exec to SQL
valArgsArray = append(valArgsArray, valueArgs)
valStringArray = append(valStringArray, valueStrings)
valueArgs = make([]interface{}, 0)
valueStrings = make([]string, 0)
}
}
if len(valueArgs) > 0 && len(valueStrings) > 0 {
valArgsArray = append(valArgsArray, valueArgs)
valStringArray = append(valStringArray, valueStrings)
}
return nil
}); err != nil {
return nil, nil, nil, err
}
return colNames, valStringArray, valArgsArray, err
}
func ExecuteQueries(tx *sql.Tx, s *ToSQLOpSpec, colNames []string, valueStrings *[]string, valueArgs *[]interface{}) (err error) {
concatValueStrings := strings.Join(*valueStrings, ",")
// PostgreSQL uses $n instead of ? for placeholders
if s.DriverName == "postgres" {
for pqCounter := 1; strings.Contains(concatValueStrings, "?"); pqCounter++ {
concatValueStrings = strings.Replace(concatValueStrings, "?", fmt.Sprintf("$%v", pqCounter), 1)
}
}
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", s.Table, strings.Join(colNames, ","), concatValueStrings)
if s.DriverName != "sqlmock" {
_, err := tx.Exec(query, *valueArgs...)
if err != nil {
// this err which is extremely helpful as it comes from the SQL driver should be
// bubbled up further up the stack so user can see the issue
if rbErr := tx.Rollback(); rbErr != nil {
return errors.Newf(codes.Aborted, "transaction failed (%s) while recovering from %s", err, rbErr)
}
return err
}
}
return err
}