Skip to content

Commit c0cc526

Browse files
committed
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.
1 parent 64e1309 commit c0cc526

File tree

8 files changed

+52
-44
lines changed

8 files changed

+52
-44
lines changed

doc/src/sgml/func.sgml

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,6 +3160,11 @@
31603160
right within each byte; for example bit 0 is the least significant bit of
31613161
the first byte, and bit 15 is the most significant bit of the second byte.
31623162
</para>
3163+
3164+
<para>
3165+
See also the aggregate function <function>string_agg</function> in
3166+
<xref linkend="functions-aggregate">.
3167+
</para>
31633168
</sect1>
31643169

31653170

@@ -10962,24 +10967,6 @@ SELECT NULLIF(value, '(none)') ...
1096210967
<entry>true if at least one input value is true, otherwise false</entry>
1096310968
</row>
1096410969

10965-
<row>
10966-
<entry>
10967-
<indexterm>
10968-
<primary>bytea_agg</primary>
10969-
</indexterm>
10970-
<function>
10971-
bytea_agg(<replaceable class="parameter">expression</replaceable>)
10972-
</function>
10973-
</entry>
10974-
<entry>
10975-
<type>bytea</type>
10976-
</entry>
10977-
<entry>
10978-
<type>bytea</type>
10979-
</entry>
10980-
<entry>input values concatenated into a bytea</entry>
10981-
</row>
10982-
1098310970
<row>
1098410971
<entry>
1098510972
<indexterm>
@@ -11061,10 +11048,10 @@ SELECT NULLIF(value, '(none)') ...
1106111048
</function>
1106211049
</entry>
1106311050
<entry>
11064-
<type>text</type>, <type>text</type>
11051+
(<type>text</type>, <type>text</type>) or (<type>bytea</type>, <type>bytea</type>)
1106511052
</entry>
1106611053
<entry>
11067-
<type>text</type>
11054+
same as argument types
1106811055
</entry>
1106911056
<entry>input values concatenated into a string, separated by delimiter</entry>
1107011057
</row>

src/backend/utils/adt/varlena.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ byteasend(PG_FUNCTION_ARGS)
397397
}
398398

399399
Datum
400-
bytea_agg_transfn(PG_FUNCTION_ARGS)
400+
bytea_string_agg_transfn(PG_FUNCTION_ARGS)
401401
{
402402
StringInfo state;
403403

@@ -408,21 +408,28 @@ bytea_agg_transfn(PG_FUNCTION_ARGS)
408408
{
409409
bytea *value = PG_GETARG_BYTEA_PP(1);
410410

411+
/* On the first time through, we ignore the delimiter. */
411412
if (state == NULL)
412413
state = makeStringAggState(fcinfo);
414+
else if (!PG_ARGISNULL(2))
415+
{
416+
bytea *delim = PG_GETARG_BYTEA_PP(2);
417+
418+
appendBinaryStringInfo(state, VARDATA_ANY(delim), VARSIZE_ANY_EXHDR(delim));
419+
}
413420

414421
appendBinaryStringInfo(state, VARDATA_ANY(value), VARSIZE_ANY_EXHDR(value));
415422
}
416423

417424
/*
418-
* The transition type for bytea_agg() is declared to be "internal",
425+
* The transition type for string_agg() is declared to be "internal",
419426
* which is a pass-by-value type the same size as a pointer.
420427
*/
421428
PG_RETURN_POINTER(state);
422429
}
423430

