得到交集, $2为真子集则返回true
create or replace function array_intersect(anyarray, anyarray)
returns anyarray
immutable
language sql
as $function$
select array(
select unnest($1)
intersect
select unnest($2)
);
$function$;
create or replace function array_intersect(text[], text)
returns anyarray
immutable
language sql
as $function$
select array_intersect($1,string_to_array($2,','))
$function$;
create or replace function array_intersect(int[], text)
returns anyarray
immutable
language sql
as $function$
select array_intersect($1,string_to_array($2,',')::int[])
$function$;
create or replace function array_intersect(bigint[], text)
returns anyarray
immutable
language sql
as $function$
select array_intersect($1,string_to_array($2,',')::bigint[])
$function$;
create or replace function array_has_intersection(anyarray, anyarray)
returns bool
immutable
language sql
as $function$
select true from (
select unnest($1)
intersect
select unnest($2)
) t limit 1;
$function$;
create or replace function array_has_intersection(bigint[], text[])
returns bool
immutable
language sql
as $function$
select array_has_intersection($1,$2::bigint[])
$function$;
create or replace function array_has_intersection(bigint[], int[])
returns bool
immutable
language sql
as $function$
select array_has_intersection($1,$2::bigint[])
$function$;
create or replace function array_has_intersection(int[], text[])
returns bool
immutable
language sql
as $function$
select array_has_intersection($1,$2::int[])
$function$;
create or replace function array_has_intersection(text[], text)
returns bool
immutable
language sql
as $function$
select array_has_intersection($1,string_to_array($2,','))
$function$;
create or replace function array_has_intersection(int[], text)
returns bool
immutable
language sql
as $function$
select array_has_intersection($1,string_to_array($2,',')::int[])
$function$;
create or replace function array_has_intersection(bigint[], text)
returns bool
immutable
language sql
as $function$
select array_has_intersection($1,string_to_array($2,',')::bigint[])
$function$;
差集, 全差集
需要用到不同入参时,仿照上边sql扩展即可
create or replace function array_diffset(anyarray, anyarray)
returns anyarray
immutable
language sql
as $function$
select array(
select unnest($1)
except
select unnest($2)
);
$function$;
create or replace function array_diffset_full(anyarray, anyarray)
returns anyarray
immutable
language sql
as $function$
with t1(n) as (
select unnest($1)
except
select unnest($2)
),t2(n) as (
select unnest($2)
except
select unnest($1)
) select array(
select n from t1
union
select n from t2
);
$function$;