From 4b53c73189676385a8aa51044d90a50494e8d41b Mon Sep 17 00:00:00 2001 From: yincong Date: Fri, 12 Jul 2024 13:48:14 +0800 Subject: [PATCH 1/5] ast left cycular reference result in oom --- pkg/logql/syntax/ast.go | 10 ++++++++-- pkg/logql/syntax/ast_test.go | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pkg/logql/syntax/ast.go b/pkg/logql/syntax/ast.go index e5e80b4d0c172..d429db67b24b9 100644 --- a/pkg/logql/syntax/ast.go +++ b/pkg/logql/syntax/ast.go @@ -185,16 +185,22 @@ func (m MultiStageExpr) reorderStages() []StageExpr { func combineFilters(in []*LineFilterExpr) StageExpr { result := in[len(in)-1] for i := len(in) - 2; i >= 0; i-- { - leafNode(result).Left = in[i] + leaf := leafNode(result, in[i]) + if leaf != nil { + leaf = in[i] + } } return result } -func leafNode(in *LineFilterExpr) *LineFilterExpr { +func leafNode(in *LineFilterExpr, child *LineFilterExpr) *LineFilterExpr { current := in //nolint:revive for ; current.Left != nil; current = current.Left { + if current == child { + return nil + } } return current } diff --git a/pkg/logql/syntax/ast_test.go b/pkg/logql/syntax/ast_test.go index d75ff2d0261b6..f508b3821a55c 100644 --- a/pkg/logql/syntax/ast_test.go +++ b/pkg/logql/syntax/ast_test.go @@ -1076,3 +1076,24 @@ func TestGroupingString(t *testing.T) { } require.Equal(t, " without ()", g.String()) } + +func TestCombineFilters(t *testing.T) { + in := []*LineFilterExpr{ + {LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "test1"}}, + {LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "tes2"}}, + } + + var combineFilter StageExpr + for i := 0; i < 2; i++ { + combineFilter = combineFilters(in) + } + + current := combineFilter.(*LineFilterExpr) + i := 0 + for ; current.Left != nil; current = current.Left { + i++ + if i > 2 { + t.Fatalf("left num isn't a correct number") + } + } +} From af6fde6838e419040ee32fb3e3094f2356b5682f Mon Sep 17 00:00:00 2001 From: yincong Date: Fri, 12 Jul 2024 13:58:50 +0800 Subject: [PATCH 2/5] update unit test --- pkg/logql/syntax/ast_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/logql/syntax/ast_test.go b/pkg/logql/syntax/ast_test.go index f508b3821a55c..195bea52ee2e6 100644 --- a/pkg/logql/syntax/ast_test.go +++ b/pkg/logql/syntax/ast_test.go @@ -1080,7 +1080,7 @@ func TestGroupingString(t *testing.T) { func TestCombineFilters(t *testing.T) { in := []*LineFilterExpr{ {LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "test1"}}, - {LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "tes2"}}, + {LineFilter: LineFilter{Ty: log.LineMatchEqual, Match: "test2"}}, } var combineFilter StageExpr From 6f91a1e27e280c8142bb8dfc559df97b8042eefd Mon Sep 17 00:00:00 2001 From: yincong Date: Fri, 12 Jul 2024 14:29:10 +0800 Subject: [PATCH 3/5] ast tree --- pkg/logql/syntax/ast.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/logql/syntax/ast.go b/pkg/logql/syntax/ast.go index d429db67b24b9..efcc8e1da51e7 100644 --- a/pkg/logql/syntax/ast.go +++ b/pkg/logql/syntax/ast.go @@ -187,7 +187,7 @@ func combineFilters(in []*LineFilterExpr) StageExpr { for i := len(in) - 2; i >= 0; i-- { leaf := leafNode(result, in[i]) if leaf != nil { - leaf = in[i] + leaf.Left = in[i] } } From 8c8b28f4dc681b96e02a5d4786ae4610d4f7b0c0 Mon Sep 17 00:00:00 2001 From: yincong Date: Fri, 12 Jul 2024 14:47:55 +0800 Subject: [PATCH 4/5] fix ast bug --- pkg/logql/syntax/ast.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/logql/syntax/ast.go b/pkg/logql/syntax/ast.go index efcc8e1da51e7..12c29d8616ed0 100644 --- a/pkg/logql/syntax/ast.go +++ b/pkg/logql/syntax/ast.go @@ -3,6 +3,7 @@ package syntax import ( "fmt" "math" + "reflect" "regexp" "strconv" "strings" @@ -198,7 +199,7 @@ func leafNode(in *LineFilterExpr, child *LineFilterExpr) *LineFilterExpr { current := in //nolint:revive for ; current.Left != nil; current = current.Left { - if current == child { + if reflect.DeepEqual(current.Left, child) { return nil } } From 5f40316d9d17532c319030252fb12faaf472fd44 Mon Sep 17 00:00:00 2001 From: yincong Date: Sat, 13 Jul 2024 09:09:12 +0800 Subject: [PATCH 5/5] remove reflect deepequal --- pkg/logql/syntax/ast.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/logql/syntax/ast.go b/pkg/logql/syntax/ast.go index 12c29d8616ed0..14377f2515cb5 100644 --- a/pkg/logql/syntax/ast.go +++ b/pkg/logql/syntax/ast.go @@ -3,7 +3,6 @@ package syntax import ( "fmt" "math" - "reflect" "regexp" "strconv" "strings" @@ -199,7 +198,7 @@ func leafNode(in *LineFilterExpr, child *LineFilterExpr) *LineFilterExpr { current := in //nolint:revive for ; current.Left != nil; current = current.Left { - if reflect.DeepEqual(current.Left, child) { + if current == child || current.Left == child { return nil } }