@@ -5,73 +5,109 @@ import (
55 "regexp/syntax"
66 "strings"
77
8+ log_parser "github.com/metrico/qryn/v4/reader/logql/logql_parser"
89 "github.com/metrico/qryn/v4/reader/logql/logql_transpiler/shared"
910 sql "github.com/metrico/qryn/v4/reader/utils/sql_select"
1011)
1112
1213type LineFilterPlanner struct {
13- Op string
14- Val string
15- Main shared.SQLRequestPlanner
14+ Filter * log_parser.LineFilter
15+ Main shared.SQLRequestPlanner
1616}
1717
1818func (l * LineFilterPlanner ) Process (ctx * shared.PlannerContext ) (sql.ISelect , error ) {
1919 req , err := l .Main .Process (ctx )
2020 if err != nil {
2121 return nil , err
2222 }
23+ clause , err := l .buildExpCondition (l .Filter .Fn , & l .Filter .Exp )
24+ if err != nil {
25+ return nil , err
26+ }
27+ return req .AndWhere (clause ), nil
28+ }
2329
24- var clause sql.SQLCondition
25- switch l .Op {
30+ func (l * LineFilterPlanner ) buildExpCondition (fn string , exp * log_parser.LineFilterExp ) (sql.SQLCondition , error ) {
31+ head , err := l .buildHeadCondition (fn , & exp .Head )
32+ if err != nil {
33+ return nil , err
34+ }
35+ if exp .Op == "" {
36+ return head , nil
37+ }
38+ tail , err := l .buildExpCondition (fn , exp .Tail )
39+ if err != nil {
40+ return nil , err
41+ }
42+ if exp .Op == "or" {
43+ return sql .Or (head , tail ), nil
44+ }
45+ return sql .And (head , tail ), nil
46+ }
47+
48+ func (l * LineFilterPlanner ) buildHeadCondition (fn string , head * log_parser.LineFilterHead ) (sql.SQLCondition , error ) {
49+ if head .Complex != nil {
50+ return l .buildExpCondition (fn , head .Complex )
51+ }
52+ return l .buildSimpleCondition (fn , head .Simple )
53+ }
54+
55+ func (l * LineFilterPlanner ) buildSimpleCondition (fn string , s * log_parser.LineFilterSimple ) (sql.SQLCondition , error ) {
56+ val , err := s .Val .Unquote ()
57+ if err != nil {
58+ return nil , err
59+ }
60+ tmp := & lineFilterOps {val : val }
61+ switch fn {
2662 case "|=" :
27- clause , err = l .doLike ("like" )
63+ return tmp .doLike ("like" )
2864 case "!=" :
29- clause , err = l .doLike ("notLike" )
65+ return tmp .doLike ("notLike" )
3066 case "|~" :
31- likeStr , isInsensitive , isLike := l .re2Like ()
67+ likeStr , isInsensitive , isLike := tmp .re2Like ()
3268 if isLike {
33- l . Val = likeStr
69+ tmp . val = likeStr
3470 like := "like"
3571 if isInsensitive {
3672 like = "ilike"
3773 }
38- clause , err = l .doLike (like )
39- } else {
40- clause = sql .Eq (& SqlMatch {
41- col : sql .NewRawObject ("string" ),
42- pattern : l .Val ,
43- }, sql .NewIntVal (1 ))
74+ return tmp .doLike (like )
4475 }
76+ return sql .Eq (& SqlMatch {
77+ col : sql .NewRawObject ("string" ),
78+ pattern : val ,
79+ }, sql .NewIntVal (1 )), nil
4580 case "!~" :
46- likeStr , isInsensitive , isLike := l .re2Like ()
81+ likeStr , isInsensitive , isLike := tmp .re2Like ()
4782 if isLike {
48- l . Val = likeStr
83+ tmp . val = likeStr
4984 like := "notLike"
5085 if isInsensitive {
5186 like = "notILike"
5287 }
53- clause , err = l .doLike (like )
54- } else {
55- clause = sql .Eq (& SqlMatch {
56- col : sql .NewRawObject ("string" ),
57- pattern : l .Val ,
58- }, sql .NewIntVal (1 ))
88+ return tmp .doLike (like )
5989 }
90+ return sql .Eq (& SqlMatch {
91+ col : sql .NewRawObject ("string" ),
92+ pattern : val ,
93+ }, sql .NewIntVal (1 )), nil
6094 case "|>" :
61- likeOp := l .patternMatch (l .Val )
62- clause = sql .BinaryLogicalOp ("LIKE" , sql .NewRawObject ("string" ), sql .NewStringVal (likeOp ))
63- default :
64- err = & shared.NotSupportedError {Msg : fmt .Sprintf ("%s not supported" , l .Op )}
95+ likeOp := patternMatch (val )
96+ return sql .BinaryLogicalOp ("LIKE" , sql .NewRawObject ("string" ), sql .NewStringVal (likeOp )), nil
6597 }
98+ return nil , & shared.NotSupportedError {Msg : fmt .Sprintf ("%s not supported" , fn )}
99+ }
66100
67- if err != nil {
68- return nil , err
69- }
70- return req .AndWhere (clause ), nil
101+ // lineFilterOps holds per-value helpers (extracted from the old LineFilterPlanner methods).
102+ type lineFilterOps struct {
103+ val string
71104}
72105
73- func (l * LineFilterPlanner ) doLike (likeOp string ) (sql.SQLCondition , error ) {
74- enqVal , err := l .enquoteStr (l .Val )
106+ func (o * lineFilterOps ) doLike (likeOp string ) (sql.SQLCondition , error ) {
107+ enqVal , err := sql .NewStringVal (o .val ).String (& sql.Ctx {
108+ Params : map [string ]sql.SQLObject {},
109+ Result : map [string ]sql.SQLObject {},
110+ })
75111 if err != nil {
76112 return nil , err
77113 }
@@ -83,7 +119,18 @@ func (l *LineFilterPlanner) doLike(likeOp string) (sql.SQLCondition, error) {
83119 ), nil
84120}
85121
86- func (l * LineFilterPlanner ) patternMatch (match string ) string {
122+ func (o * lineFilterOps ) re2Like () (string , bool , bool ) {
123+ exp , err := syntax .Parse (o .val , syntax .PerlX )
124+ if err != nil {
125+ return "" , false , false
126+ }
127+ if exp .Op != syntax .OpLiteral || exp .Flags & ^ (syntax .PerlX | syntax .FoldCase ) != 0 {
128+ return "" , false , false
129+ }
130+ return string (exp .Rune ), exp .Flags & syntax .FoldCase != 0 , true
131+ }
132+
133+ func patternMatch (match string ) string {
87134 match = strings .Replace (match , "%" , "%%" , - 1 )
88135 parts := strings .Split (match , "<_>" )
89136 for i := len (parts ) - 2 ; i >= 0 ; i -- {
@@ -102,21 +149,3 @@ func (l *LineFilterPlanner) patternMatch(match string) string {
102149 }
103150 return strings .Join (parts , "%" )
104151}
105-
106- func (l * LineFilterPlanner ) enquoteStr (str string ) (string , error ) {
107- return sql .NewStringVal (str ).String (& sql.Ctx {
108- Params : map [string ]sql.SQLObject {},
109- Result : map [string ]sql.SQLObject {},
110- })
111- }
112-
113- func (l * LineFilterPlanner ) re2Like () (string , bool , bool ) {
114- exp , err := syntax .Parse (l .Val , syntax .PerlX )
115- if err != nil {
116- return "" , false , false
117- }
118- if exp .Op != syntax .OpLiteral || exp .Flags & ^ (syntax .PerlX | syntax .FoldCase ) != 0 {
119- return "" , false , false
120- }
121- return string (exp .Rune ), exp .Flags & syntax .FoldCase != 0 , true
122- }
0 commit comments