<commit_before>create or replace function londiste.local_add_table(
    in i_queue_name     text,
    in i_table_name     text,
    in i_trg_args       text[],
    out ret_code        int4,
    out ret_note        text)
as $$
-- ----------------------------------------------------------------------
-- Function: londiste.local_add_table(3)
--
--      Register table on Londiste node, with customizable trigger args.
--
-- Parameters:
--      i_queue_name - queue name
--      i_table_name - table name
--      i_trg_args   - args to trigger, or tgflags=X for trigger creation flags
--
-- Trigger args:
--      See documentation for pgq triggers.
--
-- Trigger creation flags (default: AIUDL):
--      I - ON INSERT
--      U - ON UPDATE
--      D - ON DELETE
--      Q - use pgq.sqltriga() as trigger function
--      L - use pgq.logutriga() as trigger function
--      B - BEFORE
--      A - AFTER
--
-- Example:
--      > londiste.local_add_table('q', 'tbl', array['tgflags=BI', 'SKIP', 'pkey=col1,col2'])
--
-- Returns:
--      200 - Ok
--      301 - Warning, trigger exists that will fire before londiste one
--      400 - No such set
-- ----------------------------------------------------------------------
declare
    col_types text;
    fq_table_name text;
    new_state text;

    trunctrg_name text;
    pgversion int;
    logtrg_previous text;
    lg_name text;
    lg_func text;
    lg_pos text;
    lg_event text;
    lg_args text;
    tbl record;
    i integer;
    sql text;
    arg text;
    _node record;
begin
    fq_table_name := londiste.make_fqname(i_table_name);
    col_types := londiste.find_column_types(fq_table_name);
    if position('k' in col_types) < 1 then
        -- allow missing primary key in case of combined table where
        -- pkey was removed by londiste
        perform 1 from londiste.table_info t,
            pgq_node.node_info n_this,
            pgq_node.node_info n_other
          where n_this.queue_name = i_queue_name
            and n_other.combined_queue = n_this.combined_queue
            and n_other.queue_name <> n_this.queue_name
            and t.queue_name = n_other.queue_name
            and t.table_name = fq_table_name
            and t.dropped_ddl is not null;
        if not found then
            select 400, 'Primary key missing on table: ' || fq_table_name into ret_code, ret_note;
            return;
        end if;
    end if;

    select * from pgq_node.get_node_info(i_queue_name) into _node;
    if not found or _node.ret_code >= 400 then
        select 400, 'No such set: ' || i_queue_name into ret_code, ret_note;
        return;
    end if;

    select merge_state, local into tbl
        from londiste.table_info
        where queue_name = i_queue_name and table_name = fq_table_name;
    if not found then
        -- add to set on root
        if _node.node_type = 'root' then
            select f.ret_code, f.ret_note into ret_code, ret_note
                from londiste.global_add_table(i_queue_name, i_table_name) f;
            if ret_code <> 200 then
                return;
            end if;
        else
            select 404, 'Table not available on queue: ' || fq_table_name
                into ret_code, ret_note;
            return;
        end if;

        -- reload info
        select merge_state, local into tbl
            from londiste.table_info
            where queue_name = i_queue_name and table_name = fq_table_name;
    end if;

    if tbl.local then
        select 200, 'Table already added: ' || fq_table_name into ret_code, ret_note;
        return;
    end if;

    if _node.node_type = 'root' then
        new_state := 'ok';
        perform londiste.root_notify_change(i_queue_name, 'londiste.add-table', fq_table_name);
    elsif _node.node_type = 'leaf' and _node.combined_type = 'branch' then
        new_state := 'ok';
    else
        new_state := NULL;
    end if;

    update londiste.table_info
        set local = true,
            merge_state = new_state
        where queue_name = i_queue_name and table_name = fq_table_name;
    if not found then
        raise exception 'lost table: %', fq_table_name;
    end if;

    -- skip triggers on leaf node
    if _node.node_type = 'leaf' then
        select 200, 'Table added: ' || fq_table_name into ret_code, ret_note;
        return;
    end if;

    -- create Ins/Upd/Del trigger if it does not exists already
    lg_name := '_londiste_' || i_queue_name;
    perform 1 from pg_catalog.pg_trigger
        where tgrelid = londiste.find_table_oid(fq_table_name)
            and tgname = lg_name;
    if not found then
        -- new trigger
        lg_func := 'pgq.logutriga';
        lg_event := '';
        lg_args := quote_literal(i_queue_name);
        lg_pos := 'after';

        -- parse extra args
        if array_length(i_trg_args, 1) > 0 then
            for i in array_lower(i_trg_args, 1) .. array_upper(i_trg_args, 1) loop
                arg := i_trg_args[i];
                if arg like 'tgflags=%' then
                    -- special flag handling
                    arg := upper(substr(arg, 9));
                    if position('B' in arg) > 0 then
                        lg_pos := 'before';
                    end if;<commit_msg>londiste.local_add_table: avoid array_length(), its 8.4+<commit_after>create or replace function londiste.local_add_table(
    in i_queue_name     text,
    in i_table_name     text,
    in i_trg_args       text[],
    out ret_code        int4,
    out ret_note        text)
