Re: [Django] #28296: Add support for aggregation through subqueries

2026-03-27 Thread Django
#28296: Add support for aggregation through subqueries
-+-
 Reporter:  László Károlyi   |Owner:  B
 |  Martsberger
 Type:  New feature  |   Status:  assigned
Component:  Database layer   |  Version:  dev
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:  Accepted
Has patch:  1|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  1
Easy pickings:  0|UI/UX:  0
-+-
Changes (by Balazs Endresz):

 * cc: Balazs Endresz (added)

-- 
Ticket URL: 
Django 
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 view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019d2f3cac70-099ed759-5e6e-4171-ba55-cc044fbd412a-00%40eu-central-1.amazonses.com.


Re: [Django] #28296: Add support for aggregation through subqueries

2024-01-25 Thread Django
#28296: Add support for aggregation through subqueries
-+-
 Reporter:  László Károlyi   |Owner:  B
 |  Martsberger
 Type:  New feature  |   Status:  assigned
Component:  Database layer   |  Version:  dev
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:  Accepted
Has patch:  1|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  1
Easy pickings:  0|UI/UX:  0
-+-
Changes (by Ionel Cristian Mărieș):

 * cc: Ionel Cristian Mărieș (added)

-- 
Ticket URL: 
Django 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018d40d66c6d-caf6067c-61a8-410a-a115-e22303975db6-00%40eu-central-1.amazonses.com.


Re: [Django] #28296: Add support for aggregation through subqueries

2023-12-20 Thread Django
#28296: Add support for aggregation through subqueries
-+-
 Reporter:  László Károlyi   |Owner:  B
 |  Martsberger
 Type:  New feature  |   Status:  assigned
Component:  Database layer   |  Version:  dev
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:  Accepted
Has patch:  1|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  1
Easy pickings:  0|UI/UX:  0
-+-
Changes (by şuayip üzülmez):

 * cc: şuayip üzülmez (added)


-- 
Ticket URL: 
Django 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018c872a608e-f27a7813-42e7-4986-b9e6-91f7033ecc3f-00%40eu-central-1.amazonses.com.


Re: [Django] #28296: Add support for aggregation through subqueries

2022-08-13 Thread Django
#28296: Add support for aggregation through subqueries
-+-
 Reporter:  László Károlyi   |Owner:  B
 |  Martsberger
 Type:  New feature  |   Status:  assigned
Component:  Database layer   |  Version:  dev
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:  Accepted
Has patch:  1|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  1
Easy pickings:  0|UI/UX:  0
-+-

Comment (by Simon Charette):

 #10621 was a 13 years old duplicate.

-- 
Ticket URL: 
Django 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701829a1e7469-ebd36fc7-a29c-48cf-9a40-56502772ae3c-00%40eu-central-1.amazonses.com.


Re: [Django] #28296: Add support for aggregation through subqueries

2021-01-01 Thread Django
#28296: Add support for aggregation through subqueries
-+-
 Reporter:  László Károlyi   |Owner:  B
 |  Martsberger
 Type:  New feature  |   Status:  assigned
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:  Accepted
Has patch:  1|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  1
Easy pickings:  0|UI/UX:  0
-+-

