Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
This repository was archived by the owner on Jul 16, 2021. It is now read-only.

Improve the behaviour of query builder's count() and other aggregates #1693

@tomlankhorst

Description

@tomlankhorst

There are some issues with ->count()ing queries with groups.
The current implementation executes the relevant aggregate function (SUM, MIN, MAX...) on the query and returns the first result. See this issue, other relevant issues: #4306 #14123.

This is typically what we want when not using GROUP BY, but when we are, the result of e.g. count() - just picking the first value - makes little sense.

SELECT count(*) FROM `jobs` GROUP BY `type`

Will return something along the lines of [N_1, N_2, ..., N_M] for M groups.

The framework will just answer: N_1

First of all, we should determine, what should count(), min(), max() and other aggregate functions do? I feel like there are two major options.

A. We're trying to make a function of the query builder behave comparable to an equally named function of a collection. Such that:

assert($query->count() === $query()->get()->count());

If this is the case, we should take care that count() works for groupBy(...) queries as well.

B. The alternative is that we want aggregate functions to execute a query and return the result of the aggregate function. This result might consist of N records, where N is typically 1 or another number when a GROUP BY is used. The current implementation, however, only returns the first result of the query. This makes little sense when N ≠ 1.
Furthermore, the downside here is that we'll only retrieve the aggregate function result without any other column so there's not much to interpret here.

So, what do we want to achieve here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions