[MDEV-29646] sformat('Num [{:20}]', 42) gives incorrect result in view Created: 2022-09-27  Updated: 2023-06-07  Resolved: 2023-05-04

Status: Closed
Project: MariaDB Server
Component/s: Data types
Affects Version/s: 10.7, 10.8, 10.9, 10.10, 10.11
Fix Version/s: 10.9.8, 10.10.6, 10.11.5, 11.0.3, 11.1.2

Type: Bug Priority: Major
Reporter: Lena Startseva Assignee: Daniel Black
Resolution: Fixed Votes: 0
Labels: beginner-friendly, view-protocol

Issue Links:
PartOf
is part of MDEV-27691 make working view-protocol Open

 Description   

Test:

create view v1 as select sformat('Num [{:20}]', 42);
select * from v1;
drop view v1;

Expected result:

Num [                  42]

Actual result:

Num [   



 Comments   
Comment by Oleksandr Byelkin [ 2022-11-29 ]

It is not a problem of view print:

show create view v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select sformat('Num [{:20}]',42) AS `sformat('Num [{:20}]', 42)`latin1	latin1_swedish_ci

Comment by Weijun Huang [ 2023-02-26 ]

create view v1 as select sformat('Num [{:6}]', 42);
select * from v1;
sformat('Num [{:6}]', 42)
Num [    42]
drop view v1;
create view v1 as select sformat('Num [{:7}]', 42);
select * from v1;
sformat('Num [{:7}]', 42)
Num [     42
drop view v1;
create view v1 as select sformat('Num [{:8}]', 42);
select * from v1;
sformat('Num [{:8}]', 42)
Num [      4
drop view v1;
create view v1 as select sformat('Num [{:9}]', 42);
select * from v1;
sformat('Num [{:9}]', 42)
Num [       
drop view v1;
main.view_test                           [ pass ]     12

Comment by Weijun Huang [ 2023-02-26 ]

I have discovered a bug in `mariadbd` at `Field_varstring::store(this=0x00000001050eb6a0, from="Num [ 42]", length=15, cs=0x0000000101a23f88)` in the `field.cc` file at line 7872, column 3. The issue is that the `Field_varstring.field_length` value, 12, is smaller than the length of the string, which is causing the error. I have attempted to modify the `field_length` parameter to resolve the issue, but I have been unsuccessful so far.

Comment by Sergei Golubchik [ 2023-03-26 ]

Item_func_sformat determines the max length of the result as a sum of max length of its arguments:

bool Item_func_sformat::fix_length_and_dec(THD *thd)
{
  ...
  for (uint i=0 ; i < arg_count ; i++)
  {
    char_length+= args[i]->max_char_length();

this, of course, makes little sense, because the length of the result can be anything, depending on the format string.

Comment by Daniel Black [ 2023-05-04 ]

Thanks Weijun Huang.

Note Fix Versions may be incorrect. Merged to 10.8 however bb-10.8-release is created and no alternate version exist in JIRA yet.

Generated at Thu Feb 08 10:10:12 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.