Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2d24fd9

Browse files
committed
Add min and max aggregates for bytea type.
Similar to a0f1fce, although we chose to duplicate logic rather than invoke byteacmp, primarily to avoid repeat detoasting. Marat Buharov, Aleksander Alekseev Discussion: https://postgr.es/m/CAPCEVGXiASjodos4P8pgyV7ixfVn-ZgG9YyiRZRbVqbGmfuDyg@mail.gmail.com
1 parent 57f3702 commit 2d24fd9

File tree

8 files changed

+99
-5
lines changed

8 files changed

+99
-5
lines changed

doc/src/sgml/func.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22125,7 +22125,7 @@ SELECT NULLIF(value, '(none)') ...
2212522125
<para>
2212622126
Computes the maximum of the non-null input
2212722127
values. Available for any numeric, string, date/time, or enum type,
22128-
as well as <type>inet</type>, <type>interval</type>,
22128+
as well as <type>bytea</type>, <type>inet</type>, <type>interval</type>,
2212922129
<type>money</type>, <type>oid</type>, <type>pg_lsn</type>,
2213022130
<type>tid</type>, <type>xid8</type>,
2213122131
and also arrays and composite types containing sortable data types.
@@ -22144,7 +22144,7 @@ SELECT NULLIF(value, '(none)') ...
2214422144
<para>
2214522145
Computes the minimum of the non-null input
2214622146
values. Available for any numeric, string, date/time, or enum type,
22147-
as well as <type>inet</type>, <type>interval</type>,
22147+
as well as <type>bytea</type>, <type>inet</type>, <type>interval</type>,
2214822148
<type>money</type>, <type>oid</type>, <type>pg_lsn</type>,
2214922149
<type>tid</type>, <type>xid8</type>,
2215022150
and also arrays and composite types containing sortable data types.

src/backend/utils/adt/varlena.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3931,6 +3931,44 @@ byteacmp(PG_FUNCTION_ARGS)
39313931
PG_RETURN_INT32(cmp);
39323932
}
39333933

