Skip to content

Commit a363ccd

Browse files
committed
misc cleanups
* comment - use the standard style * no need to reimplement for with while * TODO comments for not implemented types * rename the error not to refer to the underlying library, it's the implementation detail * use res->length(0) to set length to 0, but preserve the already allocated buffer * rename main.sformat -> main.func_sformat * removed a duplicated part of the test
1 parent e214e60 commit a363ccd

File tree

5 files changed

+56
-161
lines changed

5 files changed

+56
-161
lines changed

mysql-test/main/sformat.result renamed to mysql-test/main/func_sformat.result

Lines changed: 23 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#
2+
# MDEV-25015 Custom formatting of strings in MariaDB queries
3+
#
4+
#
25
# Normal Test Cases
36
#
47
select sformat('string test');
@@ -109,7 +112,7 @@ select sformat('R={ }', 42);
109112
sformat('R={ }', 42)
110113
NULL
111114
Warnings:
112-
Warning 4183 Wrong format exception: invalid format string
115+
Warning 4183 SFORMAT error: invalid format string
113116
select sformat(NULL, 'Null', 'Test');
114117
sformat(NULL, 'Null', 'Test')
115118
NULL
@@ -123,17 +126,17 @@ select sformat('My { Test');
123126
sformat('My { Test')
124127
NULL
125128
Warnings:
126-
Warning 4183 Wrong format exception: invalid format string
129+
Warning 4183 SFORMAT error: invalid format string
127130
select sformat('Test { {');
128131
sformat('Test { {')
129132
NULL
130133
Warnings:
131-
Warning 4183 Wrong format exception: invalid format string
134+
Warning 4183 SFORMAT error: invalid format string
132135
select sformat('A{}C{}E{}', 'B', 'D');
133136
sformat('A{}C{}E{}', 'B', 'D')
134137
NULL
135138
Warnings:
136-
Warning 4183 Wrong format exception: argument not found
139+
Warning 4183 SFORMAT error: argument not found
137140
select sformat();
138141
ERROR 42000: Incorrect parameter count in the call to native function 'sformat'
139142
#
@@ -149,7 +152,7 @@ string: xyz, number: 123
149152
NULL
150153
Test case Print -> -32
151154
Warnings:
152-
Warning 4183 Wrong format exception: argument not found
155+
Warning 4183 SFORMAT error: argument not found
153156
drop table t1;
154157
create table t2 (param1 FLOAT, param2 SMALLINT, param3 CHAR, param4 DATE);
155158
insert into t2 values (0.0025, 25, 'A', DATE('2020-06-29')),
@@ -313,77 +316,77 @@ select sformat('Test {:c}', 'word');
313316
sformat('Test {:c}', 'word')
314317
NULL
315318
Warnings:
316-
Warning 4183 Wrong format exception: invalid type specifier
319+
Warning 4183 SFORMAT error: invalid type specifier
317320
select sformat('Test {one} {two} {three}', 1, 2, 3);
318321
sformat('Test {one} {two} {three}', 1, 2, 3)
319322
NULL
320323
Warnings:
321-
Warning 4183 Wrong format exception: argument not found
324+
Warning 4183 SFORMAT error: argument not found
322325
select sformat('Number {:{<}', 8);
323326
sformat('Number {:{<}', 8)
324327
NULL
325328
Warnings:
326-
Warning 4183 Wrong format exception: invalid fill character '{'
329+
Warning 4183 SFORMAT error: invalid fill character '{'
327330
select sformat('Number {:10000000000}', 5);
328331
sformat('Number {:10000000000}', 5)
329332
NULL
330333
Warnings:
331-
Warning 4183 Wrong format exception: number is too big
334+
Warning 4183 SFORMAT error: number is too big
332335
select sformat('1={1} 2={2} 0={0}', 0, 1);
333336
sformat('1={1} 2={2} 0={0}', 0, 1)
334337
NULL
335338
Warnings:
336-
Warning 4183 Wrong format exception: argument not found
339+
Warning 4183 SFORMAT error: argument not found
337340
select sformat('Number {:.2d}', 42);
338341
sformat('Number {:.2d}', 42)
339342
NULL
340343
Warnings:
341-
Warning 4183 Wrong format exception: precision not allowed for this argument type
344+
Warning 4183 SFORMAT error: precision not allowed for this argument type
342345
select sformat('You scored {:.0%}', 0.25);
343346
sformat('You scored {:.0%}', 0.25)
344347
NULL
345348
Warnings:
346-
Warning 4183 Wrong format exception: invalid type specifier
349+
Warning 4183 SFORMAT error: invalid type specifier
347350
select sformat('You scored {:%}', 0.25);
348351
sformat('You scored {:%}', 0.25)
349352
NULL
350353
Warnings:
351-
Warning 4183 Wrong format exception: invalid type specifier
354+
Warning 4183 SFORMAT error: invalid type specifier
352355
select sformat('The price is {:f} dollars.', 45);
353356
sformat('The price is {:f} dollars.', 45)
354357
NULL
355358
Warnings:
356-
Warning 4183 Wrong format exception: invalid type specifier
359+
Warning 4183 SFORMAT error: invalid type specifier
357360
select sformat('The price is {:.2f} dollars.', 45);
358361
sformat('The price is {:.2f} dollars.', 45)
359362
NULL
360363
Warnings:
361-
Warning 4183 Wrong format exception: precision not allowed for this argument type
364+
Warning 4183 SFORMAT error: precision not allowed for this argument type
362365
select sformat('We have {:E} chickens.', 5);
363366
sformat('We have {:E} chickens.', 5)
364367
NULL
365368
Warnings:
366-
Warning 4183 Wrong format exception: invalid type specifier
369+
Warning 4183 SFORMAT error: invalid type specifier
367370
select sformat('We have {:e} chickens.', 5);
368371
sformat('We have {:e} chickens.', 5)
369372
NULL
370373
Warnings:
371-
Warning 4183 Wrong format exception: invalid type specifier
374+
Warning 4183 SFORMAT error: invalid type specifier
372375
select sformat('The universe is {:,} years old.', 13800000000);
373376
sformat('The universe is {:,} years old.', 13800000000)
374377
NULL
375378
Warnings:
376-
Warning 4183 Wrong format exception: invalid type specifier
379+
Warning 4183 SFORMAT error: invalid type specifier
377380
select sformat('The universe is {:_} years old.', 13800000000);
378381
sformat('The universe is {:_} years old.', 13800000000)
379382
NULL
380383
Warnings:
381-
Warning 4183 Wrong format exception: invalid type specifier
384+
Warning 4183 SFORMAT error: invalid type specifier
382385
select sformat('String {:-}', 'hello');
383386
sformat('String {:-}', 'hello')
384387
NULL
385388
Warnings:
386-
Warning 4183 Wrong format exception: format specifier requires numeric argument
389+
Warning 4183 SFORMAT error: format specifier requires numeric argument
387390
#
388391
# Table Format Test Cases
389392
#
@@ -406,71 +409,3 @@ select a, sformat('{:.15f}', a) from t2;
406409
a sformat('{:.15f}', a)
407410
3.141592653589793 3.141592653589793
408411
drop table t2;
409-
#
410-
# Table Test Cases
411-
#
412-
create table t1 (param1 text, param2 text, param3 int);
413-
insert into t1 values ('string: {}, number: {}', 'xyz', 123),
414-
('something {} went {} wrong {}', 'foo', 0),
415-
('Test case {} -> {}', 'Print', -32);
416-
select sformat(param1, param2, param3) from t1;
417-
sformat(param1, param2, param3)
418-
string: xyz, number: 123
419-
NULL
420-
Test case Print -> -32
421-
Warnings:
422-
Warning 4183 Wrong format exception: argument not found
423-
drop table t1;
424-
create table t2 (param1 FLOAT, param2 SMALLINT, param3 CHAR, param4 DATE);
425-
insert into t2 values (0.0025, 25, 'A', DATE('2020-06-29')),
426-
(0.0005, 5, 'B', DATE('2020-6-29')),
427-
(5.5555, -5, 'C', DATE('200629')),
428-
(-9, -9, 'D', DATE('20*06*29'));
429-
select sformat('p1 {:.4f} p2 {} p3 {} p4 {}', param1, param2, param3, param4) from t2;
430-
sformat('p1 {:.4f} p2 {} p3 {} p4 {}', param1, param2, param3, param4)
431-
p1 0.0025 p2 25 p3 A p4 2020-06-29
432-
p1 0.0005 p2 5 p3 B p4 2020-06-29
433-
p1 5.5555 p2 -5 p3 C p4 2020-06-29
434-
p1 -9.0000 p2 -9 p3 D p4 2020-06-29
435-
drop table t2;
436-
set names utf8;
437-
create table t3 (format_str text character set 'latin1',
438-
first_param text character set 'utf8',
439-
second_param text character set 'latin1');
440-
insert into t3 values ('test 1 {} {}', UNHEX('C3A5'), UNHEX('E5'));
441-
select sformat(format_str, first_param, second_param) from t3;
442-
sformat(format_str, first_param, second_param)
443-
test 1 å å
444-
select HEX(sformat(format_str, first_param, second_param)) from t3;
445-
HEX(sformat(format_str, first_param, second_param))
446-
74657374203120C3A520C3A5
447-
drop table t3;
448-
set names latin1;
449-
create table t4 (p1 bit(8), p2 boolean, p3 DECIMAL, p4 TIMESTAMP);
450-
insert into t4 values (42, TRUE, 42, '2021-08-18 16:50:07'),
451-
(24, FALSE, 24, '0000-00-00 00:00:00');
452-
select sformat('{}: {} {} {} {}', 'Data', p1, p2, p3, p4) from t4;
453-
sformat('{}: {} {} {} {}', 'Data', p1, p2, p3, p4)
454-
Data: 42 1 42 2021-08-18 16:50:07
455-
Data: 24 0 24 0000-00-00 00:00:00
456-
drop table t4;
457-
set names utf8;
458-
create table t5 (param text character SET utf8 COLLATE utf8_general_ci);
459-
insert into t5 values (UNHEX('C3A5')),(UNHEX('C5BB')),(UNHEX('e0b1bb')),
460-
(UNHEX('C38A')), (NULL);
461-
select sformat('{}', param) from t5;
462-
sformat('{}', param)
463-
å
464-
Ż
465-
466-
Ê
467-
NULL
468-
select HEX(sformat('{}', param)) from t5;
469-
HEX(sformat('{}', param))
470-
C3A5
471-
C5BB
472-
E0B1BB
473-
C38A
474-
NULL
475-
drop table t5;
476-
set names latin1;

