Skip to content

Commit 162f323

Browse files
committed
fix(aggregation): if summing or averaging over nothing, we should return zero
1 parent e31c77d commit 162f323

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

query/aggregator.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,17 @@ func (ag *aggregator) divideByCount() {
601601

602602
func (ag *aggregator) Value() (types.Val, error) {
603603
if ag.result.Value == nil {
604-
return ag.result, ErrEmptyVal
604+
if ag.name == "avg" || ag.name == "sum" {
605+
switch {
606+
case ag.result.Tid != types.FloatID:
607+
ag.result.Tid = types.IntID
608+
ag.result.Value = int64(0)
609+
case ag.result.Tid == types.FloatID:
610+
ag.result.Value = float64(0)
611+
}
612+
} else {
613+
return ag.result, ErrEmptyVal
614+
}
605615
}
606616
ag.divideByCount()
607617
if ag.result.Tid == types.FloatID {

query/query1_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ func TestAggregateRoot5(t *testing.T) {
14861486
}
14871487
`
14881488
js := processQueryNoErr(t, query)
1489-
require.JSONEq(t, `{"data": {"me":[{"sum(val(m))":null}]}}`, js)
1489+
require.JSONEq(t, `{"data": {"me":[{"sum(val(m))":0}]}}`, js)
14901490
}
14911491

14921492
func TestAggregateRoot6(t *testing.T) {
@@ -1548,7 +1548,7 @@ func TestAggregateEmptyData(t *testing.T) {
15481548
}
15491549
`
15501550
js := processQueryNoErr(t, query)
1551-
require.JSONEq(t, `{"data": {"me":[{"avg(val(a))":null},{"min(val(a))":null},{"max(val(a))":null}]}}`, js)
1551+
require.JSONEq(t, `{"data": {"me":[{"avg(val(a))":0},{"min(val(a))":null},{"max(val(a))":null}]}}`, js)
15521552
}
15531553

15541554
func TestCountEmptyData(t *testing.T) {

0 commit comments

Comments
 (0)