3934+
Datum
3935+
bytea_larger(PG_FUNCTION_ARGS)
3936+
{
3937+
bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3938+
bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3939+
bytea *result;
3940+
int len1,
3941+
len2;
3942+
int cmp;
3943+
3944+
len1 = VARSIZE_ANY_EXHDR(arg1);
3945+
len2 = VARSIZE_ANY_EXHDR(arg2);
3946+
3947+
cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3948+
result = ((cmp > 0) || ((cmp == 0) && (len1 > len2)) ? arg1 : arg2);
3949+
3950+
PG_RETURN_BYTEA_P(result);
3951+
}
3952+
3953+
Datum
3954+
bytea_smaller(PG_FUNCTION_ARGS)
3955+
{
3956+
bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3957+
bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3958+
bytea *result;
3959+
int len1,
3960+
len2;
3961+
int cmp;
3962+
3963+
len1 = VARSIZE_ANY_EXHDR(arg1);
3964+
len2 = VARSIZE_ANY_EXHDR(arg2);
3965+
3966+
cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3967+
result = ((cmp < 0) || ((cmp == 0) && (len1 < len2)) ? arg1 : arg2);
3968+
3969+
PG_RETURN_BYTEA_P(result);
3970+
}
3971+
39343972
Datum
39353973
bytea_sortsupport(PG_FUNCTION_ARGS)
39363974
{

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202410021
60+
#define CATALOG_VERSION_NO 202410081
6161

6262
#endif

src/include/catalog/pg_aggregate.dat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@
161161
{ aggfnoid => 'max(xid8)', aggtransfn => 'xid8_larger',
162162
aggcombinefn => 'xid8_larger', aggsortop => '>(xid8,xid8)',
163163
aggtranstype => 'xid8' },
164+
{ aggfnoid => 'max(bytea)', aggtransfn => 'bytea_larger',
165+
aggcombinefn => 'bytea_larger', aggsortop => '>(bytea,bytea)',
166+
aggtranstype => 'bytea' },
164167

165168
# min
166169
{ aggfnoid => 'min(int8)', aggtransfn => 'int8smaller',
@@ -232,6 +235,9 @@
232235
{ aggfnoid => 'min(xid8)', aggtransfn => 'xid8_smaller',
233236
aggcombinefn => 'xid8_smaller', aggsortop => '<(xid8,xid8)',
234237
aggtranstype => 'xid8' },
238+
{ aggfnoid => 'min(bytea)', aggtransfn => 'bytea_smaller',
239+
aggcombinefn => 'bytea_smaller', aggsortop => '<(bytea,bytea)',
240+
aggtranstype => 'bytea' },
235241

236242
# count
237243
{ aggfnoid => 'count(any)', aggtransfn => 'int8inc_any',

src/include/catalog/pg_proc.dat

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,13 @@
13141314
proname => 'text_smaller', proleakproof => 't', prorettype => 'text',
13151315
proargtypes => 'text text', prosrc => 'text_smaller' },
13161316

1317+
{ oid => '8920', descr => 'larger of two',
1318+
proname => 'bytea_larger', proleakproof => 't', prorettype => 'bytea',
1319+
proargtypes => 'bytea bytea', prosrc => 'bytea_larger' },
1320+
{ oid => '8921', descr => 'smaller of two',
1321+
proname => 'bytea_smaller', proleakproof => 't', prorettype => 'bytea',
1322+
proargtypes => 'bytea bytea', prosrc => 'bytea_smaller' },
1323+
13171324
{ oid => '460', descr => 'I/O',
13181325
proname => 'int8in', prorettype => 'int8', proargtypes => 'cstring',
13191326
prosrc => 'int8in' },
@@ -6879,6 +6886,9 @@
68796886
{ oid => '5099', descr => 'maximum value of all xid8 input values',
68806887
proname => 'max', prokind => 'a', proisstrict => 'f', prorettype => 'xid8',
68816888
proargtypes => 'xid8', prosrc => 'aggregate_dummy' },
6889+
{ oid => '8922', descr => 'maximum value of all bytea input values',
6890+
proname => 'max', prokind => 'a', proisstrict => 'f', prorettype => 'bytea',
6891+
proargtypes => 'bytea', prosrc => 'aggregate_dummy' },
68826892

68836893
{ oid => '2131', descr => 'minimum value of all bigint input values',
68846894
proname => 'min', prokind => 'a', proisstrict => 'f', prorettype => 'int8',
@@ -6952,6 +6962,9 @@
69526962
{ oid => '5100', descr => 'minimum value of all xid8 input values',
69536963
proname => 'min', prokind => 'a', proisstrict => 'f', prorettype => 'xid8',
69546964
proargtypes => 'xid8', prosrc => 'aggregate_dummy' },
6965+
{ oid => '8923', descr => 'minimum value of all bytea input values',
6966+
proname => 'min', prokind => 'a', proisstrict => 'f', prorettype => 'bytea',
6967+
proargtypes => 'bytea', prosrc => 'aggregate_dummy' },
69556968

69566969
# count has two forms: count(any) and count(*)
69576970
{ oid => '2147',

src/test/regress/expected/aggregates.out

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,7 @@ select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; -
19501950
a,ab,abcd
19511951
(1 row)
19521952

1953-
-- string_agg bytea tests
1953+
-- string_agg, min, max bytea tests
19541954
create table bytea_test_table(v bytea);
19551955
select string_agg(v, '') from bytea_test_table;
19561956
string_agg
@@ -1984,6 +1984,32 @@ select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
19841984
\xffeeaa
19851985
(1 row)
19861986

1987+
select min(v) from bytea_test_table;
1988+
min
1989+
------
1990+
\xaa
1991+
(1 row)
1992+
1993+
select max(v) from bytea_test_table;
1994+
max
1995+
------
1996+
\xff
1997+
(1 row)
1998+
1999+
insert into bytea_test_table values(decode('ffff','hex'));
2000+
insert into bytea_test_table values(decode('aaaa','hex'));
2001+
select min(v) from bytea_test_table;
2002+
min
2003+
------
2004+
\xaa
2005+
(1 row)
2006+
2007+
select max(v) from bytea_test_table;
2008+
max
2009+
--------
2010+
\xffff
2011+
(1 row)
2012+
19872013
drop table bytea_test_table;
19882014
-- Test parallel string_agg and array_agg
19892015
create table pagg_test (x int, y int) with (autovacuum_enabled = off);

src/test/regress/expected/opr_sanity.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,8 @@ uuid_extract_timestamp(uuid)
876876
uuid_extract_version(uuid)
877877
crc32(bytea)
878878
crc32c(bytea)
879+
bytea_larger(bytea,bytea)
880+
bytea_smaller(bytea,bytea)
879881
-- restore normal output mode
880882
\a\t
881883
-- List of functions used by libpq's fe-lobj.c

src/test/regress/sql/aggregates.sql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl; -- not
747747
select string_agg(distinct f1, ',' order by f1::text) from varchar_tbl; -- not ok
748748
select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; -- ok
749749

750-
-- string_agg bytea tests
750+
-- string_agg, min, max bytea tests
751751
create table bytea_test_table(v bytea);
752752

753753
select string_agg(v, '') from bytea_test_table;
@@ -762,6 +762,15 @@ select string_agg(v, '') from bytea_test_table;
762762
select string_agg(v, NULL) from bytea_test_table;
763763
select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
764764

765+
select min(v) from bytea_test_table;
766+
select max(v) from bytea_test_table;
767+
768+
insert into bytea_test_table values(decode('ffff','hex'));
769+
insert into bytea_test_table values(decode('aaaa','hex'));
770+
771+
select min(v) from bytea_test_table;
772+
select max(v) from bytea_test_table;
773+
765774
drop table bytea_test_table;
766775

767776
-- Test parallel string_agg and array_agg

0 commit comments

Comments
 (0)