#26602: RawSQL window functions break count()
-------------------------------------+-------------------------------------
     Reporter:  daggaz               |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  QuerySet.extra       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by jarshwah):

 * cc: josh.smeaton@… (added)
 * version:  1.9 => master
 * stage:  Unreviewed => Accepted


Comment:

 This happens because RawSQL defines:

 {{{
 def get_group_by_cols(self):
         return [self]
 }}}

 It'd be helpful if there was a way to disable grouping in an easy way. For
 the moment, you should be able to do something like:

 {{{
 window = RawSQL("SUM(amount) OVER (ORDER BY created)", [])
 window.get_group_by_cols = lambda: []
 qs = A.objects.annotate(total=window)
 qs.count()

 # Or..

 RawAggregate(RawSQL):
     contains_aggregate = True  # may not be necessary in newer django
 versions
     def get_group_by_cols(self):
         return []

 qs = A.objects.annotate(total=RawAggregate("SUM(amount) OVER (ORDER BY
 created)", []))
 }}}

 Can you let me know if this workaround helps you?

 In the meantime, I think it's worth figuring out how we want to treat
 RawSQL when it should not be doing group bys, so I'm accepting on that
 basis.

 Two options I see. First, we can define a whole new class as I've done
 above - like RawAggregate. Second, we could use a kwarg to RawSQL to tweak
 whether or not it contains aggregate code.

 {{{
 RawSQL('SELECT SUM() ..', [], grouping=False)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26602#comment:2>
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/064.7786576036145b8094818dc1e56f0a16%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to