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

Commit a0f1fce

Browse files
committed
Add min and max aggregates for composite types (records).
Like min/max for arrays, these are just thin wrappers around the existing btree comparison function for records. Aleksander Alekseev Discussion: https://postgr.es/m/CAO=iB8L4WYSNxCJ8GURRjQsrXEQ2-zn3FiCsh2LMqvWq2WcONg@mail.gmail.com
1 parent bb19b70 commit a0f1fce

File tree

7 files changed

+71
-4
lines changed

7 files changed

+71
-4
lines changed

doc/src/sgml/func.sgml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22048,7 +22048,7 @@ SELECT NULLIF(value, '(none)') ...
2204822048
as well as <type>inet</type>, <type>interval</type>,
2204922049
<type>money</type>, <type>oid</type>, <type>pg_lsn</type>,
2205022050
<type>tid</type>, <type>xid8</type>,
22051-
and arrays of any of these types.
22051+
and also arrays and composite types containing sortable data types.
2205222052
</para></entry>
2205322053
<entry>Yes</entry>
2205422054
</row>
@@ -22067,7 +22067,7 @@ SELECT NULLIF(value, '(none)') ...
2206722067
as well as <type>inet</type>, <type>interval</type>,
2206822068
<type>money</type>, <type>oid</type>, <type>pg_lsn</type>,
2206922069
<type>tid</type>, <type>xid8</type>,
22070-
and arrays of any of these types.
22070+
and also arrays and composite types containing sortable data types.
2207122071
</para></entry>
2207222072
<entry>Yes</entry>
2207322073
</row>

src/backend/utils/adt/rowtypes.c

+18
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,24 @@ btrecordcmp(PG_FUNCTION_ARGS)
13151315
PG_RETURN_INT32(record_cmp(fcinfo));
13161316
}
13171317

1318+
Datum
1319+
record_larger(PG_FUNCTION_ARGS)
1320+
{
1321+
if (record_cmp(fcinfo) > 0)
1322+
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
1323+
else
1324+
PG_RETURN_DATUM(PG_GETARG_DATUM(1));
1325+
}
1326+
1327+
Datum
1328+
record_smaller(PG_FUNCTION_ARGS)
1329+
{
1330+
if (record_cmp(fcinfo) < 0)
1331+
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
1332+
else
1333+
PG_RETURN_DATUM(PG_GETARG_DATUM(1));
1334+
}
1335+
13181336

