Skip to content

Commit 7e6b6b1

Browse files
committed
refactor: consolidate query options into execution/execopts package
- Move query.Options to execopts.Options - Rename engine.QueryOpts to engine.QueryOptions - Simplify QueryOptions (remove promql.QueryOpts interface) - Delete the query package Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>
1 parent 460a2db commit 7e6b6b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+213
-215
lines changed

engine/engine.go

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414
"time"
1515

1616
"github.com/thanos-io/promql-engine/execution"
17+
"github.com/thanos-io/promql-engine/execution/execopts"
1718
"github.com/thanos-io/promql-engine/execution/model"
1819
"github.com/thanos-io/promql-engine/execution/parse"
1920
"github.com/thanos-io/promql-engine/execution/telemetry"
2021
"github.com/thanos-io/promql-engine/extlabels"
2122
"github.com/thanos-io/promql-engine/logicalplan"
22-
"github.com/thanos-io/promql-engine/query"
2323
engstorage "github.com/thanos-io/promql-engine/storage"
2424
promstorage "github.com/thanos-io/promql-engine/storage/prometheus"
2525
"github.com/thanos-io/promql-engine/warnings"
@@ -83,33 +83,31 @@ type Opts struct {
8383
DisableDuplicateLabelChecks bool
8484
}
8585

