Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#29172 closed Bug (fixed)

Using Window in Subquery causes AttributeError

Reported by: Tomáš Ehrlich Owned by: nobody
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords: subquery window expression orm
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using Window expression within Subquery causes an AttributeError: 'NoneType' object has no attribute ‘relabeled_clone'

Error is raised in relabeled_clone method,
https://github.com/django/django/blob/master/django/db/models/expressions.py#L319
which calls e.relabeled_clone method on every expression returned from get_source_expressions.

Window expression returns source_expression, partition_by, order_by and frame in get_source_expression:
https://github.com/django/django/blob/master/django/db/models/expressions.py#L1188
but all parameters except source_expression are optional and might be None. This leads to AttributeError.

Change History (5)

comment:1 by Tomáš Ehrlich, 6 years ago

Has patch: set

comment:2 by Carlton Gibson, 6 years ago

Triage Stage: UnreviewedAccepted

This seems reasonable yes. Test case from PR should work:

        subquery_qs = Employee.objects.annotate(
            highest=Window(FirstValue('id'), partition_by=F('department'), order_by=F('salary').desc())
        ).values('highest')

        highest_salary = Employee.objects.filter(pk__in=subquery_qs)

comment:3 by Carlton Gibson, 6 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In fa352626:

Fixed #29172 -- Fixed crash with Window expression in a subquery.

comment:5 by Tim Graham <timograham@…>, 6 years ago

In 3cdc88b:

[2.0.x] Fixed #29172 -- Fixed crash with Window expression in a subquery.

Backport of fa352626c2a80bcdcd0fc6492b5fd5130490f05e from master

Note: See TracTickets for help on using tickets.
Back to Top