Skip to content

Commit d5448c7

Browse files
committed
Add bytea_agg, parallel to string_agg.
Pavel Stehule
1 parent 0510b62 commit d5448c7

File tree

8 files changed

+116
-1
lines changed

8 files changed

+116
-1
lines changed

doc/src/sgml/func.sgml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10908,6 +10908,24 @@ SELECT NULLIF(value, '(none)') ...
1090810908
<entry>true if at least one input value is true, otherwise false</entry>
1090910909
</row>
1091010910

10911+
<row>
10912+
<entry>
10913+
<indexterm>
10914+
<primary>bytea_agg</primary>
10915+
</indexterm>
10916+
<function>
10917+
bytea_agg(<replaceable class="parameter">expression</replaceable>)
10918+
</function>
10919+
</entry>
10920+
<entry>
10921+
<type>bytea</type>
10922+
</entry>
10923+
<entry>
10924+
<type>bytea</type>
10925+
</entry>
10926+
<entry>input values concatenated into a bytea</entry>
10927+
</row>
10928+
1091110929
<row>
1091210930
<entry>
1091310931
<indexterm>

src/backend/utils/adt/varlena.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,53 @@ byteasend(PG_FUNCTION_ARGS)
396396
PG_RETURN_BYTEA_P(vlena);
397397
}
398398

399+
Datum
400+
bytea_agg_transfn(PG_FUNCTION_ARGS)
401+
{
402+
StringInfo state;
403+
404+
state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
405+
406+
/* Append the value unless null. */
407+
if (!PG_ARGISNULL(1))
408+
{
409+
bytea *value = PG_GETARG_BYTEA_PP(1);
410+
411+
if (state == NULL)
412+
state = makeStringAggState(fcinfo);
413+
414+
appendBinaryStringInfo(state, VARDATA_ANY(value), VARSIZE_ANY_EXHDR(value));
415+
}
416+
417+
/*
418+
* The transition type for bytea_agg() is declared to be "internal",
419+
* which is a pass-by-value type the same size as a pointer.
420+
*/
421+
PG_RETURN_POINTER(state);
422+
}
423+
424+
Datum
425+
bytea_agg_finalfn(PG_FUNCTION_ARGS)
426+
{
427+
StringInfo state;
428+
429+
/* cannot be called directly because of internal-type argument */
430+
Assert(AggCheckCallContext(fcinfo, NULL));
431+
432+
state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
433+
434+
if (state != NULL)
435+
{
436+
bytea *result;
437+
438+
result = (bytea *) palloc(state->len + VARHDRSZ);
439+
SET_VARSIZE(result, state->len + VARHDRSZ);
440+
memcpy(VARDATA(result), state->data, state->len);
441+
PG_RETURN_BYTEA_P(result);
442+
}
443+
else
444+
PG_RETURN_NULL();
445+
}
399446

400447
/*
401448
* textin - converts "..." to internal representation

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 201112221
56+
#define CATALOG_VERSION_NO 201112231
5757

5858
#endif

src/include/catalog/pg_aggregate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ DATA(insert ( 2335 array_agg_transfn array_agg_finalfn 0 2281 _null_ ));
226226
/* text */
227227
DATA(insert ( 3538 string_agg_transfn string_agg_finalfn 0 2281 _null_ ));
228228

229+
/* bytea */
230+
DATA(insert ( 3545 bytea_agg_transfn bytea_agg_finalfn 0 2281 _null_ ));
231+
229232
/*
230233
* prototypes for functions in pg_aggregate.c
231234
*/