13191337
/*
13201338
* record_image_cmp :

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202407101
60+
#define CATALOG_VERSION_NO 202407111
6161

6262
#endif

src/include/catalog/pg_aggregate.dat

+6
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@
140140
{ aggfnoid => 'max(anyarray)', aggtransfn => 'array_larger',
141141
aggcombinefn => 'array_larger', aggsortop => '>(anyarray,anyarray)',
142142
aggtranstype => 'anyarray' },
143+
{ aggfnoid => 'max(record)', aggtransfn => 'record_larger',
144+
aggcombinefn => 'record_larger', aggsortop => '>(record,record)',
145+
aggtranstype => 'record' },
143146
{ aggfnoid => 'max(bpchar)', aggtransfn => 'bpchar_larger',
144147
aggcombinefn => 'bpchar_larger', aggsortop => '>(bpchar,bpchar)',
145148
aggtranstype => 'bpchar' },
@@ -208,6 +211,9 @@
208211
{ aggfnoid => 'min(anyarray)', aggtransfn => 'array_smaller',
209212
aggcombinefn => 'array_smaller', aggsortop => '<(anyarray,anyarray)',
210213
aggtranstype => 'anyarray' },
214+
{ aggfnoid => 'min(record)', aggtransfn => 'record_smaller',
215+
aggcombinefn => 'record_smaller', aggsortop => '<(record,record)',
216+
aggtranstype => 'record' },
211217
{ aggfnoid => 'min(bpchar)', aggtransfn => 'bpchar_smaller',
212218
aggcombinefn => 'bpchar_smaller', aggsortop => '<(bpchar,bpchar)',
213219
aggtranstype => 'bpchar' },

src/include/catalog/pg_proc.dat

+13-1
Original file line numberDiff line numberDiff line change
@@ -6754,6 +6754,9 @@
67546754
proname => 'max', prokind => 'a', proisstrict => 'f',
67556755
prorettype => 'anyarray', proargtypes => 'anyarray',
67566756
prosrc => 'aggregate_dummy' },
6757+
{ oid => '8595', descr => 'maximum value of all record input values',
6758+
proname => 'max', prokind => 'a', proisstrict => 'f', prorettype => 'record',
6759+
proargtypes => 'record', prosrc => 'aggregate_dummy' },
67576760
{ oid => '2244', descr => 'maximum value of all bpchar input values',
67586761
proname => 'max', prokind => 'a', proisstrict => 'f', prorettype => 'bpchar',
67596762
proargtypes => 'bpchar', prosrc => 'aggregate_dummy' },
@@ -6824,6 +6827,9 @@
68246827
proname => 'min', prokind => 'a', proisstrict => 'f',
68256828
prorettype => 'anyarray', proargtypes => 'anyarray',
68266829
prosrc => 'aggregate_dummy' },
6830+
{ oid => '8596', descr => 'minimum value of all record input values',
6831+
proname => 'min', prokind => 'a', proisstrict => 'f', prorettype => 'record',
6832+
proargtypes => 'record', prosrc => 'aggregate_dummy' },
68276833
{ oid => '2245', descr => 'minimum value of all bpchar input values',
68286834
proname => 'min', prokind => 'a', proisstrict => 'f', prorettype => 'bpchar',
68296835
proargtypes => 'bpchar', prosrc => 'aggregate_dummy' },
@@ -8343,7 +8349,7 @@
83438349
prosrc => 'generate_series_timestamptz' },
83448350
{ oid => '6274', descr => 'non-persistent series generator',
83458351
proname => 'generate_series', prorows => '1000',
8346-
prosupport => 'generate_series_timestamp_support', proretset => 't',
8352+
prosupport => 'generate_series_timestamp_support', proretset => 't',
83478353
prorettype => 'timestamptz',
83488354
proargtypes => 'timestamptz timestamptz interval text',
83498355
prosrc => 'generate_series_timestamptz_at_zone' },
@@ -10358,6 +10364,12 @@
1035810364
{ oid => '2987', descr => 'less-equal-greater',
1035910365
proname => 'btrecordcmp', prorettype => 'int4',
1036010366
proargtypes => 'record record', prosrc => 'btrecordcmp' },
10367+
{ oid => '8597', descr => 'larger of two',
10368+
proname => 'record_larger', prorettype => 'record',
10369+
proargtypes => 'record record', prosrc => 'record_larger' },
10370+
{ oid => '8598', descr => 'smaller of two',
10371+
proname => 'record_smaller', prorettype => 'record',
10372+
proargtypes => 'record record', prosrc => 'record_smaller' },
1036110373

1036210374
{ oid => '6192', descr => 'hash',
1036310375
proname => 'hash_record', prorettype => 'int4', proargtypes => 'record',

src/test/regress/expected/aggregates.out

+25
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,31 @@ SELECT stddev_pop('nan'::numeric), stddev_samp('nan'::numeric);
269269
NaN |
270270
(1 row)
271271

272+
-- verify correct results for min(record) and max(record) aggregates
273+
SELECT max(row(a,b)) FROM aggtest;
274+
max
275+
--------------
276+
(100,99.097)
277+
(1 row)
278+
279+
SELECT max(row(b,a)) FROM aggtest;
280+
max
281+
-------------
282+
(324.78,42)
283+
(1 row)
284+
285+
SELECT min(row(a,b)) FROM aggtest;
286+
min
287+
-------------
288+
(0,0.09561)
289+
(1 row)
290+
291+
SELECT min(row(b,a)) FROM aggtest;
292+
min
293+
-------------
294+
(0.09561,0)
295+
(1 row)
296+
272297
-- verify correct results for null and NaN inputs
273298
select sum(null::int4) from generate_series(1,3);
274299
sum

src/test/regress/sql/aggregates.sql

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ SELECT stddev_pop('inf'::numeric), stddev_samp('inf'::numeric);
7878
SELECT var_pop('nan'::numeric), var_samp('nan'::numeric);
7979
SELECT stddev_pop('nan'::numeric), stddev_samp('nan'::numeric);
8080

81+
-- verify correct results for min(record) and max(record) aggregates
82+
SELECT max(row(a,b)) FROM aggtest;
83+
SELECT max(row(b,a)) FROM aggtest;
84+
SELECT min(row(a,b)) FROM aggtest;
85+
SELECT min(row(b,a)) FROM aggtest;
86+
8187
-- verify correct results for null and NaN inputs
8288
select sum(null::int4) from generate_series(1,3);
8389
select sum(null::int8) from generate_series(1,3);

0 commit comments

Comments
 (0)