I have the below function.
CREATE OR REPLACE FUNCTION pick_values RETURN t1_table
PIPELINED
IS
TYPE t2_type IS
TABLE OF t2%rowtype;
t2_data t2_type;
BEGIN
-- https://stackoverflow.com/a/67398434/1509264
-- License: CC BY-SA 4.0
SELECT
*
BULK COLLECT
INTO t2_data
FROM
t2;
FOR cur IN (
SELECT
*
FROM
t1
ORDER BY
r
) LOOP DECLARE
a_freqs int_list := int_list();
cum_freq INT := 0;
taken string_list := split_string(cur.an, ', ');
idx INT;
c t2.a%TYPE;
BEGIN
a_freqs.extend(t2_data.count);
FOR i IN 1..t2_data.count LOOP IF ( t2_data(i).a = cur.ay AND t2_data(i).c > 0 ) OR ( cur.ay IS NULL AND t2_data(i).a NOT
MEMBER OF taken AND t2_data(i).c > 0 ) THEN
a_freqs(i) := cum_freq + t2_data(i).c;
cum_freq := cum_freq + t2_data(i).c;
ELSE
a_freqs(i) := cum_freq;
END IF;
END LOOP;
IF cum_freq > 0 THEN
idx := floor(dbms_random.value(0, cum_freq));
FOR i IN 1..t2_data.count LOOP IF idx < a_freqs(i) THEN
c := t2_data(i).a;
t2_data(i).c := t2_data(i).c - 1;
EXIT;
END IF;
END LOOP;
END IF;
PIPE ROW ( t1_data(cur.vk, cur.ay, cur.an, cur.r, c) );
END;
END LOOP;
END;
However, I'm struggling to understand the below lines:
a_freqs(i) := cum_freq + t2_data(i).c;
cum_freq := cum_freq + t2_data(i).c;
ELSE
a_freqs(i) := cum_freq;
Could you please help me on what do these lines do? A line over the code with a brief explanation would do. Thanks!
PD: kindly let me know if you need additional info. However, there's the link to the original SO question in the function code.