as $$
-- ----------------------------------------------------------------------
-- Function: londiste.local_add_table(3)
--
--      Register table on Londiste node, with customizable trigger args.
--
-- Parameters:
--      i_queue_name - queue name
--      i_table_name - table name
--      i_trg_args   - args to trigger, or tgflags=X for trigger creation flags
--
-- Trigger args:
--      See documentation for pgq triggers.
--
-- Trigger creation flags (default: AIUDL):
--      I - ON INSERT
--      U - ON UPDATE
--      D - ON DELETE
--      Q - use pgq.sqltriga() as trigger function
--      L - use pgq.logutriga() as trigger function
--      B - BEFORE
--      A - AFTER
--
-- Example:
--      > londiste.local_add_table('q', 'tbl', array['tgflags=BI', 'SKIP', 'pkey=col1,col2'])
--
-- Returns:
--      200 - Ok
--      301 - Warning, trigger exists that will fire before londiste one
--      400 - No such set
-- ----------------------------------------------------------------------
declare
    col_types text;
    fq_table_name text;
    new_state text;

    trunctrg_name text;
    pgversion int;
    logtrg_previous text;
    lg_name text;
    lg_func text;
    lg_pos text;
    lg_event text;
    lg_args text;
    tbl record;
    i integer;
    sql text;
    arg text;
    _node record;
begin
    fq_table_name := londiste.make_fqname(i_table_name);
    col_types := londiste.find_column_types(fq_table_name);
    if position('k' in col_types) < 1 then
        -- allow missing primary key in case of combined table where
        -- pkey was removed by londiste
        perform 1 from londiste.table_info t,
            pgq_node.node_info n_this,
            pgq_node.node_info n_other
          where n_this.queue_name = i_queue_name
            and n_other.combined_queue = n_this.combined_queue
            and n_other.queue_name <> n_this.queue_name
            and t.queue_name = n_other.queue_name
            and t.table_name = fq_table_name
            and t.dropped_ddl is not null;
        if not found then
            select 400, 'Primary key missing on table: ' || fq_table_name into ret_code, ret_note;
            return;
        end if;
    end if;

    select * from pgq_node.get_node_info(i_queue_name) into _node;
    if not found or _node.ret_code >= 400 then
        select 400, 'No such set: ' || i_queue_name into ret_code, ret_note;
        return;
    end if;

    select merge_state, local into tbl
        from londiste.table_info
        where queue_name = i_queue_name and table_name = fq_table_name;
    if not found then
        -- add to set on root
        if _node.node_type = 'root' then
            select f.ret_code, f.ret_note into ret_code, ret_note
                from londiste.global_add_table(i_queue_name, i_table_name) f;
            if ret_code <> 200 then
                return;
            end if;
        else
            select 404, 'Table not available on queue: ' || fq_table_name
                into ret_code, ret_note;
            return;
        end if;

        -- reload info
        select merge_state, local into tbl
            from londiste.table_info
            where queue_name = i_queue_name and table_name = fq_table_name;
    end if;

    if tbl.local then
        select 200, 'Table already added: ' || fq_table_name into ret_code, ret_note;
        return;
    end if;

    if _node.node_type = 'root' then
        new_state := 'ok';
        perform londiste.root_notify_change(i_queue_name, 'londiste.add-table', fq_table_name);
    elsif _node.node_type = 'leaf' and _node.combined_type = 'branch' then
        new_state := 'ok';
    else
        new_state := NULL;
    end if;

    update londiste.table_info
        set local = true,
            merge_state = new_state
        where queue_name = i_queue_name and table_name = fq_table_name;
    if not found then
        raise exception 'lost table: %', fq_table_name;
    end if;

    -- skip triggers on leaf node
    if _node.node_type = 'leaf' then
        select 200, 'Table added: ' || fq_table_name into ret_code, ret_note;
        return;
    end if;

    -- create Ins/Upd/Del trigger if it does not exists already
    lg_name := '_londiste_' || i_queue_name;
    perform 1 from pg_catalog.pg_trigger
        where tgrelid = londiste.find_table_oid(fq_table_name)
            and tgname = lg_name;
    if not found then
        -- new trigger
        lg_func := 'pgq.logutriga';
        lg_event := '';
        lg_args := quote_literal(i_queue_name);
        lg_pos := 'after';

        -- parse extra args
        if array_lower(i_trg_args, 1) is not null then
            for i in array_lower(i_trg_args, 1) .. array_upper(i_trg_args, 1) loop
                arg := i_trg_args[i];
                if arg like 'tgflags=%' then
                    -- special flag handling
                    arg := upper(substr(arg, 9));
                    if position('B' in arg) > 0 then
                        lg_pos := 'before';
                    end if;
{'fraction_non_alphanumeric': 0.10823170731707317, 'fraction_numerical': 0.008288871951219513, 'mean_word_length': 1.817230273752013, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7386100', 'n_tokens_mistral': 3417, 'n_tokens_neox': 3251, 'n_words': 951}