mirror of https://github.com/grafana/loki
chore(engine): Add utility function `canExecuteWithNewEngine` (#16892)
The function determines whether a query can be executed by the new execution engine. In the first step, the new execution engine will only be able to execute limited/filter queries without any transformation stages. Signed-off-by: Christian Haudum <christian.haudum@gmail.com>pull/16799/head^2
parent
810a93f3a1
commit
f79906ca75
@ -0,0 +1,25 @@ |
||||
package engine |
||||
|
||||
import "github.com/grafana/loki/v3/pkg/logql/syntax" |
||||
|
||||
// canExecuteWithNewEngine determines whether a query can be executed by the new execution engine.
|
||||
func canExecuteWithNewEngine(expr syntax.Expr) bool { |
||||
switch expr := expr.(type) { |
||||
case syntax.SampleExpr: |
||||
return false |
||||
case syntax.LogSelectorExpr: |
||||
ret := true |
||||
expr.Walk(func(e syntax.Expr) { |
||||
switch e.(type) { |
||||
case *syntax.LineParserExpr, *syntax.LogfmtParserExpr, *syntax.LogfmtExpressionParserExpr, *syntax.JSONExpressionParserExpr: |
||||
ret = false |
||||
case *syntax.LineFmtExpr, *syntax.LabelFmtExpr: |
||||
ret = false |
||||
case *syntax.KeepLabelsExpr, *syntax.DropLabelsExpr: |
||||
ret = false |
||||
} |
||||
}) |
||||
return ret |
||||
} |
||||
return false |
||||
} |
||||
@ -0,0 +1,87 @@ |
||||
package engine |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
|
||||
"github.com/grafana/loki/v3/pkg/logql/syntax" |
||||
) |
||||
|
||||
func TestCanExecuteWithNewEngine(t *testing.T) { |
||||
for _, tt := range []struct { |
||||
statement string |
||||
expected bool |
||||
}{ |
||||
{ |
||||
statement: `{env="prod"}`, |
||||
expected: true, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} |= "metrics.go"`, |
||||
expected: true, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} |= "metrics.go"`, |
||||
expected: true, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | tenant="loki"`, |
||||
expected: true, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | tenant="loki" != "foo"`, |
||||
expected: true, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | json`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | json foo="bar"`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | logfmt`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | logfmt foo="bar"`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | pattern "<_> foo=<foo> <_>"`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | regexp ".* foo=(?P<foo>.+) .*"`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | unpack`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} |= "metrics.go" | logfmt`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | line_format "{.cluster}"`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `{env="prod"} | label_format cluster="us"`, |
||||
expected: false, |
||||
}, |
||||
{ |
||||
statement: `sum(rate({env="prod"}[1m]))`, |
||||
expected: false, |
||||
}, |
||||
} { |
||||
t.Run(tt.statement, func(t *testing.T) { |
||||
expr := syntax.MustParseExpr(tt.statement) |
||||
canExecute := canExecuteWithNewEngine(expr) |
||||
require.Equal(t, tt.expected, canExecute) |
||||
}) |
||||
} |
||||
} |
||||
Loading…
Reference in new issue