Re: BUG #18632: Whether you need to consider modifying the array's handling of delimiters?

From: Erik Wienhold <ewie(at)ewie(dot)name>
To: zengman(at)halodbtech(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18632: Whether you need to consider modifying the array's handling of delimiters?
Date: 2024-09-25 08:31:59
Message-ID: 44d2a0a2-17ed-4ff9-9076-b05996f2d5a8@ewie.name
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 2024-09-25 09:57 +0200, PG Bug reporting form wrote:
> The following bug has been logged on the website:
>
> Bug reference: 18632
> Logged by: Man Zeng
> Email address: zengman(at)halodbtech(dot)com
> PostgreSQL version: 14.10
> Operating system: centos-8
> Description:
>
> Hi, I found a problem with array separator handling.
> The current handling of delimiters is not quite as expected (not very
> flexible).
> The test SQL and results are shown below.
>
> [postgres(at)halo-centos-8-release ~]$ psql
> psql (14.10)
> Type "help" for help.
>
> postgres=# CREATE OR REPLACE FUNCTION arrayfunc()
> postgres-# RETURNS _varchar
> postgres-# AS $$
> postgres$# SELECT '{''a,3'',''b'',''c''}'::_varchar;
> postgres$# $$ LANGUAGE SQL;
> CREATE FUNCTION
> postgres=# -- array cstring
> postgres=# SELECT arrayfunc();
> arrayfunc
> -----------------
> {'a,3','b','c'}
> (1 row)
>
> postgres=# -- length is 4
> postgres=# SELECT array_length(arrayfunc(), 1);
> array_length
> --------------
> 4
> (1 row)
>
> postgres=# -- first element
> postgres=# SELECT (arrayfunc())[1];
> arrayfunc
> -----------
> 'a
> (1 row)
>
> postgres=# -- second element
> postgres=# SELECT (arrayfunc())[2];
> arrayfunc
> -----------
> 3'
> (1 row)

You need to double-quote elements that contain the separator:

SELECT '{"''a,3''",''b'',''c''}'::varchar[];

That's also documented in the first paragraph of
https://www.postgresql.org/docs/current/arrays.html#ARRAYS-INPUT

So, not a bug.

> postgres=# -- other
> postgres=# SELECT (arrayfunc())[3];
> arrayfunc
> -----------
> 'b'
> (1 row)
>
> postgres=# SELECT (arrayfunc())[4];
> arrayfunc
> -----------
> 'c'
> (1 row)
>
> postgres=# -- The following SQL tests are as expected
> postgres=# CREATE OR REPLACE FUNCTION arrayfunc2()
> postgres-# RETURNS _varchar
> postgres-# AS $$
> postgres$# SELECT '{''a-3'',''b'',''c''}'::_varchar;
> postgres$# $$ LANGUAGE SQL;
> CREATE FUNCTION
> postgres=# -- array cstring
> postgres=# SELECT arrayfunc2();
> arrayfunc2
> -----------------
> {'a-3','b','c'}
> (1 row)
>
> postgres=# -- length is 3
> postgres=# SELECT array_length(arrayfunc2(), 1);
> array_length
> --------------
> 3
> (1 row)
>
> postgres=# -- first element
> postgres=# SELECT (arrayfunc2())[1];
> arrayfunc2
> ------------
> 'a-3'
> (1 row)
>
> So should we consider modifying "array_in" to enhance the handling of
> separators to be more consistent with people's expectations?
>

--
Erik

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message 曾满 2024-09-25 08:54:41 回复:Re: BUG #18632: Whether you need to consider modifying the array's handling of delimiters?
Previous Message PG Bug reporting form 2024-09-25 07:57:22 BUG #18632: Whether you need to consider modifying the array's handling of delimiters?