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

Commit 6c40f83

Browse files
committed
Add min and max aggregates for inet/cidr data types.
Haribabu Kommi, reviewed by Muhammad Asif Naeem
1 parent ec544a6 commit 6c40f83

File tree

8 files changed

+59
-3
lines changed

8 files changed

+59
-3
lines changed

doc/src/sgml/func.sgml

+4-2
Original file line numberDiff line numberDiff line change
@@ -12192,7 +12192,8 @@ NULL baz</literallayout>(3 rows)</entry>
1219212192
</indexterm>
1219312193
<function>max(<replaceable class="parameter">expression</replaceable>)</function>
1219412194
</entry>
12195-
<entry>any array, numeric, string, or date/time type</entry>
12195+
<entry>any numeric, string, date/time, network, or enum type,
12196+
or arrays of these types</entry>
1219612197
<entry>same as argument type</entry>
1219712198
<entry>
1219812199
maximum value of <replaceable
@@ -12208,7 +12209,8 @@ NULL baz</literallayout>(3 rows)</entry>
1220812209
</indexterm>
1220912210
<function>min(<replaceable class="parameter">expression</replaceable>)</function>
1221012211
</entry>
12211-
<entry>any array, numeric, string, or date/time type</entry>
12212+
<entry>any numeric, string, date/time, network, or enum type,
12213+
or arrays of these types</entry>
1221212214
<entry>same as argument type</entry>
1221312215
<entry>
1221412216
minimum value of <replaceable

src/backend/utils/adt/network.c

+27
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,33 @@ network_ne(PG_FUNCTION_ARGS)
471471
PG_RETURN_BOOL(network_cmp_internal(a1, a2) != 0);
472472
}
473473

474+
/*
475+
* MIN/MAX support functions.
476+
*/
477+
Datum
478+
network_smaller(PG_FUNCTION_ARGS)
479+
{
480+
inet *a1 = PG_GETARG_INET_PP(0);
481+
inet *a2 = PG_GETARG_INET_PP(1);
482+
483+
if (network_cmp_internal(a1, a2) < 0)
484+
PG_RETURN_INET_P(a1);
485+
else
486+
PG_RETURN_INET_P(a2);
487+
}
488+
489+
Datum
490+
network_larger(PG_FUNCTION_ARGS)
491+
{
492+
inet *a1 = PG_GETARG_INET_PP(0);
493+
inet *a2 = PG_GETARG_INET_PP(1);
494+
495+
if (network_cmp_internal(a1, a2) > 0)
496+
PG_RETURN_INET_P(a1);
497+
else
498+
PG_RETURN_INET_P(a2);
499+
}
500+
474501
/*
475502
* Support function for hash indexes on inet/cidr.
476503
*/

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201408162
56+
#define CATALOG_VERSION_NO 201408281
5757

5858
#endif

