-
Notifications
You must be signed in to change notification settings - Fork 108
/
transporter_objectStore_to_duckDB.go
344 lines (299 loc) · 9.7 KB
/
transporter_objectStore_to_duckDB.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
package duckdb
import (
"context"
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/duckdbsql"
"github.com/rilldata/rill/runtime/pkg/fileutil"
"github.com/rilldata/rill/runtime/pkg/observability"
"go.uber.org/zap"
)
type objectStoreToDuckDB struct {
to drivers.OLAPStore
from drivers.ObjectStore
logger *zap.Logger
}
var _ drivers.Transporter = &objectStoreToDuckDB{}
func NewObjectStoreToDuckDB(from drivers.ObjectStore, to drivers.OLAPStore, logger *zap.Logger) drivers.Transporter {
return &objectStoreToDuckDB{
to: to,
from: from,
logger: logger,
}
}
func (t *objectStoreToDuckDB) Transfer(ctx context.Context, srcProps, sinkProps map[string]any, opts *drivers.TransferOptions) error {
sinkCfg, err := parseSinkProperties(sinkProps)
if err != nil {
return err
}
t.logger = t.logger.With(zap.String("source", sinkCfg.Table))
t.logger.Debug("source properties", zap.Any("srcProps", srcProps))
srcCfg, err := parseFileSourceProperties(srcProps)
if err != nil {
return err
}
iterator, err := t.from.DownloadFiles(ctx, srcProps)
if err != nil {
return err
}
defer iterator.Close()
size, ok := iterator.Size(drivers.ProgressUnitByte)
if ok && !sizeWithinStorageLimits(t.to, size) {
return drivers.ErrStorageLimitExceeded
}
// if sql is specified use ast rewrite to fill in the downloaded files
if srcCfg.SQL != "" {
return t.ingestDuckDBSQL(ctx, srcCfg.SQL, iterator, srcCfg, sinkCfg, opts)
}
opts.Progress.Target(size, drivers.ProgressUnitByte)
appendToTable := false
var format string
if srcCfg.Format != "" {
format = fmt.Sprintf(".%s", srcCfg.Format)
}
if srcCfg.AllowSchemaRelaxation {
// set union_by_name to unify the schema of the files
srcCfg.DuckDB["union_by_name"] = true
}
a := newAppender(t.to, sinkCfg, srcCfg.AllowSchemaRelaxation, t.logger, func(files []string) (string, error) {
from, err := sourceReader(files, format, srcCfg.DuckDB)
if err != nil {
return "", err
}
return fmt.Sprintf("SELECT * FROM %s", from), nil
})
for {
files, err := iterator.Next()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
if format == "" {
format = fileutil.FullExt(files[0])
}
st := time.Now()
t.logger.Debug("ingesting files", zap.Strings("files", files), observability.ZapCtx(ctx))
if appendToTable {
if err := a.appendData(ctx, files); err != nil {
return err
}
} else {
from, err := sourceReader(files, format, srcCfg.DuckDB)
if err != nil {
return err
}
err = t.to.CreateTableAsSelect(ctx, sinkCfg.Table, false, fmt.Sprintf("SELECT * FROM %s", from))
if err != nil {
return err
}
}
size := fileSize(files)
t.logger.Debug("ingested files", zap.Strings("files", files), zap.Int64("bytes_ingested", size), zap.Duration("duration", time.Since(st)), observability.ZapCtx(ctx))
opts.Progress.Observe(size, drivers.ProgressUnitByte)
appendToTable = true
}
// convert to enum
if len(srcCfg.CastToENUM) > 0 {
conn, _ := t.to.(*connection)
return conn.convertToEnum(ctx, sinkCfg.Table, srcCfg.CastToENUM)
}
return nil
}
func (t *objectStoreToDuckDB) ingestDuckDBSQL(ctx context.Context, originalSQL string, iterator drivers.FileIterator, srcCfg *fileSourceProperties, dbSink *sinkProperties, opts *drivers.TransferOptions) error {
ast, err := duckdbsql.Parse(originalSQL)
if err != nil {
return err
}
// Validate the sql is supported for sources
// TODO: find a good common place for this validation and avoid code duplication here and in sources packages as well
refs := ast.GetTableRefs()
if len(refs) != 1 {
return errors.New("sql source should have exactly one table reference")
}
ref := refs[0]
if len(ref.Paths) == 0 {
return errors.New("only read_* functions with a single path is supported")
}
if len(ref.Paths) > 1 {
return errors.New("invalid source, only a single path for source is supported")
}
a := newAppender(t.to, dbSink, srcCfg.AllowSchemaRelaxation, t.logger, func(files []string) (string, error) {
return rewriteSQL(ast, files)
})
appendToTable := false
for {
files, err := iterator.Next()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
st := time.Now()
t.logger.Debug("ingesting files", zap.Strings("files", files), observability.ZapCtx(ctx))
if appendToTable {
if err := a.appendData(ctx, files); err != nil {
return err
}
} else {
sql, err := rewriteSQL(ast, files)
if err != nil {
return err
}
err = t.to.CreateTableAsSelect(ctx, dbSink.Table, false, sql)
if err != nil {
return err
}
}
size := fileSize(files)
t.logger.Debug("ingested files", zap.Strings("files", files), zap.Int64("bytes_ingested", size), zap.Duration("duration", time.Since(st)), observability.ZapCtx(ctx))
opts.Progress.Observe(size, drivers.ProgressUnitByte)
appendToTable = true
}
// convert to enum
if len(srcCfg.CastToENUM) > 0 {
conn, _ := t.to.(*connection)
return conn.convertToEnum(ctx, dbSink.Table, srcCfg.CastToENUM)
}
return nil
}
type appender struct {
to drivers.OLAPStore
sink *sinkProperties
allowSchemaRelaxation bool
tableSchema map[string]string
logger *zap.Logger
sqlFunc func([]string) (string, error)
}
func newAppender(to drivers.OLAPStore, sink *sinkProperties, allowSchemaRelaxation bool, logger *zap.Logger, sqlFunc func([]string) (string, error)) *appender {
return &appender{
to: to,
sink: sink,
allowSchemaRelaxation: allowSchemaRelaxation,
logger: logger,
tableSchema: nil,
sqlFunc: sqlFunc,
}
}
func (a *appender) appendData(ctx context.Context, files []string) error {
sql, err := a.sqlFunc(files)
if err != nil {
return err
}
err = a.to.InsertTableAsSelect(ctx, a.sink.Table, sql, a.allowSchemaRelaxation, true, drivers.IncrementalStrategyAppend, nil)
if err == nil || !a.allowSchemaRelaxation || !containsAny(err.Error(), []string{"binder error", "conversion error"}) {
return err
}
// error is of type binder error (more or less columns than current table schema)
// or of type conversion error (datatype changed or column sequence changed)
err = a.updateSchema(ctx, sql, files)
if err != nil {
return fmt.Errorf("failed to update schema %w", err)
}
return a.to.InsertTableAsSelect(ctx, a.sink.Table, sql, true, true, drivers.IncrementalStrategyAppend, nil)
}
// updateSchema updates the schema of the table in case new file adds a new column or
// updates the datatypes of an existing columns with a wider datatype.
func (a *appender) updateSchema(ctx context.Context, sql string, fileNames []string) error {
// schema of new files
srcSchema, err := a.scanSchemaFromQuery(ctx, fmt.Sprintf("DESCRIBE (%s);", sql))
if err != nil {
return err
}
// combined schema
qry := fmt.Sprintf("DESCRIBE ((SELECT * FROM %s LIMIT 0) UNION ALL BY NAME (%s));", safeName(a.sink.Table), sql)
unionSchema, err := a.scanSchemaFromQuery(ctx, qry)
if err != nil {
return err
}
// current schema
if a.tableSchema == nil {
a.tableSchema, err = a.scanSchemaFromQuery(ctx, fmt.Sprintf("DESCRIBE %s;", safeName(a.sink.Table)))
if err != nil {
return err
}
}
newCols := make(map[string]string)
colTypeChanged := make(map[string]string)
for colName, colType := range unionSchema {
oldType, ok := a.tableSchema[colName]
if !ok {
newCols[colName] = colType
} else if oldType != colType {
colTypeChanged[colName] = colType
}
}
if !a.allowSchemaRelaxation {
if len(srcSchema) < len(unionSchema) {
fileNames := strings.Join(names(fileNames), ",")
columns := strings.Join(missingMapKeys(a.tableSchema, srcSchema), ",")
return fmt.Errorf("new files %q are missing columns %q and schema relaxation not allowed", fileNames, columns)
}
if len(colTypeChanged) != 0 {
fileNames := strings.Join(names(fileNames), ",")
columns := strings.Join(keys(colTypeChanged), ",")
return fmt.Errorf("new files %q change datatypes of some columns %q and schema relaxation not allowed", fileNames, columns)
}
}
if len(newCols) != 0 && !a.allowSchemaRelaxation {
fileNames := strings.Join(names(fileNames), ",")
columns := strings.Join(missingMapKeys(srcSchema, a.tableSchema), ",")
return fmt.Errorf("new files %q have new columns %q and schema relaxation not allowed", fileNames, columns)
}
for colName, colType := range newCols {
a.tableSchema[colName] = colType
if err := a.to.AddTableColumn(ctx, a.sink.Table, colName, colType); err != nil {
return err
}
}
for colName, colType := range colTypeChanged {
a.tableSchema[colName] = colType
if err := a.to.AlterTableColumn(ctx, a.sink.Table, colName, colType); err != nil {
return err
}
}
return nil
}
func (a *appender) scanSchemaFromQuery(ctx context.Context, qry string) (map[string]string, error) {
result, err := a.to.Execute(ctx, &drivers.Statement{Query: qry, Priority: 1, LongRunning: true})
if err != nil {
return nil, err
}
defer result.Close()
schema := make(map[string]string)
for result.Next() {
var s duckDBTableSchemaResult
if err := result.StructScan(&s); err != nil {
return nil, err
}
schema[s.ColumnName] = s.ColumnType
}
if err := result.Err(); err != nil {
return nil, err
}
return schema, nil
}
func rewriteSQL(ast *duckdbsql.AST, allFiles []string) (string, error) {
err := ast.RewriteTableRefs(func(table *duckdbsql.TableRef) (*duckdbsql.TableRef, bool) {
return &duckdbsql.TableRef{
Paths: allFiles,
Function: table.Function,
Properties: table.Properties,
Params: table.Params,
}, true
})
if err != nil {
return "", err
}
sql, err := ast.Format()
if err != nil {
return "", err
}
return sql, nil
}