424431
Datum
425-
bytea_agg_finalfn(PG_FUNCTION_ARGS)
432+
bytea_string_agg_finalfn(PG_FUNCTION_ARGS)
426433
{
427434
StringInfo state;
428435

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201204053
56+
#define CATALOG_VERSION_NO 201204131
5757

5858
#endif

src/include/catalog/pg_aggregate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ DATA(insert ( 2335 array_agg_transfn array_agg_finalfn 0 2281 _null_ ));
229229
DATA(insert ( 3538 string_agg_transfn string_agg_finalfn 0 2281 _null_ ));
230230

231231
/* bytea */
232-
DATA(insert ( 3545 bytea_agg_transfn bytea_agg_finalfn 0 2281 _null_ ));
232+
DATA(insert ( 3545 bytea_string_agg_transfn bytea_string_agg_finalfn 0 2281 _null_ ));
233233

234234
/*
235235
* prototypes for functions in pg_aggregate.c

src/include/catalog/pg_proc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,11 +2433,11 @@ DATA(insert OID = 3536 ( string_agg_finalfn PGNSP PGUID 12 1 0 0 0 f f f f f f
24332433
DESCR("aggregate final function");
24342434
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_ ));
24352435
DESCR("concatenate aggregate input into a string");
2436-
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_ ));
2436+
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_ ));
24372437
DESCR("aggregate transition function");
2438-
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_ ));
2438+
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_ ));
24392439
DESCR("aggregate final function");
2440-
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_ ));
2440+
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_ ));
24412441
DESCR("concatenate aggregate input into a bytea");
24422442

24432443
/* To ASCII conversion */

src/include/utils/builtins.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,8 @@ extern Datum unknownsend(PG_FUNCTION_ARGS);
771771

772772
extern Datum pg_column_size(PG_FUNCTION_ARGS);
773773

774-
extern Datum bytea_agg_transfn(PG_FUNCTION_ARGS);
775-
extern Datum bytea_agg_finalfn(PG_FUNCTION_ARGS);
774+
extern Datum bytea_string_agg_transfn(PG_FUNCTION_ARGS);
775+
extern Datum bytea_string_agg_finalfn(PG_FUNCTION_ARGS);
776776
extern Datum string_agg_transfn(PG_FUNCTION_ARGS);
777777
extern Datum string_agg_finalfn(PG_FUNCTION_ARGS);
778778

src/test/regress/expected/aggregates.out

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,26 +1061,38 @@ select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; -
10611061
a,ab,abcd
10621062
(1 row)
10631063

1064-
-- bytea_agg tests
1064+
-- string_agg bytea tests
10651065
create table bytea_test_table(v bytea);
1066-
select bytea_agg(v) from bytea_test_table;
1067-
bytea_agg
1068-
-----------
1066+
select string_agg(v, '') from bytea_test_table;
1067+
string_agg
1068+
------------
10691069

10701070
(1 row)
10711071

10721072
insert into bytea_test_table values(decode('ff','hex'));
1073-
select bytea_agg(v) from bytea_test_table;
1074-
bytea_agg
1075-
-----------
1073+
select string_agg(v, '') from bytea_test_table;
1074+
string_agg
1075+
------------
10761076
\xff
10771077
(1 row)
10781078

10791079
insert into bytea_test_table values(decode('aa','hex'));
1080-
select bytea_agg(v) from bytea_test_table;
1081-
bytea_agg
1082-
-----------
1080+
select string_agg(v, '') from bytea_test_table;
1081+
string_agg
1082+
------------
10831083
\xffaa
10841084
(1 row)
10851085

1086+
select string_agg(v, NULL) from bytea_test_table;
1087+
string_agg
1088+
------------
1089+
\xffaa
1090+
(1 row)
1091+
1092+
select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
1093+
string_agg
1094+
------------
1095+
\xffeeaa
1096+
(1 row)
1097+
10861098
drop table bytea_test_table;

src/test/regress/sql/aggregates.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,19 @@ select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl; -- not
417417
select string_agg(distinct f1, ',' order by f1::text) from varchar_tbl; -- not ok
418418
select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; -- ok
419419

420-
-- bytea_agg tests
420+
-- string_agg bytea tests
421421
create table bytea_test_table(v bytea);
422422

423-
select bytea_agg(v) from bytea_test_table;
423+
select string_agg(v, '') from bytea_test_table;
424424

425425
insert into bytea_test_table values(decode('ff','hex'));
426426

427-
select bytea_agg(v) from bytea_test_table;
427+
select string_agg(v, '') from bytea_test_table;
428428

429429
insert into bytea_test_table values(decode('aa','hex'));
430430

431-
select bytea_agg(v) from bytea_test_table;
431+
select string_agg(v, '') from bytea_test_table;
432+
select string_agg(v, NULL) from bytea_test_table;
433+
select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
432434

433435
drop table bytea_test_table;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy