array_dedup_append: implemented faster version

uses nested array remove for merge+deduplication
This commit is contained in:
GroovieGermanikus 2024-01-10 20:11:14 +01:00
parent 82e679af8c
commit 333df8d3ab
No known key found for this signature in database
GPG Key ID: 5B6EB831A5CD2015
2 changed files with 14 additions and 29 deletions

View File

@ -132,36 +132,21 @@ CREATE TABLE banking_stage_results_2.accounts_map_transaction_latest(
tx_ids BIGINT[]
);
CREATE OR REPLACE FUNCTION __array_reverse_bigint(list bigint[]) RETURNS bigint[] AS $$
DECLARE
reversed_out bigint[];
j int;
BEGIN
j := CARDINALITY(list);
FOR i IN 1..CARDINALITY(list) LOOP
reversed_out[j] := list[i];
j := j - 1;
END LOOP;
RETURN reversed_out;
END;
$$ LANGUAGE plpgsql STRICT IMMUTABLE;
CREATE OR REPLACE FUNCTION array_dedup_append(base bigint[], append bigint[], n_limit int)
RETURNS bigint[]
AS $$
DECLARE
outarr bigint[];
tmplist bigint[];
el bigint;
len int;
BEGIN
SELECT __array_reverse_bigint(array_agg(val)) FROM (
SELECT val FROM (
SELECT DISTINCT ON (val) pos, val FROM unnest(__array_reverse_bigint(array_cat(base, append))) WITH ORDINALITY as t(val, pos)
) AS deduped
ORDER BY pos
LIMIT n_limit
) AS result
INTO outarr;
RETURN outarr;
END;
$$ LANGUAGE plpgsql STRICT IMMUTABLE;
tmplist := base;
FOREACH el IN ARRAY append LOOP
tmplist := array_remove(tmplist, el);
tmplist := tmplist || el;
END LOOP;
len := CARDINALITY(tmplist);
RETURN tmplist[(len + 1 - n_limit):];
END
$$ LANGUAGE plpgsql IMMUTABLE CALLED ON NULL INPUT;

View File

@ -454,7 +454,7 @@ impl PostgresSession {
SELECT
acc_id,
array_dedup_append(
COALESCE((SELECT tx_ids FROM banking_stage_results_2.accounts_map_transaction_latest WHERE acc_id=amt_new.acc_id ), array[]::bigint[]),
(SELECT tx_ids FROM banking_stage_results_2.accounts_map_transaction_latest WHERE acc_id=amt_new.acc_id),
amt_new.tx_agged,
{limit}) AS tx_ids_agg
FROM amt_new