[Django] #32126: Empty Case() annotation crashes when used with GROUP BY

20 views
Skip to first unread message

Django

unread,
Oct 20, 2020, 11:09:43 AM10/20/20
to django-...@googlegroups.com
#32126: Empty Case() annotation crashes when used with GROUP BY
-------------------------------------+-------------------------------------
Reporter: Hannes | Owner: Hannes Ljungberg
Ljungberg |
Type: Bug | Status: assigned
Component: Database | Version: 3.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Annotating a queryset with a `Case` without any cases will generate an
invalid query when used with a `GROUP BY`.

Example:

{{{
qs = (
CaseTestModel.objects.values('string')
.annotate(test=Case(output_field=IntegerField()),
integer_sum=Sum('integer'))
.order_by('string')
)
}}}

Executing this queryset generates the following SQL:

{{{
SELECT "expressions_case_casetestmodel"."string", NULL AS "test",
SUM("expressions_case_casetestmodel"."integer") AS "integer_sum" FROM
"expressions_case_casetestmodel" GROUP BY
"expressions_case_casetestmodel"."string", NULL ORDER BY
"expressions_case_casetestmodel"."string" ASC
}}}

Causing the following error:

{{{
django.db.utils.ProgrammingError: non-integer constant in GROUP BY
LINE 1: ...OUP BY "expressions_case_casetestmodel"."string", NULL ORDER...
}}}

The `Case` annotation should be excluded from the `GROUP BY` if it
contains no cases.

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

Django

unread,
Oct 20, 2020, 11:12:40 AM10/20/20
to django-...@googlegroups.com
#32126: Empty Case() annotation crashes when used with GROUP BY
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes

| Ljungberg
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

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

* has_patch: 0 => 1


Comment:

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

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

Django

unread,
Oct 20, 2020, 2:01:43 PM10/20/20
to django-...@googlegroups.com
#32126: Empty Case() annotation crashes when used with GROUP BY
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes

| Ljungberg
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(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 Mariusz Felisiak):

* stage: Unreviewed => Accepted


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

Django

unread,
Oct 20, 2020, 2:20:05 PM10/20/20
to django-...@googlegroups.com
#32126: Empty Case() annotation crashes when used with GROUP BY
-------------------------------------+-------------------------------------
Reporter: Hannes Ljungberg | Owner: Hannes

| Ljungberg
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(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 Simon Charette):

* stage: Accepted => Ready for checkin


Comment:

Patch LGTM assuming it passes the suite on Oracle.

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

Reply all
Reply to author
Forward
0 new messages