Skip to content

Commit

Permalink
Fix empty filter (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
trzysiek authored May 15, 2024
1 parent da6622c commit 7e1372f
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 2 deletions.
3 changes: 3 additions & 0 deletions quesma/queryparser/query_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ func (cw *ClickhouseQueryTranslator) parseQueryMap(queryMap QueryMap) SimpleQuer
logger.WarnWithCtxAndReason(cw.Ctx, logger.ReasonUnsupportedQuery(k)).Msgf("unsupported query type: %s, value: %v", k, v)
}
}
if len(queryMap) == 0 {
return newSimpleQuery(NewSimpleStatement(""), true)
}
return newSimpleQuery(NewSimpleStatement("can't parse query: "+pp.Sprint(queryMap)), false)
}

Expand Down
145 changes: 143 additions & 2 deletions quesma/testdata/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,97 @@ var TestsSearch = []SearchTestCase{
[]model.Query{newSimplestQuery()},
[]string{`SELECT "message" FROM "logs-generic-default" LIMIT 500`},
},
{ // [26]
"Empty must",
`
{
"query": {
"bool": {
"must": {}
}
}
}`,
[]string{``},
model.Normal,
[]model.Query{justSimplestWhere(``)},
[]string{qToStr(justSimplestWhere(``))},
},
{ // [27]
"Empty must not",
`
{
"query": {
"bool": {
"must_not": {}
}
}
}`,
[]string{``},
model.Normal,
[]model.Query{justSimplestWhere(``)},
[]string{qToStr(justSimplestWhere(``))},
},
{ // [28]
"Empty should",
`
{
"query": {
"bool": {
"should": {}
}
}
}`,
[]string{``},
model.Normal,
[]model.Query{justSimplestWhere(``)},
[]string{qToStr(justSimplestWhere(``))},
},
{ // [29]
"Empty all bools",
`
{
"query": {
"bool": {
"should": {},
"must": {},
"must_not": {},
"filter": {}
}
}
}`,
[]string{``},
model.Normal,
[]model.Query{justSimplestWhere(``)},
[]string{qToStr(justSimplestWhere(``))},
},
{ // [30]
"Some bools empty, some not",
`
{
"query": {
"bool": {
"should": [],
"must": {
"match_phrase": {
"message": "User logged out"
}
},
"must_not": {},
"filter": [
{
"match_phrase": {
"message": "User logged out"
}
}
]
}
}
}`,
[]string{`"message" iLIKE '%User logged out%' AND "message" iLIKE '%User logged out%'`},
model.Normal,
[]model.Query{justSimplestWhere(`"message" iLIKE '%User logged out%' AND "message" iLIKE '%User logged out%'`)},
[]string{qToStr(justSimplestWhere(`"message" iLIKE '%User logged out%' AND "message" iLIKE '%User logged out%'`))},
},
}

var TestsSearchNoAttrs = []SearchTestCase{
Expand Down Expand Up @@ -1834,7 +1925,7 @@ var TestsSearchNoAttrs = []SearchTestCase{
}

var TestSearchFilter = []SearchTestCase{
{
{ // [0]
"Empty filter clause",
`{
"_source": {
Expand Down Expand Up @@ -1887,7 +1978,7 @@ var TestSearchFilter = []SearchTestCase{
"SELECT " + clickhouse.TimestampGroupBy("@timestamp", clickhouse.DateTime64, 30*time.Second) + ", count() FROM " + QuotedTableName + " GROUP BY (" + clickhouse.TimestampGroupBy("@timestamp", clickhouse.DateTime64, 30*time.Second) + ") ORDER BY (" + clickhouse.TimestampGroupBy("@timestamp", clickhouse.DateTime64, 30*time.Second) + ")",
},
},
{
{ // [1]
"Filter with now in range",
`{
"_source": {
Expand Down Expand Up @@ -1947,4 +2038,54 @@ var TestSearchFilter = []SearchTestCase{
"SELECT " + clickhouse.TimestampGroupBy("@timestamp", clickhouse.DateTime64, 30*time.Second) + `, count() FROM ` + QuotedTableName + ` WHERE "@timestamp">subDate(now(), INTERVAL 15 minute) GROUP BY (` + clickhouse.TimestampGroupBy("@timestamp", clickhouse.DateTime64, 30*time.Second) + `) ORDER BY (` + clickhouse.TimestampGroupBy("@timestamp", clickhouse.DateTime64, 30*time.Second) + `)`,
},
},
{ // [2]
"Empty filter",
`
{
"query": {
"bool": {
"filter": {}
}
}
}`,
[]string{``},
model.Normal,
[]model.Query{justSimplestWhere(``)},
[]string{qToStr(justSimplestWhere(``))},
},
{ // [3]
"Empty filter with other clauses",
`
{
"query": {
"bool" : {
"must" : {
"term" : { "user.id" : "kimchy" }
},
"filter": {},
"must_not" : {
"range" : {
"age" : { "gte" : 10, "lte" : 20 }
}
},
"should" : [
{ "term" : { "tags" : "env1" } },
{ "term" : { "tags" : "deployed" } }
],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}`,
[]string{
`("user.id"='kimchy' AND ("tags"='env1' OR "tags"='deployed')) AND NOT ("age"<=20 AND "age">=10)`,
`("user.id"='kimchy' AND ("tags"='env1' OR "tags"='deployed')) AND NOT ("age">=10 AND "age"<=20)`,
},
model.Normal,
[]model.Query{
justSimplestWhere(`("user.id"='kimchy' AND ("tags"='env1' OR "tags"='deployed')) AND NOT ("age"<=20 AND "age">=10)`),
justSimplestWhere(`("user.id"='kimchy' AND ("tags"='env1' OR "tags"='deployed')) AND NOT ("age">=10 AND "age"<=20)`),
},
[]string{`SELECT "message" FROM "logs-generic-default" WHERE ("user.id"='kimchy' AND ("tags"='env1' OR "tags"='deployed')) AND NOT ("age".=.0 AND "age".=.0)`},
},
}

0 comments on commit 7e1372f

Please sign in to comment.