# HG changeset patch # User Julien Cristau # Date 1384428389 -3600 # Node ID 73bd5012336fbde0253c01ae0930fc3a05ff4bd0 # Parent d773589b6d46d2de303cb0d97256e0043dde78c8 Make the GROUP_CONCAT aggregate function not repeat values (closes #3223975) Work on sets instead of arrays, so if the same value appears twice it's not repeated in the concatenated output. This patch handles the postgresql and sqlite backends, mysql is left alone at this point (seems doable, but I don't have time or motivation to fix and test it). diff -r d773589b6d46 -r 73bd5012336f schemas/_regproc.postgres.sql --- a/schemas/_regproc.postgres.sql Tue Jan 07 15:48:31 2014 +0100 +++ b/schemas/_regproc.postgres.sql Thu Nov 14 12:26:29 2013 +0100 @@ -10,10 +10,15 @@ SELECT array_to_string($1, ', ') $$ LANGUAGE SQL;; + +CREATE FUNCTION cw_array_append_unique (anyarray, anyelement) RETURNS anyarray AS $$ + SELECT array_append($1, (SELECT $2 WHERE $2 <> ALL($1))) +$$ LANGUAGE SQL + DROP AGGREGATE IF EXISTS group_concat (anyelement) CASCADE; CREATE AGGREGATE group_concat ( basetype = anyelement, - sfunc = array_append, + sfunc = cw_array_append_unique, stype = anyarray, finalfunc = comma_join, initcond = '{}' diff -r d773589b6d46 -r 73bd5012336f server/sqlutils.py --- a/server/sqlutils.py Tue Jan 07 15:48:31 2014 +0100 +++ b/server/sqlutils.py Thu Nov 14 12:26:29 2013 +0100 @@ -327,10 +327,10 @@ class group_concat(object): def __init__(self): - self.values = [] + self.values = set() def step(self, value): if value is not None: - self.values.append(value) + self.values.add(value) def finalize(self): return ', '.join(unicode(v) for v in self.values)