From 6eb9389e81630dd26bb7775c201bd03d7188ab8d Mon Sep 17 00:00:00 2001 From: nityanandagohain Date: Mon, 1 Aug 2022 12:17:15 +0530 Subject: [PATCH] parser updated to include or as well --- pkg/query-service/app/logs/parser.go | 27 +++++++++++++++-------- pkg/query-service/app/logs/parser_test.go | 9 ++++++-- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/pkg/query-service/app/logs/parser.go b/pkg/query-service/app/logs/parser.go index ec1fb773d4..8b24c940ac 100644 --- a/pkg/query-service/app/logs/parser.go +++ b/pkg/query-service/app/logs/parser.go @@ -31,7 +31,7 @@ const ( IDEND = "idEnd" ) -var tokenRegex, _ = regexp.Compile(`(?i)(and( )*?)?(([\w.-]+ (in|nin) \([\S ]+\))|([\w.]+ (gt|lt|gte|lte) (')?[\S]+(')?)|([\w.]+ (contains|ncontains)) (')?[^']+(')?)`) +var tokenRegex, _ = regexp.Compile(`(?i)(and( )*?|or( )*?)?(([\w.-]+ (in|nin) \([\S ]+\))|([\w.]+ (gt|lt|gte|lte) (')?[\S]+(')?)|([\w.]+ (contains|ncontains)) (')?[^']+(')?)`) var operatorRegex, _ = regexp.Compile(`(?i)(?: )(in|nin|gt|lt|gte|lte|contains|ncontains)(?: )`) func ParseLogFilterParams(r *http.Request) (*model.LogsFilterParams, error) { @@ -259,33 +259,42 @@ func GenerateSQLWhere(allFields *model.GetFieldsResponse, params *model.LogsFilt return sqlWhere, err } + filterTokens := []string{} if params.TimestampStart != 0 { filter := fmt.Sprintf("timestamp >= '%d' ", params.TimestampStart) - if len(tokens) > 0 { + if len(filterTokens) > 0 { filter = "and " + filter } - tokens = append(tokens, filter) + filterTokens = append(filterTokens, filter) } if params.TimestampEnd != 0 { filter := fmt.Sprintf("timestamp <= '%d' ", params.TimestampEnd) - if len(tokens) > 0 { + if len(filterTokens) > 0 { filter = "and " + filter } - tokens = append(tokens, filter) + filterTokens = append(filterTokens, filter) } if params.IdStart != "" { filter := fmt.Sprintf("id > '%v' ", params.IdStart) - if len(tokens) > 0 { + if len(filterTokens) > 0 { filter = "and " + filter } - tokens = append(tokens, filter) + filterTokens = append(filterTokens, filter) } if params.IdEnd != "" { filter := fmt.Sprintf("id < '%v' ", params.IdEnd) - if len(tokens) > 0 { + if len(filterTokens) > 0 { filter = "and " + filter } - tokens = append(tokens, filter) + filterTokens = append(filterTokens, filter) + } + + if len(filterTokens) > 0 { + if len(tokens) > 0 { + tokens[0] = fmt.Sprintf("and %s", tokens[0]) + } + filterTokens = append(filterTokens, tokens...) + tokens = filterTokens } sqlWhere = strings.Join(tokens, "") diff --git a/pkg/query-service/app/logs/parser_test.go b/pkg/query-service/app/logs/parser_test.go index 517247122e..819f9d0c9e 100644 --- a/pkg/query-service/app/logs/parser_test.go +++ b/pkg/query-service/app/logs/parser_test.go @@ -42,6 +42,11 @@ var correctQueriesTest = []struct { `id lt 100 and id gt 50 and code lte 500 and code gte 400`, []string{`id < 100 `, `and id > 50 `, `and code <= 500 `, `and code >= 400 `}, }, + { + `filters with lt,gt,lte,gte operators seprated by OR`, + `id lt 100 or id gt 50 or code lte 500 or code gte 400`, + []string{`id < 100 `, `or id > 50 `, `or code <= 500 `, `or code >= 400 `}, + }, { `filter with number`, `status gte 200 AND FULLTEXT ncontains '"key"'`, @@ -216,8 +221,8 @@ func TestGenerateSQLQuery(t *testing.T) { tsEnd := uint64(1657689294000) idStart := "2BsKLKv8cZrLCn6rkOcRGkdjBdM" idEnd := "2BsKG6tRpFWjYMcWsAGKfSxoQdU" - sqlWhere := "id < 100 and id > 50 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] <= 500 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] >= 400 and timestamp >= '1657689292000' and timestamp <= '1657689294000' and id > '2BsKLKv8cZrLCn6rkOcRGkdjBdM' and id < '2BsKG6tRpFWjYMcWsAGKfSxoQdU' " - Convey("testInterestingFields", t, func() { + sqlWhere := "timestamp >= '1657689292000' and timestamp <= '1657689294000' and id > '2BsKLKv8cZrLCn6rkOcRGkdjBdM' and id < '2BsKG6tRpFWjYMcWsAGKfSxoQdU' and id < 100 and id > 50 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] <= 500 and attributes_int64_value[indexOf(attributes_int64_key, 'code')] >= 400 " + Convey("testGenerateSQL", t, func() { res, _ := GenerateSQLWhere(&allFields, &model.LogsFilterParams{Query: query, TimestampStart: tsStart, TimestampEnd: tsEnd, IdStart: idStart, IdEnd: idEnd}) So(res, ShouldEqual, sqlWhere) })