forked from rin-nas/postgresql-patterns-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_unique.sql
More file actions
29 lines (28 loc) · 737 Bytes
/
array_unique.sql
File metadata and controls
29 lines (28 loc) · 737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
--simplest and faster
CREATE FUNCTION array_unique(anyarray)
RETURNS anyarray
stable
returns null on null input
parallel safe
language sql
set search_path = ''
AS $$
SELECT array_agg(DISTINCT x) --using DISTINCT implicitly sorts the array
FROM unnest($1) t(x);
$$;
CREATE FUNCTION array_unique(
anyarray, -- input array
boolean -- flag to drop nulls
)
RETURNS anyarray
stable
returns null on null input
parallel safe
language sql
set search_path = ''
AS $$
SELECT array_agg(DISTINCT x) --using DISTINCT implicitly sorts the array
FROM unnest($1) t(x)
--WHERE CASE WHEN $2 THEN x IS NOT NULL ELSE true END;
WHERE NOT $2 OR x IS NOT NULL;
$$;