#32297: QuerySet.get() method not working as expected with Window functions
-------------------------------------+-------------------------------------
Reporter: Jerin Peter George | 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 Simon Charette):
It's just how window functions behave when mixed with a query level
constraint (`WHERE`) or `LIMIT`'ing;
In your first example the `[:10]` performs a `LIMIT 10` so the `RANK` will
be over ten results while `get(pk=1442)` will be against in a single
result through `WHERE id = 1442 LIMIT 21`.
To me this is a similar class of problem to #24462, #28333. If it was
possible to use the current result set as a subquery then the following
would work as expected
{{{#!python
Task.objects.annotate(
rank=Window(expression=Rank(), order_by=F('logged').desc())
)[0:10].subquery().get(pk=1442)
}}}
As it would result in
{{{#!sql
SELECT * FROM (
SELECT task.*, RANK() OVER (ORDER BY logged DESC) AS "rank"
FROM task
LIMIT 10
) subquery WHERE id=1442 LIMIT 21
}}}
Where the window function would span over the full ten set of rows.
Whether or not `get` should do the subquery pushdown automatically is
debatable. I get a feeling it should as the fact it uses `LIMIT` is more
of an implementation detail but it would be slightly backward
incompatible.
--
Ticket URL: <https://code.djangoproject.com/ticket/32297#comment:6>
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 on the web visit
https://groups.google.com/d/msgid/django-updates/074.e328c79d8309ff8dcf5b807905fe1c87%40djangoproject.com.