fix sql function which did not deduplicate

This commit is contained in:
GroovieGermanikus 2024-01-09 11:26:26 +01:00
parent 4f84c8e772
commit d18c563447
No known key found for this signature in database
GPG Key ID: 5B6EB831A5CD2015
1 changed files with 17 additions and 2 deletions

View File

@ -132,8 +132,23 @@ CREATE TABLE banking_stage_results_2.accounts_map_transaction_latest(
tx_ids BIGINT[]
);
CREATE OR REPLACE FUNCTION __array_reverse(anyarray) RETURNS anyarray AS $$
SELECT ARRAY(
SELECT $1[i]
FROM generate_subscripts($1,1) AS s(i)
ORDER BY i DESC
);
$$ LANGUAGE SQL STRICT IMMUTABLE;
CREATE OR REPLACE FUNCTION array_dedup_append(base bigint[], append bigint[], n_limit int)
RETURNS bigint[]
AS $$
SELECT (array_agg(val))[1+count(*)-n_limit:] FROM unnest(array_cat(base,append)) AS t(val)
$$ LANGUAGE SQL;
SELECT __array_reverse(array_agg(val)) FROM (
SELECT val FROM (
SELECT DISTINCT ON (val) pos, val FROM unnest(__array_reverse(array_cat(base, append))) WITH ORDINALITY as t(val, pos)
) AS deduped
ORDER BY pos
LIMIT n_limit
) AS result
$$ LANGUAGE SQL STRICT IMMUTABLE;