fix: allow multiple spaces between a filter expression (#1897)

* fix: allow multiple spaces between a filter expression

* fix: regex updated to respect spaces between a search string


Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
This commit is contained in:
Nityananda Gohain 2022-12-26 15:08:43 +05:30 committed by GitHub
parent dbba8b5b55
commit 9c80ba6b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -36,7 +36,7 @@ const (
DESC = "desc" DESC = "desc"
) )
var tokenRegex, _ = regexp.Compile(`(?i)(and( )*?|or( )*?)?(([\w.-]+ (in|nin) \([^(]+\))|([\w.]+ (gt|lt|gte|lte) (')?[\S]+(')?)|([\w.]+ (contains|ncontains)) [^\\]?'(.*?[^\\])')`) var tokenRegex, _ = regexp.Compile(`(?i)(and( )*?|or( )*?)?(([\w.-]+( )+(in|nin)( )+\([^(]+\))|([\w.]+( )+(gt|lt|gte|lte)( )+(')?[\S]+(')?)|([\w.]+( )+(contains|ncontains))( )+[^\\]?'(.*?[^\\])')`)
var operatorRegex, _ = regexp.Compile(`(?i)(?: )(in|nin|gt|lt|gte|lte|contains|ncontains)(?: )`) var operatorRegex, _ = regexp.Compile(`(?i)(?: )(in|nin|gt|lt|gte|lte|contains|ncontains)(?: )`)
func ParseLogFilterParams(r *http.Request) (*model.LogsFilterParams, error) { func ParseLogFilterParams(r *http.Request) (*model.LogsFilterParams, error) {
@ -152,6 +152,7 @@ func ParseLogAggregateParams(r *http.Request) (*model.LogsAggregateParams, error
func parseLogQuery(query string) ([]string, error) { func parseLogQuery(query string) ([]string, error) {
sqlQueryTokens := []string{} sqlQueryTokens := []string{}
filterTokens := tokenRegex.FindAllString(query, -1) filterTokens := tokenRegex.FindAllString(query, -1)
if len(filterTokens) == 0 { if len(filterTokens) == 0 {
@ -190,7 +191,13 @@ func parseLogQuery(query string) ([]string, error) {
sqlQueryTokens = append(sqlQueryTokens, f) sqlQueryTokens = append(sqlQueryTokens, f)
} else { } else {
symbol := operatorMapping[strings.ToLower(op)] symbol := operatorMapping[strings.ToLower(op)]
sqlQueryTokens = append(sqlQueryTokens, strings.Replace(v, " "+op+" ", " "+symbol+" ", 1)+" ") sqlExpr := strings.Replace(v, " "+op+" ", " "+symbol+" ", 1)
splittedExpr := strings.Split(sqlExpr, symbol)
if len(splittedExpr) != 2 {
return nil, fmt.Errorf("error while splitting expression: %s", sqlExpr)
}
trimmedSqlExpr := fmt.Sprintf("%s %s %s ", strings.Join(strings.Fields(splittedExpr[0]), " "), symbol, strings.TrimSpace(splittedExpr[1]))
sqlQueryTokens = append(sqlQueryTokens, trimmedSqlExpr)
} }
} }

View File

@ -80,7 +80,17 @@ var correctQueriesTest = []struct {
{ {
`filters with extra spaces`, `filters with extra spaces`,
`service IN ('name > 100') AND length gt 100`, `service IN ('name > 100') AND length gt 100`,
[]string{`service IN ('name > 100') `, `AND length > 100 `}, []string{`service IN ('name > 100') `, `AND length > 100 `},
},
{
`Extra space within a filter expression`,
`service IN ('name > 100')`,
[]string{`service IN ('name > 100') `},
},
{
`Extra space between a query filter`,
`data contains 'hello world .'`,
[]string{`data ILIKE '%hello world .%' `},
}, },
{ {
`filters with special characters in key name`, `filters with special characters in key name`,