mysql-test/main/sformat.test renamed to mysql-test/main/func_sformat.test

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
echo #;
2+
echo # MDEV-25015 Custom formatting of strings in MariaDB queries;
3+
echo #;
4+
15
# Description
26
# -----------
37
# Test cases for the sformat function
@@ -61,9 +65,9 @@ select sformat(NULL);
6165
select sformat('My { Test');
6266
select sformat('Test { {');
6367
select sformat('A{}C{}E{}', 'B', 'D');
64-
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
68+
error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT;
6569
select sformat();
66-
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
70+
error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT;
6771
echo #;
6872
echo # Table Test Cases;
6973
echo #;
@@ -179,46 +183,3 @@ create table t2 (a double);
179183
insert into t2 values (3.14159265358979323846);
180184
select a, sformat('{:.15f}', a) from t2;
181185
drop table t2;
182-
183-
echo #;
184-
echo # Table Test Cases;
185-
echo #;
186-
create table t1 (param1 text, param2 text, param3 int);
187-
insert into t1 values ('string: {}, number: {}', 'xyz', 123),
188-
('something {} went {} wrong {}', 'foo', 0),
189-
('Test case {} -> {}', 'Print', -32);
190-
select sformat(param1, param2, param3) from t1;
191-
drop table t1;
192-
193-
create table t2 (param1 FLOAT, param2 SMALLINT, param3 CHAR, param4 DATE);
194-
insert into t2 values (0.0025, 25, 'A', DATE('2020-06-29')),
195-
(0.0005, 5, 'B', DATE('2020-6-29')),
196-
(5.5555, -5, 'C', DATE('200629')),
197-
(-9, -9, 'D', DATE('20*06*29'));
198-
select sformat('p1 {:.4f} p2 {} p3 {} p4 {}', param1, param2, param3, param4) from t2;
199-
drop table t2;
200-
201-
set names utf8;
202-
create table t3 (format_str text character set 'latin1',
203-
first_param text character set 'utf8',
204-
second_param text character set 'latin1');
205-
insert into t3 values ('test 1 {} {}', UNHEX('C3A5'), UNHEX('E5'));
206-
select sformat(format_str, first_param, second_param) from t3;
207-
select HEX(sformat(format_str, first_param, second_param)) from t3;
208-
drop table t3;
209-
set names latin1;
210-
211-
create table t4 (p1 bit(8), p2 boolean, p3 DECIMAL, p4 TIMESTAMP);
212-
insert into t4 values (42, TRUE, 42, '2021-08-18 16:50:07'),
213-
(24, FALSE, 24, '0000-00-00 00:00:00');
214-
select sformat('{}: {} {} {} {}', 'Data', p1, p2, p3, p4) from t4;
215-
drop table t4;
216-
217-
set names utf8;
218-
create table t5 (param text character SET utf8 COLLATE utf8_general_ci);
219-
insert into t5 values (UNHEX('C3A5')),(UNHEX('C5BB')),(UNHEX('e0b1bb')),
220-
(UNHEX('C38A')), (NULL);
221-
select sformat('{}', param) from t5;
222-
select HEX(sformat('{}', param)) from t5;
223-
drop table t5;
224-
set names latin1;

sql/item_create.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5027,16 +5027,16 @@ Create_func_sformat::create_native(THD *thd, LEX_CSTRING *name,
50275027
List<Item> *item_list)
50285028
{
50295029
int arg_count= 0;
5030-
5030+
50315031
if (item_list != NULL)
50325032
arg_count= item_list->elements;
5033-
5033+
50345034
if (unlikely(arg_count < 1))
50355035
{
50365036
my_error(ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, MYF(0), name->str);
50375037
return NULL;
50385038
}
5039-
5039+
50405040
return new (thd->mem_root) Item_func_sformat(thd, *item_list);
50415041
}
50425042

0 commit comments

Comments
 (0)