[Django] #32662: Refactor a generator out from part of SQLCompiler.get_order_by()

25 views
Skip to first unread message

Django

unread,
Apr 18, 2021, 10:00:38 AM4/18/21
to django-...@googlegroups.com
#32662: Refactor a generator out from part of SQLCompiler.get_order_by()
------------------------------------------------+------------------------
Reporter: Chris Jerdonek | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Uncategorized | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
I noticed that `SQLCompiler.get_order_by()` has a 70-line
[https://github.com/django/django/blob/aa4acc164d1247c0de515c959f7b09648b57dc42/django/db/models/sql/compiler.py#L297-L368
chunk of code] whose job is to build up an `order_by` list, which is then
iterated over
[https://github.com/django/django/blob/aa4acc164d1247c0de515c959f7b09648b57dc42/django/db/models/sql/compiler.py#L372
right after]. That chunk of code has 8 `order_by.append()` lines. It looks
like this could be made cleaner by pulling those lines out into a private
method that is instead a generator that yields those pairs. That would
eliminate the need for having `order_by.append()` in several spots, and it
would reduce the size of the method into two smaller methods.

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

Django

unread,
Apr 18, 2021, 7:32:31 PM4/18/21
to django-...@googlegroups.com
#32662: Refactor a generator out from part of SQLCompiler.get_order_by()
-------------------------------------+-------------------------------------

Reporter: Chris Jerdonek | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Uncategorized | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Chris Jerdonek:

Old description:

> I noticed that `SQLCompiler.get_order_by()` has a 70-line
> [https://github.com/django/django/blob/aa4acc164d1247c0de515c959f7b09648b57dc42/django/db/models/sql/compiler.py#L297-L368
> chunk of code] whose job is to build up an `order_by` list, which is then
> iterated over
> [https://github.com/django/django/blob/aa4acc164d1247c0de515c959f7b09648b57dc42/django/db/models/sql/compiler.py#L372
> right after]. That chunk of code has 8 `order_by.append()` lines. It
> looks like this could be made cleaner by pulling those lines out into a
> private method that is instead a generator that yields those pairs. That
> would eliminate the need for having `order_by.append()` in several spots,
> and it would reduce the size of the method into two smaller methods.

New description:

right after]. That chunk of code has 8 `order_by.append()` lines and an
[https://github.com/django/django/blob/aa4acc164d1247c0de515c959f7b09648b57dc42/django/db/models/sql/compiler.py#L357-L358
order_by.extend()]. It looks like this could be made cleaner by pulling


those lines out into a private method that is instead a generator that
yields those pairs. That would eliminate the need for having
`order_by.append()` in several spots, and it would reduce the size of the
method into two smaller methods.

--

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

Django

unread,
Apr 19, 2021, 2:25:56 AM4/19/21
to django-...@googlegroups.com
#32662: Refactor a generator out from part of SQLCompiler.get_order_by()
-------------------------------------+-------------------------------------

Reporter: Chris Jerdonek | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(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 Mariusz Felisiak):

* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Accepted


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

Django

unread,
Apr 21, 2021, 3:04:52 AM4/21/21
to django-...@googlegroups.com
#32662: Refactor a generator out from part of SQLCompiler.get_order_by()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned

Component: Database layer | Version: dev
(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 Chris Jerdonek):

* owner: nobody => Chris Jerdonek
* status: new => assigned


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

Django

unread,
Apr 21, 2021, 5:29:20 AM4/21/21
to django-...@googlegroups.com
#32662: Refactor a generator out from part of SQLCompiler.get_order_by()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Chris Jerdonek):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/14292

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

Django

unread,
Apr 21, 2021, 6:33:58 AM4/21/21
to django-...@googlegroups.com
#32662: Refactor a generator out from part of SQLCompiler.get_order_by()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


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

Django

unread,
Apr 21, 2021, 10:20:44 AM4/21/21
to django-...@googlegroups.com
#32662: Refactor a generator out from part of SQLCompiler.get_order_by()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
Type: | Jerdonek
Cleanup/optimization | Status: closed

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

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"0461b7a6b62a5acd0db47f8dd76f3dfe7c62b0f5" 0461b7a6]:
{{{
#!CommitTicketReference repository=""
revision="0461b7a6b62a5acd0db47f8dd76f3dfe7c62b0f5"
Fixed #32662 -- Refactored a generator out of SQLCompiler.get_order_by().

This also renames the `asc` variable to `default_order`, markes the
`desc` variable as unused, fixes a typo in SQLCompiler.get_order_by()
docstring, and reorders some blocks in SQLCompiler._order_by_pairs().
}}}

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

Reply all
Reply to author
Forward
0 new messages