Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plan, engine: get rid of trim sorts optimizer #332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
yeya24 marked this conversation as resolved.
Show resolved Hide resolved

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.

Loading