Skip to content

Commit

Permalink
Terms aggregation - add support to 'missing' parameter (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
nablaone authored Jul 8, 2024
1 parent 0ddad27 commit e5c4000
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
14 changes: 14 additions & 0 deletions http_requests/search-terms-missing.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GET http://localhost:8080/windows_logs/_search
Content-Type: application/json


{
"aggs": {
"tags": {
"terms": {
"field": "registry::key",
"missing": "n/a"
}
}
}
}
22 changes: 19 additions & 3 deletions quesma/queryparser/aggregation_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,23 @@ func (cw *ClickhouseQueryTranslator) tryBucketAggregation(currentAggr *aggrQuery

isEmptyGroupBy := len(currentAggr.SelectCommand.GroupBy) == 0

currentAggr.SelectCommand.GroupBy = append(currentAggr.SelectCommand.GroupBy, cw.parseFieldField(terms, termsType))
currentAggr.SelectCommand.Columns = append(currentAggr.SelectCommand.Columns, cw.parseFieldField(terms, termsType))
// parse missing paremeter
var missingPlaceholder string
if m, ok := terms.(QueryMap); ok {
if m["missing"] != nil {
missingPlaceholder = m["missing"].(string)
}
}

fieldExpression := cw.parseFieldField(terms, termsType)

// apply missing placeholder if it is set
if missingPlaceholder != "" {
fieldExpression = model.NewFunction("COALESCE", fieldExpression, model.NewLiteral("'"+missingPlaceholder+"'"))
}

currentAggr.SelectCommand.GroupBy = append(currentAggr.SelectCommand.GroupBy, fieldExpression)
currentAggr.SelectCommand.Columns = append(currentAggr.SelectCommand.Columns, fieldExpression)

orderByAdded := false
size := 10
Expand All @@ -751,14 +766,15 @@ func (cw *ClickhouseQueryTranslator) tryBucketAggregation(currentAggr *aggrQuery
logger.WarnWithCtx(cw.Ctx).Msgf("size is not an float64, but %T, value: %v. Using default", sizeRaw, sizeRaw)
}
}

}
currentAggr.SelectCommand.Limit = size
currentAggr.SelectCommand.OrderBy = append(currentAggr.SelectCommand.OrderBy, model.NewSortByCountColumn(model.DescOrder))
orderByAdded = true
}
delete(queryMap, termsType)
if !orderByAdded {
currentAggr.SelectCommand.OrderBy = append(currentAggr.SelectCommand.OrderBy, model.NewOrderByExprWithoutOrder(cw.parseFieldField(terms, termsType)))
currentAggr.SelectCommand.OrderBy = append(currentAggr.SelectCommand.OrderBy, model.NewOrderByExprWithoutOrder(fieldExpression))
}
return success, 1, nil
/* will remove later
Expand Down
12 changes: 6 additions & 6 deletions quesma/testdata/aggregation_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2414,16 +2414,16 @@ var AggregationTests = []AggregationTestCase{
},
},
ExpectedSQLs: []string{
`SELECT "event.dataset", toInt64(toUnixTimestamp64Milli("@timestamp") / 60000), count() ` +
`SELECT COALESCE("event.dataset",'unknown'), toInt64(toUnixTimestamp64Milli("@timestamp") / 60000), count() ` +
`FROM ` + QuotedTableName + ` ` +
`WHERE ("@timestamp">parseDateTime64BestEffort('2024-01-25T14:53:59.033Z') AND "@timestamp"<=parseDateTime64BestEffort('2024-01-25T15:08:59.033Z')) ` +
`GROUP BY "event.dataset", toInt64(toUnixTimestamp64Milli("@timestamp") / 60000) ` +
`ORDER BY "event.dataset", toInt64(toUnixTimestamp64Milli("@timestamp") / 60000)`,
`SELECT "event.dataset", count() FROM ` + QuotedTableName + ` ` +
`GROUP BY COALESCE("event.dataset",'unknown'), toInt64(toUnixTimestamp64Milli("@timestamp") / 60000) ` +
`ORDER BY COALESCE("event.dataset",'unknown'), toInt64(toUnixTimestamp64Milli("@timestamp") / 60000)`,
`SELECT COALESCE("event.dataset",'unknown'), count() FROM ` + QuotedTableName + ` ` +
`WHERE ("@timestamp">parseDateTime64BestEffort('2024-01-25T14:53:59.033Z') ` +
`AND "@timestamp"<=parseDateTime64BestEffort('2024-01-25T15:08:59.033Z')) ` +
`GROUP BY "event.dataset" ` +
`ORDER BY "event.dataset"`,
`GROUP BY COALESCE("event.dataset",'unknown') ` +
`ORDER BY COALESCE("event.dataset",'unknown')`,
},
},
{ // [14], "old" test, also can be found in testdata/requests.go TestAsyncSearch[5]
Expand Down
12 changes: 6 additions & 6 deletions quesma/testdata/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,18 +727,18 @@ var TestsAsyncSearch = []AsyncSearchTestCase{
"no comment yet",
model.SearchQueryInfo{Typ: model.Normal},
[]string{
`SELECT "event.dataset", ` +
`SELECT COALESCE("event.dataset",'unknown'), ` +
groupBySQL("@timestamp", clickhouse.DateTime64, time.Minute) +
`, count() FROM ` + QuotedTableName + ` ` +
`WHERE ("@timestamp".*parseDateTime64BestEffort('2024-01-25T1.:..:59.033Z') ` +
`AND "@timestamp".*parseDateTime64BestEffort('2024-01-25T1.:..:59.033Z')) ` +
`GROUP BY "event.dataset", ` + groupBySQL("@timestamp", clickhouse.DateTime64, time.Minute) + ` ` +
`ORDER BY "event.dataset", ` + groupBySQL("@timestamp", clickhouse.DateTime64, time.Minute),
`SELECT "event.dataset", count() FROM ` + QuotedTableName + ` ` +
`GROUP BY COALESCE("event.dataset",'unknown'), ` + groupBySQL("@timestamp", clickhouse.DateTime64, time.Minute) + ` ` +
`ORDER BY COALESCE("event.dataset",'unknown'), ` + groupBySQL("@timestamp", clickhouse.DateTime64, time.Minute),
`SELECT COALESCE("event.dataset",'unknown'), count() FROM ` + QuotedTableName + ` ` +
`WHERE ("@timestamp".*parseDateTime64BestEffort('2024-01-25T1.:..:59.033Z') ` +
`AND "@timestamp".*parseDateTime64BestEffort('2024-01-25T1.:..:59.033Z')) ` +
`GROUP BY "event.dataset" ` +
`ORDER BY "event.dataset"`,
`GROUP BY COALESCE("event.dataset",'unknown') ` +
`ORDER BY COALESCE("event.dataset",'unknown')`,
},
true,
},
Expand Down

0 comments on commit e5c4000

Please sign in to comment.