src/include/catalog/pg_proc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,12 +2403,19 @@ DATA(insert OID = 2816 ( float8_covar_samp PGNSP PGUID 12 1 0 0 0 f f f t f i
24032403
DESCR("aggregate final function");
24042404
DATA(insert OID = 2817 ( float8_corr PGNSP PGUID 12 1 0 0 0 f f f t f i 1 0 701 "1022" _null_ _null_ _null_ _null_ float8_corr _null_ _null_ _null_ ));
24052405
DESCR("aggregate final function");
2406+
24062407
DATA(insert OID = 3535 ( string_agg_transfn PGNSP PGUID 12 1 0 0 0 f f f f f i 3 0 2281 "2281 25 25" _null_ _null_ _null_ _null_ string_agg_transfn _null_ _null_ _null_ ));
24072408
DESCR("aggregate transition function");
24082409
DATA(insert OID = 3536 ( string_agg_finalfn PGNSP PGUID 12 1 0 0 0 f f f f f i 1 0 25 "2281" _null_ _null_ _null_ _null_ string_agg_finalfn _null_ _null_ _null_ ));
24092410
DESCR("aggregate final function");
24102411
DATA(insert OID = 3538 ( string_agg PGNSP PGUID 12 1 0 0 0 t f f f f i 2 0 25 "25 25" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
24112412
DESCR("concatenate aggregate input into a string");
2413+
DATA(insert OID = 3543 ( bytea_agg_transfn PGNSP PGUID 12 1 0 0 0 f f f f f i 2 0 2281 "2281 17" _null_ _null_ _null_ _null_ bytea_agg_transfn _null_ _null_ _null_ ));
2414+
DESCR("aggregate transition function");
2415+
DATA(insert OID = 3544 ( bytea_agg_finalfn PGNSP PGUID 12 1 0 0 0 f f f f f i 1 0 17 "2281" _null_ _null_ _null_ _null_ bytea_agg_finalfn _null_ _null_ _null_ ));
2416+
DESCR("aggregate final function");
2417+
DATA(insert OID = 3545 ( bytea_agg PGNSP PGUID 12 1 0 0 0 t f f f f i 1 0 17 "17" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
2418+
DESCR("concatenate aggregate input into a bytea");
24122419

24132420
/* To ASCII conversion */
24142421
DATA(insert OID = 1845 ( to_ascii PGNSP PGUID 12 1 0 0 0 f f f t f i 1 0 25 "25" _null_ _null_ _null_ _null_ to_ascii_default _null_ _null_ _null_ ));

src/include/utils/builtins.h

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

770770
extern Datum pg_column_size(PG_FUNCTION_ARGS);
771771

772+
extern Datum bytea_agg_transfn(PG_FUNCTION_ARGS);
773+
extern Datum bytea_agg_finalfn(PG_FUNCTION_ARGS);
772774
extern Datum string_agg_transfn(PG_FUNCTION_ARGS);
773775
extern Datum string_agg_finalfn(PG_FUNCTION_ARGS);
774776

src/test/regress/expected/aggregates.out

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,3 +1061,26 @@ 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
1065+
create table bytea_test_table(v bytea);
1066+
select bytea_agg(v) from bytea_test_table;
1067+
bytea_agg
1068+
-----------
1069+
1070+
(1 row)
1071+
1072+
insert into bytea_test_table values(decode('ff','hex'));
1073+
select bytea_agg(v) from bytea_test_table;
1074+
bytea_agg
1075+
-----------
1076+
\xff
1077+
(1 row)
1078+
1079+
insert into bytea_test_table values(decode('aa','hex'));
1080+
select bytea_agg(v) from bytea_test_table;
1081+
bytea_agg
1082+
-----------
1083+
\xffaa
1084+
(1 row)
1085+
1086+
drop table bytea_test_table;

src/test/regress/sql/aggregates.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,18 @@ select string_agg(distinct f1, ',' order by f1) from varchar_tbl; -- ok
416416
select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl; -- not ok
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
419+
420+
-- bytea_agg tests
421+
create table bytea_test_table(v bytea);
422+
423+
select bytea_agg(v) from bytea_test_table;
424+
425+
insert into bytea_test_table values(decode('ff','hex'));
426+
427+
select bytea_agg(v) from bytea_test_table;
428+
429+
insert into bytea_test_table values(decode('aa','hex'));
430+
431+
select bytea_agg(v) from bytea_test_table;
432+
433+
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