#26434: Inconsistent results of QuerySet.count() when ordering is not a subset 
of
explicit grouping.
-------------------------------------+-------------------------------------
     Reporter:  kamandol             |                    Owner:  Michal
                                     |  Mládek
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  fixed
     Keywords:  postgresql queryset  |             Triage Stage:  Ready for
  count annotate aggreagate          |  checkin
  order_by                           |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):

 This caused a [https://djangoci.com/job/django-
 
oracle/database=oracle19,label=oracle,python=python3.14/lastCompletedBuild/testReport/
 regression] on Oracle but not only. `group_by` is a tuple of `Col`
 expressions, `order_by` is a list of strings, so `{*self.order_by,
 *self.extra_order_by}` will never be a subset of `group_by`.

 Previously:

 {{{#!sql
 SELECT
    ...
 FROM "AGGREGATION_REGRESS_BOOK"
 WHERE "AGGREGATION_REGRESS_BOOK"."ID" IN (
    SELECT MAX(U0."ID") AS "ID__MAX"
    FROM "AGGREGATION_REGRESS_BOOK" U0
    GROUP BY U0."CONTACT_ID"
 )
 ORDER BY "AGGREGATION_REGRESS_BOOK"."ID" ASC
 }}}
 with this patch

 {{{#!sql
 SELECT
    ...
 FROM "AGGREGATION_REGRESS_BOOK"
 WHERE "AGGREGATION_REGRESS_BOOK"."ID" IN (
    SELECT MAX(U0."ID") AS "ID__MAX"
    FROM "AGGREGATION_REGRESS_BOOK" U0
    GROUP BY U0."CONTACT_ID"
    ORDER BY U0."CONTACT_ID" ASC
 )
 ORDER BY "AGGREGATION_REGRESS_BOOK"."ID" ASC
 }}}

 As far as I'm aware, we would need to translate string aliases (`order_by`
 and `extra_order_by`) to `Col` expressions but this may be expensive here
 (or at least non-trivial).
-- 
Ticket URL: <https://code.djangoproject.com/ticket/26434#comment:29>
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 view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019a29f962be-9cda7fc2-0f6f-4688-8ac2-82c2205e244c-000000%40eu-central-1.amazonses.com.

Reply via email to