what is good solution for support NULL inside string_to_array function?

Поиск
Список
Период
Сортировка
Искать
От
Pavel Stehule
Тема
what is good solution for support NULL inside string_to_array function?
Дата
в 19:06:12
Msg-id
AANLkTinzwMEPss-SO-w3p6AoQ23hBdcuiMQAeXSUc_xd@mail.gmail.com
Список
Дерево обсуждения
what is good solution for support NULL inside string_to_array function? Pavel Stehule <pavel.stehule@gmail.com>
Re: what is good solution for support NULL inside string_to_array function? Josh Berkus <josh@agliodbs.com>
Re: what is good solution for support NULL inside string_to_array function? Tom Lane <tgl@sss.pgh.pa.us>
Re: what is good solution for support NULL inside string_to_array function? Andrew Dunstan <andrew@dunslane.net>
Re: what is good solution for support NULL inside string_to_array function? Steve Crawford <scrawford@pinpointresearch.com>
Re: what is good solution for support NULL inside string_to_array function? Pavel Stehule <pavel.stehule@gmail.com>
Re: what is good solution for support NULL inside string_to_array function? Pavel Stehule <pavel.stehule@gmail.com>
Re: what is good solution for support NULL inside string_to_array function? Merlin Moncure <mmoncure@gmail.com>
Re: what is good solution for support NULL inside string_to_array function? Pavel Stehule <pavel.stehule@gmail.com>
Re: what is good solution for support NULL inside string_to_array function? Pavel Stehule <pavel.stehule@gmail.com>
Hello

I understand why we don't support expression 'null'::sometype. But it
does problems with array deserialisation.

postgres=# select array_to_string(ARRAY[10,20,30,NULL,30], '|');array_to_string
-----------------10|20|30|30
(1 row)

quietly removing NULL is maybe good for compatibility but is wrong for
functionality. Can we enhance function array_to_string and
string_to_array like:

CREATE OR REPLACE FUNCTION array_to_string(dta anyarray, sep text,
nullsym text)
RETURNS text AS $$ SELECT array_to_string(ARRAY(SELECT coalesce(v::text,$3)                                                   FROM unnest($1) g(v)),$2)
$$ LANGUAGE sql;
CREATE FUNCTION
Time: 231.445 ms
postgres=# select array_to_string(ARRAY[10,20,30,NULL,30], '|', '');array_to_string
-----------------10|20|30||30
(1 row)

Time: 230.879 ms
postgres=# select array_to_string(ARRAY[10,20,30,NULL,30], '|', 'NULL');array_to_string
------------------10|20|30|NULL|30
(1 row)

Time: 2.031 ms

CREATE OR REPLACE FUNCTION string_to_array(str text, sep text, nullsym text)
RETURNS text[] AS $$ SELECT ARRAY(SELECT CASE WHEN v <> $3 THEN v ELSE NULL END                                FROM unnest(string_to_array($1,$2)) g(v))
$$ LANGUAGE sql;
CREATE FUNCTION
Time: 29.044 ms

postgres=# SELECT string_to_array('10,20,30,,40',',',''); string_to_array
--------------------{10,20,30,NULL,40}
(1 row)

postgres=# SELECT string_to_array('10,20,30,,40',',','')::int[]; string_to_array
--------------------{10,20,30,NULL,40}
(1 row)

it is correct?

other ideas?

Regards
Pavel Stehule

В списке pgsql-hackers по дате отправления
От: Josh Berkus
Дата:
От: Josh Berkus
Дата:
FAQ