-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathentropy_redundancy.sql
39 lines (33 loc) · 1.31 KB
/
entropy_redundancy.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
create or replace function public.entropy_redundancy(s text)
returns numeric
immutable
strict -- returns null if any parameter is null
parallel safe -- Postgres 10 or later
security invoker
language sql
set search_path = ''
as $func$
select log(2, char_length(entropy_redundancy.s)) - public.entropy(entropy_redundancy.s);
$func$;
comment on function public.entropy_redundancy(s text) is 'Returns text redundancy > 0';
create or replace function public.entropy_redundancy(data bytea)
returns numeric
immutable
strict -- returns null if any parameter is null
parallel safe -- Postgres 10 or later
security invoker
language sql
set search_path = ''
as $func$
select log(2, octet_length(entropy_redundancy.data)) - public.entropy(entropy_redundancy.data);
$func$;
comment on function public.entropy_redundancy(s text) is 'Returns data redundancy > 0. For compressed data the value tends to 0';
-- TEST
do $$
begin
assert round(public.entropy_redundancy('abracadabra'), 2) = 1.42;
assert round(public.entropy_redundancy('абракадабра'), 2) = 1.42;
assert round(public.entropy_redundancy('абракадабра'::bytea), 2) = 2.1;
assert round(public.entropy_redundancy('\x11D481C8E032988E9763FD2780'::bytea), 2) = 0;
end;
$$;