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

fix(orFilters): fix multiple or filters would get wrong filtertype #13169

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 7 additions & 8 deletions pkg/logql/syntax/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,6 @@ func newLineFilterExpr(ty log.LineMatchType, op, match string) *LineFilterExpr {
func newOrLineFilter(left, right *LineFilterExpr) *LineFilterExpr {
right.Ty = left.Ty

if left.Ty == log.LineMatchEqual || left.Ty == log.LineMatchRegexp || left.Ty == log.LineMatchPattern {
left.Or = right
right.IsOrChild = true
return left
}

// !(left or right) == (!left and !right).

// NOTE: Consider, we have chain of "or", != "foo" or "bar" or "baz"
// we parse from right to left, so first time left="bar", right="baz", and we don't know the actual `Ty` (equal: |=, notequal: !=, regex: |~, etc). So
// it will have default (0, LineMatchEqual).
Expand All @@ -385,6 +377,13 @@ func newOrLineFilter(left, right *LineFilterExpr) *LineFilterExpr {
tmp = tmp.Or
}

if left.Ty == log.LineMatchEqual || left.Ty == log.LineMatchRegexp || left.Ty == log.LineMatchPattern {
left.Or = right
right.IsOrChild = true
return left
}

// !(left or right) == (!left and !right).
return newNestedLineFilterExpr(left, right)
}

Expand Down
23 changes: 21 additions & 2 deletions pkg/logql/syntax/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,20 @@ func Test_FilterMatcher(t *testing.T) {
mustNewMatcher(labels.MatchEqual, "app", "foo"),
},
[]linecheck{{"foo", false}, {"bar", true}, {"127.0.0.2", true}, {"127.0.0.1", false}},
},
{
`{app="foo"} |> "<_>foo<_>" or "<_>bar<_>"`,
[]*labels.Matcher{
mustNewMatcher(labels.MatchEqual, "app", "foo"),
},
[]linecheck{{"test foo test", true}, {"test bar test", true}, {"none", false}},
},
{
`{app="foo"} |> "foo" or "bar"`,
`{app="foo"} |> "<_>foo<_>" or "<_>bar<_>" or "<_>baz<_>"`,
[]*labels.Matcher{
mustNewMatcher(labels.MatchEqual, "app", "foo"),
},
[]linecheck{{"foo", true}, {"bar", true}, {"none", false}},
[]linecheck{{"test foo test", true}, {"test bar test", true}, {"test baz test", true}, {"none", false}},
},
{
`{app="foo"} !> "foo" or "bar"`,
Expand Down Expand Up @@ -618,6 +625,18 @@ func TestOrLineFilterTypes(t *testing.T) {

_ = newOrLineFilter(left, right)
require.Equal(t, tt.ty, right.Ty)
require.Equal(t, tt.ty, left.Ty)
})

t.Run("right inherits left's type with multiple or filters", func(t *testing.T) {
f1 := &LineFilterExpr{LineFilter: LineFilter{Ty: tt.ty, Match: "something"}}
f2 := &LineFilterExpr{LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "something"}}
f3 := &LineFilterExpr{LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "something"}}

_ = newOrLineFilter(f1, newOrLineFilter(f2, f3))
require.Equal(t, tt.ty, f1.Ty)
require.Equal(t, tt.ty, f2.Ty)
require.Equal(t, tt.ty, f3.Ty)
})
}
}
Expand Down
Loading