简体   繁体   中英

Array error from psql: Array value must begin with "}" or dimension information

I have three tables:

  1. text: text in lines

  2. trigram: trigram of all text lines

  3. text_trigram: trigrams that a text line contains, intermediate table

I tried to execute this:

create or replace function get_values(IN t_id integer[], IN tri_id integer[], OUT count_values integer[][])
as $$
declare 
    count_value integer[];
    rec integer;
    tri_n integer := array_length(tri_id, 1);
begin 
    for rec in select unnest(tri_id)
    loop
        select into count_value count(text_id) from unnest(t_id) as required_t_id(r_t_id) left join text_trigram on text_id = required_t_id.r_t_id and trigram_id = rec group by text_id;
        count_values := array_cat(count_values, count_value);
    end loop;
end; $$
language plpgsql;

Execution was fine, after I executed this:

select * from get_values(ARRAY[9,210,999], ARRAY[621, 3266]);

the error message came out

Array error: Array value must begin with "}" or dimension information

I tried below as well,

select * from get_values('{9,210,999}','{621,3266}');

and I got a error message:

function get_values(unkonwn, unknown) is not clear

You are trying to store multiple rows returned by the query into a single integer (the result of a count(text_id) ) into an array ( count_value ) - that won't work.

You need to aggregate the values into a single array and put that result into the variable, as you have a group by, you need to wrap the query.

select array_agg(cnt)
  into count_value 
from (
  select count(text_id) as cnt
  from unnest(tri_id) as required_t_id(r_t_id) 
     left join text_trigram on text_id = required_t_id.r_t_id and trigram_id = rec 
  group by text_id
) t;

The group by looks extremely strange though, I wonder if you actually meant:

select count(text_id)
  into count_value 
from unnest(tri_id) as required_t_id(r_t_id) 
  left join text_trigram on text_id = required_t_id.r_t_id and trigram_id = rec;

You also don't need to unnest the array you want to loop over.

Your loop should look like this:

foreach rec IN ARRAY t_id
loop
...
end loop;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM