-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[11.x] Support DB aggregate by group #53209
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 tasks
GromNaN
changed the title
[11.x] Support DB aggregation by group
[11.x] Support DB aggregate by group
Oct 17, 2024
GromNaN
force-pushed
the
group-aggregate
branch
from
October 18, 2024 00:44
5e65913
to
cb896d6
Compare
GromNaN
force-pushed
the
group-aggregate
branch
from
October 18, 2024 07:50
cb896d6
to
d7258ce
Compare
@GromNaN Would this ever break something like: $builder->from('users')->groupBy('role')->paginate(); We run count queries on pagination. |
Things are well done: the pagination count query encapsulates the initial SQL query as a subquery in a framework/src/Illuminate/Database/Query/Builder.php Lines 3299 to 3303 in 61e221c
|
taylorotwell
added a commit
that referenced
this pull request
Nov 19, 2024
This reverts commit 6a491c2.
taylorotwell
added a commit
that referenced
this pull request
Nov 19, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the aggregate methods ignore the
group by
clause. This PR change the result ofto return a Collection of
[role, count]
instead of a single total count.Result before:
40
Result after:
The aggregate result is always in a column named
aggregate
and the "group by" columns are added to the result.Why?
I'm implementing
withCount
for MongoDB models and hybrid MongoDB to SQL models. This implementation is done duringeagerLoad
by doing a count by foreign key in the related table (i.e.select foreign_id, count(*) from mytable where foreign_id in (1,2,3) group by foreign_id
). I managed to do that for MongoDB, and this PR makes it work for hybrid MongoDB to SQL models.Union and group by
This also works with unions. I have to change the generated SQL to apply the
group by
clause to the union result instead of the first query of the union.For the following query:
The generated SQL is before/after the PR:
If anyone need to generate the previous SQL, the query builder for the 1st subquery would have to be wrapped:
Return type of count/sum/avg/min/max
The return type of the aggregate methods indicate that a single value is expected: the result of the aggregation. But since the group by clause is used, the result is a collection of
[...groups, aggregate]
. I had to change the return type in phpdoc to addCollection
. This may impact thecount
function only, as other returnmixed
that already includesCollection
.An alternative could have been to add a new methods
sumBy($function, $column, ...$groups)
but I think this is already the role of->groupBy(...$groups)
to specify the expected groups.