Skip to content

Commit

Permalink
Minor refactor query translator (#517)
Browse files Browse the repository at this point in the history
I aim to unify json response generation. Currently, logic is split into
many places. I started with simplifying logic building aggregation
response tree.
  • Loading branch information
jakozaur authored Jul 11, 2024
1 parent f7ec90c commit 340a550
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions quesma/queryparser/query_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,21 @@ func (cw *ClickhouseQueryTranslator) makeResponseAggregationRecursive(query *mod
// fmt.Println("level1 :/", level1, " level2 B):", level2)

// or we need to go deeper
qp := queryprocessor.NewQueryProcessor(cw.Ctx)
var bucketsReturnMap []model.JsonMap
if query.Aggregators[aggregatorsLevel].SplitOverHowManyFields == 0 {
bucketsReturnMap = append(bucketsReturnMap, cw.makeResponseAggregationRecursive(query, ResultSet, aggregatorsLevel+1, selectLevel)...)
subResult := make(model.JsonMap, 1)
currentAggregator := query.Aggregators[aggregatorsLevel]
if currentAggregator.SplitOverHowManyFields == 0 {
subSubResult := cw.makeResponseAggregationRecursive(query, ResultSet, aggregatorsLevel+1, selectLevel)[0]
// Keyed and Filters aggregations are special and need to be wrapped in "buckets"
if currentAggregator.Keyed || currentAggregator.Filters {
subResult["buckets"] = subSubResult
} else {
subResult = subSubResult
}
} else {
var bucketsReturnMap []model.JsonMap
// normally it's just 1. It used to be just 1 before multi_terms aggregation, where we usually split over > 1 field
weSplitOverHowManyFields := query.Aggregators[aggregatorsLevel].SplitOverHowManyFields
qp := queryprocessor.NewQueryProcessor(cw.Ctx)
weSplitOverHowManyFields := currentAggregator.SplitOverHowManyFields
buckets := qp.SplitResultSetIntoBuckets(ResultSet, selectLevel+weSplitOverHowManyFields)
for _, bucket := range buckets {
newBuckets := cw.makeResponseAggregationRecursive(query, bucket, aggregatorsLevel+1, selectLevel+weSplitOverHowManyFields)
Expand All @@ -147,32 +155,14 @@ func (cw *ClickhouseQueryTranslator) makeResponseAggregationRecursive(query *mod
}
bucketsReturnMap = append(bucketsReturnMap, newBuckets...)
}
}

result := make(model.JsonMap, 1)
subResult := make(model.JsonMap, 1)

// The if below: very hacky, but works for now. I have an idea how to fix this and make code nice, but it'll take a while to refactor.
// Basically, for now every not-ending subaggregation has "buckets" key. Only exception is "sampler", which doesn't, thus this if.
//
// I'd like to keep an actual tree after the refactor, not a list of paths from root to leaf, as it is now.
// Then in the tree (in each node) I'd remember where I am at the moment (e.g. here I'm in "sampler",
// so I don't need buckets). It'd enable some custom handling for another weird types of requests.

if query.Aggregators[aggregatorsLevel].Filters {
subResult["buckets"] = bucketsReturnMap[0]
} else if query.Aggregators[aggregatorsLevel].Keyed {
subResult["buckets"] = bucketsReturnMap[0]
} else if query.Aggregators[aggregatorsLevel].SplitOverHowManyFields == 0 {
subResult = bucketsReturnMap[0]
} else {
subResult["buckets"] = bucketsReturnMap
}

_ = cw.addMetadataIfNeeded(query, subResult, aggregatorsLevel)

result[query.Aggregators[aggregatorsLevel].Name] = subResult
return []model.JsonMap{result}
return []model.JsonMap{{
currentAggregator.Name: subResult,
}}
}

// addMetadataIfNeeded adds metadata to the `result` dictionary, if needed.
Expand Down

0 comments on commit 340a550

Please sign in to comment.