Comment (by B Martsberger):

 Matt, I'm not sure your use case will be improved by this ticket due to
 the need to group by a computed field. But it also looks like it's not
 necessary to subclass Subquery with a new template.


 {{{
 event_day_subquery = Subquery(
 Event.objects.annotate(day=TruncDay('timestamp'))
  .values('project_id', 'day')
  .annotate(cnt=Count(Value(1)))
  .values('cnt')
  .filter(project_id=OuterRef('id'))

 Player.objects.annotate(day_count=event_day_count).values('id',
 'day_count')
 }}}

 Produces SQL like

 {{{
 SELECT "player_player"."id",
 (SELECT COUNT(1) AS "cnt"
  FROM "player_event" U0
  WHERE U0."player_id" = "player_player"."id"
  GROUP BY U0."player_id",
   DATE_TRUNC('day', U0."timestamp" )) AS "day_count"
 FROM "player_player"
 }}}

 Replying to [comment:17 Matt Hegarty]:

-- 
Ticket URL: 
Django 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.9e7921a4f7a3687d7cd65d71a9a0b8be%40djangoproject.com.


Re: [Django] #28296: Add support for aggregation through subqueries

2021-01-01 Thread Django
#28296: Add support for aggregation through subqueries
-+-
 Reporter:  László Károlyi   |Owner:  B
 |  Martsberger
 Type:  New feature  |   Status:  assigned
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:  Accepted
Has patch:  1|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  1
Easy pickings:  0|UI/UX:  0
-+-

Comment (by Matt Hegarty):

 I wanted to comment in case it helps anyone else.

 My use case was to use Django's subquery API to retrieve a count of
 "active" days.  That is, the number of days on which events were recorded.

 The (postgres) SQL for this query would be:

 {{{
 SELECT "player_player"."id",
(SELECT count(1) from (select date_trunc('day', evt.timestamp)
 "day"
  FROM "player_event" evt
  WHERE (evt."player_id" = "player_player"."id")
  GROUP BY evt."player_id", "day") a) AS "day_count"

 FROM "player_player"
 }}}

 I had to solve this using a subclass of Subquery:

 {{{
 class EventDayCountSubquery(Subquery):
 template = "(SELECT COUNT(1) FROM (%(subquery)s) _count)"
 output_field = IntegerField()
 }}}

 I could then use this as a regular annotation:

 {{{
 event_day_count = (
 Event.objects.filter(player=OuterRef("pk"))
 .annotate(day=TruncDay("timestamp")).values("day")
 .annotate(count=Count("pk"))
 )

 Player.objects.annotate(
 event_day_count_90_day=EventDayCountSubquery(event_day_count)
 )
 }}}

-- 
Ticket URL: 
Django 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.b2b1d971afc1fa3ca3e20f36a222f20d%40djangoproject.com.


Re: [Django] #28296: Add support for aggregation through subqueries

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

 * needs_better_patch:  0 => 1


Comment:

 Marking as "needs improvement" due to Simon's comments.

-- 
Ticket URL: 
Django 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.3122271c937cd923606cdb7507f3ff3f%40djangoproject.com.


Re: [Django] #28296: Add support for aggregation through subqueries

2019-10-24 Thread Django
#28296: Add support for aggregation through subqueries
-+-
 Reporter:  László Károlyi   |Owner:  B
 |  Martsberger
 Type:  New feature  |   Status:  assigned
Component:  Database layer   |  Version:  master
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:  Accepted
Has patch:  1|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-
Changes (by Evgeny Arshinov):

 * cc: Evgeny Arshinov (added)


-- 
Ticket URL: 
Django 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.361a0dbca49a473f58554a76a486f2ea%40djangoproject.com.


Re: [Django] #28296: Add support for aggregation through subqueries

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

 * has_patch:  0 => 1


Comment:

 [https://github.com/django/django/pull/11841 PR]

-- 
Ticket URL: 
Django 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.180fe9d648ad2efe971d2eb5fe3b235b%40djangoproject.com.


Re: [Django] #28296: Add support for aggregation through subqueries

2019-09-27 Thread Django
#28296: Add support for aggregation through subqueries
-+-
 Reporter:  László Károlyi   |Owner:  B
 |  Martsberger
 Type:  New feature  |   Status:  assigned
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 B Martsberger):

 * owner:  nobody => B Martsberger
 * status:  new => assigned


-- 
Ticket URL: 
Django 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.624a3d7a42c68a7b78874f31bb5fef50%40djangoproject.com.


Re: [Django] #28296: Add support for aggregation through subqueries

2019-04-02 Thread Django
#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
-+-

Comment (by Simon Charette):

 I looks like someone went ahead [https://github.com/martsberger/django-
 sql-utils and implemented] the `AggregateSubquery` API.

 I'd like to throw another one into the mix which I believe would be more
 suitable than the `AggregateSubquery` and `subquery` kwarg idea; an
 `as_subquery()` method that returns a `Subquery` instance. It would offer
 a nicer interface through composition and allow encapsulation of most of
 the subquery creation logic instead of doing it at `Aggregate` expression
 resolving and compiling time. The `as_*` pattern is also coherent with the
 `Queryset.as_manager` interface so it shouldn't be completely alien to
 most of our users.

-- 
Ticket URL: 
Django 
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.e0ddf587de6dce38dbd70f9259e5ddda%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28296: Add support for aggregation through subqueries

2018-11-28 Thread Django
#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
-+-

Comment (by Simon Charette):

 Another API alternative could be allow queries to be passed directly to
 aggregation functions

 {{{#!python
 # Implicit Count(*)
 Performance.objects.annotate(
 reserved_seats=Count(SeatReservation.objects.filter(
 performance=OuterRef('pk'),
 status__in=TAKEN_TYPES,
 ))
 )

 # Explicit Count('pk')
 Performance.objects.annotate(
 reserved_seats=Count(SeatReservation.objects.filter(
 performance=OuterRef('pk'),
 status__in=TAKEN_TYPES,
 ).values('pk'))
 )

 # Sum('seat__amount')
 Performance.objects.annotate(
 reserved_seats_total_amount=Sum(SeatReservation.objects.filter(
 performance=OuterRef('pk'),
 status__in=TAKEN_TYPES,
 ).values('seat__amount')
 )
 }}}

 Or to allow passing a `subquery` kwarg to aggregation expressions. It can
 either be `True` or a query

 {{{#!python
 # subquery=True
 Performance.objects.annotate(
 reserved_seats=Count(
 'seat_reservations',
 filter=Q(status__in=TAKEN_TYPES),
 subquery=True,
 ),
 )

 # explicit subquery
 Performance.objects.annotate(
 reserved_seats=Count(
 'seat_reservations',
 subquery=SeatReservation.objects.filter(
 status__in=TAKEN_TYPES,
 ),
 ),
 )

 # Sum('seat_reservations__seat__amount')
 Performance.objects.annotate(
 reserved_seats_total_amount=Sum(
 'seat_reservations__seat__amount'',
 subquery=True,
 ),
 )
 }}}

 I think I like  the`subquery` kwarg alternative better. It blends
 naturally with the newly added `filter` kwarg and result in less verbose
 expressions as the `subquery` shouldn't have to be provided most of the
 time.

-- 
Ticket URL: 
Django 
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.59b24de8741c893d2a4146e4aebee6b9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28296: Add support for aggregation through subqueries

2018-11-27 Thread Django
#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
-+-

Comment (by Simon Charette):

 I did a bit of investigation to determine the length of changes this would
 require. I started with trying to make `count()` return a lazy object as
 it's a bit easier to implement than `aggregate()`. I didn't implement the
 `resolve_expression` or `as_sql` for now.

 As expected making `count()` lazy breaks a few tests that expect the query
 to be immediately executed but but only a few adjustments to `LazyObject`
 are required to get most of the suite passing.

 Still this breaks backward compatiblity and would certainly break more
 than just test code out there. For example, a view a could be getting a
 count, perfoming some alterations and retrieving a new count and that
 would break if the first query is not immediately executed.

 The work I've done can be found here
 https://github.com/django/django/compare/master...charettes:ticket-28296

 What I suggest we do here instead is to introduce a new
 `AggregateSubQuery(queryset, aggregate)` expression instead. It's
 certainly not as elegant as using `count()` or `aggregate()` but it does
 maintain backward compatiblity.

 e.g.

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

-- 
Ticket URL: 
Django 
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.103247dd598354dbe184f8333cb1955d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28296: Add support for aggregation through subqueries

2018-11-27 Thread Django
#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
-+-

Comment (by Simon Charette):

 > A lazy dict or AggregateResult doesn't have to be too complicated.
 Providing __getitem__, __contains__, etc.. like UserDict would do the
 trick.

 The main challenge here is that it changes the nature of `count()` and
 `aggregate()` methods so that they no longer perform a query
 automatically. This is certainly going to break a few tests but it should
 be possible to provide shims through `__int__` for `count()` and `dict`
 subclassing for `aggregate`.

-- 
Ticket URL: 
Django 
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.11652e283064d46a48f3291d26b65792%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28296: Add support for aggregation through subqueries

2018-11-27 Thread Django
#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):

 * cc: Simon Charette (added)


-- 
Ticket URL: 
Django 
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.9046bbf312670175b94115a4de23cad2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28296: Add support for aggregation through subqueries

2018-09-18 Thread Django
#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
-+-

Comment (by Diederik van der Boor):

 > ... either make aggregate return a lazy object that Subquery can deal
 with (right now it returns a dict on call)

 I second that! It would make subqueries much easier to use.
 A lazy dict or `AggregateResult` doesn't have to be too complicated.
 Providing `__getitem__`, `__contains__`, etc.. like `UserDict` would do
 the trick.
 The same could happen with `.count()`. By letting it return a lazy
 `CountResult` object that casts to an int, it doesn't change the existing
 code, but also offers some `.query` attribute that `Subquery()` can use.

 This would allow things like:

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

 Or even:

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

-- 
Ticket URL: 
Django 
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.167f98471ab2158ec9efcbab779383f5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28296: Add support for aggregation through subqueries

2018-07-22 Thread Django
#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
-+-

Comment (by S. Dole Melipone):

 I would like to offer the following solution using the new SQL `Window`
 class. This allows the aggregation of annotated values by calculating the
 aggregate over partitions based on the outer query model (in the GROUP BY
 clause), then annotating that data to every row in the subquery queryset.
 The subquery can then use the aggregated data from the first row returned
 and ignore the other rows.

 I'd like some opinions and feedback on whether this is the optimal
 solution for aggregating arbitrary annotated data in a subquery. If so I'd
 be happy to write something up for the documentation. I couldn't find any
 other way to do this in the ORM! This also works well for calling `Sum`,
 `Avg` etc. rather than `Count` on annotated values on the subquery
 queryset.

 {{{
 Performance.objects.annotate(
 reserved_seats=Subquery(
 SeatReservation.objects.filter(
 performance=OuterRef(name='pk'),
 status__in=TAKEN_TYPES,
 ).annotate(
 reserved_seat_count=Window(
 expression=Count('pk'),
 partition_by=[F('performance')]
 ),
 ).values('reserved_seat_count')[:1],
 output_field=FloatField()
 )
 )
 }}}

-- 
Ticket URL: 
Django 
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.61cfe5e38916eb452c078a465900a437%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28296: Add support for aggregation through subqueries

2018-07-16 Thread Django
#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
-+-

Comment (by Sylvain Zimmer):

 Hello !

 I solved this with a custom class:

 {{{#!python

 class SubQueryCount(Subquery):
 output_field = models.IntegerField()
 def __init__(self, *args, **kwargs):
 Subquery.__init__(self, *args, **kwargs)
 self.queryset =
 self.queryset.annotate(cnt=Count("*")).values("cnt")
 self.queryset.query.set_group_by()  # values() adds a GROUP BY we
 don't want here

 subquery = Media.objects.filter(artist=OuterRef('id'))
 artists_with_count_medias =
 Artist.objects.all().annotate(count_medias=SubQueryCount(subquery))

 }}}

-- 
Ticket URL: 
Django 
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.6bc881c37f6cf8097043b262b623faac%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28296: Add support for aggregation through subqueries

2017-06-11 Thread Django
#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
-+-

Comment (by Matthew Schinckel):

 I attempted to comment in the SO post, but comments are hardly conducive
 to code blocks.

 I've written several `Subquery` subclasses to do aggregation of the
 subquery: perhaps that will work here.


 {{{
 class Count(Subquery):
 template = "(SELECT COUNT(*) FROM (%(subquery)s) _count)"

 @property
 def output_field(self):
 return models.IntegerField()
 }}}

--
Ticket URL: 
Django 
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.8e2602917ce10f05bcd32ecdfd632d4b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28296: Add support for aggregation through subqueries (was: Subquery with annotation fails without tweaking)

2017-06-11 Thread Django
#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: 
Django 
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.