diff --git a/src/core/etl/src/Flow/ETL/GroupBy/Aggregator/Average.php b/src/core/etl/src/Flow/ETL/GroupBy/Aggregator/Average.php index 7e6ab8003..b70748f97 100644 --- a/src/core/etl/src/Flow/ETL/GroupBy/Aggregator/Average.php +++ b/src/core/etl/src/Flow/ETL/GroupBy/Aggregator/Average.php @@ -46,8 +46,13 @@ public function result() : Entry $this->ref->as($this->ref->to() . '_avg'); } - $result = $this->sum / $this->count; - $resultInt = (int) $result; + if (0 !== $this->count) { + $result = $this->sum / $this->count; + $resultInt = (int) $result; + } else { + $result = 0.0; + $resultInt = 0; + } if ($result - $resultInt === 0.0) { return \Flow\ETL\DSL\Entry::integer($this->ref->name(), (int) $result); diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/GroupBy/Aggregator/AverageTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/GroupBy/Aggregator/AverageTest.php index ecb66d5a2..9a7b8c719 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/GroupBy/Aggregator/AverageTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/GroupBy/Aggregator/AverageTest.php @@ -72,4 +72,14 @@ public function test_average_with_integer_result() : void $aggregator->result()->value() ); } + + public function test_average_with_zero_result() : void + { + $aggregator = new Average('int'); + + $this->assertSame( + 0, + $aggregator->result()->value() + ); + } }