feat: Add Avg, Min, and Max aggregate functions to DB Query#1240
feat: Add Avg, Min, and Max aggregate functions to DB Query#1240hwbrzzl merged 10 commits intogoravel:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1240 +/- ##
==========================================
- Coverage 66.09% 65.73% -0.36%
==========================================
Files 248 248
Lines 17069 17185 +116
==========================================
+ Hits 11282 11297 +15
- Misses 5410 5511 +101
Partials 377 377 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
contracts/database/db/db.go
Outdated
| @@ -148,6 +148,12 @@ type Query interface { | |||
| SharedLock() Query | |||
| // Sum calculates the sum of a column's values and populates the destination object. | |||
| Sum(column string) (int64, error) | |||
There was a problem hiding this comment.
We consider changing the Sum function to Sum(column string, dest any) error to adapt the int* and float* situation. Hence, Avag, Min, Max can do that as well. If it makes sense, could you optimize it in this PR simultaneously?
There was a problem hiding this comment.
Hello, I have changed the Sum methods signature and added tests and same for the other methods.
tests/query_test.go
Outdated
|
|
||
| avg, err := query.Query().Table("users").Avg("id") | ||
| s.Nil(err) | ||
| s.True(avg > 0) |
There was a problem hiding this comment.
Could you precisely assert the value?
tests/query_test.go
Outdated
|
|
||
| min, err := query.Query().Table("users").Min("id") | ||
| s.Nil(err) | ||
| s.True(min > 0) |
tests/query_test.go
Outdated
|
|
||
| max, err := query.Query().Table("users").Max("id") | ||
| s.Nil(err) | ||
| s.True(max > 0) |
…o use pointer based solution
… and `Max` methods
There was a problem hiding this comment.
Pull Request Overview
This PR refactors database aggregation methods (Sum, Avg, Min, Max) to use a pointer-based pattern for improved type safety and error handling. The new implementation requires callers to pass a destination pointer that will be populated with the result, rather than returning the value directly.
Key changes:
- Refactored
Summethod to accept a destination pointer parameter and return only an error - Added three new aggregation methods:
Avg,Min, andMax, all following the same pointer-based pattern - Updated all contract interfaces, implementations, mocks, and tests to use the new signatures
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| contracts/database/orm/orm.go | Updated Query interface with new signatures for Sum and added Avg, Min, Max methods |
| contracts/database/db/db.go | Updated Query interface with new signatures for Sum and added Avg, Min, Max methods |
| database/gorm/query.go | Implemented pointer-based Sum, Avg, Min, Max with pointer validation |
| database/db/query.go | Implemented pointer-based Sum, Avg, Min, Max with pointer validation |
| mocks/database/orm/Query.go | Generated mock implementations for updated method signatures |
| mocks/database/db/Query.go | Generated mock implementations for updated method signatures |
| tests/models.go | Added Ratio field to User model for testing aggregation functions |
| tests/table.go | Added ratio column to user table schemas |
| tests/query_test.go | Updated Sum tests and added comprehensive tests for Avg, Min, Max |
| tests/db_test.go | Updated Sum tests and added comprehensive tests for Avg, Min, Max |
| tests/to_sql_test.go | Updated SQL assertions to include new ratio column |
| database/db/query_test.go | Updated unit tests for pointer-based aggregation methods |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
Hey @oguzhankrcb FYI, added you to the contributor list, looking forward to more amazing PRs, thanks again. |
Database Query Methods Refactoring
Summary
This PR refactors the database query aggregation methods to use a pointer pattern instead of returning values directly. This change provides better type safety and allows for more flexible error handling.
Key Changes
1. Pointer Pattern Implementation
We've refactored the aggregation methods to use a pointer pattern where the result is populated into a destination variable passed by reference:
Avg(column string, dest any) errorSum(column string, dest any) errorMin(column string, dest any) errorMax(column string, dest any) error2. New Sum Method
We've changed the
Summethod to use pointer implementation:3. Updated Method Signatures
All aggregation methods now follow the same pattern:
4. Type Safety Improvements
The implementation includes validation to ensure the destination parameter is a pointer:
Usage Examples
Before (hypothetical example)
After (current implementation)
Return Types
All aggregation methods now return:
error- indicating success or failure of the operationImplementation Details
The implementation uses the following approach:
Benefits
Testing
All tests pass successfully with the new implementation.