group-by promotion

69 views
Skip to first unread message

Curtis Maloney

unread,
Jan 9, 2016, 12:01:38 AM1/9/16
to django-d...@googlegroups.com
Hey all,

I'm trying to write a PR for adding Postgres9.4's "FILTER" syntax for
aggregates, but I've hit a bug I can't figure how to fix [and nobody's
awake on IRC :)]

https://github.com/django/django/pull/5956

Basically, for the query:

qset = FilterAggParentModel.objects.annotate(
zero_count=FilterAgg(Count('filteraggtestmodel'),
Q(filteraggtestmodel__integer_field=1)),
child_count=Count('filteraggtestmodel'),
)

It generates the SQL:

SELECT "postgres_tests_filteraggparentmodel"."id",
COUNT("postgres_tests_filteraggtestmodel"."id") FILTER(WHERE
"postgres_tests_filteraggtestmodel"."integer_field" = 0) AS "zero_count",
COUNT("postgres_tests_filteraggtestmodel"."id") AS "child_count"
FROM "postgres_tests_filteraggparentmodel"
LEFT OUTER JOIN "postgres_tests_filteraggtestmodel" ON
("postgres_tests_filteraggparentmodel"."id" =
"postgres_tests_filteraggtestmodel"."parent_id")
GROUP BY "postgres_tests_filteraggparentmodel"."id",
"postgres_tests_filteraggtestmodel"."integer_field"


The problem being that
"postgres_tests_filteraggtestmodel"."integer_field" is included in the
GROUP BY clause...

Does anyone who understands this part of the ORM have a suggestion for
how to fix this?

--
Curtis

Anssi Kääriäinen

unread,
Jan 11, 2016, 1:54:38 AM1/11/16
to Django developers (Contributions to Django itself)

If you don't return anything from get_group_by_cols() no part of the expression should be added to the GROUP BY clause. I wonder why the integer_field is added to the group by, a bug somewhere in compiler maybe?

 - Anssi
Reply all
Reply to author
Forward
0 new messages