Обсуждение: BUG #17664: The subsctring() function with the char() text type does not work correctly

Поиск
Список
Период
Сортировка

BUG #17664: The subsctring() function with the char() text type does not work correctly

От
PG Bug reporting form
Дата:
The following bug has been logged on the website:

Bug reference:      17664
Logged by:          Evgenii Timofeev
Email address:      snztim@yandex.ru
PostgreSQL version: 15.0
Operating system:   ALT Linux 10 (ALT Sisyphus 10.3.1)
Description:

Hi!

PostgreSQL version:
PostgreSQL 15.0 on x86_64-pc-linux-gnu, compiled by x86_64-alt-linux-gcc
(GCC) 10.3.1 20210703 (ALT Sisyphus 10.3.1-alt2), 64-bit

The situation is as follows:
First, we declare a char(10) text variable with the value 'abc' assigned to
it and a varchar(12) text variable within an anonymous block.
Within the body of the anonymous block we saddle a varchar(12) variable with
a char(10) variable and print the result.
The result will be valid data (example 1).

If we do the same, but add the substring() function to concat(), the result
will be incorrect (example 2).
As seen in example 2, the function substring(), which should have printed 4
characters, has printed only 3.

Please tell me why the result is not correct?

Example 1:
do $$
declare
    M1 char(10) := 'abc';
    M2 varchar(12);
begin
    M2 := concat('-',M1,'-');
    raise info '%', M2;
end;
$$;
Execution result:  -abc       -

Example 2:
do $$
declare
    M1 char(10) := 'abc';
    M2 varchar(12);
begin
    M2 := concat('-',substring(M1,1,4),'-');
    raise info '%', M2;
end;
$$;
Execution result:  -abc-


Re: BUG #17664: The subsctring() function with the char() text type does not work correctly

От
"David G. Johnston"
Дата:


On Thu, Oct 27, 2022 at 5:02 AM PG Bug reporting form <noreply@postgresql.org> wrote:
The following bug has been logged on the website:

Bug reference:      17664
Logged by:          Evgenii Timofeev
Email address:      snztim@yandex.ru
PostgreSQL version: 15.0
Operating system:   ALT Linux 10 (ALT Sisyphus 10.3.1)
Description:       

Hi!

If we do the same, but add the substring() function to concat(), the result
will be incorrect (example 2).
As seen in example 2, the function substring(), which should have printed 4
characters, has printed only 3.

Please tell me why the result is not correct?

Because substring only works for text inputs and produces text output so your insignificant whitespace is thrown away if you pass data through that function.

It isn't a bug but a missing feature.  Given that the project basically doesn't recommend people use char this gap in functionality isn't surprising.

David J.

Note that concat is defined polymorphically.