Ticket #26617: 26617.patch
File 26617.patch, 3.5 KB (added by , 8 years ago) |
---|
-
django/contrib/postgres/aggregates/general.py
diff --git a/django/contrib/postgres/aggregates/general.py b/django/contrib/postgres/aggregates/general.py index 1dda69c..6ff6727 100644
a b class BoolOr(Aggregate): 32 32 33 33 class StringAgg(Aggregate): 34 34 function = 'STRING_AGG' 35 template = "%(function)s(%( expressions)s, '%(delimiter)s')"35 template = "%(function)s(%(distinct)s%(expressions)s, '%(delimiter)s')" 36 36 37 def __init__(self, expression, delimiter, **extra): 38 super(StringAgg, self).__init__(expression, delimiter=delimiter, **extra) 37 def __init__(self, expression, delimiter, distinct=False, **extra): 38 distinct = 'DISTINCT ' if distinct else '' 39 super(StringAgg, self).__init__(expression, delimiter=delimiter, distinct=distinct, **extra) 39 40 40 41 def convert_value(self, value, expression, connection, context): 41 42 if not value: -
docs/ref/contrib/postgres/aggregates.txt
diff --git a/docs/ref/contrib/postgres/aggregates.txt b/docs/ref/contrib/postgres/aggregates.txt index 3097498..ba912cb 100644
a b General-purpose aggregation functions 63 63 ``StringAgg`` 64 64 ------------- 65 65 66 .. class:: StringAgg(expression, delimiter )66 .. class:: StringAgg(expression, delimiter, distinct=False) 67 67 68 68 Returns the input values concatenated into a string, separated by 69 69 the ``delimiter`` string. … … General-purpose aggregation functions 72 72 73 73 Required argument. Needs to be a string. 74 74 75 .. attribute:: distinct 76 77 .. versionadded: 1.10 78 79 This is a optional boolean argument. 80 81 If ``True``, a concatenated values will be distinct. 82 Default is ``False``. 83 75 84 Aggregate functions for statistics 76 85 ================================== 77 86 -
tests/postgres_tests/test_aggregates.py
diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py index 07e4ea0..7714c73 100644
a b class TestGeneralAggregate(PostgreSQLTestCase): 110 110 values = AggregateTestModel.objects.aggregate(stringagg=StringAgg('char_field', delimiter=';')) 111 111 self.assertEqual(values, {'stringagg': ''}) 112 112 113 def test_string_agg_distinct_false(self): 114 AggregateTestModel.objects.all().delete() 115 AggregateTestModel.objects.create(char_field='Foo') 116 AggregateTestModel.objects.create(char_field='Foo') 117 AggregateTestModel.objects.create(char_field='Bar') 118 models = AggregateTestModel.objects.order_by('char_field') 119 values = models.aggregate(stringagg=StringAgg('char_field', delimiter=' ', distinct=False)) 120 aggregates = values['stringagg'] 121 self.assertEqual(aggregates.count('Foo'), 2) 122 self.assertEqual(aggregates.count('Bar'), 1) 123 124 def test_string_agg_distinct_true(self): 125 AggregateTestModel.objects.all().delete() 126 AggregateTestModel.objects.create(char_field='Foo') 127 AggregateTestModel.objects.create(char_field='Foo') 128 AggregateTestModel.objects.create(char_field='Bar') 129 models = AggregateTestModel.objects.order_by('char_field') 130 values = models.aggregate(stringagg=StringAgg('char_field', delimiter=' ', distinct=True)) 131 aggregates = values['stringagg'] 132 self.assertEqual(aggregates.count('Foo'), 1) 133 self.assertEqual(aggregates.count('Bar'), 1) 134 113 135 114 136 class TestStatisticsAggregate(PostgreSQLTestCase): 115 137 @classmethod