#28296: Add support for aggregation through subqueries
-------------------------------------+-------------------------------------
     Reporter:  László Károlyi       |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * version:  1.11 => master
 * type:  Bug => New feature
 * stage:  Unreviewed => Accepted


Comment:

 Hello László, thanks for taking the time to provide all these details.

 I think that what you'd like to do use is the `aggregate` method instead
 of the `annotate` one to avoid the group by. Something along the lines of

 {{{#!python
 Performance.objects.annotate(
     reserved_seats=Subquery(
         SeatReservation.objects.filter(
             performance=OuterRef(name='pk'),
             status__in=TAKEN_TYPES,
         ).aggregate(Count('pk'))
     ),
 )
 }}}


 Unfortunately the `Subquery` API
 [https://github.com/django/django/pull/6478#issuecomment-217759006 doesn't
 allow that yet].

 It would require to either make `aggregate` return a lazy object that
 `Subquery` can deal with (right now it returns a `dict` on call) which has
 the potential of breaking backward compatibility or introducing a new kind
 of expression to deal with this case (e.g. `AggregateSubquery(query,
 Count('pk'))`).

 In an ideal world we'd be able to call `count()` directly and avoid the
 `Subquery` wrapper


 {{{#!python
 Performance.objects.annotate(
     reserved_seats=SeatReservation.objects.filter(
         performance=OuterRef(name='pk'),
         status__in=TAKEN_TYPES,
     ).count()
 )
 }}}

 I'm converting the ticket to a feature request for subquery aggregation.
 It has a bit of overlap with #10060 as it would add a way to work around
 the issue by explicitly using subqueries as opposed to having the ORM do
 automatic pushdown but I still consider this a feature on it's own.

--
Ticket URL: <https://code.djangoproject.com/ticket/28296#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.9d3073a6a9559da81d1d338771780301%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to