﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28762	Can't aggregate annotations with array parameters	Daniel Keller		"If I use a database function that takes an array parameter, for example a histogram:

{{{
import math
from django.db import models
from django.contrib.postgres.fields import ArrayField

class Log2Bucket(models.Func):
    function = 'width_bucket'

    def __init__(self, expression, a, b, **extra):
        max_exp = math.ceil(math.log2(b / a))
        buckets = [a * 2**exp for exp in range(max_exp + 1)]
        buckets = models.Value(buckets, output_field=ArrayField(models.FloatField())) ###
        super().__init__(expression, buckets,
            output_field=models.PositiveSmallIntegerField(), **extra)
}}}

and a simple model
{{{
class Foo(models.Model):
    bar = models.FloatField()
}}}

It works fine in queries like
{{{
Foo.objects.annotate(b=Log2Bucket('bar', 1, 100)).values('b')
}}}

but for
{{{
Foo.objects.annotate(b=Log2Bucket('bar', 1, 100)).values('b').annotate(count=models.Count('pk'))
}}}

I get
{{{
...
  File ""/home/daniel_keller/shared/raumplus_2/env/lib/python3.4/site-packages/django/db/models/sql/compiler.py"", line 128, in get_group_by
    if (sql, tuple(params)) not in seen:
TypeError: unhashable type: 'list'
}}}
referring to the list passed to `Value`.

Honestly, I'm not sure if this counts as a bug or a new feature, since it's clearly unintended behavior, but none of Django's database functions need it to work.

I can work around it by replacing the marked line with
{{{
buckets = Value(""{%s}"" % ','.join(map(str, buckets))) ###
}}}
but this is IMO very ugly."	Bug	closed	Database layer (models, ORM)	2.2	Normal	fixed		Josh Smeaton Tomer Chachamu	Accepted	1	0	0	0	0	0
