mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-31 12:36:46 +00:00
fix(query): distinct count in user list (#10840)
# Which Problems Are Solved
When listing / searching users, each user got multiplied by the amount
of metadata entries they have, towards the `total_results` count. In
PostgreSQL the `COUNT(*) OVER()` window function does not support
`DISTINCT`. Even tho the query did a distinct select, the count would
still include duplicates.
# How the Problems Are Solved
Wrap the original query in a sub-select, so that the `DISTINCT` gets
handled before the count window function is executed in the outer
function. Filters, permission and solting is applied to the inner query.
Offset, limit and count are applied to the outer query.
# Additional Changes
- none
# Additional Context
- Closes https://github.com/zitadel/zitadel/issues/10825
- Backport to 4v
(cherry picked from commit f27ca69749)
This commit is contained in:
committed by
Livio Spring
parent
45c7354234
commit
cd059dc0cb
@@ -23,6 +23,8 @@ type SearchRequest struct {
|
||||
Limit uint64
|
||||
SortingColumn Column
|
||||
Asc bool
|
||||
|
||||
sortingConsumed bool
|
||||
}
|
||||
|
||||
func (req *SearchRequest) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
||||
@@ -32,15 +34,20 @@ func (req *SearchRequest) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
||||
if req.Limit > 0 {
|
||||
query = query.Limit(req.Limit)
|
||||
}
|
||||
return req.consumeSorting(query)
|
||||
}
|
||||
|
||||
if !req.SortingColumn.isZero() {
|
||||
// consumeSorting sets the sorting column to the query once.
|
||||
// subsequent calls will not set the sorting column again.
|
||||
func (req *SearchRequest) consumeSorting(query sq.SelectBuilder) sq.SelectBuilder {
|
||||
if !req.sortingConsumed && !req.SortingColumn.isZero() {
|
||||
clause := req.SortingColumn.orderBy()
|
||||
if !req.Asc {
|
||||
clause += " DESC"
|
||||
}
|
||||
query = query.OrderByClause(clause)
|
||||
req.sortingConsumed = true
|
||||
}
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user