laravel/pulse

SUM calculation appears to be same as COUNT

Closed this issue · 1 comments

Pulse Version

1.3.2

Laravel Version

11.37.0

PHP Version

8.3.8

Livewire Version

Latest

Database Driver & Version

MySQL 8.0.33

Description

I'm not sure whether this is intentional or not, but looking through the DatabaseStorage,php file, the upsert calculations for MySQL for both count and sum are the same.

count:

Expression('`value` + values(`value`)')

sum:

Expression('`value` + values(`value`)')

In what scenario then would sum be different from count

Steps To Reproduce

Look a DatabaseStorage.php

The data is pre-aggregated in PHP before being sent to the database to reduce the number of insert rows and work around a limitation of Postgres being unable to handle insert conflicts.

protected function preaggregateCounts(Collection $entries): Collection
{
return $this->preaggregate($entries, 'count', fn ($aggregate) => [
...$aggregate,
'value' => ($aggregate['value'] ?? 0) + 1,
]);
}

protected function preaggregateSums(Collection $entries): Collection
{
return $this->preaggregate($entries, 'sum', fn ($aggregate, $entry) => [
...$aggregate,
'value' => ($aggregate['value'] ?? 0) + $entry->value,
]);
}

After this stage, insertion into the database is an addition operation for both count and sum.

You can check out the tests to confirm the behaviour.