Skip to content

Commit

Permalink
plan, engine: get rid of trim sorts optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaHoffmann committed Nov 25, 2023
1 parent fc12861 commit 9702bcd
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 101 deletions.
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (o Opts) getLogicalOptimizers() []logicalplan.Optimizer {
optimizers = make([]logicalplan.Optimizer, len(o.LogicalOptimizers))
copy(optimizers, o.LogicalOptimizers)
}
return append(optimizers, logicalplan.TrimSortFunctions{})
return optimizers
}

type remoteEngine struct {
Expand Down
10 changes: 5 additions & 5 deletions logicalplan/distribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,22 @@ max by (pod) (
name: "unsupported aggregation in the operand path",
expr: `max by (pod) (sort(quantile(0.9, http_requests_total)))`,
expected: `
max by (pod) (sort(quantile(0.9,
max by (pod) (quantile(0.9,
dedup(
remote(http_requests_total),
remote(http_requests_total)
)
)))`,
))`,
},
{
name: "binary operation in the operand path",
expr: `max by (pod) (sort(metric_a / metric_b))`,
expr: `max by (pod) (metric_a / metric_b)`,
expected: `
max by (pod) (sort(
max by (pod) (
dedup(remote(metric_a), remote(metric_a))
/
dedup(remote(metric_b), remote(metric_b))
))`,
)`,
},
{
name: "binary operation with aggregations",
Expand Down
20 changes: 20 additions & 0 deletions logicalplan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func New(expr parser.Expr, opts *query.Options) Plan {
setOffsetForAtModifier(opts.Start.UnixMilli(), expr)
setOffsetForInnerSubqueries(expr, opts)

// the engine handles sorting at the presentation layer
expr = trimSorts(expr)

return &plan{
expr: expr,
opts: opts,
Expand Down Expand Up @@ -143,6 +146,23 @@ func TraverseBottomUp(parent *parser.Expr, current *parser.Expr, transform func(
return true
}

func trimSorts(expr parser.Expr) parser.Expr {
TraverseBottomUp(nil, &expr, func(parent, current *parser.Expr) bool {
if current == nil || parent == nil {
return true
}
switch e := (*parent).(type) {
case *parser.Call:
switch e.Func.Name {
case "sort", "sort_desc":
*parent = *current
}
}
return false
})
return expr
}

// preprocessExpr wraps all possible step invariant parts of the given expression with
// StepInvariantExpr. It also resolves the preprocessors.
// Copied from Prometheus and adjusted to work with the vendored parser:
Expand Down
44 changes: 44 additions & 0 deletions logicalplan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,50 @@ func TestMatcherPropagation(t *testing.T) {
}
}

func TestTrimSorts(t *testing.T) {
cases := []struct {
name string
expr string
expected string
}{
// this test case is ok since the engine determines sorting order
// before running optimziers
{
name: "simple sort",
expr: "sort(X)",
expected: "X",
},
{
name: "sort",
expr: "sum(sort(X))",
expected: "sum(X)",
},
{
name: "nested",
expr: "sum(sort(rate(X[1m])))",
expected: "sum(rate(X[1m]))",
},
{
name: "weirdly nested",
expr: "sum(sort(sqrt(sort(X))))",
expected: "sum(sqrt(X))",
},
{
name: "sort in binary expression",
expr: "sort(sort(sqrt(X))/sort(sqrt(Y)))",
expected: "sqrt(X) / sqrt(Y)",
},
}
for _, tcase := range cases {
t.Run(tcase.name, func(t *testing.T) {
expr, err := parser.ParseExpr(tcase.expr)
testutil.Ok(t, err)

testutil.Equals(t, tcase.expected, trimSorts(expr).String())
})
}
}

func cleanUp(replacements map[string]*regexp.Regexp, expr string) string {
for replacement, match := range replacements {
expr = match.ReplaceAllString(expr, replacement)
Expand Down
34 changes: 0 additions & 34 deletions logicalplan/trim_sorts.go

This file was deleted.

61 changes: 0 additions & 61 deletions logicalplan/trim_sorts_test.go

This file was deleted.

0 comments on commit 9702bcd

Please sign in to comment.