-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquery.go
More file actions
517 lines (491 loc) · 13.1 KB
/
query.go
File metadata and controls
517 lines (491 loc) · 13.1 KB
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
package hugr
import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"runtime/debug"
"sync"
"time"
"github.com/hugr-lab/query-engine/pkg/auth"
"github.com/hugr-lab/query-engine/pkg/cache"
"github.com/hugr-lab/query-engine/pkg/catalog"
"github.com/hugr-lab/query-engine/pkg/catalog/compiler/base"
"github.com/hugr-lab/query-engine/pkg/catalog/sdl"
"github.com/hugr-lab/query-engine/pkg/db"
"github.com/hugr-lab/query-engine/pkg/jq"
"github.com/hugr-lab/query-engine/pkg/metadata"
"github.com/hugr-lab/query-engine/types"
"github.com/vektah/gqlparser/v2/ast"
"golang.org/x/sync/errgroup"
)
// recoverPanic converts a panic into an error.
// Use as: defer recoverPanic(&err)
func recoverPanic(errp *error) {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
*errp = fmt.Errorf("internal error: %w", e)
} else {
*errp = fmt.Errorf("internal error: %v", r)
}
log.Printf("panic recovered: %v\n%s", r, debug.Stack())
}
}
var ErrParallelMutationNotSupported = errors.New("parallel mutation queries are not supported")
type result struct {
data any
extensions map[string]any
name string
path []string
}
func (s *Service) processQuery(ctx context.Context, provider catalog.Provider, op *catalog.Operation) (map[string]any, map[string]any, error) {
start := time.Now()
queries := op.Queries
qtt := op.QueryType
vars := op.Variables
// create response data structure
dataCh := make(chan result)
eg, ctx := errgroup.WithContext(ctx)
if s.config.MaxParallelQueries != 0 {
eg.SetLimit(s.config.MaxParallelQueries + 1)
}
wg := sync.WaitGroup{}
// if requested at least one mutation query need to run in a transaction and sequentially
if !s.config.AllowParallel || qtt&(base.QueryTypeMutation|base.QueryTypeFunctionMutation) != 0 {
wg.Add(1)
eg.Go(func() (err error) {
defer recoverPanic(&err)
defer wg.Done()
if qtt&(base.QueryTypeMutation|base.QueryTypeFunctionMutation) == 0 {
return s.processQuerySequential(ctx, provider, queries, vars, nil, dataCh)
}
ctx, err := s.db.WithTx(ctx)
if err != nil {
return err
}
defer s.db.Rollback(ctx)
err = s.processQuerySequential(ctx, provider, queries, vars, nil, dataCh)
if err != nil {
return err
}
return s.db.Commit(ctx)
})
} else {
// if requested only query queries can run in parallel
s.processQueryParallel(ctx, &wg, eg, provider, queries, vars, nil, dataCh)
}
data := map[string]any{}
extensions := map[string]any{}
eg.Go(func() error {
for {
select {
case <-ctx.Done():
return nil
case res, ok := <-dataCh:
if !ok {
return nil
}
data, extensions = collectResult(data, extensions, res)
}
}
})
wg.Wait()
close(dataCh)
err := eg.Wait()
if err != nil {
types.DataClose(data)
return nil, nil, err
}
if len(data) == 1 {
for _, v := range data {
if v == nil {
data = nil
}
}
}
if len(extensions) == 0 && op.Definition.Directives.ForName(base.StatsDirectiveName) == nil {
return data, nil, nil
}
ext := map[string]any{}
if op.Definition.Directives.ForName(base.StatsDirectiveName) != nil {
opStats := map[string]any{
"total_time": time.Since(start).String(),
}
if op.Definition.Name != "" {
opStats["name"] = op.Definition.Name
}
ext["stats"] = opStats
}
if len(ext) != 0 {
ext["children"] = extensions
} else {
ext = extensions
}
return data, ext, nil
}
func collectResult(data, extensions map[string]any, res result) (map[string]any, map[string]any) {
if len(res.path) == 0 {
if res.data != nil || data[res.name] == nil {
data[res.name] = res.data
}
if res.extensions != nil {
var v any = res.extensions
if p, ok := extensions[res.name]; ok {
for k, vv := range res.extensions {
p.(map[string]any)[k] = vv
}
v = p
}
extensions[res.name] = v
}
return data, extensions
}
node := res.path[0]
d, ok := data[node]
if !ok {
d = make(map[string]any)
}
res.path = res.path[1:]
e, ok := extensions[node]
if ok {
e = e.(map[string]any)["children"]
}
if !ok {
e = make(map[string]any)
}
var ext map[string]any
data[node], ext = collectResult(d.(map[string]any), e.(map[string]any), res)
if len(ext) == 0 {
return data, extensions
}
c, ok := extensions[node]
if !ok {
extensions[node] = map[string]any{"children": ext}
return data, extensions
}
c.(map[string]any)["children"] = ext
extensions[node] = c
return data, extensions
}
func (s *Service) processQuerySequential(ctx context.Context,
provider catalog.Provider,
queries []base.QueryRequest,
vars map[string]any,
path []string,
dataCh chan<- result,
) error {
for _, query := range queries {
var err error
var res any
var ext map[string]any
switch query.QueryType {
case base.QueryTypeNone:
start := time.Now()
if query.Subset != nil {
err = s.processQuerySequential(ctx, provider, query.Subset, vars, append(path, query.Name), dataCh)
if err != nil {
return err
}
}
if query.Field.Directives.ForName(base.StatsDirectiveName) != nil {
ext = map[string]any{
"stats": map[string]any{
"name": query.Name,
"node_time": time.Since(start).String(),
},
}
}
case base.QueryTypeMeta:
res, err = metadata.ProcessQuery(ctx, provider, query, s.config.MaxDepth, vars)
case base.QueryTypeQuery, base.QueryTypeFunction, base.QueryTypeH3Aggregation:
res, ext, err = s.processDataQuery(ctx, provider, query, vars)
case base.QueryTypeMutation, base.QueryTypeFunctionMutation:
res, ext, err = s.processDataQuery(ctx, provider, query, vars)
case base.QueryTypeJQTransform:
res, ext, err = s.processJQTransformation(ctx, provider, query, vars)
}
if err != nil {
return err
}
if res == nil && ext == nil {
continue
}
select {
case <-ctx.Done():
case dataCh <- result{data: res, extensions: ext, name: query.Name, path: path}:
}
}
return nil
}
func (s *Service) processQueryParallel(
ctx context.Context,
wg *sync.WaitGroup,
eg *errgroup.Group,
provider catalog.Provider,
queries []base.QueryRequest,
vars map[string]any,
path []string,
dataCh chan<- result,
) {
for _, query := range queries {
switch query.QueryType {
case base.QueryTypeNone:
s.processQueryParallel(ctx, wg, eg, provider, query.Subset, vars, append(path, query.Name), dataCh)
case base.QueryTypeJQTransform:
wg.Add(1)
eg.Go(func() (err error) {
defer recoverPanic(&err)
defer wg.Done()
res, ext, err := s.processJQTransformation(ctx, provider, query, vars)
if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case dataCh <- result{data: res, extensions: ext, name: query.Name, path: path}:
}
return nil
})
case base.QueryTypeMeta:
wg.Add(1)
eg.Go(func() (err error) {
defer recoverPanic(&err)
defer wg.Done()
res, err := metadata.ProcessQuery(ctx, provider, query, s.config.MaxDepth, vars)
if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case dataCh <- result{data: res, name: query.Name, path: path}:
}
return nil
})
case base.QueryTypeQuery, base.QueryTypeFunction, base.QueryTypeH3Aggregation:
wg.Add(1)
eg.Go(func() (err error) {
defer recoverPanic(&err)
defer wg.Done()
res, ext, err := s.processDataQuery(ctx, provider, query, vars)
if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case dataCh <- result{data: res, extensions: ext, name: query.Name, path: path}:
}
return nil
})
case base.QueryTypeMutation, base.QueryTypeFunctionMutation:
wg.Add(1)
eg.Go(func() (err error) {
defer recoverPanic(&err)
defer wg.Done()
return ErrParallelMutationNotSupported
})
}
}
}
func (s *Service) processDataQuery(ctx context.Context, provider catalog.Provider, query base.QueryRequest, vars map[string]any) (data any, ext map[string]any, err error) {
defer recoverPanic(&err)
start := time.Now()
var plannerTime, compileTime time.Duration
dataFunc := func() (any, error) {
plan, err := s.planner.Plan(ctx, provider, query.Field, vars)
if err != nil {
return nil, err
}
plannerTime = time.Since(start)
err = plan.Compile()
if err != nil {
return nil, err
}
compileTime = time.Since(start)
if s.config.Debug {
ai := auth.AuthInfoFromContext(ctx)
if ai != nil {
log.Printf("User: %s, Role: %s, Query: %s (%s), SQL: %s",
ai.UserName,
ai.Role,
query.Field.Alias,
query.Field.Name,
plan.Log(),
)
}
if auth.IsFullAccess(ctx) {
log.Printf("Internal query: %s (%s), SQL: %s",
query.Field.Alias,
query.Field.Name,
plan.Log(),
)
}
}
if types.IsValidateOnlyContext(ctx) {
return nil, nil
}
// execute query
return plan.Execute(ctx, s.db)
}
ci := cache.QueryInfo(query.Field, vars)
if !ci.Use {
data, err = dataFunc()
}
if ci.Use {
if ci.Key == "" {
return nil, nil, sdl.ErrorPosf(query.Field.Position, "cache key is empty")
}
ci.Key = query.Field.Name + "_" + ci.Key
data, err = s.cache.Load(ctx, ci.Key, dataFunc, ci.Options()...)
}
if errors.Is(err, sql.ErrNoRows) {
return nil, nil, nil
}
if err != nil {
return nil, nil, err
}
if ci.Invalidate {
if ci.Key != "" {
err = s.cache.Delete(ctx, ci.Key)
if err != nil {
return nil, nil, err
}
}
if len(ci.Tags) != 0 {
err = s.cache.Invalidate(ctx, ci.Tags...)
if err != nil {
return nil, nil, err
}
}
}
execTime := time.Since(start)
if query.Field.Directives.ForName(base.StatsDirectiveName) != nil {
ext = map[string]any{
"stats": map[string]any{
"name": query.Field.Alias,
"node_time": execTime.String(),
"planning_time": plannerTime.String(),
"compile_time": compileTime.String(),
"exec_time": (execTime - compileTime).String(),
},
}
}
return data, ext, nil
}
func (s *Service) processJQTransformation(ctx context.Context, provider catalog.Provider, query base.QueryRequest, vars map[string]any) (data any, ext map[string]any, err error) {
defer recoverPanic(&err)
start := time.Now()
var dataTime, compilerTime, serializationTime, execTime time.Duration
var rn, tn int
dataFunc := func() (any, error) {
am := query.Field.ArgumentMap(vars)
if len(am) == 0 {
return nil, sdl.ErrorPosf(query.Field.Position, "jq requires arguments")
}
if _, ok := am["query"]; !ok {
return nil, sdl.ErrorPosf(query.Field.Position, "jq requires query argument")
}
q, ok := am["query"].(string)
if !ok {
return nil, sdl.ErrorPosf(query.Field.Position, "jq query argument should be string")
}
t, err := jq.NewTransformer(db.ClearTxContext(ctx), q, jq.WithVariables(vars), jq.WithQuerier(s), jq.WithCollectStat())
if err != nil {
return nil, sdl.ErrorPosf(query.Field.Position, "jq query compile error: %v", err)
}
a, includeResults := am["include_origin"]
if includeResults {
includeResults, ok = a.(bool)
if !ok {
return nil, sdl.ErrorPosf(query.Field.Position, "includeResults argument should be boolean")
}
}
subQueries, subQtt := catalog.QueryRequestInfo(query.Field.SelectionSet)
subOp := &catalog.Operation{
Definition: &ast.OperationDefinition{
Operation: ast.Query,
Name: query.Field.Alias,
SelectionSet: query.Field.SelectionSet,
Position: query.Field.Position,
Comment: query.Field.Comment,
},
Variables: vars,
Queries: subQueries,
QueryType: subQtt,
}
data, ext, err := s.processQuery(ctx, provider, subOp)
if err != nil {
return nil, err
}
if types.IsValidateOnlyContext(ctx) {
return map[string]any{"ext": map[string]any{}}, nil
}
if !includeResults {
defer types.DataClose(data)
}
transformed, err := t.Transform(ctx, data, nil)
if err != nil {
return nil, sdl.ErrorPosf(query.Field.Position, "jq query execution error: %v", err)
}
extension := map[string]any{}
if ext != nil {
extension["children"] = ext
}
extension["jq"] = transformed
out := map[string]any{
"ext": extension,
}
if includeResults {
out["data"] = data
}
js := t.Stats()
dataTime = time.Since(start)
compilerTime = js.CompilerTime
serializationTime = js.SerializationTime
execTime = js.ExecutionTime
rn = js.Runs
tn = js.Transformed
return out, nil
}
ci := cache.QueryInfo(query.Field, vars)
var res any
if !ci.Use {
res, err = dataFunc()
}
if ci.Use {
if ci.Key == "" {
return nil, nil, sdl.ErrorPosf(query.Field.Position, "cache key is empty")
}
ci.Key = query.Field.Name + "_" + ci.Key
res, err = s.cache.Load(ctx, ci.Key, dataFunc, ci.Options()...)
}
if err != nil {
return nil, nil, err
}
if ci.Invalidate {
if ci.Key != "" {
err = s.cache.Delete(ctx, ci.Key)
if err != nil {
return nil, nil, err
}
}
}
out := res.(map[string]any)
data = out["data"]
ext = out["ext"].(map[string]any)
if query.Field.Directives.ForName(base.StatsDirectiveName) != nil {
ext["stats"] = map[string]any{
"data_request_time": dataTime.String(),
"compiler_time": compilerTime.String(),
"serialization_time": serializationTime.String(),
"execution_time": execTime.String(),
"node_time": time.Since(start).String(),
"runs": rn,
"transformed": tn,
}
}
return data, ext, nil
}