-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathset_batch_size.go
More file actions
56 lines (50 loc) · 1.64 KB
/
set_batch_size.go
File metadata and controls
56 lines (50 loc) · 1.64 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
// Copyright (c) The Thanos Community Authors.
// Licensed under the Apache License 2.0.
package logicalplan
import (
"github.com/thanos-io/promql-engine/query"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/util/annotations"
)
// aggregationLikeFuncs contains functions that modify the labelset or series count.
var aggregationLikeFuncs = map[string]struct{}{
"scalar": {},
"absent": {},
"absent_over_time": {},
"histogram_quantile": {},
"histogram_fraction": {},
}
// SelectorBatchSize configures the batch size of selector based on
// aggregates present in the plan.
type SelectorBatchSize struct {
Size int64
}
// Optimize configures the batch size of selector based on the query plan.
// If any aggregate is present in the plan, the batch size is set to the configured value.
// The two exceptions where this cannot be done is if the aggregate is quantile, or
// when a binary expression precedes the aggregate.
func (m SelectorBatchSize) Optimize(plan Node, _ *query.Options) (Node, annotations.Annotations) {
canBatch := false
Traverse(&plan, func(current *Node) {
switch e := (*current).(type) {
case *FunctionCall:
if _, aggregationLike := aggregationLikeFuncs[e.Func.Name]; aggregationLike {
canBatch = false
}
case *Binary:
canBatch = false
case *Aggregation:
if e.Op == parser.QUANTILE || e.Op == parser.TOPK || e.Op == parser.BOTTOMK || e.Op == parser.LIMITK || e.Op == parser.LIMIT_RATIO {
canBatch = false
return
}
canBatch = true
case *VectorSelector:
if canBatch {
e.BatchSize = m.Size
}
canBatch = false
}
})
return plan, nil
}