fix(db): collapse presentation-key groups without min(uuid)
PostgreSQL has no min(uuid) aggregate, so every query that picked a group representative with MIN over the item id failed with 42883: library browse, search, Recently Added, the by-name endpoints and Upcoming. - Pick the representative with an anti-join on (primary version, id) - Cover the collapse with a repository test against a real PostgreSQL
This commit is contained in:
@@ -96,22 +96,36 @@ public sealed partial class BaseItemRepository
|
||||
// primary version (PrimaryVersionId is null) so detail pages and actions target it instead
|
||||
// of an arbitrary alternate. Keep the grouped ids as an IQueryable sub-select; materializing
|
||||
// to a List would inline one bound parameter per id and hit SQLite's variable cap.
|
||||
// The representative is the row no other row in its group sorts before, not MIN(Id):
|
||||
// PostgreSQL has no min(uuid) aggregate, while comparing two uuids is supported everywhere.
|
||||
// The anti-join reads the filtered set twice, so it has to close over a local that the
|
||||
// reassignment below cannot reach - capturing dbQuery itself makes the tree self-referential.
|
||||
var candidates = dbQuery;
|
||||
var enableGroupByPresentationUniqueKey = EnableGroupByPresentationUniqueKey(filter);
|
||||
if (enableGroupByPresentationUniqueKey && filter.GroupBySeriesPresentationUniqueKey)
|
||||
{
|
||||
var groupedIds = dbQuery.GroupBy(e => new { e.PresentationUniqueKey, e.SeriesPresentationUniqueKey })
|
||||
.Select(g => g.Where(e => e.PrimaryVersionId == null).Min(e => (Guid?)e.Id) ?? g.Min(e => (Guid?)e.Id));
|
||||
var groupedIds = candidates
|
||||
.Where(e => !candidates.Any(o => o.PresentationUniqueKey == e.PresentationUniqueKey
|
||||
&& o.SeriesPresentationUniqueKey == e.SeriesPresentationUniqueKey
|
||||
&& ((o.PrimaryVersionId == null && e.PrimaryVersionId != null)
|
||||
|| ((o.PrimaryVersionId == null) == (e.PrimaryVersionId == null) && o.Id.CompareTo(e.Id) < 0))))
|
||||
.Select(e => e.Id);
|
||||
dbQuery = context.BaseItems.AsNoTracking().Where(e => groupedIds.Contains(e.Id));
|
||||
}
|
||||
else if (enableGroupByPresentationUniqueKey)
|
||||
{
|
||||
var groupedIds = dbQuery.GroupBy(e => e.PresentationUniqueKey)
|
||||
.Select(g => g.Where(e => e.PrimaryVersionId == null).Min(e => (Guid?)e.Id) ?? g.Min(e => (Guid?)e.Id));
|
||||
var groupedIds = candidates
|
||||
.Where(e => !candidates.Any(o => o.PresentationUniqueKey == e.PresentationUniqueKey
|
||||
&& ((o.PrimaryVersionId == null && e.PrimaryVersionId != null)
|
||||
|| ((o.PrimaryVersionId == null) == (e.PrimaryVersionId == null) && o.Id.CompareTo(e.Id) < 0))))
|
||||
.Select(e => e.Id);
|
||||
dbQuery = context.BaseItems.AsNoTracking().Where(e => groupedIds.Contains(e.Id));
|
||||
}
|
||||
else if (filter.GroupBySeriesPresentationUniqueKey)
|
||||
{
|
||||
var groupedIds = dbQuery.GroupBy(e => e.SeriesPresentationUniqueKey).Select(e => e.Min(x => x.Id));
|
||||
var groupedIds = candidates
|
||||
.Where(e => !candidates.Any(o => o.SeriesPresentationUniqueKey == e.SeriesPresentationUniqueKey && o.Id.CompareTo(e.Id) < 0))
|
||||
.Select(e => e.Id);
|
||||
dbQuery = context.BaseItems.AsNoTracking().Where(e => groupedIds.Contains(e.Id));
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user