Commit c0cc526e authored by Peter Eisentraut's avatar Peter Eisentraut

Rename bytea_agg to string_agg and add delimiter argument

Per mailing list discussion, we would like to keep the bytea functions
parallel to the text functions, so rename bytea_agg to string_agg,
which already exists for text.

Also, to satisfy the rule that we don't want aggregate functions of
the same name with a different number of arguments, add a delimiter
argument, just like string_agg for text already has.
parent 64e1309c
...@@ -3160,6 +3160,11 @@ ...@@ -3160,6 +3160,11 @@
right within each byte; for example bit 0 is the least significant bit of right within each byte; for example bit 0 is the least significant bit of
the first byte, and bit 15 is the most significant bit of the second byte. the first byte, and bit 15 is the most significant bit of the second byte.
</para> </para>
<para>
See also the aggregate function <function>string_agg</function> in
<xref linkend="functions-aggregate">.
</para>
</sect1> </sect1>
...@@ -10962,24 +10967,6 @@ SELECT NULLIF(value, '(none)') ... ...@@ -10962,24 +10967,6 @@ SELECT NULLIF(value, '(none)') ...
<entry>true if at least one input value is true, otherwise false</entry> <entry>true if at least one input value is true, otherwise false</entry>
</row> </row>
<row>
<entry>
<indexterm>
<primary>bytea_agg</primary>
</indexterm>
<function>
bytea_agg(<replaceable class="parameter">expression</replaceable>)
</function>
</entry>
<entry>
<type>bytea</type>
</entry>
<entry>
<type>bytea</type>
</entry>
<entry>input values concatenated into a bytea</entry>
</row>
<row> <row>
<entry> <entry>
<indexterm> <indexterm>
...@@ -11061,10 +11048,10 @@ SELECT NULLIF(value, '(none)') ... ...@@ -11061,10 +11048,10 @@ SELECT NULLIF(value, '(none)') ...
</function> </function>
</entry> </entry>
<entry> <entry>
<type>text</type>, <type>text</type> (<type>text</type>, <type>text</type>) or (<type>bytea</type>, <type>bytea</type>)
</entry> </entry>
<entry> <entry>
<type>text</type> same as argument types
</entry> </entry>
<entry>input values concatenated into a string, separated by delimiter</entry> <entry>input values concatenated into a string, separated by delimiter</entry>
</row> </row>
......
...@@ -397,7 +397,7 @@ byteasend(PG_FUNCTION_ARGS) ...@@ -397,7 +397,7 @@ byteasend(PG_FUNCTION_ARGS)
} }
Datum Datum
bytea_agg_transfn(PG_FUNCTION_ARGS) bytea_string_agg_transfn(PG_FUNCTION_ARGS)
{ {
StringInfo state; StringInfo state;
...@@ -408,21 +408,28 @@ bytea_agg_transfn(PG_FUNCTION_ARGS) ...@@ -408,21 +408,28 @@ bytea_agg_transfn(PG_FUNCTION_ARGS)
{ {
bytea *value = PG_GETARG_BYTEA_PP(1); bytea *value = PG_GETARG_BYTEA_PP(1);
/* On the first time through, we ignore the delimiter. */
if (state == NULL) if (state == NULL)
state = makeStringAggState(fcinfo); state = makeStringAggState(fcinfo);
else if (!PG_ARGISNULL(2))
{
bytea *delim = PG_GETARG_BYTEA_PP(2);
appendBinaryStringInfo(state, VARDATA_ANY(delim), VARSIZE_ANY_EXHDR(delim));
}
appendBinaryStringInfo(state, VARDATA_ANY(value), VARSIZE_ANY_EXHDR(value)); appendBinaryStringInfo(state, VARDATA_ANY(value), VARSIZE_ANY_EXHDR(value));
} }
/* /*
* The transition type for bytea_agg() is declared to be "internal", * The transition type for string_agg() is declared to be "internal",
* which is a pass-by-value type the same size as a pointer. * which is a pass-by-value type the same size as a pointer.
*/ */
PG_RETURN_POINTER(state); PG_RETURN_POINTER(state);
} }
Datum Datum
bytea_agg_finalfn(PG_FUNCTION_ARGS) bytea_string_agg_finalfn(PG_FUNCTION_ARGS)
{ {
StringInfo state; StringInfo state;
......
...@@ -53,6 +53,6 @@ ...@@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 201204053 #define CATALOG_VERSION_NO 201204131
#endif #endif
...@@ -229,7 +229,7 @@ DATA(insert ( 2335 array_agg_transfn array_agg_finalfn 0 2281 _null_ )); ...@@ -229,7 +229,7 @@ DATA(insert ( 2335 array_agg_transfn array_agg_finalfn 0 2281 _null_ ));
DATA(insert ( 3538 string_agg_transfn string_agg_finalfn 0 2281 _null_ )); DATA(insert ( 3538 string_agg_transfn string_agg_finalfn 0 2281 _null_ ));
/* bytea */ /* bytea */
DATA(insert ( 3545 bytea_agg_transfn bytea_agg_finalfn 0 2281 _null_ )); DATA(insert ( 3545 bytea_string_agg_transfn bytea_string_agg_finalfn 0 2281 _null_ ));
/* /*
* prototypes for functions in pg_aggregate.c * prototypes for functions in pg_aggregate.c
......
...@@ -2433,11 +2433,11 @@ DATA(insert OID = 3536 ( string_agg_finalfn PGNSP PGUID 12 1 0 0 0 f f f f f f ...@@ -2433,11 +2433,11 @@ DATA(insert OID = 3536 ( string_agg_finalfn PGNSP PGUID 12 1 0 0 0 f f f f f f
DESCR("aggregate final function"); DESCR("aggregate final function");
DATA(insert OID = 3538 ( string_agg PGNSP PGUID 12 1 0 0 0 t f f f f f i 2 0 25 "25 25" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ )); DATA(insert OID = 3538 ( string_agg PGNSP PGUID 12 1 0 0 0 t f f f f f i 2 0 25 "25 25" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
DESCR("concatenate aggregate input into a string"); DESCR("concatenate aggregate input into a string");
DATA(insert OID = 3543 ( bytea_agg_transfn PGNSP PGUID 12 1 0 0 0 f f f f f f i 2 0 2281 "2281 17" _null_ _null_ _null_ _null_ bytea_agg_transfn _null_ _null_ _null_ )); DATA(insert OID = 3543 ( bytea_string_agg_transfn PGNSP PGUID 12 1 0 0 0 f f f f f f i 3 0 2281 "2281 17 17" _null_ _null_ _null_ _null_ bytea_string_agg_transfn _null_ _null_ _null_ ));
DESCR("aggregate transition function"); DESCR("aggregate transition function");
DATA(insert OID = 3544 ( bytea_agg_finalfn PGNSP PGUID 12 1 0 0 0 f f f f f f i 1 0 17 "2281" _null_ _null_ _null_ _null_ bytea_agg_finalfn _null_ _null_ _null_ )); DATA(insert OID = 3544 ( bytea_string_agg_finalfn PGNSP PGUID 12 1 0 0 0 f f f f f f i 1 0 17 "2281" _null_ _null_ _null_ _null_ bytea_string_agg_finalfn _null_ _null_ _null_ ));
DESCR("aggregate final function"); DESCR("aggregate final function");
DATA(insert OID = 3545 ( bytea_agg PGNSP PGUID 12 1 0 0 0 t f f f f f i 1 0 17 "17" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ )); DATA(insert OID = 3545 ( string_agg PGNSP PGUID 12 1 0 0 0 t f f f f f i 2 0 17 "17 17" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
DESCR("concatenate aggregate input into a bytea"); DESCR("concatenate aggregate input into a bytea");
/* To ASCII conversion */ /* To ASCII conversion */
......
...@@ -771,8 +771,8 @@ extern Datum unknownsend(PG_FUNCTION_ARGS); ...@@ -771,8 +771,8 @@ extern Datum unknownsend(PG_FUNCTION_ARGS);
extern Datum pg_column_size(PG_FUNCTION_ARGS); extern Datum pg_column_size(PG_FUNCTION_ARGS);
extern Datum bytea_agg_transfn(PG_FUNCTION_ARGS); extern Datum bytea_string_agg_transfn(PG_FUNCTION_ARGS);
extern Datum bytea_agg_finalfn(PG_FUNCTION_ARGS); extern Datum bytea_string_agg_finalfn(PG_FUNCTION_ARGS);
extern Datum string_agg_transfn(PG_FUNCTION_ARGS); extern Datum string_agg_transfn(PG_FUNCTION_ARGS);
extern Datum string_agg_finalfn(PG_FUNCTION_ARGS); extern Datum string_agg_finalfn(PG_FUNCTION_ARGS);
......
...@@ -1061,26 +1061,38 @@ select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; - ...@@ -1061,26 +1061,38 @@ select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; -
a,ab,abcd a,ab,abcd
(1 row) (1 row)
-- bytea_agg tests -- string_agg bytea tests
create table bytea_test_table(v bytea); create table bytea_test_table(v bytea);
select bytea_agg(v) from bytea_test_table; select string_agg(v, '') from bytea_test_table;
bytea_agg string_agg
----------- ------------
(1 row) (1 row)
insert into bytea_test_table values(decode('ff','hex')); insert into bytea_test_table values(decode('ff','hex'));
select bytea_agg(v) from bytea_test_table; select string_agg(v, '') from bytea_test_table;
bytea_agg string_agg
----------- ------------
\xff \xff
(1 row) (1 row)
insert into bytea_test_table values(decode('aa','hex')); insert into bytea_test_table values(decode('aa','hex'));
select bytea_agg(v) from bytea_test_table; select string_agg(v, '') from bytea_test_table;
bytea_agg string_agg
----------- ------------
\xffaa \xffaa
(1 row) (1 row)
select string_agg(v, NULL) from bytea_test_table;
string_agg
------------
\xffaa
(1 row)
select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
string_agg
------------
\xffeeaa
(1 row)
drop table bytea_test_table; drop table bytea_test_table;
...@@ -417,17 +417,19 @@ select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl; -- not ...@@ -417,17 +417,19 @@ select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl; -- not
select string_agg(distinct f1, ',' order by f1::text) from varchar_tbl; -- not ok select string_agg(distinct f1, ',' order by f1::text) from varchar_tbl; -- not ok
select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; -- ok select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; -- ok
-- bytea_agg tests -- string_agg bytea tests
create table bytea_test_table(v bytea); create table bytea_test_table(v bytea);
select bytea_agg(v) from bytea_test_table; select string_agg(v, '') from bytea_test_table;
insert into bytea_test_table values(decode('ff','hex')); insert into bytea_test_table values(decode('ff','hex'));
select bytea_agg(v) from bytea_test_table; select string_agg(v, '') from bytea_test_table;
insert into bytea_test_table values(decode('aa','hex')); insert into bytea_test_table values(decode('aa','hex'));
select bytea_agg(v) from bytea_test_table; select string_agg(v, '') from bytea_test_table;
select string_agg(v, NULL) from bytea_test_table;
select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
drop table bytea_test_table; drop table bytea_test_table;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment