[Django] #23836: aggregate with a sliced queryset doesn't work with relations

27 views
Skip to first unread message

Django

unread,
Nov 15, 2014, 10:20:01 AM11/15/14
to django-...@googlegroups.com
#23836: aggregate with a sliced queryset doesn't work with relations
----------------------------------------------+--------------------
Reporter: reinout | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
#12886 fixed aggregation for sliced querysets by using subqueries.

This also fixed the GIS "Extent" aggregation for sliced querysets, which
was reported in #15101.

While investigating #15101, a test we made failed initially. We noticed
however that we were doing an extent aggregation over a relation. So we
also made a test for the simple slice+extent() case, which worked.

Aha! Then we made a test similar to the one made to test #12886, but which
followed a relation. At least with postgres this resulted in an error:
```column subquery.num_awards does not exist```.

```
Traceback (most recent call last):
File "/d/dev/repos/django/django/tests/aggregation/tests.py", line 664,
in test_aggregation_with_limit_and_relation
vals =
qs.aggregate(average_num_awards=Avg('publisher__num_awards'))['average_num_awards']
File "/d/dev/repos/django/django/django/db/models/query.py", line 342,
in aggregate
return query.get_aggregation(using=self.db, force_subq=force_subq)
File "/d/dev/repos/django/django/django/db/models/sql/query.py", line
366, in get_aggregation
result = compiler.execute_sql(SINGLE)
File "/d/dev/repos/django/django/django/db/models/sql/compiler.py", line
817, in execute_sql
cursor.execute(sql, params)
File "/d/dev/repos/django/django/django/db/backends/utils.py", line 65,
in execute
return self.cursor.execute(sql, params)
File "/d/dev/repos/django/django/django/db/utils.py", line 95, in
__exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/d/dev/repos/django/django/django/db/backends/utils.py", line 65,
in execute
return self.cursor.execute(sql, params)
ProgrammingError: column subquery.num_awards does not exist
LINE 1: SELECT AVG("subquery"."num_awards") FROM (SELECT "aggregatio...
```

I don't know how bad this is. Whether it is even supposed to work.

I also don't know how hard to fix it is :-) Might be just a matter of
doing ```AVG("subquery"."publisher"."num_awards")```, so adding the
```.publisher``` relation name in there.

Pull request with the failing test is upcoming.

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

Django

unread,
Nov 15, 2014, 10:21:20 AM11/15/14
to django-...@googlegroups.com
#23836: aggregate with a sliced queryset doesn't work with relations
-------------------------------------+-------------------------------------

Reporter: reinout | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by reinout):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

New description:

--

--
Ticket URL: <https://code.djangoproject.com/ticket/23836#comment:1>

Django

unread,
Nov 15, 2014, 10:48:14 AM11/15/14
to django-...@googlegroups.com
#23836: aggregate with a sliced queryset doesn't work with relations
-------------------------------------+-------------------------------------

Reporter: reinout | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by reinout):

Here is a branch demonstrating the issue with a test:
https://github.com/jsmits/django/compare/jsmits_ticket_23836

--
Ticket URL: <https://code.djangoproject.com/ticket/23836#comment:2>

Django

unread,
Nov 21, 2014, 3:46:18 PM11/21/14
to django-...@googlegroups.com
#23836: aggregate with a sliced queryset doesn't work with relations
-------------------------------------+-------------------------------------

Reporter: reinout | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/23836#comment:3>

Django

unread,
Apr 14, 2015, 12:58:49 PM4/14/15
to django-...@googlegroups.com
#23836: aggregate with a sliced queryset doesn't work with relations
-------------------------------------+-------------------------------------

Reporter: reinout | Owner: nobody
Type: Bug | 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 yitzc):

* cc: yitzc (added)


Comment:

Hi,
The test from reinout's branch passes against the current master.
Looking at the sql run in the aggregate function seems to properly join
the relation, which allows completing the query successfully.
I think it can be closed.

--
Ticket URL: <https://code.djangoproject.com/ticket/23836#comment:4>

Django

unread,
Apr 14, 2015, 1:03:36 PM4/14/15
to django-...@googlegroups.com
#23836: aggregate with a sliced queryset doesn't work with relations
-------------------------------------+-------------------------------------

Reporter: reinout | Owner: nobody
Type: Bug | 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 timgraham):

It would be nice to bisect to determine the commit where it was fixed to
ensure that a sufficient test was added.

--
Ticket URL: <https://code.djangoproject.com/ticket/23836#comment:5>

Django

unread,
Apr 16, 2015, 12:48:32 PM4/16/15
to django-...@googlegroups.com
#23836: aggregate with a sliced queryset doesn't work with relations
-------------------------------------+-------------------------------------

Reporter: reinout | Owner: nobody
Type: Bug | 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 yitzc):

Looks like it was fixed in #23877

--
Ticket URL: <https://code.djangoproject.com/ticket/23836#comment:6>

Django

unread,
Apr 16, 2015, 12:54:17 PM4/16/15
to django-...@googlegroups.com
#23836: aggregate with a sliced queryset doesn't work with relations
-------------------------------------+-------------------------------------
Reporter: reinout | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

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 timgraham):

* status: new => closed
* resolution: => fixed


Comment:

The tests that were added there look good to me.

--
Ticket URL: <https://code.djangoproject.com/ticket/23836#comment:7>

Reply all
Reply to author
Forward
0 new messages