fix sql function which did not deduplicate
This commit is contained in:
parent
4f84c8e772
commit
d18c563447
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue