Commit 62d4e7e2 authored by Sergei Golubchik's avatar Sergei Golubchik

remove auto-switch between char and string based on the string length

1. it is against sql way, auto-detection should use metadata, not data,
consider:

create table t1 (qwe varchar(10)) as values ('qwe'),('qw'),('q'),('werty');
select sformat('{:*>5s}', qwe) from t1;

this will auto-break on the third row.

2. using max_char_length() instead of length() fixes that, but
there's a second big issue, fmt < 8.0 doesn't natively support unicode,
so {:c} would only work for one-byte strings, for 'a', not for 'я'

because of all that let's always format strings as strings.
{:c} will only now work for numbers and still only in the ascii range.
parent ba7287df
...@@ -222,8 +222,8 @@ Number: **4** ...@@ -222,8 +222,8 @@ Number: **4**
select sformat('{:02} - {:02} - {:02}', 1, 2, 3); select sformat('{:02} - {:02} - {:02}', 1, 2, 3);
sformat('{:02} - {:02} - {:02}', 1, 2, 3) sformat('{:02} - {:02} - {:02}', 1, 2, 3)
01 - 02 - 03 01 - 02 - 03
select sformat('Character {:c}', 'h'); select sformat('Character {:c}', 104);
sformat('Character {:c}', 'h') sformat('Character {:c}', 104)
Character h Character h
select sformat('String {:s}', 'hello'); select sformat('String {:s}', 'hello');
sformat('String {:s}', 'hello') sformat('String {:s}', 'hello')
...@@ -391,17 +391,17 @@ Warning 4183 SFORMAT error: format specifier requires numeric argument ...@@ -391,17 +391,17 @@ Warning 4183 SFORMAT error: format specifier requires numeric argument
# Table Format Test Cases # Table Format Test Cases
# #
create table t1 (pformat text, data1 FLOAT, data2 INT, create table t1 (pformat text, data1 FLOAT, data2 INT,
data3 VARCHAR(30), data4 CHAR); data3 VARCHAR(30));
insert into t1 values ('{:.2f} {: } |{:*^15}| {:c}', 1.11, 1, 'example I', 'A'), insert into t1 values ('{:.2f} {: } |{:*^15}', 1.11, 1, 'example I'),
('{:.1f} {:-} |{:^15}| {:c}', -2.22, -2, 'example II', 'B'), ('{:.1f} {:-} |{:^15}', -2.22, -2, 'example II'),
('{:.2f} {:+} |{:<15}| {:c}', 3.33, 3, 'example III', 'C'), ('{:.2f} {:+} |{:<15}', 3.33, 3, 'example III'),
('{:.4f} {:d} |{:^15}| {:c}', -4.44, -4, 'example IV', 'D'); ('{:.4f} {:d} |{:^15}', -4.44, -4, 'example IV');
select sformat(pformat, data1, data2, data3, data4) from t1; select sformat(pformat, data1, data2, data3) from t1;
sformat(pformat, data1, data2, data3, data4) sformat(pformat, data1, data2, data3)
1.11 1 |***example I***| A 1.11 1 |***example I***
-2.2 -2 | example II | B -2.2 -2 | example II
3.33 +3 |example III | C 3.33 +3 |example III
-4.4400 -4 | example IV | D -4.4400 -4 | example IV
drop table t1; drop table t1;
create table t2 (a double); create table t2 (a double);
insert into t2 values (3.14159265358979323846); insert into t2 values (3.14159265358979323846);
......
...@@ -118,7 +118,7 @@ select sformat('Num {:L}', 13800000000); ...@@ -118,7 +118,7 @@ select sformat('Num {:L}', 13800000000);
select sformat('Num [{:20}]', 42); select sformat('Num [{:20}]', 42);
select sformat('Number: {:*^{}}', 4, 5); select sformat('Number: {:*^{}}', 4, 5);
select sformat('{:02} - {:02} - {:02}', 1, 2, 3); select sformat('{:02} - {:02} - {:02}', 1, 2, 3);
select sformat('Character {:c}', 'h'); select sformat('Character {:c}', 104);
select sformat('String {:s}', 'hello'); select sformat('String {:s}', 'hello');
select sformat('Large {0:+010.4g}', 392.64); select sformat('Large {0:+010.4g}', 392.64);
select sformat('Large {:g}', 392.65); select sformat('Large {:g}', 392.65);
...@@ -171,12 +171,12 @@ echo #; ...@@ -171,12 +171,12 @@ echo #;
echo # Table Format Test Cases; echo # Table Format Test Cases;
echo #; echo #;
create table t1 (pformat text, data1 FLOAT, data2 INT, create table t1 (pformat text, data1 FLOAT, data2 INT,
data3 VARCHAR(30), data4 CHAR); data3 VARCHAR(30));
insert into t1 values ('{:.2f} {: } |{:*^15}| {:c}', 1.11, 1, 'example I', 'A'), insert into t1 values ('{:.2f} {: } |{:*^15}', 1.11, 1, 'example I'),
('{:.1f} {:-} |{:^15}| {:c}', -2.22, -2, 'example II', 'B'), ('{:.1f} {:-} |{:^15}', -2.22, -2, 'example II'),
('{:.2f} {:+} |{:<15}| {:c}', 3.33, 3, 'example III', 'C'), ('{:.2f} {:+} |{:<15}', 3.33, 3, 'example III'),
('{:.4f} {:d} |{:^15}| {:c}', -4.44, -4, 'example IV', 'D'); ('{:.4f} {:d} |{:^15}', -4.44, -4, 'example IV');
select sformat(pformat, data1, data2, data3, data4) from t1; select sformat(pformat, data1, data2, data3) from t1;
drop table t1; drop table t1;
create table t2 (a double); create table t2 (a double);
......
...@@ -1401,10 +1401,7 @@ String *Item_func_sformat::val_str(String *res) ...@@ -1401,10 +1401,7 @@ String *Item_func_sformat::val_str(String *res)
delete [] vargs; delete [] vargs;
return NULL; return NULL;
} }
if (parg->length() == 1) vargs[carg-1]= fmt::detail::make_arg<ctx>(*parg);
vargs[carg-1]= fmt::detail::make_arg<ctx>(parg->ptr()[0]);
else
vargs[carg-1]= fmt::detail::make_arg<ctx>(*parg);
break; break;
case TIME_RESULT: // TODO case TIME_RESULT: // TODO
case ROW_RESULT: // TODO case ROW_RESULT: // TODO
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment