-
Notifications
You must be signed in to change notification settings - Fork 117
/
metricsview.go
595 lines (516 loc) · 17 KB
/
metricsview.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package queries
import (
"context"
"encoding/csv"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/apache/arrow/go/v13/arrow"
"github.com/apache/arrow/go/v13/arrow/array"
"github.com/apache/arrow/go/v13/arrow/memory"
"github.com/apache/arrow/go/v13/parquet/pqarrow"
"github.com/google/uuid"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/pbutil"
"github.com/xuri/excelize/v2"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
)
// resolveMeasures returns the selected measures
func resolveMeasures(mv *runtimev1.MetricsView, inlines []*runtimev1.InlineMeasure, selectedNames []string) ([]*runtimev1.MetricsView_Measure, error) {
// Build combined measures
ms := make([]*runtimev1.MetricsView_Measure, len(selectedNames))
for i, n := range selectedNames {
found := false
// Search in the inlines (take precedence)
for _, m := range inlines {
if m.Name == n {
ms[i] = &runtimev1.MetricsView_Measure{
Name: m.Name,
Expression: m.Expression,
}
found = true
break
}
}
if found {
continue
}
// Search in the metrics view
for _, m := range mv.Measures {
if m.Name == n {
ms[i] = m
found = true
break
}
}
if !found {
return nil, fmt.Errorf("measure does not exist: '%s'", n)
}
}
return ms, nil
}
func metricsQuery(ctx context.Context, olap drivers.OLAPStore, priority int, sql string, args []any) ([]*runtimev1.MetricsViewColumn, []*structpb.Struct, error) {
rows, err := olap.Execute(ctx, &drivers.Statement{
Query: sql,
Args: args,
Priority: priority,
ExecutionTimeout: defaultExecutionTimeout,
})
if err != nil {
return nil, nil, status.Error(codes.InvalidArgument, err.Error())
}
defer rows.Close()
data, err := rowsToData(rows)
if err != nil {
return nil, nil, status.Error(codes.Internal, err.Error())
}
return structTypeToMetricsViewColumn(rows.Schema), data, nil
}
func olapQuery(ctx context.Context, olap drivers.OLAPStore, priority int, sql string, args []any) (*runtimev1.StructType, []*structpb.Struct, error) {
rows, err := olap.Execute(ctx, &drivers.Statement{
Query: sql,
Args: args,
Priority: priority,
ExecutionTimeout: defaultExecutionTimeout,
})
if err != nil {
return nil, nil, status.Error(codes.InvalidArgument, err.Error())
}
defer rows.Close()
data, err := rowsToData(rows)
if err != nil {
return nil, nil, status.Error(codes.Internal, err.Error())
}
return rows.Schema, data, nil
}
func rowsToData(rows *drivers.Result) ([]*structpb.Struct, error) {
var data []*structpb.Struct
for rows.Next() {
rowMap := make(map[string]any)
err := rows.MapScan(rowMap)
if err != nil {
return nil, err
}
rowStruct, err := pbutil.ToStruct(rowMap, rows.Schema)
if err != nil {
return nil, err
}
data = append(data, rowStruct)
}
err := rows.Err()
if err != nil {
return nil, err
}
return data, nil
}
func structTypeToMetricsViewColumn(v *runtimev1.StructType) []*runtimev1.MetricsViewColumn {
res := make([]*runtimev1.MetricsViewColumn, len(v.Fields))
for i, f := range v.Fields {
res[i] = &runtimev1.MetricsViewColumn{
Name: f.Name,
Type: f.Type.Code.String(),
Nullable: f.Type.Nullable,
}
}
return res
}
// buildFilterClauseForMetricsViewFilter builds a SQL string of conditions joined with AND.
// Unless the result is empty, it is prefixed with "AND".
// I.e. it has the format "AND (...) AND (...) ...".
func buildFilterClauseForMetricsViewFilter(mv *runtimev1.MetricsView, filter *runtimev1.MetricsViewFilter, dialect drivers.Dialect, policy *runtime.ResolvedMetricsViewSecurity) (string, []any, error) {
var clauses []string
var args []any
if filter != nil && filter.Include != nil {
clause, clauseArgs, err := buildFilterClauseForConditions(mv, filter.Include, false, dialect)
if err != nil {
return "", nil, err
}
clauses = append(clauses, clause)
args = append(args, clauseArgs...)
}
if filter != nil && filter.Exclude != nil {
clause, clauseArgs, err := buildFilterClauseForConditions(mv, filter.Exclude, true, dialect)
if err != nil {
return "", nil, err
}
clauses = append(clauses, clause)
args = append(args, clauseArgs...)
}
if policy != nil && policy.RowFilter != "" {
clauses = append(clauses, "AND "+policy.RowFilter)
}
return strings.Join(clauses, " "), args, nil
}
// buildFilterClauseForConditions returns a string with the format "AND (...) AND (...) ..."
func buildFilterClauseForConditions(mv *runtimev1.MetricsView, conds []*runtimev1.MetricsViewFilter_Cond, exclude bool, dialect drivers.Dialect) (string, []any, error) {
var clauses []string
var args []any
for _, cond := range conds {
condClause, condArgs, err := buildFilterClauseForCondition(mv, cond, exclude, dialect)
if err != nil {
return "", nil, err
}
if condClause == "" {
continue
}
clauses = append(clauses, condClause)
args = append(args, condArgs...)
}
return strings.Join(clauses, " "), args, nil
}
// buildFilterClauseForCondition returns a string with the format "AND (...)"
func buildFilterClauseForCondition(mv *runtimev1.MetricsView, cond *runtimev1.MetricsViewFilter_Cond, exclude bool, dialect drivers.Dialect) (string, []any, error) {
var clauses []string
var args []any
// NOTE: Looking up for dimension like this will lead to O(nm).
// Ideal way would be to create a map, but we need to find a clean solution down the line
name, err := metricsViewDimensionToSafeColumn(mv, cond.Name)
if err != nil {
return "", nil, err
}
notKeyword := ""
if exclude {
notKeyword = "NOT"
}
// Tracks if we found NULL(s) in cond.In
inHasNull := false
// Build "dim [NOT] IN (?, ?, ...)" clause
if len(cond.In) > 0 {
// Add to args, skipping nulls
for _, val := range cond.In {
if _, ok := val.Kind.(*structpb.Value_NullValue); ok {
inHasNull = true
continue // Handled later using "dim IS [NOT] NULL" clause
}
arg, err := pbutil.FromValue(val)
if err != nil {
return "", nil, fmt.Errorf("filter error: %w", err)
}
args = append(args, arg)
}
// If there were non-null args, add a "dim [NOT] IN (...)" clause
if len(args) > 0 {
questionMarks := strings.Join(repeatString("?", len(args)), ",")
clause := fmt.Sprintf("%s %s IN (%s)", name, notKeyword, questionMarks)
clauses = append(clauses, clause)
}
}
// Build "dim [NOT] ILIKE ?"
if len(cond.Like) > 0 {
for _, val := range cond.Like {
var clause string
if dialect == drivers.DialectDruid {
// Druid does not support ILIKE
clause = fmt.Sprintf("LOWER(%s) %s LIKE LOWER(?)", name, notKeyword)
} else {
clause = fmt.Sprintf("%s %s ILIKE ?", name, notKeyword)
}
args = append(args, val)
clauses = append(clauses, clause)
}
}
// Add null check
// NOTE: DuckDB doesn't handle NULL values in an "IN" expression. They must be checked with a "dim IS [NOT] NULL" clause.
if inHasNull {
clauses = append(clauses, fmt.Sprintf("%s IS %s NULL", name, notKeyword))
}
// If no checks were added, exit
if len(clauses) == 0 {
return "", nil, nil
}
// Join conditions
var condJoiner string
if exclude {
condJoiner = " AND "
} else {
condJoiner = " OR "
}
condsClause := strings.Join(clauses, condJoiner)
// When you have "dim NOT IN (a, b, ...)", then NULL values are always excluded, even if NULL is not in the list.
// E.g. this returns zero rows: "select * from (select 1 as a union select null as a) where a not in (1)"
// We need to explicitly include it.
if exclude && !inHasNull && len(condsClause) > 0 {
condsClause += fmt.Sprintf(" OR %s IS NULL", name)
}
// Done
return fmt.Sprintf("AND (%s) ", condsClause), args, nil
}
func repeatString(val string, n int) []string {
res := make([]string, n)
for i := 0; i < n; i++ {
res[i] = val
}
return res
}
func convertToString(pbvalue *structpb.Value) (string, error) {
switch pbvalue.GetKind().(type) {
case *structpb.Value_StructValue:
bts, err := protojson.Marshal(pbvalue)
if err != nil {
return "", err
}
return string(bts), nil
case *structpb.Value_NullValue:
return "", nil
default:
return fmt.Sprintf("%v", pbvalue.AsInterface()), nil
}
}
func convertToXLSXValue(pbvalue *structpb.Value) (interface{}, error) {
switch pbvalue.GetKind().(type) {
case *structpb.Value_StructValue:
bts, err := protojson.Marshal(pbvalue)
if err != nil {
return "", err
}
return string(bts), nil
case *structpb.Value_NullValue:
return "", nil
default:
return pbvalue.AsInterface(), nil
}
}
func metricsViewDimensionToSafeColumn(mv *runtimev1.MetricsView, dimName string) (string, error) {
dimName = strings.ToLower(dimName)
for _, dimension := range mv.Dimensions {
if strings.EqualFold(dimension.Name, dimName) {
if dimension.Column != "" {
return safeName(dimension.Column), nil
}
// backwards compatibility for older projects that have not run reconcile on this dashboard
// in that case `column` will not be present
return safeName(dimension.Name), nil
}
}
return "", fmt.Errorf("dimension %s not found", dimName)
}
func metricsViewMeasureExpression(mv *runtimev1.MetricsView, measureName string) (string, error) {
for _, measure := range mv.Measures {
if strings.EqualFold(measure.Name, measureName) {
return measure.Expression, nil
}
}
return "", fmt.Errorf("measure %s not found", measureName)
}
func writeCSV(meta []*runtimev1.MetricsViewColumn, data []*structpb.Struct, writer io.Writer) error {
w := csv.NewWriter(writer)
record := make([]string, 0, len(meta))
for _, field := range meta {
record = append(record, field.Name)
}
if err := w.Write(record); err != nil {
return err
}
record = record[:0]
for _, structs := range data {
for _, field := range meta {
pbvalue := structs.Fields[field.Name]
str, err := convertToString(pbvalue)
if err != nil {
return err
}
record = append(record, str)
}
if err := w.Write(record); err != nil {
return err
}
record = record[:0]
}
w.Flush()
return nil
}
func writeXLSX(meta []*runtimev1.MetricsViewColumn, data []*structpb.Struct, writer io.Writer) error {
f := excelize.NewFile()
defer func() {
_ = f.Close()
}()
sw, err := f.NewStreamWriter("Sheet1")
if err != nil {
return err
}
headers := make([]interface{}, 0, len(meta))
for _, v := range meta {
headers = append(headers, v.Name)
}
if err := sw.SetRow("A1", headers, excelize.RowOpts{Height: 45, Hidden: false}); err != nil {
return err
}
row := make([]interface{}, 0, len(meta))
for i, structs := range data {
for _, field := range meta {
pbvalue := structs.Fields[field.Name]
value, err := convertToXLSXValue(pbvalue)
if err != nil {
return err
}
row = append(row, value)
}
cell, err := excelize.CoordinatesToCellName(1, i+2) // 1-based, and +1 for headers
if err != nil {
return err
}
if err := sw.SetRow(cell, row); err != nil {
return err
}
row = row[:0]
}
if err := sw.Flush(); err != nil {
return err
}
err = f.Write(writer)
return err
}
func writeParquet(meta []*runtimev1.MetricsViewColumn, data []*structpb.Struct, ioWriter io.Writer) error {
fields := make([]arrow.Field, 0, len(meta))
for _, f := range meta {
arrowField := arrow.Field{}
arrowField.Name = f.Name
typeCode := runtimev1.Type_Code(runtimev1.Type_Code_value[f.Type])
switch typeCode {
case runtimev1.Type_CODE_BOOL:
arrowField.Type = arrow.FixedWidthTypes.Boolean
case runtimev1.Type_CODE_INT8:
arrowField.Type = arrow.PrimitiveTypes.Int8
case runtimev1.Type_CODE_INT16:
arrowField.Type = arrow.PrimitiveTypes.Int16
case runtimev1.Type_CODE_INT32:
arrowField.Type = arrow.PrimitiveTypes.Int32
case runtimev1.Type_CODE_INT64:
arrowField.Type = arrow.PrimitiveTypes.Int64
case runtimev1.Type_CODE_INT128:
arrowField.Type = arrow.PrimitiveTypes.Float64
case runtimev1.Type_CODE_UINT8:
arrowField.Type = arrow.PrimitiveTypes.Uint8
case runtimev1.Type_CODE_UINT16:
arrowField.Type = arrow.PrimitiveTypes.Uint16
case runtimev1.Type_CODE_UINT32:
arrowField.Type = arrow.PrimitiveTypes.Uint32
case runtimev1.Type_CODE_UINT64:
arrowField.Type = arrow.PrimitiveTypes.Uint64
case runtimev1.Type_CODE_DECIMAL:
arrowField.Type = arrow.PrimitiveTypes.Float64
case runtimev1.Type_CODE_FLOAT32:
arrowField.Type = arrow.PrimitiveTypes.Float32
case runtimev1.Type_CODE_FLOAT64:
arrowField.Type = arrow.PrimitiveTypes.Float64
case runtimev1.Type_CODE_STRUCT, runtimev1.Type_CODE_UUID, runtimev1.Type_CODE_ARRAY, runtimev1.Type_CODE_STRING, runtimev1.Type_CODE_MAP:
arrowField.Type = arrow.BinaryTypes.String
case runtimev1.Type_CODE_TIMESTAMP, runtimev1.Type_CODE_DATE, runtimev1.Type_CODE_TIME:
arrowField.Type = arrow.FixedWidthTypes.Timestamp_us
case runtimev1.Type_CODE_BYTES:
arrowField.Type = arrow.BinaryTypes.Binary
}
fields = append(fields, arrowField)
}
schema := arrow.NewSchema(fields, nil)
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
recordBuilder := array.NewRecordBuilder(mem, schema)
defer recordBuilder.Release()
for _, s := range data {
for idx, t := range meta {
v := s.Fields[t.Name]
typeCode := runtimev1.Type_Code(runtimev1.Type_Code_value[t.Type])
switch typeCode {
case runtimev1.Type_CODE_BOOL:
recordBuilder.Field(idx).(*array.BooleanBuilder).Append(v.GetBoolValue())
case runtimev1.Type_CODE_INT8:
recordBuilder.Field(idx).(*array.Int8Builder).Append(int8(v.GetNumberValue()))
case runtimev1.Type_CODE_INT16:
recordBuilder.Field(idx).(*array.Int16Builder).Append(int16(v.GetNumberValue()))
case runtimev1.Type_CODE_INT32:
recordBuilder.Field(idx).(*array.Int32Builder).Append(int32(v.GetNumberValue()))
case runtimev1.Type_CODE_INT64:
recordBuilder.Field(idx).(*array.Int64Builder).Append(int64(v.GetNumberValue()))
case runtimev1.Type_CODE_UINT8:
recordBuilder.Field(idx).(*array.Uint8Builder).Append(uint8(v.GetNumberValue()))
case runtimev1.Type_CODE_UINT16:
recordBuilder.Field(idx).(*array.Uint16Builder).Append(uint16(v.GetNumberValue()))
case runtimev1.Type_CODE_UINT32:
recordBuilder.Field(idx).(*array.Uint32Builder).Append(uint32(v.GetNumberValue()))
case runtimev1.Type_CODE_UINT64:
recordBuilder.Field(idx).(*array.Uint64Builder).Append(uint64(v.GetNumberValue()))
case runtimev1.Type_CODE_INT128:
recordBuilder.Field(idx).(*array.Float64Builder).Append((v.GetNumberValue()))
case runtimev1.Type_CODE_FLOAT32:
recordBuilder.Field(idx).(*array.Float32Builder).Append(float32(v.GetNumberValue()))
case runtimev1.Type_CODE_FLOAT64, runtimev1.Type_CODE_DECIMAL:
recordBuilder.Field(idx).(*array.Float64Builder).Append(v.GetNumberValue())
case runtimev1.Type_CODE_STRING, runtimev1.Type_CODE_UUID:
recordBuilder.Field(idx).(*array.StringBuilder).Append(v.GetStringValue())
case runtimev1.Type_CODE_TIMESTAMP, runtimev1.Type_CODE_DATE, runtimev1.Type_CODE_TIME:
tmp, err := arrow.TimestampFromString(v.GetStringValue(), arrow.Microsecond)
if err != nil {
return err
}
recordBuilder.Field(idx).(*array.TimestampBuilder).Append(tmp)
case runtimev1.Type_CODE_ARRAY, runtimev1.Type_CODE_MAP, runtimev1.Type_CODE_STRUCT:
bts, err := protojson.Marshal(v)
if err != nil {
return err
}
recordBuilder.Field(idx).(*array.StringBuilder).Append(string(bts))
}
}
}
parquetwriter, err := pqarrow.NewFileWriter(schema, ioWriter, nil, pqarrow.ArrowWriterProperties{})
if err != nil {
return err
}
defer parquetwriter.Close()
rec := recordBuilder.NewRecord()
err = parquetwriter.Write(rec)
return err
}
func duckDBCopyExport(ctx context.Context, w io.Writer, opts *runtime.ExportOptions, sql string, args []any, filename string, olap drivers.OLAPStore, exportFormat runtimev1.ExportFormat) error {
var extension string
switch exportFormat {
case runtimev1.ExportFormat_EXPORT_FORMAT_PARQUET:
extension = "parquet"
case runtimev1.ExportFormat_EXPORT_FORMAT_CSV:
extension = "csv"
}
tmpPath := fmt.Sprintf("export_%s.%s", uuid.New().String(), extension)
tmpPath = filepath.Join(os.TempDir(), tmpPath)
defer os.Remove(tmpPath)
sql = fmt.Sprintf("COPY (%s) TO '%s'", sql, tmpPath)
if extension == "csv" {
sql += " (FORMAT CSV, HEADER)"
}
rows, err := olap.Execute(ctx, &drivers.Statement{
Query: sql,
Args: args,
Priority: opts.Priority,
ExecutionTimeout: defaultExecutionTimeout,
})
if err != nil {
return err
}
defer rows.Close()
if opts.PreWriteHook != nil {
err = opts.PreWriteHook(filename)
if err != nil {
return err
}
}
f, err := os.Open(tmpPath)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(w, f)
return err
}
func (q *MetricsViewRows) generateFilename(mv *runtimev1.MetricsView) string {
filename := strings.ReplaceAll(mv.Model, `"`, `_`)
if q.TimeStart != nil || q.TimeEnd != nil || q.Filter != nil && (len(q.Filter.Include) > 0 || len(q.Filter.Exclude) > 0) {
filename += "_filtered"
}
return filename
}