-
Notifications
You must be signed in to change notification settings - Fork 144
/
array_unique.sql
50 lines (46 loc) · 1.58 KB
/
array_unique.sql
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
create or replace function public.array_unique(anyarray)
returns anyarray
immutable
strict -- returns null if any parameter is null
parallel safe -- Postgres 10 or later
security invoker
language sql
set search_path = ''
as $$
select array(
select distinct t.x --using DISTINCT implicitly sorts the array
from unnest($1) t(x)
);
$$;
create or replace function public.array_unique(
anyarray, -- input array
boolean -- flag to drop nulls
)
returns anyarray
immutable
strict -- returns null if any parameter is null
parallel safe -- Postgres 10 or later
security invoker
language sql
set search_path = ''
as $$
select array(
SELECT DISTINCT t.x --using DISTINCT implicitly sorts the array
FROM unnest($1) t(x)
WHERE NOT $2 OR t.x IS NOT NULL
);
$$;
--TEST
do $$
begin
assert public.array_unique('{}'::int[]) = '{}'::int[];
assert public.array_unique('{1,1,2,2,null,null}'::int[]) = '{null,1,2}';
assert public.array_unique('{x,x,y,y,null,null}'::text[]) = '{null,x,y}';
assert public.array_unique('{}'::int[], false) = '{}'::int[];
assert public.array_unique('{1,1,2,2,null,null}'::int[], false) = '{null,1,2}';
assert public.array_unique('{x,x,y,y,null,null}'::text[], false) = '{null,x,y}';
assert public.array_unique('{}'::int[], true) = '{}'::int[];
assert public.array_unique('{1,1,2,2,null,null}'::int[], true) = '{1,2}';
assert public.array_unique('{x,x,y,y,null,null}'::text[], true) = '{x,y}';
end;
$$;