src/include/catalog/pg_aggregate.h

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ DATA(insert ( 2050 n 0 array_larger - - - - f f 1073 2277 0 0 0 _nu
164164
DATA(insert ( 2244 n 0 bpchar_larger - - - - f f 1060 1042 0 0 0 _null_ _null_ ));
165165
DATA(insert ( 2797 n 0 tidlarger - - - - f f 2800 27 0 0 0 _null_ _null_ ));
166166
DATA(insert ( 3526 n 0 enum_larger - - - - f f 3519 3500 0 0 0 _null_ _null_ ));
167+
DATA(insert ( 3564 n 0 network_larger - - - - f f 1205 869 0 0 0 _null_ _null_ ));
167168

168169
/* min */
169170
DATA(insert ( 2131 n 0 int8smaller - - - - f f 412 20 0 0 0 _null_ _null_ ));
@@ -186,6 +187,7 @@ DATA(insert ( 2051 n 0 array_smaller - - - - f f 1072 2277 0 0 0 _n
186187
DATA(insert ( 2245 n 0 bpchar_smaller - - - - f f 1058 1042 0 0 0 _null_ _null_ ));
187188
DATA(insert ( 2798 n 0 tidsmaller - - - - f f 2799 27 0 0 0 _null_ _null_ ));
188189
DATA(insert ( 3527 n 0 enum_smaller - - - - f f 3518 3500 0 0 0 _null_ _null_ ));
190+
DATA(insert ( 3565 n 0 network_smaller - - - - f f 1203 869 0 0 0 _null_ _null_ ));
189191

190192
/* count */
191193
DATA(insert ( 2147 n 0 int8inc_any - int8inc_any int8dec_any - f f 0 20 0 20 0 "0" "0" ));

src/include/catalog/pg_proc.h

+8
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,10 @@ DATA(insert OID = 922 ( network_le PGNSP PGUID 12 1 0 0 0 f f f t t f i 2 0 1
21222122
DATA(insert OID = 923 ( network_gt PGNSP PGUID 12 1 0 0 0 f f f t t f i 2 0 16 "869 869" _null_ _null_ _null_ _null_ network_gt _null_ _null_ _null_ ));
21232123
DATA(insert OID = 924 ( network_ge PGNSP PGUID 12 1 0 0 0 f f f t t f i 2 0 16 "869 869" _null_ _null_ _null_ _null_ network_ge _null_ _null_ _null_ ));
21242124
DATA(insert OID = 925 ( network_ne PGNSP PGUID 12 1 0 0 0 f f f t t f i 2 0 16 "869 869" _null_ _null_ _null_ _null_ network_ne _null_ _null_ _null_ ));
2125+
DATA(insert OID = 3562 ( network_larger PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 869 "869 869" _null_ _null_ _null_ _null_ network_larger _null_ _null_ _null_ ));
2126+
DESCR("larger of two");
2127+
DATA(insert OID = 3563 ( network_smaller PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 869 "869 869" _null_ _null_ _null_ _null_ network_smaller _null_ _null_ _null_ ));
2128+
DESCR("smaller of two");
21252129
DATA(insert OID = 926 ( network_cmp PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 23 "869 869" _null_ _null_ _null_ _null_ network_cmp _null_ _null_ _null_ ));
21262130
DESCR("less-equal-greater");
21272131
DATA(insert OID = 927 ( network_sub PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 16 "869 869" _null_ _null_ _null_ _null_ network_sub _null_ _null_ _null_ ));
@@ -3163,6 +3167,8 @@ DATA(insert OID = 2244 ( max PGNSP PGUID 12 1 0 0 0 t f f f f f i 1 0 1042 "
31633167
DESCR("maximum value of all bpchar input values");
31643168
DATA(insert OID = 2797 ( max PGNSP PGUID 12 1 0 0 0 t f f f f f i 1 0 27 "27" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
31653169
DESCR("maximum value of all tid input values");
3170+
DATA(insert OID = 3564 ( max PGNSP PGUID 12 1 0 0 0 t f f f f f i 1 0 869 "869" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
3171+
DESCR("maximum value of all inet input values");
31663172

31673173
DATA(insert OID = 2131 ( min PGNSP PGUID 12 1 0 0 0 t f f f f f i 1 0 20 "20" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
31683174
DESCR("minimum value of all bigint input values");
@@ -3202,6 +3208,8 @@ DATA(insert OID = 2245 ( min PGNSP PGUID 12 1 0 0 0 t f f f f f i 1 0 1042 "
32023208
DESCR("minimum value of all bpchar input values");
32033209
DATA(insert OID = 2798 ( min PGNSP PGUID 12 1 0 0 0 t f f f f f i 1 0 27 "27" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
32043210
DESCR("minimum value of all tid input values");
3211+
DATA(insert OID = 3565 ( min PGNSP PGUID 12 1 0 0 0 t f f f f f i 1 0 869 "869" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
3212+
DESCR("minimum value of all inet input values");
32053213

32063214
/* count has two forms: count(any) and count(*) */
32073215
DATA(insert OID = 2147 ( count PGNSP PGUID 12 1 0 0 0 t f f f f f i 1 0 20 "2276" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));

src/include/utils/builtins.h

+2
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,8 @@ extern Datum network_eq(PG_FUNCTION_ARGS);
908908
extern Datum network_ge(PG_FUNCTION_ARGS);
909909
extern Datum network_gt(PG_FUNCTION_ARGS);
910910
extern Datum network_ne(PG_FUNCTION_ARGS);
911+
extern Datum network_smaller(PG_FUNCTION_ARGS);
912+
extern Datum network_larger(PG_FUNCTION_ARGS);
911913
extern Datum hashinet(PG_FUNCTION_ARGS);
912914
extern Datum network_sub(PG_FUNCTION_ARGS);
913915
extern Datum network_subeq(PG_FUNCTION_ARGS);

src/test/regress/expected/inet.out

+12
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ SELECT '' AS ten, i, c,
204204
| ::4.3.2.1/24 | ::ffff:1.2.3.4/128 | t | t | f | f | f | t | f | f | t | t | t
205205
(17 rows)
206206

207+
SELECT max(i) AS max, min(i) AS min FROM INET_TBL;
208+
max | min
209+
-------------+-----------
210+
10:23::ffff | 9.1.2.3/8
211+
(1 row)
212+
213+
SELECT max(c) AS max, min(c) AS min FROM INET_TBL;
214+
max | min
215+
-----------------+------------
216+
10:23::8000/113 | 10.0.0.0/8
217+
(1 row)
218+
207219
-- check the conversion to/from text and set_netmask
208220
SELECT '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
209221
ten | set_masklen

src/test/regress/sql/inet.sql

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ SELECT '' AS ten, i, c,
5656
i && c AS ovr
5757
FROM INET_TBL;
5858

59+
SELECT max(i) AS max, min(i) AS min FROM INET_TBL;
60+
SELECT max(c) AS max, min(c) AS min FROM INET_TBL;
61+
5962
-- check the conversion to/from text and set_netmask
6063
SELECT '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
6164

0 commit comments

Comments
 (0)