Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit c6ed018

Browse files
authored
Fix sometimes not adding empty buckets (#948)
1 parent 4415532 commit c6ed018

File tree

8 files changed

+129
-26
lines changed

8 files changed

+129
-26
lines changed

quesma/model/metrics_aggregations/quantile.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ func (query Quantile) TranslateSqlResponseToJson(rows []model.QueryResultRow) mo
3434
valueAsStringMap := make(model.JsonMap)
3535

3636
if len(rows) == 0 {
37-
return emptyPercentilesResult
37+
return query.emptyPercentilesResult()
3838
}
3939
if len(rows[0].Cols) == 0 {
40-
return emptyPercentilesResult
40+
return query.emptyPercentilesResult()
4141
}
4242

4343
percentileIdx := -1
@@ -151,8 +151,12 @@ func (query Quantile) processResult(colName string, percentileReturnedByClickhou
151151
return percentile, percentileAsString, percentileIsNanOrInvalid
152152
}
153153

154-
var emptyPercentilesResult = model.JsonMap{
155-
"values": 0,
154+
func (query Quantile) emptyPercentilesResult() model.JsonMap {
155+
result := make(model.JsonMap, len(query.percentileNames))
156+
for _, percentileName := range query.percentileNames {
157+
result[query.createPercentileNameToReturn(percentileName)] = nil
158+
}
159+
return model.JsonMap{"values": result}
156160
}
157161

158162
// Kibana requires .0 at the end of the percentile name if it's an integer.

quesma/queryparser/pancake_json_rendering.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,6 @@ func (p *pancakeJSONRenderer) layerToJSON(remainingLayers []*pancakeModelLayer,
316316
bucketArr[i][pipelineAggrName] = pipelineAggrResult[i]
317317
}
318318

319-
if docCount, ok := bucket["doc_count"]; ok && fmt.Sprintf("%v", docCount) == "0" {
320-
// Not sure, but it does the trick.
321-
continue
322-
}
323-
324319
subAggr, err := p.layerToJSON(remainingLayers[1:], subAggrRows[i])
325320
if err != nil {
326321
return nil, err
@@ -337,11 +332,6 @@ func (p *pancakeJSONRenderer) layerToJSON(remainingLayers []*pancakeModelLayer,
337332
bucketArr[i][pipelineAggrName] = pipelineAggrResult[i]
338333
}
339334

340-
if docCount, ok := bucket["doc_count"]; ok && fmt.Sprintf("%v", docCount) == "0" {
341-
// Not sure, but it does the trick.
342-
continue
343-
}
344-
345335
// if our bucket aggregation is a date_histogram, we need original key, not processed one, which is "key"
346336
key, exists := bucket[bucket_aggregations.OriginalKeyName]
347337
if !exists {
@@ -372,11 +362,12 @@ func (p *pancakeJSONRenderer) layerToJSON(remainingLayers []*pancakeModelLayer,
372362
return nil, err
373363
}
374364
bucketArr[i] = util.MergeMaps(p.ctx, bucket, subAggr)
375-
if _, exists = bucketArr[i][bucket_aggregations.OriginalKeyName]; exists {
376-
delete(bucketArr[i], bucket_aggregations.OriginalKeyName)
377-
}
378365
}
379366
}
367+
368+
for i := 0; i < len(bucketArr); i++ {
369+
delete(bucketArr[i], bucket_aggregations.OriginalKeyName)
370+
}
380371
}
381372

382373
if layer.nextBucketAggregation.metadata != nil {

quesma/testdata/aggregation_requests_2.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,23 +1780,38 @@ var AggregationTests2 = []AggregationTestCase{
17801780
},
17811781
{
17821782
"doc_count": 0,
1783-
"key": 9200.0
1783+
"key": 9200.0,
1784+
"3": {
1785+
"buckets": []
1786+
}
17841787
},
17851788
{
17861789
"doc_count": 0,
1787-
"key": 9300.0
1790+
"key": 9300.0,
1791+
"3": {
1792+
"buckets": []
1793+
}
17881794
},
17891795
{
17901796
"doc_count": 0,
1791-
"key": 9400.0
1797+
"key": 9400.0,
1798+
"3": {
1799+
"buckets": []
1800+
}
17921801
},
17931802
{
17941803
"doc_count": 0,
1795-
"key": 9500.0
1804+
"key": 9500.0,
1805+
"3": {
1806+
"buckets": []
1807+
}
17961808
},
17971809
{
17981810
"doc_count": 0,
1799-
"key": 9600.0
1811+
"key": 9600.0,
1812+
"3": {
1813+
"buckets": []
1814+
}
18001815
},
18011816
{
18021817
"doc_count": 2,

quesma/testdata/clients/clover.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -876,17 +876,26 @@ var CloverTests = []testdata.AggregationTestCase{
876876
{
877877
"doc_count": 0,
878878
"key": 1727308800000,
879-
"key_as_string": "2024-09-26T00:00:00.000"
879+
"key_as_string": "2024-09-26T00:00:00.000",
880+
"count": {
881+
"value": 0
882+
}
880883
},
881884
{
882885
"doc_count": 0,
883886
"key": 1727913600000,
884-
"key_as_string": "2024-10-03T00:00:00.000"
887+
"key_as_string": "2024-10-03T00:00:00.000",
888+
"count": {
889+
"value": 0
890+
}
885891
},
886892
{
887893
"doc_count": 0,
888894
"key": 1728518400000,
889-
"key_as_string": "2024-10-10T00:00:00.000"
895+
"key_as_string": "2024-10-10T00:00:00.000",
896+
"count": {
897+
"value": 0
898+
}
890899
},
891900
{
892901
"doc_count": 1,

quesma/testdata/dashboard-1/aggregation_requests.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,16 @@ var AggregationTests = []testdata.AggregationTestCase{
134134
"key": 0
135135
},
136136
{
137+
"1": {
138+
"buckets": []
139+
},
137140
"doc_count": 0,
138141
"key": 2000000
139142
},
140143
{
144+
"1": {
145+
"buckets": []
146+
},
141147
"doc_count": 0,
142148
"key": 4000000
143149
},
@@ -310,7 +316,12 @@ var AggregationTests = []testdata.AggregationTestCase{
310316
},
311317
{
312318
"doc_count": 0,
313-
"key": 1
319+
"key": 1,
320+
"2": {
321+
"values": {
322+
"95.0": null
323+
}
324+
}
314325
},
315326
{
316327
"2": {
@@ -328,6 +339,9 @@ var AggregationTests = []testdata.AggregationTestCase{
328339
"key_as_string": "2024-04-24T11:15:30.000"
329340
},
330341
{
342+
"1": {
343+
"buckets": []
344+
},
331345
"doc_count": 0,
332346
"key": 1713957360000,
333347
"key_as_string": "2024-04-24T11:16:00.000"
@@ -345,6 +359,11 @@ var AggregationTests = []testdata.AggregationTestCase{
345359
"key": 1
346360
},
347361
{
362+
"2": {
363+
"values": {
364+
"95.0": null
365+
}
366+
},
348367
"doc_count": 0,
349368
"key": 1.5
350369
},
@@ -358,6 +377,11 @@ var AggregationTests = []testdata.AggregationTestCase{
358377
"key": 2
359378
},
360379
{
380+
"2": {
381+
"values": {
382+
"95.0": null
383+
}
384+
},
361385
"doc_count": 0,
362386
"key": 2.5
363387
},

quesma/testdata/dates.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,33 @@ var AggregationTestsWithDates = []AggregationTestCase{
198198
"timeseries": {
199199
"buckets": [
200200
{
201+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
202+
"value": 0
203+
},
201204
"doc_count": 0,
202205
"key": 1730370420000,
203206
"key_as_string": "2024-10-31T10:27:00.000"
204207
},
205208
{
209+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
210+
"value": 0
211+
},
206212
"doc_count": 0,
207213
"key": 1730370430000,
208214
"key_as_string": "2024-10-31T10:27:10.000"
209215
},
210216
{
217+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
218+
"value": 0
219+
},
211220
"doc_count": 0,
212221
"key": 1730370440000,
213222
"key_as_string": "2024-10-31T10:27:20.000"
214223
},
215224
{
225+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
226+
"value": 0
227+
},
216228
"doc_count": 0,
217229
"key": 1730370450000,
218230
"key_as_string": "2024-10-31T10:27:30.000"
@@ -226,21 +238,33 @@ var AggregationTestsWithDates = []AggregationTestCase{
226238
"key_as_string": "2024-10-31T10:27:40.000"
227239
},
228240
{
241+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
242+
"value": 0
243+
},
229244
"doc_count": 0,
230245
"key": 1730370470000,
231246
"key_as_string": "2024-10-31T10:27:50.000"
232247
},
233248
{
249+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
250+
"value": 0
251+
},
234252
"doc_count": 0,
235253
"key": 1730370480000,
236254
"key_as_string": "2024-10-31T10:28:00.000"
237255
},
238256
{
257+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
258+
"value": 0
259+
},
239260
"doc_count": 0,
240261
"key": 1730370490000,
241262
"key_as_string": "2024-10-31T10:28:10.000"
242263
},
243264
{
265+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
266+
"value": 0
267+
},
244268
"doc_count": 0,
245269
"key": 1730370500000,
246270
"key_as_string": "2024-10-31T10:28:20.000"
@@ -387,21 +411,33 @@ var AggregationTestsWithDates = []AggregationTestCase{
387411
"key_as_string": "2024-10-31T10:27:40.000"
388412
},
389413
{
414+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
415+
"value": 0
416+
},
390417
"doc_count": 0,
391418
"key": 1730370470000,
392419
"key_as_string": "2024-10-31T10:27:50.000"
393420
},
394421
{
422+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
423+
"value": 0
424+
},
395425
"doc_count": 0,
396426
"key": 1730370480000,
397427
"key_as_string": "2024-10-31T10:28:00.000"
398428
},
399429
{
430+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
431+
"value": 0
432+
},
400433
"doc_count": 0,
401434
"key": 1730370490000,
402435
"key_as_string": "2024-10-31T10:28:10.000"
403436
},
404437
{
438+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
439+
"value": 0
440+
},
405441
"doc_count": 0,
406442
"key": 1730370500000,
407443
"key_as_string": "2024-10-31T10:28:20.000"
@@ -415,6 +451,9 @@ var AggregationTestsWithDates = []AggregationTestCase{
415451
"key_as_string": "2024-10-31T10:28:30.000"
416452
},
417453
{
454+
"c4a48962-08e6-4791-ae9e-b50f1b111488": {
455+
"value": 0
456+
},
418457
"doc_count": 0,
419458
"key": 1730370520000,
420459
"key_as_string": "2024-10-31T10:28:40.000"

quesma/testdata/kibana-visualize/pipeline_aggregation_requests.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ var PipelineAggregationTests = []testdata.AggregationTestCase{
283283
"1": {
284284
"value": 5.0
285285
},
286+
"1-metric": {
287+
"value": null
288+
},
286289
"doc_count": 0,
287290
"key": 1728144360000,
288291
"key_as_string": "2024-10-05T16:06:00.000"
@@ -291,6 +294,9 @@ var PipelineAggregationTests = []testdata.AggregationTestCase{
291294
"1": {
292295
"value": 5.0
293296
},
297+
"1-metric": {
298+
"value": null
299+
},
294300
"doc_count": 0,
295301
"key": 1728144420000,
296302
"key_as_string": "2024-10-05T16:07:00.000"

quesma/testdata/opensearch-visualize/pipeline_aggregation_requests.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ var PipelineAggregationTests = []testdata.AggregationTestCase{
755755
"1": {
756756
"value": null
757757
},
758+
"1-metric": {
759+
"value": null
760+
},
758761
"doc_count": 0,
759762
"key": 1715197200000,
760763
"key_as_string": "2024-05-08T19:40:00.000"
@@ -763,6 +766,9 @@ var PipelineAggregationTests = []testdata.AggregationTestCase{
763766
"1": {
764767
"value": null
765768
},
769+
"1-metric": {
770+
"value": null
771+
},
766772
"doc_count": 0,
767773
"key": 1715197800000,
768774
"key_as_string": "2024-05-08T19:50:00.000"
@@ -1625,6 +1631,9 @@ var PipelineAggregationTests = []testdata.AggregationTestCase{
16251631
"1": {
16261632
"value": null
16271633
},
1634+
"1-metric": {
1635+
"value": null
1636+
},
16281637
"doc_count": 0,
16291638
"key": 1715197200000,
16301639
"key_as_string": "2024-05-08T19:40:00.000"
@@ -1633,6 +1642,9 @@ var PipelineAggregationTests = []testdata.AggregationTestCase{
16331642
"1": {
16341643
"value": null
16351644
},
1645+
"1-metric": {
1646+
"value": null
1647+
},
16361648
"doc_count": 0,
16371649
"key": 1715197800000,
16381650
"key_as_string": "2024-05-08T19:50:00.000"
@@ -3691,6 +3703,9 @@ var PipelineAggregationTests = []testdata.AggregationTestCase{
36913703
"key": 3.0
36923704
},
36933705
{
3706+
"1-metric": {
3707+
"value": null
3708+
},
36943709
"3": {
36953710
"value": null
36963711
},

0 commit comments

Comments
 (0)