86-
// QueryOpts implements promql.QueryOpts but allows to override more engine default options.
87-
type QueryOpts struct {
88-
// These values are used to implement promql.QueryOpts, they have weird "Param" suffix because
89-
// they are accessed by methods of the same name.
90-
LookbackDeltaParam time.Duration
91-
EnablePerStepStatsParam bool
86+
// QueryOptions allows overriding engine default options on a per-query basis.
87+
type QueryOptions struct {
88+
// LookbackDelta overrides the engine's default lookback delta for this query.
89+
LookbackDelta time.Duration
9290

93-
// DecodingConcurrency can be used to override the DecodingConcurrency engine setting.
91+
// EnablePerStepStats enables per-step statistics for this query.
92+
EnablePerStepStats bool
93+
94+
// DecodingConcurrency overrides the engine's default decoding concurrency for this query.
9495
DecodingConcurrency int
9596

96-
// SelectorBatchSize can be used to override the SelectorBatchSize engine setting.
97+
// SelectorBatchSize overrides the engine's default selector batch size for this query.
9798
SelectorBatchSize int64
9899

99-
// LogicalOptimizers can be used to override the LogicalOptimizers engine setting.
100+
// LogicalOptimizers overrides the engine's default logical optimizers for this query.
100101
LogicalOptimizers []logicalplan.Optimizer
101102
}
102103

103-
func (opts QueryOpts) LookbackDelta() time.Duration { return opts.LookbackDeltaParam }
104-
func (opts QueryOpts) EnablePerStepStats() bool { return opts.EnablePerStepStatsParam }
105-
106-
func fromPromQLOpts(opts promql.QueryOpts) *QueryOpts {
104+
func fromPromQLOpts(opts promql.QueryOpts) *QueryOptions {
107105
if opts == nil {
108-
return &QueryOpts{}
106+
return &QueryOptions{}
109107
}
110-
return &QueryOpts{
111-
LookbackDeltaParam: opts.LookbackDelta(),
112-
EnablePerStepStatsParam: opts.EnablePerStepStats(),
108+
return &QueryOptions{
109+
LookbackDelta: opts.LookbackDelta(),
110+
EnablePerStepStats: opts.EnablePerStepStats(),
113111
}
114112
}
115113

@@ -229,7 +227,7 @@ type Engine struct {
229227
noStepSubqueryIntervalFn func(time.Duration) time.Duration
230228
}
231229

232-
func (e *Engine) MakeInstantQuery(ctx context.Context, q storage.Queryable, opts *QueryOpts, qs string, ts time.Time) (promql.Query, error) {
230+
func (e *Engine) MakeInstantQuery(ctx context.Context, q storage.Queryable, opts *QueryOptions, qs string, ts time.Time) (promql.Query, error) {
233231
idx, err := e.activeQueryTracker.Insert(ctx, qs)
234232
if err != nil {
235233
return nil, err
@@ -284,7 +282,7 @@ func (e *Engine) MakeInstantQuery(ctx context.Context, q storage.Queryable, opts
284282
}, nil
285283
}
286284

287-
func (e *Engine) MakeInstantQueryFromPlan(ctx context.Context, q storage.Queryable, opts *QueryOpts, root logicalplan.Node, ts time.Time) (promql.Query, error) {
285+
func (e *Engine) MakeInstantQueryFromPlan(ctx context.Context, q storage.Queryable, opts *QueryOptions, root logicalplan.Node, ts time.Time) (promql.Query, error) {
288286
idx, err := e.activeQueryTracker.Insert(ctx, root.String())
289287
if err != nil {
290288
return nil, err
@@ -327,7 +325,7 @@ func (e *Engine) MakeInstantQueryFromPlan(ctx context.Context, q storage.Queryab
327325
}, nil
328326
}
329327

330-
func (e *Engine) MakeRangeQuery(ctx context.Context, q storage.Queryable, opts *QueryOpts, qs string, start, end time.Time, step time.Duration) (promql.Query, error) {
328+
func (e *Engine) MakeRangeQuery(ctx context.Context, q storage.Queryable, opts *QueryOptions, qs string, start, end time.Time, step time.Duration) (promql.Query, error) {
331329
idx, err := e.activeQueryTracker.Insert(ctx, qs)
332330
if err != nil {
333331
return nil, err
@@ -381,7 +379,7 @@ func (e *Engine) MakeRangeQuery(ctx context.Context, q storage.Queryable, opts *
381379
}, nil
382380
}
383381

384-
func (e *Engine) MakeRangeQueryFromPlan(ctx context.Context, q storage.Queryable, opts *QueryOpts, root logicalplan.Node, start, end time.Time, step time.Duration) (promql.Query, error) {
382+
func (e *Engine) MakeRangeQueryFromPlan(ctx context.Context, q storage.Queryable, opts *QueryOptions, root logicalplan.Node, start, end time.Time, step time.Duration) (promql.Query, error) {
385383
idx, err := e.activeQueryTracker.Insert(ctx, root.String())
386384
if err != nil {
387385
return nil, err
@@ -432,8 +430,8 @@ func (e *Engine) NewRangeQuery(ctx context.Context, q storage.Queryable, opts pr
432430
return e.MakeRangeQuery(ctx, q, fromPromQLOpts(opts), qs, start, end, step)
433431
}
434432

435-
func (e *Engine) makeQueryOpts(start time.Time, end time.Time, step time.Duration, opts *QueryOpts) *query.Options {
436-
res := &query.Options{
433+
func (e *Engine) makeQueryOpts(start time.Time, end time.Time, step time.Duration, opts *QueryOptions) *execopts.Options {
434+
res := &execopts.Options{
437435
Start: start,
438436
End: end,
439437
Step: step,
@@ -449,11 +447,11 @@ func (e *Engine) makeQueryOpts(start time.Time, end time.Time, step time.Duratio
449447
return res
450448
}
451449

452-
if opts.LookbackDelta() > 0 {
453-
res.LookbackDelta = opts.LookbackDelta()
450+
if opts.LookbackDelta > 0 {
451+
res.LookbackDelta = opts.LookbackDelta
454452
}
455-
if opts.EnablePerStepStats() {
456-
res.EnablePerStepStats = opts.EnablePerStepStats()
453+
if opts.EnablePerStepStats {
454+
res.EnablePerStepStats = opts.EnablePerStepStats
457455
}
458456

459457
if opts.DecodingConcurrency != 0 {
@@ -463,21 +461,21 @@ func (e *Engine) makeQueryOpts(start time.Time, end time.Time, step time.Duratio
463461
return res
464462
}
465463

466-
func (e *Engine) getLogicalOptimizers(opts *QueryOpts) []logicalplan.Optimizer {
464+
func (e *Engine) getLogicalOptimizers(opts *QueryOptions) []logicalplan.Optimizer {
467465
var optimizers []logicalplan.Optimizer
468-
if len(opts.LogicalOptimizers) != 0 {
466+
if opts != nil && len(opts.LogicalOptimizers) != 0 {
469467
optimizers = slices.Clone(opts.LogicalOptimizers)
470468
} else {
471469
optimizers = slices.Clone(e.logicalOptimizers)
472470
}
473471
selectorBatchSize := e.selectorBatchSize
474-
if opts.SelectorBatchSize != 0 {
472+
if opts != nil && opts.SelectorBatchSize != 0 {
475473
selectorBatchSize = opts.SelectorBatchSize
476474
}
477475
return append(optimizers, logicalplan.SelectorBatchSize{Size: selectorBatchSize})
478476
}
479477

480-
func (e *Engine) storageScanners(queryable storage.Queryable, qOpts *query.Options, lplan logicalplan.Plan) (engstorage.Scanners, error) {
478+
func (e *Engine) storageScanners(queryable storage.Queryable, qOpts *execopts.Options, lplan logicalplan.Plan) (engstorage.Scanners, error) {
481479
if e.scanners == nil {
482480
return promstorage.NewPrometheusScanners(queryable, qOpts, lplan)
483481
}
@@ -486,7 +484,7 @@ func (e *Engine) storageScanners(queryable storage.Queryable, qOpts *query.Optio
486484

487485
type Query struct {
488486
exec model.VectorOperator
489-
opts *query.Options
487+
opts *execopts.Options
490488
}
491489

492490
// Explain returns human-readable explanation of the created executor.

engine/engine_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222
"time"
2323

2424
"github.com/thanos-io/promql-engine/engine"
25+
"github.com/thanos-io/promql-engine/execution/execopts"
2526
"github.com/thanos-io/promql-engine/execution/model"
2627
"github.com/thanos-io/promql-engine/extlabels"
2728
"github.com/thanos-io/promql-engine/logicalplan"
28-
"github.com/thanos-io/promql-engine/query"
2929
"github.com/thanos-io/promql-engine/storage/prometheus"
3030
"github.com/thanos-io/promql-engine/warnings"
3131

@@ -2482,7 +2482,7 @@ type scannersWithWarns struct {
24822482
promScanners *prometheus.Scanners
24832483
}
24842484

2485-
func newScannersWithWarns(warn error, qOpts *query.Options, lplan logicalplan.Plan) (*scannersWithWarns, error) {
2485+
func newScannersWithWarns(warn error, qOpts *execopts.Options, lplan logicalplan.Plan) (*scannersWithWarns, error) {
24862486
scanners, err := prometheus.NewPrometheusScanners(&storage.MockQueryable{
24872487
MockQuerier: storage.NoopQuerier(),
24882488
}, qOpts, lplan)
@@ -2497,12 +2497,12 @@ func newScannersWithWarns(warn error, qOpts *query.Options, lplan logicalplan.Pl
24972497

24982498
func (s *scannersWithWarns) Close() error { return nil }
24992499

2500-
func (s scannersWithWarns) NewVectorSelector(ctx context.Context, opts *query.Options, hints storage.SelectHints, selector logicalplan.VectorSelector) (model.VectorOperator, error) {
2500+
func (s scannersWithWarns) NewVectorSelector(ctx context.Context, opts *execopts.Options, hints storage.SelectHints, selector logicalplan.VectorSelector) (model.VectorOperator, error) {
25012501
warnings.AddToContext(s.warn, ctx)
25022502
return s.promScanners.NewVectorSelector(ctx, opts, hints, selector)
25032503
}
25042504

2505-
func (s scannersWithWarns) NewMatrixSelector(ctx context.Context, opts *query.Options, hints storage.SelectHints, selector logicalplan.MatrixSelector, call logicalplan.FunctionCall) (model.VectorOperator, error) {
2505+
func (s scannersWithWarns) NewMatrixSelector(ctx context.Context, opts *execopts.Options, hints storage.SelectHints, selector logicalplan.MatrixSelector, call logicalplan.FunctionCall) (model.VectorOperator, error) {
25062506
warnings.AddToContext(s.warn, ctx)
25072507
return s.promScanners.NewMatrixSelector(ctx, opts, hints, selector, call)
25082508
}
@@ -2513,7 +2513,7 @@ func TestWarningsPlanCreation(t *testing.T) {
25132513
expectedWarn = errors.New("test warning")
25142514
)
25152515

2516-
scnrs, err := newScannersWithWarns(expectedWarn, &query.Options{}, nil)
2516+
scnrs, err := newScannersWithWarns(expectedWarn, &execopts.Options{}, nil)
25172517
testutil.Ok(t, err)
25182518
newEngine := engine.NewWithScanners(opts, scnrs)
25192519
q1, err := newEngine.NewRangeQuery(context.Background(), nil, nil, "http_requests_total", time.UnixMilli(0), time.UnixMilli(600), 30*time.Second)

engine/projection_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func TestProjectionWithFuzz(t *testing.T) {
242242
Queryable: storage,
243243
}
244244

245-
normalQuery, err := normalEngine.NewInstantQuery(ctx, storage, &engine.QueryOpts{}, query, queryTime)
245+
normalQuery, err := normalEngine.NewInstantQuery(ctx, storage, nil, query, queryTime)
246246
testutil.Ok(t, err)
247247
defer normalQuery.Close()
248248
normalResult := normalQuery.Exec(ctx)
@@ -252,7 +252,7 @@ func TestProjectionWithFuzz(t *testing.T) {
252252
}
253253
testutil.Ok(t, normalResult.Err, "query: %s", query)
254254

255-
projectionQuery, err := projectionEngine.MakeInstantQuery(ctx, projectionStorage, &engine.QueryOpts{}, query, queryTime)
255+
projectionQuery, err := projectionEngine.MakeInstantQuery(ctx, projectionStorage, nil, query, queryTime)
256256
testutil.Ok(t, err)
257257

258258
defer projectionQuery.Close()

engine/propagate_selector_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestPropagateMatchers(t *testing.T) {
120120
}
121121

122122
t.Run(fmt.Sprintf("Query_%d", i), func(t *testing.T) {
123-
normalQuery, err := normalEngine.NewInstantQuery(ctx, storage, &engine.QueryOpts{}, query, queryTime)
123+
normalQuery, err := normalEngine.NewInstantQuery(ctx, storage, nil, query, queryTime)
124124
testutil.Ok(t, err)
125125
defer normalQuery.Close()
126126
normalResult := normalQuery.Exec(ctx)
@@ -130,7 +130,7 @@ func TestPropagateMatchers(t *testing.T) {
130130
}
131131
testutil.Ok(t, normalResult.Err, "query: %s", query)
132132

133-
optimizedQuery, err := optimizedEngine.MakeInstantQuery(ctx, storage, &engine.QueryOpts{}, query, queryTime)
133+
optimizedQuery, err := optimizedEngine.MakeInstantQuery(ctx, storage, nil, query, queryTime)
134134
testutil.Ok(t, err)
135135

136136
defer optimizedQuery.Close()

engine/user_defined_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"time"
1111

1212
"github.com/thanos-io/promql-engine/engine"
13+
"github.com/thanos-io/promql-engine/execution/execopts"
1314
"github.com/thanos-io/promql-engine/execution/model"
1415
"github.com/thanos-io/promql-engine/logicalplan"
15-
"github.com/thanos-io/promql-engine/query"
1616

1717
"github.com/efficientgo/core/testutil"
1818
"github.com/prometheus/prometheus/model/labels"
@@ -60,7 +60,7 @@ load 30s
6060

6161
type injectVectorSelector struct{}
6262

63-
func (i injectVectorSelector) Optimize(plan logicalplan.Node, _ *query.Options) (logicalplan.Node, annotations.Annotations) {
63+
func (i injectVectorSelector) Optimize(plan logicalplan.Node, _ *execopts.Options) (logicalplan.Node, annotations.Annotations) {
6464
logicalplan.TraverseBottomUp(nil, &plan, func(_, current *logicalplan.Node) bool {
6565
switch t := (*current).(type) {
6666
case *logicalplan.VectorSelector:
@@ -77,7 +77,7 @@ type logicalVectorSelector struct {
7777
*logicalplan.VectorSelector
7878
}
7979

80-
func (c logicalVectorSelector) MakeExecutionOperator(_ context.Context, vectors *model.VectorPool, opts *query.Options, _ storage.SelectHints) (model.VectorOperator, error) {
80+
func (c logicalVectorSelector) MakeExecutionOperator(_ context.Context, vectors *model.VectorPool, opts *execopts.Options, _ storage.SelectHints) (model.VectorOperator, error) {
8181
oper := &vectorSelectorOperator{
8282
stepsBatch: opts.StepsBatch,
8383
vectors: vectors,

execution/aggregate/count_values.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"strconv"
1111
"sync"
1212

13+
"github.com/thanos-io/promql-engine/execution/execopts"
1314
"github.com/thanos-io/promql-engine/execution/model"
1415
"github.com/thanos-io/promql-engine/execution/telemetry"
15-
"github.com/thanos-io/promql-engine/query"
1616

1717
"github.com/efficientgo/core/errors"
1818
prommodel "github.com/prometheus/common/model"
@@ -37,7 +37,7 @@ type countValuesOperator struct {
3737
once sync.Once
3838
}
3939

40-
func NewCountValues(pool *model.VectorPool, next model.VectorOperator, param string, by bool, grouping []string, opts *query.Options) model.VectorOperator {
40+
func NewCountValues(pool *model.VectorPool, next model.VectorOperator, param string, by bool, grouping []string, opts *execopts.Options) model.VectorOperator {
4141
// Grouping labels need to be sorted in order for metric hashing to work.
4242
// https://github.com/prometheus/prometheus/blob/8ed39fdab1ead382a354e45ded999eb3610f8d5f/model/labels/labels.go#L162-L181
4343
slices.Sort(grouping)

execution/aggregate/hashaggregate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"math"
1010
"sync"
1111

12+
"github.com/thanos-io/promql-engine/execution/execopts"
1213
"github.com/thanos-io/promql-engine/execution/model"
1314
"github.com/thanos-io/promql-engine/execution/parse"
1415
"github.com/thanos-io/promql-engine/execution/telemetry"
15-
"github.com/thanos-io/promql-engine/query"
1616
"github.com/thanos-io/promql-engine/warnings"
1717

1818
"github.com/efficientgo/core/errors"
@@ -49,7 +49,7 @@ func NewHashAggregate(
4949
aggregation parser.ItemType,
5050
by bool,
5151
labels []string,
52-
opts *query.Options,
52+
opts *execopts.Options,
5353
) (model.VectorOperator, error) {
5454
// Verify that the aggregation is supported.
5555
if _, err := newScalarAccumulator(aggregation); err != nil {

execution/aggregate/khashaggregate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"sort"
1212
"sync"
1313

14+
"github.com/thanos-io/promql-engine/execution/execopts"
1415
"github.com/thanos-io/promql-engine/execution/model"
1516
"github.com/thanos-io/promql-engine/execution/telemetry"
16-
"github.com/thanos-io/promql-engine/query"
1717
"github.com/thanos-io/promql-engine/warnings"
1818

1919
"github.com/efficientgo/core/errors"
@@ -51,7 +51,7 @@ func NewKHashAggregate(
5151
aggregation parser.ItemType,
5252
by bool,
5353
labels []string,
54-
opts *query.Options,
54+
opts *execopts.Options,
5555
) (model.VectorOperator, error) {
5656
var compare func(float64, float64) bool
5757

execution/binary/scalar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"fmt"
99
"sync"
1010

11+
"github.com/thanos-io/promql-engine/execution/execopts"
1112
"github.com/thanos-io/promql-engine/execution/model"
1213
"github.com/thanos-io/promql-engine/execution/telemetry"
1314
"github.com/thanos-io/promql-engine/extlabels"
14-
"github.com/thanos-io/promql-engine/query"
1515
"github.com/thanos-io/promql-engine/warnings"
1616

1717
"github.com/prometheus/prometheus/model/histogram"
@@ -44,7 +44,7 @@ func NewScalar(
4444
rhsType parser.ValueType,
4545
opType parser.ItemType,
4646
returnBool bool,
47-
opts *query.Options,
47+
opts *execopts.Options,
4848
) (model.VectorOperator, error) {
4949
op := &scalarOperator{
5050
pool: pool,

execution/binary/vector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"fmt"
99
"sync"
1010

11+
"github.com/thanos-io/promql-engine/execution/execopts"
1112
"github.com/thanos-io/promql-engine/execution/model"
1213
"github.com/thanos-io/promql-engine/execution/telemetry"
1314
"github.com/thanos-io/promql-engine/extlabels"
14-
"github.com/thanos-io/promql-engine/query"
1515
"github.com/thanos-io/promql-engine/warnings"
1616

1717
"github.com/cespare/xxhash/v2"
@@ -63,7 +63,7 @@ func NewVectorOperator(
6363
matching *parser.VectorMatching,
6464
opType parser.ItemType,
6565
returnBool bool,
66-
opts *query.Options,
66+
opts *execopts.Options,
6767
) (model.VectorOperator, error) {
6868
op := &vectorOperator{
6969
pool: pool,

0 commit comments

Comments
 (0)