#30188: Aggregate annotation, Case() - When(): AssertionError No exception
message
supplied
-------------------------------------+-------------------------------------
Reporter: Lukas Klement | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: query, aggregate, | Triage Stage:
case/when | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Lukas Klement:
Old description:
> Aggregating annotations works for simple Sum, Count, etc. operations, but
> fails when the Sum() contains a Case() When() operation.
>
> Example:
> Objective: sum the number of None values for the annotated field
> field_name across a queryset.
> {{{
> field_name = <name of annotated field>
> missing_data_agg=Sum(
> Case(
> When(**{field_name: None}, then=Value(1)),
> default=Value(0),
> output_field=IntegerField()
> )
> )
> }}}
> Expected behaviour: calling
> queryset.aggregate(missing_data=missing_data_agg) returns a dict
> {'missing_data': <value>}
> Actual behaviour: AssertionError No exception message supplied is
> returned.
>
> When field_name is set to a field that exists on the model of the
> queryset, the aggregation works. When field_name is set to a field that
> is neither in the model of the queryset, nor has it been annotated to the
> queryset, the exception returned is as expected: Cannot resolve keyword
> <name of the non existing field> into field . Choices are: <list of model
> and annotated fields. In this list, the field_name that has been
> annotated to the queryset is shown -> hence it should work, hence I
> assume this is a Django bug.
>
> Stacktrace:
>
> {{{
> <Line with the .aggregate(...)>
> return query.get_aggregation(self.db, kwargs) ...
> /Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
> in get_aggregation
> expression, col_cnt =
> inner_query.rewrite_cols(expression, col_cnt) ...
> /Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
> in rewrite_cols
> new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
> /Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
> in rewrite_cols
> new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
> /Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
> in rewrite_cols
> new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
> /Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
> in rewrite_cols
> new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
> /Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
> in rewrite_cols
> new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
> /Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
> in rewrite_cols
> new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
> /Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
> in rewrite_cols
> annotation.set_source_expressions(new_exprs) ...
> /Users/{{path}}/lib/python3.6/site-
> packages/django/db/models/expressions.py in set_source_expressions
> assert not exprs
> }}}
>
> The database is a PostgreSQL database, the Django version is 2.1.7, the
> Python version is 3.6.4.
New description:
Aggregating annotations works for simple Sum, Count, etc. operations, but
fails when the Sum() contains a Case() When() operation.
Example:
Objective: sum the number of None values for the annotated field
field_name across a queryset.
{{{
field_name = <name of annotated field>
missing_data_agg=Sum(
Case(
When(**{field_name: None}, then=Value(1)),
default=Value(0),
output_field=IntegerField()
)
)
}}}
Expected behaviour: calling
queryset.aggregate(missing_data=missing_data_agg) returns a dict
{'missing_data': <value>}
Actual behaviour: AssertionError No exception message supplied is
returned.
When field_name is set to a field that exists on the model of the
queryset, the aggregation works. When field_name is set to a field that is
neither in the model of the queryset, nor has it been annotated to the
queryset, the exception returned is as expected: Cannot resolve keyword
<name of the non existing field> into field . Choices are: <list of model
and annotated fields. In this list, the field_name that has been annotated
to the queryset is shown -> hence it should work, hence I assume this is a
Django bug.
Stacktrace:
{{{
<Line with the .aggregate(...)>
return query.get_aggregation(self.db, kwargs) ...
/Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
in get_aggregation
expression, col_cnt =
inner_query.rewrite_cols(expression, col_cnt) ...
/Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
in rewrite_cols
new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
/Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
in rewrite_cols
new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
/Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
in rewrite_cols
new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
/Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
in rewrite_cols
new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
/Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
in rewrite_cols
new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
/Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
in rewrite_cols
new_expr, col_cnt = self.rewrite_cols(expr, col_cnt) ...
/Users/{{path}}/lib/python3.6/site-packages/django/db/models/sql/query.py
in rewrite_cols
annotation.set_source_expressions(new_exprs) ...
/Users/{{path}}/lib/python3.6/site-
packages/django/db/models/expressions.py in set_source_expressions
assert not exprs
}}}
The database is a PostgreSQL database, the Django version is 2.1.7, the
Python version is 3.6.4. The problem also exists under Django version
2.2b1
--
--
Ticket URL: <https://code.djangoproject.com/ticket/30188#comment:1>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/070.bb561ef6e6ffe75f0fe4a177c9e01db9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.