-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast.go
1012 lines (868 loc) · 23.6 KB
/
ast.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package logql
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/promql"
"github.com/credativ/vali/pkg/iter"
"github.com/credativ/vali/pkg/logproto"
"github.com/credativ/vali/pkg/logql/log"
)
// Expr is the root expression which can be a SampleExpr or LogSelectorExpr
type Expr interface {
logQLExpr() // ensure it's not implemented accidentally
Shardable() bool // A recursive check on the AST to see if it's shardable.
fmt.Stringer
}
type QueryParams interface {
LogSelector() (LogSelectorExpr, error)
GetStart() time.Time
GetEnd() time.Time
GetShards() []string
}
// implicit holds default implementations
type implicit struct{}
func (implicit) logQLExpr() {}
// SelectParams specifies parameters passed to data selections.
type SelectLogParams struct {
*logproto.QueryRequest
}
// LogSelector returns the LogSelectorExpr from the SelectParams.
// The `LogSelectorExpr` can then returns all matchers and filters to use for that request.
func (s SelectLogParams) LogSelector() (LogSelectorExpr, error) {
return ParseLogSelector(s.Selector)
}
type SelectSampleParams struct {
*logproto.SampleQueryRequest
}
// Expr returns the SampleExpr from the SelectSampleParams.
// The `LogSelectorExpr` can then returns all matchers and filters to use for that request.
func (s SelectSampleParams) Expr() (SampleExpr, error) {
return ParseSampleExpr(s.Selector)
}
// LogSelector returns the LogSelectorExpr from the SelectParams.
// The `LogSelectorExpr` can then returns all matchers and filters to use for that request.
func (s SelectSampleParams) LogSelector() (LogSelectorExpr, error) {
expr, err := ParseSampleExpr(s.Selector)
if err != nil {
return nil, err
}
return expr.Selector(), nil
}
// Querier allows a LogQL expression to fetch an EntryIterator for a
// set of matchers and filters
type Querier interface {
SelectLogs(context.Context, SelectLogParams) (iter.EntryIterator, error)
SelectSamples(context.Context, SelectSampleParams) (iter.SampleIterator, error)
}
// LogSelectorExpr is a LogQL expression filtering and returning logs.
type LogSelectorExpr interface {
Matchers() []*labels.Matcher
PipelineExpr
HasFilter() bool
Expr
}
// Type alias for backward compatibility
type (
Pipeline = log.Pipeline
SampleExtractor = log.SampleExtractor
)
// PipelineExpr is an expression defining a log pipeline.
type PipelineExpr interface {
Pipeline() (Pipeline, error)
Expr
}
// StageExpr is an expression defining a single step into a log pipeline
type StageExpr interface {
Stage() (log.Stage, error)
Expr
}
// MultiStageExpr is multiple stages which implement a PipelineExpr.
type MultiStageExpr []StageExpr
func (m MultiStageExpr) Pipeline() (log.Pipeline, error) {
stages, err := m.stages()
if err != nil {
return nil, err
}
return log.NewPipeline(stages), nil
}
func (m MultiStageExpr) stages() ([]log.Stage, error) {
c := make([]log.Stage, 0, len(m))
for _, e := range m {
p, err := e.Stage()
if err != nil {
return nil, newStageError(e, err)
}
if p == log.NoopStage {
continue
}
c = append(c, p)
}
return c, nil
}
func (m MultiStageExpr) String() string {
var sb strings.Builder
for i, e := range m {
sb.WriteString(e.String())
if i+1 != len(m) {
sb.WriteString(" ")
}
}
return sb.String()
}
func (MultiStageExpr) logQLExpr() {} // nolint:unused
type matchersExpr struct {
matchers []*labels.Matcher
implicit
}
func newMatcherExpr(matchers []*labels.Matcher) *matchersExpr {
return &matchersExpr{matchers: matchers}
}
func (e *matchersExpr) Matchers() []*labels.Matcher {
return e.matchers
}
func (e *matchersExpr) Shardable() bool { return true }
func (e *matchersExpr) String() string {
var sb strings.Builder
sb.WriteString("{")
for i, m := range e.matchers {
sb.WriteString(m.String())
if i+1 != len(e.matchers) {
sb.WriteString(", ")
}
}
sb.WriteString("}")
return sb.String()
}
func (e *matchersExpr) Pipeline() (log.Pipeline, error) {
return log.NewNoopPipeline(), nil
}
func (e *matchersExpr) HasFilter() bool {
return false
}
type pipelineExpr struct {
pipeline MultiStageExpr
left *matchersExpr
implicit
}
func newPipelineExpr(left *matchersExpr, pipeline MultiStageExpr) LogSelectorExpr {
return &pipelineExpr{
left: left,
pipeline: pipeline,
}
}
func (e *pipelineExpr) Shardable() bool {
for _, p := range e.pipeline {
if !p.Shardable() {
return false
}
}
return true
}
func (e *pipelineExpr) Matchers() []*labels.Matcher {
return e.left.Matchers()
}
func (e *pipelineExpr) String() string {
var sb strings.Builder
sb.WriteString(e.left.String())
sb.WriteString(" ")
sb.WriteString(e.pipeline.String())
return sb.String()
}
func (e *pipelineExpr) Pipeline() (log.Pipeline, error) {
return e.pipeline.Pipeline()
}
// HasFilter returns true if the pipeline contains stage that can filter out lines.
func (e *pipelineExpr) HasFilter() bool {
for _, p := range e.pipeline {
switch p.(type) {
case *lineFilterExpr, *labelFilterExpr:
return true
default:
continue
}
}
return false
}
type lineFilterExpr struct {
left *lineFilterExpr
ty labels.MatchType
match string
implicit
}
func newLineFilterExpr(left *lineFilterExpr, ty labels.MatchType, match string) *lineFilterExpr {
return &lineFilterExpr{
left: left,
ty: ty,
match: match,
}
}
// AddFilterExpr adds a filter expression to a logselector expression.
func AddFilterExpr(expr LogSelectorExpr, ty labels.MatchType, match string) (LogSelectorExpr, error) {
filter := newLineFilterExpr(nil, ty, match)
switch e := expr.(type) {
case *matchersExpr:
return newPipelineExpr(e, MultiStageExpr{filter}), nil
case *pipelineExpr:
e.pipeline = append(e.pipeline, filter)
return e, nil
default:
return nil, fmt.Errorf("unknown LogSelector: %v+", expr)
}
}
func (e *lineFilterExpr) Shardable() bool { return true }
func (e *lineFilterExpr) String() string {
var sb strings.Builder
if e.left != nil {
sb.WriteString(e.left.String())
sb.WriteString(" ")
}
switch e.ty {
case labels.MatchRegexp:
sb.WriteString("|~")
case labels.MatchNotRegexp:
sb.WriteString("!~")
case labels.MatchEqual:
sb.WriteString("|=")
case labels.MatchNotEqual:
sb.WriteString("!=")
}
sb.WriteString(" ")
sb.WriteString(strconv.Quote(e.match))
return sb.String()
}
func (e *lineFilterExpr) Filter() (log.Filterer, error) {
f, err := log.NewFilter(e.match, e.ty)
if err != nil {
return nil, err
}
if e.left != nil {
nextFilter, err := e.left.Filter()
if err != nil {
return nil, err
}
if nextFilter != nil {
f = log.NewAndFilter(nextFilter, f)
}
}
return f, nil
}
func (e *lineFilterExpr) Stage() (log.Stage, error) {
f, err := e.Filter()
if err != nil {
return nil, err
}
return f.ToStage(), nil
}
type labelParserExpr struct {
op string
param string
implicit
}
func newLabelParserExpr(op, param string) *labelParserExpr {
return &labelParserExpr{
op: op,
param: param,
}
}
func (e *labelParserExpr) Shardable() bool { return true }
func (e *labelParserExpr) Stage() (log.Stage, error) {
switch e.op {
case OpParserTypeJSON:
return log.NewJSONParser(), nil
case OpParserTypeLogfmt:
return log.NewLogfmtParser(), nil
case OpParserTypeRegexp:
return log.NewRegexpParser(e.param)
case OpParserTypeUnpack:
return log.NewUnpackParser(), nil
default:
return nil, fmt.Errorf("unknown parser operator: %s", e.op)
}
}
func (e *labelParserExpr) String() string {
var sb strings.Builder
sb.WriteString(OpPipe)
sb.WriteString(" ")
sb.WriteString(e.op)
if e.param != "" {
sb.WriteString(" ")
sb.WriteString(strconv.Quote(e.param))
}
return sb.String()
}
type labelFilterExpr struct {
log.LabelFilterer
implicit
}
func (e *labelFilterExpr) Shardable() bool { return true }
func (e *labelFilterExpr) Stage() (log.Stage, error) {
return e.LabelFilterer, nil
}
func (e *labelFilterExpr) String() string {
return fmt.Sprintf("%s %s", OpPipe, e.LabelFilterer.String())
}
type lineFmtExpr struct {
value string
implicit
}
func newLineFmtExpr(value string) *lineFmtExpr {
return &lineFmtExpr{
value: value,
}
}
func (e *lineFmtExpr) Shardable() bool { return true }
func (e *lineFmtExpr) Stage() (log.Stage, error) {
return log.NewFormatter(e.value)
}
func (e *lineFmtExpr) String() string {
return fmt.Sprintf("%s %s %s", OpPipe, OpFmtLine, strconv.Quote(e.value))
}
type labelFmtExpr struct {
formats []log.LabelFmt
implicit
}
func newLabelFmtExpr(fmts []log.LabelFmt) *labelFmtExpr {
return &labelFmtExpr{
formats: fmts,
}
}
func (e *labelFmtExpr) Shardable() bool { return false }
func (e *labelFmtExpr) Stage() (log.Stage, error) {
return log.NewLabelsFormatter(e.formats)
}
func (e *labelFmtExpr) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s %s ", OpPipe, OpFmtLabel))
for i, f := range e.formats {
sb.WriteString(f.Name)
sb.WriteString("=")
if f.Rename {
sb.WriteString(f.Value)
} else {
sb.WriteString(strconv.Quote(f.Value))
}
if i+1 != len(e.formats) {
sb.WriteString(",")
}
}
return sb.String()
}
type jsonExpressionParser struct {
expressions []log.JSONExpression
implicit
}
func newJSONExpressionParser(expressions []log.JSONExpression) *jsonExpressionParser {
return &jsonExpressionParser{
expressions: expressions,
}
}
func (j *jsonExpressionParser) Shardable() bool { return true }
func (j *jsonExpressionParser) Stage() (log.Stage, error) {
return log.NewJSONExpressionParser(j.expressions)
}
func (j *jsonExpressionParser) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s %s ", OpPipe, OpParserTypeJSON))
for i, exp := range j.expressions {
sb.WriteString(exp.Identifier)
sb.WriteString("=")
sb.WriteString(strconv.Quote(exp.Expression))
if i+1 != len(j.expressions) {
sb.WriteString(",")
}
}
return sb.String()
}
func mustNewMatcher(t labels.MatchType, n, v string) *labels.Matcher {
m, err := labels.NewMatcher(t, n, v)
if err != nil {
panic(newParseError(err.Error(), 0, 0))
}
return m
}
func mustNewFloat(s string) float64 {
n, err := strconv.ParseFloat(s, 64)
if err != nil {
panic(newParseError(fmt.Sprintf("unable to parse float: %s", err.Error()), 0, 0))
}
return n
}
type unwrapExpr struct {
identifier string
operation string
postFilters []log.LabelFilterer
}
func (u unwrapExpr) String() string {
var sb strings.Builder
if u.operation != "" {
sb.WriteString(fmt.Sprintf(" %s %s %s(%s)", OpPipe, OpUnwrap, u.operation, u.identifier))
} else {
sb.WriteString(fmt.Sprintf(" %s %s %s", OpPipe, OpUnwrap, u.identifier))
}
for _, f := range u.postFilters {
sb.WriteString(fmt.Sprintf(" %s %s", OpPipe, f))
}
return sb.String()
}
func (u *unwrapExpr) addPostFilter(f log.LabelFilterer) *unwrapExpr {
u.postFilters = append(u.postFilters, f)
return u
}
func newUnwrapExpr(id string, operation string) *unwrapExpr {
return &unwrapExpr{identifier: id, operation: operation}
}
type logRange struct {
left LogSelectorExpr
interval time.Duration
unwrap *unwrapExpr
}
// impls Stringer
func (r logRange) String() string {
var sb strings.Builder
sb.WriteString(r.left.String())
if r.unwrap != nil {
sb.WriteString(r.unwrap.String())
}
sb.WriteString(fmt.Sprintf("[%v]", model.Duration(r.interval)))
return sb.String()
}
func (r *logRange) Shardable() bool { return r.left.Shardable() }
func newLogRange(left LogSelectorExpr, interval time.Duration, u *unwrapExpr) *logRange {
return &logRange{
left: left,
interval: interval,
unwrap: u,
}
}
const (
// vector ops
OpTypeSum = "sum"
OpTypeAvg = "avg"
OpTypeMax = "max"
OpTypeMin = "min"
OpTypeCount = "count"
OpTypeStddev = "stddev"
OpTypeStdvar = "stdvar"
OpTypeBottomK = "bottomk"
OpTypeTopK = "topk"
// range vector ops
OpRangeTypeCount = "count_over_time"
OpRangeTypeRate = "rate"
OpRangeTypeBytes = "bytes_over_time"
OpRangeTypeBytesRate = "bytes_rate"
OpRangeTypeAvg = "avg_over_time"
OpRangeTypeSum = "sum_over_time"
OpRangeTypeMin = "min_over_time"
OpRangeTypeMax = "max_over_time"
OpRangeTypeStdvar = "stdvar_over_time"
OpRangeTypeStddev = "stddev_over_time"
OpRangeTypeQuantile = "quantile_over_time"
OpRangeTypeAbsent = "absent_over_time"
// binops - logical/set
OpTypeOr = "or"
OpTypeAnd = "and"
OpTypeUnless = "unless"
// binops - operations
OpTypeAdd = "+"
OpTypeSub = "-"
OpTypeMul = "*"
OpTypeDiv = "/"
OpTypeMod = "%"
OpTypePow = "^"
// binops - comparison
OpTypeCmpEQ = "=="
OpTypeNEQ = "!="
OpTypeGT = ">"
OpTypeGTE = ">="
OpTypeLT = "<"
OpTypeLTE = "<="
// parsers
OpParserTypeJSON = "json"
OpParserTypeLogfmt = "logfmt"
OpParserTypeRegexp = "regexp"
OpParserTypeUnpack = "unpack"
OpFmtLine = "line_format"
OpFmtLabel = "label_format"
OpPipe = "|"
OpUnwrap = "unwrap"
// conversion Op
OpConvBytes = "bytes"
OpConvDuration = "duration"
OpConvDurationSeconds = "duration_seconds"
OpLabelReplace = "label_replace"
)
func IsComparisonOperator(op string) bool {
switch op {
case OpTypeCmpEQ, OpTypeNEQ, OpTypeGT, OpTypeGTE, OpTypeLT, OpTypeLTE:
return true
default:
return false
}
}
// IsLogicalBinOp tests whether an operation is a logical/set binary operation
func IsLogicalBinOp(op string) bool {
switch op {
case OpTypeOr, OpTypeAnd, OpTypeUnless:
return true
default:
return false
}
}
// SampleExpr is a LogQL expression filtering logs and returning metric samples.
type SampleExpr interface {
// Selector is the LogQL selector to apply when retrieving logs.
Selector() LogSelectorExpr
Extractor() (SampleExtractor, error)
Expr
}
type rangeAggregationExpr struct {
left *logRange
operation string
params *float64
grouping *grouping
implicit
}
func newRangeAggregationExpr(left *logRange, operation string, gr *grouping, stringParams *string) SampleExpr {
var params *float64
if stringParams != nil {
if operation != OpRangeTypeQuantile {
panic(newParseError(fmt.Sprintf("parameter %s not supported for operation %s", *stringParams, operation), 0, 0))
}
var err error
params = new(float64)
*params, err = strconv.ParseFloat(*stringParams, 64)
if err != nil {
panic(newParseError(fmt.Sprintf("invalid parameter for operation %s: %s", operation, err), 0, 0))
}
} else {
if operation == OpRangeTypeQuantile {
panic(newParseError(fmt.Sprintf("parameter required for operation %s", operation), 0, 0))
}
}
e := &rangeAggregationExpr{
left: left,
operation: operation,
grouping: gr,
params: params,
}
if err := e.validate(); err != nil {
panic(newParseError(err.Error(), 0, 0))
}
return e
}
func (e *rangeAggregationExpr) Selector() LogSelectorExpr {
return e.left.left
}
func (e rangeAggregationExpr) validate() error {
if e.grouping != nil {
switch e.operation {
case OpRangeTypeAvg, OpRangeTypeStddev, OpRangeTypeStdvar, OpRangeTypeQuantile, OpRangeTypeMax, OpRangeTypeMin:
default:
return fmt.Errorf("grouping not allowed for %s aggregation", e.operation)
}
}
if e.left.unwrap != nil {
switch e.operation {
case OpRangeTypeRate, OpRangeTypeAvg, OpRangeTypeSum, OpRangeTypeMax, OpRangeTypeMin, OpRangeTypeStddev, OpRangeTypeStdvar, OpRangeTypeQuantile, OpRangeTypeAbsent:
return nil
default:
return fmt.Errorf("invalid aggregation %s with unwrap", e.operation)
}
}
switch e.operation {
case OpRangeTypeBytes, OpRangeTypeBytesRate, OpRangeTypeCount, OpRangeTypeRate, OpRangeTypeAbsent:
return nil
default:
return fmt.Errorf("invalid aggregation %s without unwrap", e.operation)
}
}
// impls Stringer
func (e *rangeAggregationExpr) String() string {
var sb strings.Builder
sb.WriteString(e.operation)
sb.WriteString("(")
if e.params != nil {
sb.WriteString(strconv.FormatFloat(*e.params, 'f', -1, 64))
sb.WriteString(",")
}
sb.WriteString(e.left.String())
sb.WriteString(")")
if e.grouping != nil {
sb.WriteString(e.grouping.String())
}
return sb.String()
}
// impl SampleExpr
func (e *rangeAggregationExpr) Shardable() bool {
return shardableOps[e.operation] && e.left.Shardable()
}
type grouping struct {
groups []string
without bool
}
// impls Stringer
func (g grouping) String() string {
var sb strings.Builder
if g.without {
sb.WriteString(" without")
} else if len(g.groups) > 0 {
sb.WriteString(" by")
}
if len(g.groups) > 0 {
sb.WriteString("(")
sb.WriteString(strings.Join(g.groups, ","))
sb.WriteString(")")
}
return sb.String()
}
type vectorAggregationExpr struct {
left SampleExpr
grouping *grouping
params int
operation string
implicit
}
func mustNewVectorAggregationExpr(left SampleExpr, operation string, gr *grouping, params *string) SampleExpr {
var p int
var err error
switch operation {
case OpTypeBottomK, OpTypeTopK:
if params == nil {
panic(newParseError(fmt.Sprintf("parameter required for operation %s", operation), 0, 0))
}
if p, err = strconv.Atoi(*params); err != nil {
panic(newParseError(fmt.Sprintf("invalid parameter %s(%s,", operation, *params), 0, 0))
}
default:
if params != nil {
panic(newParseError(fmt.Sprintf("unsupported parameter for operation %s(%s,", operation, *params), 0, 0))
}
}
if gr == nil {
gr = &grouping{}
}
return &vectorAggregationExpr{
left: left,
operation: operation,
grouping: gr,
params: p,
}
}
func (e *vectorAggregationExpr) Selector() LogSelectorExpr {
return e.left.Selector()
}
func (e *vectorAggregationExpr) Extractor() (log.SampleExtractor, error) {
// inject in the range vector extractor the outer groups to improve performance.
// This is only possible if the operation is a sum. Anything else needs all labels.
if r, ok := e.left.(*rangeAggregationExpr); ok && canInjectVectorGrouping(e.operation, r.operation) {
// if the range vec operation has no grouping we can push down the vec one.
if r.grouping == nil {
return r.extractor(e.grouping)
}
}
return e.left.Extractor()
}
// canInjectVectorGrouping tells if a vector operation can inject grouping into the nested range vector.
func canInjectVectorGrouping(vecOp, rangeOp string) bool {
if vecOp != OpTypeSum {
return false
}
switch rangeOp {
case OpRangeTypeBytes, OpRangeTypeBytesRate, OpRangeTypeSum, OpRangeTypeRate, OpRangeTypeCount:
return true
default:
return false
}
}
func (e *vectorAggregationExpr) String() string {
var params []string
if e.params != 0 {
params = []string{fmt.Sprintf("%d", e.params), e.left.String()}
} else {
params = []string{e.left.String()}
}
return formatOperation(e.operation, e.grouping, params...)
}
// impl SampleExpr
func (e *vectorAggregationExpr) Shardable() bool {
return shardableOps[e.operation] && e.left.Shardable()
}
type BinOpOptions struct {
ReturnBool bool
}
type binOpExpr struct {
SampleExpr
RHS SampleExpr
op string
opts BinOpOptions
}
func (e *binOpExpr) String() string {
if e.opts.ReturnBool {
return fmt.Sprintf("(%s %s bool %s)", e.SampleExpr.String(), e.op, e.RHS.String())
}
return fmt.Sprintf("(%s %s %s)", e.SampleExpr.String(), e.op, e.RHS.String())
}
// impl SampleExpr
func (e *binOpExpr) Shardable() bool {
return shardableOps[e.op] && e.SampleExpr.Shardable() && e.RHS.Shardable()
}
func mustNewBinOpExpr(op string, opts BinOpOptions, lhs, rhs Expr) SampleExpr {
left, ok := lhs.(SampleExpr)
if !ok {
panic(newParseError(fmt.Sprintf(
"unexpected type for left leg of binary operation (%s): %T",
op,
lhs,
), 0, 0))
}
right, ok := rhs.(SampleExpr)
if !ok {
panic(newParseError(fmt.Sprintf(
"unexpected type for right leg of binary operation (%s): %T",
op,
rhs,
), 0, 0))
}
leftLit, lOk := left.(*literalExpr)
rightLit, rOk := right.(*literalExpr)
if IsLogicalBinOp(op) {
if lOk {
panic(newParseError(fmt.Sprintf(
"unexpected literal for left leg of logical/set binary operation (%s): %f",
op,
leftLit.value,
), 0, 0))
}
if rOk {
panic(newParseError(fmt.Sprintf(
"unexpected literal for right leg of logical/set binary operation (%s): %f",
op,
rightLit.value,
), 0, 0))
}
}
// map expr like (1+1) -> 2
if lOk && rOk {
return reduceBinOp(op, leftLit, rightLit)
}
return &binOpExpr{
SampleExpr: left,
RHS: right,
op: op,
opts: opts,
}
}
// Reduces a binary operation expression. A binop is reducible if both of its legs are literal expressions.
// This is because literals need match all labels, which is currently difficult to encode into StepEvaluators.
// Therefore, we ensure a binop can be reduced/simplified, maintaining the invariant that it does not have two literal legs.
func reduceBinOp(op string, left, right *literalExpr) *literalExpr {
merged := mergeBinOp(
op,
&promql.Sample{Point: promql.Point{V: left.value}},
&promql.Sample{Point: promql.Point{V: right.value}},
false,
false,
)
return &literalExpr{value: merged.V}
}
type literalExpr struct {
value float64
implicit
}
func mustNewLiteralExpr(s string, invert bool) *literalExpr {
n, err := strconv.ParseFloat(s, 64)
if err != nil {
panic(newParseError(fmt.Sprintf("unable to parse literal as a float: %s", err.Error()), 0, 0))
}
if invert {
n = -n
}
return &literalExpr{
value: n,
}
}
func (e *literalExpr) String() string {
return fmt.Sprint(e.value)
}
// literlExpr impls SampleExpr & LogSelectorExpr mainly to reduce the need for more complicated typings
// to facilitate sum types. We'll be type switching when evaluating them anyways
// and they will only be present in binary operation legs.
func (e *literalExpr) Selector() LogSelectorExpr { return e }
func (e *literalExpr) HasFilter() bool { return false }
func (e *literalExpr) Shardable() bool { return true }
func (e *literalExpr) Pipeline() (log.Pipeline, error) { return log.NewNoopPipeline(), nil }
func (e *literalExpr) Matchers() []*labels.Matcher { return nil }
func (e *literalExpr) Extractor() (log.SampleExtractor, error) { return nil, nil }
// helper used to impl Stringer for vector and range aggregations
// nolint:interfacer
func formatOperation(op string, grouping *grouping, params ...string) string {
nonEmptyParams := make([]string, 0, len(params))
for _, p := range params {
if p != "" {
nonEmptyParams = append(nonEmptyParams, p)
}
}
var sb strings.Builder
sb.WriteString(op)
if grouping != nil {
sb.WriteString(grouping.String())
}
sb.WriteString("(")
sb.WriteString(strings.Join(nonEmptyParams, ","))
sb.WriteString(")")
return sb.String()
}
type labelReplaceExpr struct {
left SampleExpr
dst string
replacement string
src string
regex string
re *regexp.Regexp
implicit
}
func mustNewLabelReplaceExpr(left SampleExpr, dst, replacement, src, regex string) *labelReplaceExpr {
re, err := regexp.Compile("^(?:" + regex + ")$")
if err != nil {
panic(newParseError(fmt.Sprintf("invalid regex in label_replace: %s", err.Error()), 0, 0))
}
return &labelReplaceExpr{
left: left,
dst: dst,
replacement: replacement,
src: src,
re: re,
regex: regex,
}
}
func (e *labelReplaceExpr) Selector() LogSelectorExpr {
return e.left.Selector()
}
func (e *labelReplaceExpr) Extractor() (SampleExtractor, error) {
return e.left.Extractor()
}
func (e *labelReplaceExpr) Shardable() bool {
return false
}
func (e *labelReplaceExpr) String() string {
var sb strings.Builder
sb.WriteString(OpLabelReplace)
sb.WriteString("(")