-
Notifications
You must be signed in to change notification settings - Fork 111
/
objects.go
467 lines (402 loc) · 13.2 KB
/
objects.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
package yaml
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/c2h5oh/datasize"
"github.com/jinzhu/copier"
"github.com/mitchellh/mapstructure"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/duration"
"github.com/rilldata/rill/runtime/pkg/fileutil"
"google.golang.org/protobuf/types/known/structpb"
// Load IANA time zone data
_ "time/tzdata"
)
/**
* This file contains the mapping from CatalogObject to Yaml files
*/
type Source struct {
Type string
Path string `yaml:"path,omitempty"`
CsvDelimiter string `yaml:"csv.delimiter,omitempty" mapstructure:"csv.delimiter,omitempty"`
URI string `yaml:"uri,omitempty"`
Region string `yaml:"region,omitempty" mapstructure:"region,omitempty"`
S3Endpoint string `yaml:"endpoint,omitempty" mapstructure:"endpoint,omitempty"`
GlobMaxTotalSize int64 `yaml:"glob.max_total_size,omitempty" mapstructure:"glob.max_total_size,omitempty"`
GlobMaxObjectsMatched int `yaml:"glob.max_objects_matched,omitempty" mapstructure:"glob.max_objects_matched,omitempty"`
GlobMaxObjectsListed int64 `yaml:"glob.max_objects_listed,omitempty" mapstructure:"glob.max_objects_listed,omitempty"`
GlobPageSize int `yaml:"glob.page_size,omitempty" mapstructure:"glob.page_size,omitempty"`
HivePartition *bool `yaml:"hive_partitioning,omitempty" mapstructure:"hive_partitioning,omitempty"`
Timeout int32 `yaml:"timeout,omitempty"`
ExtractPolicy *ExtractPolicy `yaml:"extract,omitempty"`
Format string `yaml:"format,omitempty" mapstructure:"format,omitempty"`
DuckDBProps map[string]any `yaml:"duckdb,omitempty" mapstructure:"duckdb,omitempty"`
Headers map[string]any `yaml:"headers,omitempty" mapstructure:"headers,omitempty"`
AllowSchemaRelaxation *bool `yaml:"ingest.allow_schema_relaxation,omitempty" mapstructure:"allow_schema_relaxation,omitempty"`
SQL string `yaml:"sql,omitempty" mapstructure:"sql,omitempty"`
DB string `yaml:"db,omitempty" mapstructure:"db,omitempty"`
ProjectID string `yaml:"project_id,omitempty" mapstructure:"project_id,omitempty"`
}
type ExtractPolicy struct {
Row *ExtractConfig `yaml:"rows,omitempty" mapstructure:"rows,omitempty"`
File *ExtractConfig `yaml:"files,omitempty" mapstructure:"files,omitempty"`
}
type ExtractConfig struct {
Strategy string `yaml:"strategy,omitempty" mapstructure:"strategy,omitempty"`
Size string `yaml:"size,omitempty" mapstructure:"size,omitempty"`
}
type MetricsView struct {
Label string `yaml:"title"`
DisplayName string `yaml:"display_name,omitempty"` // for backwards compatibility
Description string
Model string
TimeDimension string `yaml:"timeseries"`
SmallestTimeGrain string `yaml:"smallest_time_grain"`
DefaultTimeRange string `yaml:"default_time_range"`
AvailableTimeZones []string `yaml:"available_time_zones,omitempty"`
Dimensions []*Dimension
Measures []*Measure
}
type Measure struct {
Label string
Name string
Expression string
Description string
Format string `yaml:"format_preset"`
Ignore bool `yaml:"ignore,omitempty"`
ValidPercentOfTotal bool `yaml:"valid_percent_of_total,omitempty"`
}
type Dimension struct {
Name string
Label string
Property string `yaml:"property,omitempty"`
Column string
Description string
Ignore bool `yaml:"ignore,omitempty"`
}
func toSourceArtifact(catalog *drivers.CatalogEntry) (*Source, error) {
source := &Source{
Type: catalog.GetSource().Connector,
}
props := catalog.GetSource().Properties.AsMap()
err := mapstructure.Decode(props, source)
if err != nil {
return nil, err
}
if source.Path != "" && catalog.GetSource().Connector != "local_file" {
source.URI = source.Path
source.Path = ""
}
extract, err := toExtractArtifact(catalog.GetSource().GetPolicy())
if err != nil {
return nil, err
}
source.ExtractPolicy = extract
return source, nil
}
func toExtractArtifact(extract *runtimev1.Source_ExtractPolicy) (*ExtractPolicy, error) {
if extract == nil {
return nil, nil
}
sourceExtract := &ExtractPolicy{}
// set file
if extract.FilesStrategy != runtimev1.Source_ExtractPolicy_STRATEGY_UNSPECIFIED {
sourceExtract.File = &ExtractConfig{}
sourceExtract.File.Strategy = extract.FilesStrategy.String()
sourceExtract.File.Size = fmt.Sprintf("%v", extract.FilesLimit)
}
// set row
if extract.RowsStrategy != runtimev1.Source_ExtractPolicy_STRATEGY_UNSPECIFIED {
sourceExtract.Row = &ExtractConfig{}
sourceExtract.Row.Strategy = extract.RowsStrategy.String()
bytes := datasize.ByteSize(extract.RowsLimitBytes)
sourceExtract.Row.Size = bytes.HumanReadable()
}
return sourceExtract, nil
}
func toMetricsViewArtifact(catalog *drivers.CatalogEntry) (*MetricsView, error) {
metricsArtifact := &MetricsView{}
err := copier.Copy(metricsArtifact, catalog.Object)
metricsArtifact.SmallestTimeGrain = getTimeGrainString(catalog.GetMetricsView().SmallestTimeGrain)
metricsArtifact.DefaultTimeRange = catalog.GetMetricsView().DefaultTimeRange
if err != nil {
return nil, err
}
return metricsArtifact, nil
}
func fromSourceArtifact(source *Source, path string) (*drivers.CatalogEntry, error) {
props := map[string]interface{}{}
if source.Type == "local_file" {
props["path"] = source.Path
} else if source.URI != "" {
props["path"] = source.URI
}
if source.Region != "" {
props["region"] = source.Region
}
if source.DuckDBProps != nil {
props["duckdb"] = source.DuckDBProps
}
if source.CsvDelimiter != "" {
// backward compatibility
if _, defined := props["duckdb"]; !defined {
props["duckdb"] = map[string]any{}
}
props["duckdb"].(map[string]any)["delim"] = fmt.Sprintf("'%v'", source.CsvDelimiter)
}
if source.HivePartition != nil {
// backward compatibility
if _, defined := props["duckdb"]; !defined {
props["duckdb"] = map[string]any{}
}
props["duckdb"].(map[string]any)["hive_partitioning"] = *source.HivePartition
}
if source.GlobMaxTotalSize != 0 {
props["glob.max_total_size"] = source.GlobMaxTotalSize
}
if source.GlobMaxObjectsMatched != 0 {
props["glob.max_objects_matched"] = source.GlobMaxObjectsMatched
}
if source.GlobMaxObjectsListed != 0 {
props["glob.max_objects_listed"] = source.GlobMaxObjectsListed
}
if source.GlobPageSize != 0 {
props["glob.page_size"] = source.GlobPageSize
}
if source.S3Endpoint != "" {
props["endpoint"] = source.S3Endpoint
}
if source.Format != "" {
props["format"] = source.Format
}
if source.Headers != nil {
props["headers"] = source.Headers
}
if source.AllowSchemaRelaxation != nil {
props["allow_schema_relaxation"] = *source.AllowSchemaRelaxation
}
if source.SQL != "" {
props["sql"] = source.SQL
}
if source.DB != "" {
props["db"] = source.DB
}
if source.ProjectID != "" {
props["project_id"] = source.ProjectID
}
propsPB, err := structpb.NewStruct(props)
if err != nil {
return nil, err
}
extract, err := fromExtractArtifact(source.ExtractPolicy)
if err != nil {
return nil, err
}
name := fileutil.Stem(path)
return &drivers.CatalogEntry{
Name: name,
Type: drivers.ObjectTypeSource,
Path: path,
Object: &runtimev1.Source{
Name: name,
Connector: source.Type,
Properties: propsPB,
Policy: extract,
TimeoutSeconds: source.Timeout,
},
}, nil
}
func fromExtractArtifact(policy *ExtractPolicy) (*runtimev1.Source_ExtractPolicy, error) {
if policy == nil {
return nil, nil
}
extractPolicy := &runtimev1.Source_ExtractPolicy{}
// parse file
if policy.File != nil {
// parse strategy
strategy, err := parseStrategy(policy.File.Strategy)
if err != nil {
return nil, err
}
extractPolicy.FilesStrategy = strategy
// parse size
size, err := strconv.ParseUint(policy.File.Size, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid size, parse failed with error %w", err)
}
if size <= 0 {
return nil, fmt.Errorf("invalid size %q", size)
}
extractPolicy.FilesLimit = size
}
// parse rows
if policy.Row != nil {
// parse strategy
strategy, err := parseStrategy(policy.Row.Strategy)
if err != nil {
return nil, err
}
extractPolicy.RowsStrategy = strategy
// parse size
// todo :: add support for number of rows
size, err := getBytes(policy.Row.Size)
if err != nil {
return nil, fmt.Errorf("invalid size, parse failed with error %w", err)
}
if size <= 0 {
return nil, fmt.Errorf("invalid size %q", size)
}
extractPolicy.RowsLimitBytes = size
}
return extractPolicy, nil
}
func parseStrategy(s string) (runtimev1.Source_ExtractPolicy_Strategy, error) {
switch strings.ToLower(s) {
case "tail":
return runtimev1.Source_ExtractPolicy_STRATEGY_TAIL, nil
case "head":
return runtimev1.Source_ExtractPolicy_STRATEGY_HEAD, nil
default:
return runtimev1.Source_ExtractPolicy_STRATEGY_UNSPECIFIED, fmt.Errorf("invalid extract strategy %q", s)
}
}
func getBytes(size string) (uint64, error) {
var s datasize.ByteSize
if err := s.UnmarshalText([]byte(size)); err != nil {
return 0, err
}
return s.Bytes(), nil
}
func fromMetricsViewArtifact(metrics *MetricsView, path string) (*drivers.CatalogEntry, error) {
if metrics.DisplayName != "" && metrics.Label == "" {
// backwards compatibility
metrics.Label = metrics.DisplayName
}
// remove ignored measures and dimensions
var measures []*Measure
for _, measure := range metrics.Measures {
if measure.Ignore {
continue
}
measures = append(measures, measure)
}
metrics.Measures = measures
var dimensions []*Dimension
for _, dimension := range metrics.Dimensions {
if dimension.Ignore {
continue
}
if dimension.Property != "" && dimension.Column == "" {
// backwards compatibility when we were using `property` instead of `column`
dimension.Column = dimension.Property
}
dimensions = append(dimensions, dimension)
}
metrics.Dimensions = dimensions
apiMetrics := &runtimev1.MetricsView{}
// validate correctness of default time range
if metrics.DefaultTimeRange != "" {
_, err := duration.ParseISO8601(metrics.DefaultTimeRange)
if err != nil {
return nil, fmt.Errorf("invalid default_time_range: %w", err)
}
apiMetrics.DefaultTimeRange = metrics.DefaultTimeRange
}
// validate time zone locations
for _, tz := range metrics.AvailableTimeZones {
_, err := time.LoadLocation(tz)
if err != nil {
return nil, err
}
}
err := copier.Copy(apiMetrics, metrics)
if err != nil {
return nil, err
}
// this is needed since measure names are not given by the user
for i, measure := range apiMetrics.Measures {
if measure.Name == "" {
measure.Name = fmt.Sprintf("measure_%d", i)
}
}
// backwards compatibility where name was used as property
for i, dimension := range apiMetrics.Dimensions {
if dimension.Name == "" {
if dimension.Column == "" {
// if there is no name and property add dimension_<index> as name
dimension.Name = fmt.Sprintf("dimension_%d", i)
} else {
// else use property as name
dimension.Name = dimension.Column
}
}
}
timeGrainEnum, err := getTimeGrainEnum(metrics.SmallestTimeGrain)
if err != nil {
return nil, err
}
apiMetrics.SmallestTimeGrain = timeGrainEnum
name := fileutil.Stem(path)
apiMetrics.Name = name
return &drivers.CatalogEntry{
Name: name,
Type: drivers.ObjectTypeMetricsView,
Path: path,
Object: apiMetrics,
}, nil
}
// Get TimeGrain enum from string
func getTimeGrainEnum(timeGrain string) (runtimev1.TimeGrain, error) {
switch strings.ToLower(timeGrain) {
case "":
return runtimev1.TimeGrain_TIME_GRAIN_UNSPECIFIED, nil
case "millisecond":
return runtimev1.TimeGrain_TIME_GRAIN_MILLISECOND, nil
case "second":
return runtimev1.TimeGrain_TIME_GRAIN_SECOND, nil
case "minute":
return runtimev1.TimeGrain_TIME_GRAIN_MINUTE, nil
case "hour":
return runtimev1.TimeGrain_TIME_GRAIN_HOUR, nil
case "day":
return runtimev1.TimeGrain_TIME_GRAIN_DAY, nil
case "week":
return runtimev1.TimeGrain_TIME_GRAIN_WEEK, nil
case "month":
return runtimev1.TimeGrain_TIME_GRAIN_MONTH, nil
case "quarter":
return runtimev1.TimeGrain_TIME_GRAIN_QUARTER, nil
case "year":
return runtimev1.TimeGrain_TIME_GRAIN_YEAR, nil
default:
return runtimev1.TimeGrain_TIME_GRAIN_UNSPECIFIED, fmt.Errorf("invalid time grain: %s", timeGrain)
}
}
// Get TimeGrain string from enum
func getTimeGrainString(timeGrain runtimev1.TimeGrain) string {
switch timeGrain {
case runtimev1.TimeGrain_TIME_GRAIN_MILLISECOND:
return "millisecond"
case runtimev1.TimeGrain_TIME_GRAIN_SECOND:
return "second"
case runtimev1.TimeGrain_TIME_GRAIN_MINUTE:
return "minute"
case runtimev1.TimeGrain_TIME_GRAIN_HOUR:
return "hour"
case runtimev1.TimeGrain_TIME_GRAIN_DAY:
return "day"
case runtimev1.TimeGrain_TIME_GRAIN_WEEK:
return "week"
case runtimev1.TimeGrain_TIME_GRAIN_MONTH:
return "month"
case runtimev1.TimeGrain_TIME_GRAIN_QUARTER:
return "quarter"
case runtimev1.TimeGrain_TIME_GRAIN_YEAR:
return "year"
default:
return ""
}
}