3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing built-in functions.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 # Create a table to work with.
21 execsql {CREATE TABLE tbl1(t1 text)}
22 foreach word {this program is free software} {
23 execsql "INSERT INTO tbl1 VALUES('$word')"
25 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
26 } {free is program software this}
30 INSERT INTO t2 VALUES(1);
31 INSERT INTO t2 VALUES(NULL);
32 INSERT INTO t2 VALUES(345);
33 INSERT INTO t2 VALUES(NULL);
34 INSERT INTO t2 VALUES(67890);
39 # Check out the length() function
42 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
45 set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
47 } {1 {wrong number of arguments to function length()}}
49 set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
51 } {1 {wrong number of arguments to function length()}}
53 execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
57 execsql {SELECT coalesce(length(a),-1) FROM t2}
60 # Check out the substr() function
63 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
66 execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
69 execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
72 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
75 execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
78 execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
81 execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
84 execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
87 execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
88 } {this software free program is}
90 execsql {SELECT substr(a,1,1) FROM t2}
93 execsql {SELECT substr(a,2,2) FROM t2}
96 # Only do the following tests if TCL has UTF-8 capabilities
98 if {"\u1234"!="u1234"} {
100 # Put some UTF-8 characters in the database
103 execsql {DELETE FROM tbl1}
104 foreach word "contains UTF-8 characters hi\u1234ho" {
105 execsql "INSERT INTO tbl1 VALUES('$word')"
107 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
108 } "UTF-8 characters contains hi\u1234ho"
110 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
113 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
116 execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
117 } "UTF cha con hi\u1234"
119 execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
122 execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
123 } "TF- har ont i\u1234h"
125 execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
128 execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
131 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
134 execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
137 execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
138 } "TF- ter ain i\u1234h"
140 execsql {DELETE FROM tbl1}
141 foreach word {this program is free software} {
142 execsql "INSERT INTO tbl1 VALUES('$word')"
144 execsql {SELECT t1 FROM tbl1}
145 } {this program is free software}
147 } ;# End \u1234!=u1234
149 # Test the abs() and round() functions.
151 ifcapable !floatingpoint {
154 CREATE TABLE t1(a,b,c);
155 INSERT INTO t1 VALUES(1,2,3);
156 INSERT INTO t1 VALUES(2,12345678901234,-1234567890);
157 INSERT INTO t1 VALUES(3,-2,-5);
159 catchsql {SELECT abs(a,b) FROM t1}
160 } {1 {wrong number of arguments to function abs()}}
162 ifcapable floatingpoint {
165 CREATE TABLE t1(a,b,c);
166 INSERT INTO t1 VALUES(1,2,3);
167 INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890);
168 INSERT INTO t1 VALUES(3,-2,-5);
170 catchsql {SELECT abs(a,b) FROM t1}
171 } {1 {wrong number of arguments to function abs()}}
174 catchsql {SELECT abs() FROM t1}
175 } {1 {wrong number of arguments to function abs()}}
176 ifcapable floatingpoint {
178 catchsql {SELECT abs(b) FROM t1 ORDER BY a}
179 } {0 {2 1.2345678901234 2}}
181 catchsql {SELECT abs(c) FROM t1 ORDER BY a}
182 } {0 {3 12345.6789 5}}
184 ifcapable !floatingpoint {
185 if {[working_64bit_int]} {
187 catchsql {SELECT abs(b) FROM t1 ORDER BY a}
188 } {0 {2 12345678901234 2}}
191 catchsql {SELECT abs(c) FROM t1 ORDER BY a}
192 } {0 {3 1234567890 5}}
195 execsql {SELECT abs(a) FROM t2}
196 } {1 {} 345 {} 67890}
198 execsql {SELECT abs(t1) FROM tbl1}
199 } {0.0 0.0 0.0 0.0 0.0}
201 ifcapable floatingpoint {
203 catchsql {SELECT round(a,b,c) FROM t1}
204 } {1 {wrong number of arguments to function round()}}
206 catchsql {SELECT round(b,2) FROM t1 ORDER BY b}
207 } {0 {-2.0 1.23 2.0}}
209 catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
212 catchsql {SELECT round(c) FROM t1 ORDER BY a}
213 } {0 {3.0 -12346.0 -5.0}}
215 catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
216 } {0 {3.0 -12345.68 -5.0}}
218 catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
219 } {0 {x3.0y x-12345.68y x-5.0y}}
221 catchsql {SELECT round() FROM t1 ORDER BY a}
222 } {1 {wrong number of arguments to function round()}}
224 execsql {SELECT coalesce(round(a,2),'nil') FROM t2}
225 } {1.0 nil 345.0 nil 67890.0}
227 execsql {SELECT round(t1,2) FROM tbl1}
228 } {0.0 0.0 0.0 0.0 0.0}
230 execsql {SELECT typeof(round(5.1,1));}
233 execsql {SELECT typeof(round(5.1));}
236 catchsql {SELECT round(b,2.0) FROM t1 ORDER BY b}
237 } {0 {-2.0 1.23 2.0}}
238 # Verify some values reported on the mailing list.
239 # Some of these fail on MSVC builds with 64-bit
240 # long doubles, but not on GCC builds with 80-bit
242 for {set i 1} {$i<999} {incr i} {
243 set x1 [expr 40222.5 + $i]
244 set x2 [expr 40223.0 + $i]
245 do_test func-4.17.$i {
246 execsql {SELECT round($x1);}
249 for {set i 1} {$i<999} {incr i} {
250 set x1 [expr 40222.05 + $i]
251 set x2 [expr 40222.10 + $i]
252 do_test func-4.18.$i {
253 execsql {SELECT round($x1,1);}
257 execsql {SELECT round(40223.4999999999);}
260 execsql {SELECT round(40224.4999999999);}
263 execsql {SELECT round(40225.4999999999);}
265 for {set i 1} {$i<10} {incr i} {
266 do_test func-4.23.$i {
267 execsql {SELECT round(40223.4999999999,$i);}
269 do_test func-4.24.$i {
270 execsql {SELECT round(40224.4999999999,$i);}
272 do_test func-4.25.$i {
273 execsql {SELECT round(40225.4999999999,$i);}
276 for {set i 10} {$i<32} {incr i} {
277 do_test func-4.26.$i {
278 execsql {SELECT round(40223.4999999999,$i);}
280 do_test func-4.27.$i {
281 execsql {SELECT round(40224.4999999999,$i);}
283 do_test func-4.28.$i {
284 execsql {SELECT round(40225.4999999999,$i);}
288 execsql {SELECT round(1234567890.5);}
291 execsql {SELECT round(12345678901.5);}
294 execsql {SELECT round(123456789012.5);}
297 execsql {SELECT round(1234567890123.5);}
300 execsql {SELECT round(12345678901234.5);}
303 execsql {SELECT round(1234567890123.35,1);}
306 execsql {SELECT round(1234567890123.445,2);}
309 execsql {SELECT round(99999999999994.5);}
312 execsql {SELECT round(9999999999999.55,1);}
315 execsql {SELECT round(9999999999999.555,2);}
319 # Test the upper() and lower() functions
322 execsql {SELECT upper(t1) FROM tbl1}
323 } {THIS PROGRAM IS FREE SOFTWARE}
325 execsql {SELECT lower(upper(t1)) FROM tbl1}
326 } {this program is free software}
328 execsql {SELECT upper(a), lower(a) FROM t2}
329 } {1 1 {} {} 345 345 {} {} 67890 67890}
332 catchsql {SELECT upper(a,5) FROM t2}
333 } {1 {wrong number of arguments to function upper()}}
336 catchsql {SELECT upper(*) FROM t2}
337 } {1 {wrong number of arguments to function upper()}}
339 # Test the coalesce() and nullif() functions
342 execsql {SELECT coalesce(a,'xyz') FROM t2}
343 } {1 xyz 345 xyz 67890}
345 execsql {SELECT coalesce(upper(a),'nil') FROM t2}
346 } {1 nil 345 nil 67890}
348 execsql {SELECT coalesce(nullif(1,1),'nil')}
351 execsql {SELECT coalesce(nullif(1,2),'nil')}
354 execsql {SELECT coalesce(nullif(1,NULL),'nil')}
358 # Test the last_insert_rowid() function
361 execsql {SELECT last_insert_rowid()}
362 } [db last_insert_rowid]
364 # Tests for aggregate functions and how they handle NULLs.
366 ifcapable floatingpoint {
369 execsql {EXPLAIN SELECT sum(a) FROM t2;}
372 SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
374 } {68236 3 22745.33 1 67890 5}
376 ifcapable !floatingpoint {
379 execsql {EXPLAIN SELECT sum(a) FROM t2;}
382 SELECT sum(a), count(a), avg(a), min(a), max(a), count(*) FROM t2;
384 } {68236 3 22745.0 1 67890 5}
388 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2;
390 } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
395 CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
396 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
398 } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
402 CREATE TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
403 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
405 } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
409 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
411 } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
415 SELECT sum(x) FROM (SELECT '9223372036' || '854775807' AS x
416 UNION ALL SELECT -9223372036854775807)
421 SELECT typeof(sum(x)) FROM (SELECT '9223372036' || '854775807' AS x
422 UNION ALL SELECT -9223372036854775807)
427 SELECT typeof(sum(x)) FROM (SELECT '9223372036' || '854775808' AS x
428 UNION ALL SELECT -9223372036854775807)
431 ifcapable floatingpoint {
434 SELECT sum(x)>0.0 FROM (SELECT '9223372036' || '854775808' AS x
435 UNION ALL SELECT -9223372036850000000)
439 ifcapable !floatingpoint {
442 SELECT sum(x)>0 FROM (SELECT '9223372036' || '854775808' AS x
443 UNION ALL SELECT -9223372036850000000)
449 # How do you test the random() function in a meaningful, deterministic way?
453 SELECT random() is not null;
458 SELECT typeof(random());
463 SELECT randomblob(32) is not null;
468 SELECT typeof(randomblob(32));
473 SELECT length(randomblob(32)), length(randomblob(-5)),
474 length(randomblob(2000))
478 # The "hex()" function was added in order to be able to render blobs
479 # generated by randomblob(). So this seems like a good place to test
484 execsql {SELECT hex(x'00112233445566778899aAbBcCdDeEfF')}
485 } {00112233445566778899AABBCCDDEEFF}
487 set encoding [db one {PRAGMA encoding}]
488 if {$encoding=="UTF-16le"} {
489 do_test func-9.11-utf16le {
490 execsql {SELECT hex(replace('abcdefg','ef','12'))}
491 } {6100620063006400310032006700}
492 do_test func-9.12-utf16le {
493 execsql {SELECT hex(replace('abcdefg','','12'))}
494 } {6100620063006400650066006700}
495 do_test func-9.13-utf16le {
496 execsql {SELECT hex(replace('aabcdefg','a','aaa'))}
497 } {610061006100610061006100620063006400650066006700}
498 } elseif {$encoding=="UTF-8"} {
499 do_test func-9.11-utf8 {
500 execsql {SELECT hex(replace('abcdefg','ef','12'))}
502 do_test func-9.12-utf8 {
503 execsql {SELECT hex(replace('abcdefg','','12'))}
505 do_test func-9.13-utf8 {
506 execsql {SELECT hex(replace('aabcdefg','a','aaa'))}
507 } {616161616161626364656667}
510 # Use the "sqlite_register_test_function" TCL command which is part of
511 # the text fixture in order to verify correct operation of some of
512 # the user-defined SQL function APIs that are not used by the built-in
515 set ::DB [sqlite3_connection_pointer db]
516 sqlite_register_test_function $::DB testfunc
519 SELECT testfunc(NULL,NULL);
521 } {1 {first argument should be one of: int int64 string double null value}}
525 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
533 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
539 ifcapable floatingpoint {
543 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
551 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
553 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
555 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
557 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
559 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
561 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
568 # Test the built-in sqlite_version(*) SQL function.
572 SELECT sqlite_version(*);
576 # Test that destructors passed to sqlite3 by calls to sqlite3_result_text()
577 # etc. are called. These tests use two special user-defined functions
578 # (implemented in func.c) only available in test builds.
580 # Function test_destructor() takes one argument and returns a copy of the
581 # text form of that argument. A destructor is associated with the return
582 # value. Function test_destructor_count() returns the number of outstanding
583 # destructor calls for values returned by test_destructor().
585 if {[db eval {PRAGMA encoding}]=="UTF-8"} {
586 do_test func-12.1-utf8 {
588 SELECT test_destructor('hello world'), test_destructor_count();
593 do_test func-12.1-utf16 {
595 SELECT test_destructor16('hello world'), test_destructor_count();
602 SELECT test_destructor_count();
607 SELECT test_destructor('hello')||' world'
612 SELECT test_destructor_count();
618 INSERT INTO t4 VALUES(test_destructor('hello'));
619 INSERT INTO t4 VALUES(test_destructor('world'));
620 SELECT min(test_destructor(x)), max(test_destructor(x)) FROM t4;
625 SELECT test_destructor_count();
635 # Test that the auxdata API for scalar functions works. This test uses
636 # a special user-defined function only available in test builds,
637 # test_auxdata(). Function test_auxdata() takes any number of arguments.
640 SELECT test_auxdata('hello world');
646 CREATE TABLE t4(a, b);
647 INSERT INTO t4 VALUES('abc', 'def');
648 INSERT INTO t4 VALUES('ghi', 'jkl');
653 SELECT test_auxdata('hello world') FROM t4;
658 SELECT test_auxdata('hello world', 123) FROM t4;
663 SELECT test_auxdata('hello world', a) FROM t4;
668 SELECT test_auxdata('hello'||'world', a) FROM t4;
672 # Test that auxilary data is preserved between calls for SQL variables.
674 set DB [sqlite3_connection_pointer db]
675 set sql "SELECT test_auxdata( ? , a ) FROM t4;"
676 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
677 sqlite3_bind_text $STMT 1 hello\000 -1
679 while { "SQLITE_ROW"==[sqlite3_step $STMT] } {
680 lappend res [sqlite3_column_text $STMT 0]
682 lappend res [sqlite3_finalize $STMT]
683 } {{0 0} {1 0} SQLITE_OK}
685 # Make sure that a function with a very long name is rejected
688 db function [string repeat X 254] {return "hello"}
693 db function [string repeat X 256] {return "hello"}
698 catchsql {select test_error(NULL)}
701 catchsql {select test_error('this is the error message')}
702 } {1 {this is the error message}}
704 catchsql {select test_error('this is the error message',12)}
705 } {1 {this is the error message}}
710 # Test the quote function for BLOB and NULL values.
713 CREATE TABLE tbl2(a, b);
715 set STMT [sqlite3_prepare $::DB "INSERT INTO tbl2 VALUES(?, ?)" -1 TAIL]
716 sqlite3_bind_blob $::STMT 1 abc 3
718 sqlite3_finalize $::STMT
720 SELECT quote(a), quote(b) FROM tbl2;
724 # Correctly handle function error messages that include %. Ticket #1354
727 proc testfunc1 args {error "Error %d with %s percents %p"}
728 db function testfunc1 ::testfunc1
730 SELECT testfunc1(1,2,3);
732 } {1 {Error %d with %s percents %p}}
734 # The SUM function should return integer results when all inputs are integer.
739 INSERT INTO t5 VALUES(1);
740 INSERT INTO t5 VALUES(-99);
741 INSERT INTO t5 VALUES(10000);
742 SELECT sum(x) FROM t5;
745 ifcapable floatingpoint {
748 INSERT INTO t5 VALUES(0.0);
749 SELECT sum(x) FROM t5;
754 # The sum of nothing is NULL. But the sum of all NULLs is NULL.
756 # The TOTAL of nothing is 0.0.
761 SELECT sum(x), total(x) FROM t5;
766 INSERT INTO t5 VALUES(NULL);
767 SELECT sum(x), total(x) FROM t5
772 INSERT INTO t5 VALUES(NULL);
773 SELECT sum(x), total(x) FROM t5
778 INSERT INTO t5 VALUES(123);
779 SELECT sum(x), total(x) FROM t5
783 # Ticket #1664, #1669, #1670, #1674: An integer overflow on SUM causes
784 # an error. The non-standard TOTAL() function continues to give a helpful
789 CREATE TABLE t6(x INTEGER);
790 INSERT INTO t6 VALUES(1);
791 INSERT INTO t6 VALUES(1<<62);
792 SELECT sum(x) - ((1<<62)+1) from t6;
797 SELECT typeof(sum(x)) FROM t6
800 ifcapable floatingpoint {
803 INSERT INTO t6 VALUES(1<<62);
804 SELECT sum(x) - ((1<<62)*2.0+1) from t6;
806 } {1 {integer overflow}}
809 SELECT total(x) - ((1<<62)*2.0+1) FROM t6
813 ifcapable !floatingpoint {
816 INSERT INTO t6 VALUES(1<<62);
817 SELECT sum(x) - ((1<<62)*2+1) from t6;
819 } {1 {integer overflow}}
822 SELECT total(x) - ((1<<62)*2+1) FROM t6
826 if {[working_64bit_int]} {
829 SELECT sum(-9223372036854775805);
831 } -9223372036854775805
833 ifcapable compound&&subquery {
838 (SELECT 9223372036854775807 AS x UNION ALL
841 } {1 {integer overflow}}
842 if {[working_64bit_int]} {
846 (SELECT 9223372036854775807 AS x UNION ALL
849 } {0 9223372036854775797}
853 (SELECT -9223372036854775807 AS x UNION ALL
856 } {0 -9223372036854775797}
861 (SELECT -9223372036854775807 AS x UNION ALL
864 } {1 {integer overflow}}
867 SELECT sum(x) FROM (SELECT 9 AS x UNION ALL SELECT -10 AS x);
872 SELECT sum(x) FROM (SELECT -9 AS x UNION ALL SELECT 10 AS x);
877 SELECT sum(x) FROM (SELECT -10 AS x UNION ALL SELECT 9 AS x);
882 SELECT sum(x) FROM (SELECT 10 AS x UNION ALL SELECT -9 AS x);
886 } ;# ifcapable compound&&subquery
888 # Integer overflow on abs()
890 if {[working_64bit_int]} {
893 SELECT abs(-9223372036854775807);
895 } {0 9223372036854775807}
899 SELECT abs(-9223372036854775807-1);
901 } {1 {integer overflow}}
903 # The MATCH function exists but is only a stub and always throws an error.
907 SELECT match(a,b) FROM t1 WHERE 0;
912 SELECT 'abc' MATCH 'xyz';
914 } {1 {unable to use function MATCH in the requested context}}
917 SELECT 'abc' NOT MATCH 'xyz';
919 } {1 {unable to use function MATCH in the requested context}}
924 } {1 {wrong number of arguments to function match()}}
928 if {![catch {db eval {SELECT soundex('hello')}}]} {
950 execsql {SELECT soundex($name)}
955 # Tests of the REPLACE function.
961 } {1 {wrong number of arguments to function replace()}}
964 SELECT replace(1,2,3,4);
966 } {1 {wrong number of arguments to function replace()}}
969 SELECT typeof(replace("This is the main test string", NULL, "ALT"));
974 SELECT typeof(replace(NULL, "main", "ALT"));
979 SELECT typeof(replace("This is the main test string", "main", NULL));
984 SELECT replace("This is the main test string", "main", "ALT");
986 } {{This is the ALT test string}}
989 SELECT replace("This is the main test string", "main", "larger-main");
991 } {{This is the larger-main test string}}
994 SELECT replace("aaaaaaa", "a", "0123456789");
996 } {0123456789012345678901234567890123456789012345678901234567890123456789}
1000 # Attempt to exploit a buffer-overflow that at one time existed
1001 # in the REPLACE function.
1002 set ::str "[string repeat A 29998]CC[string repeat A 35537]"
1003 set ::rep [string repeat B 65536]
1005 SELECT LENGTH(REPLACE($::str, 'C', $::rep));
1007 } [expr 29998 + 2*65536 + 35537]
1010 # Tests for the TRIM, LTRIM and RTRIM functions.
1013 catchsql {SELECT trim(1,2,3)}
1014 } {1 {wrong number of arguments to function trim()}}
1016 catchsql {SELECT ltrim(1,2,3)}
1017 } {1 {wrong number of arguments to function ltrim()}}
1019 catchsql {SELECT rtrim(1,2,3)}
1020 } {1 {wrong number of arguments to function rtrim()}}
1022 execsql {SELECT trim(' hi ');}
1025 execsql {SELECT ltrim(' hi ');}
1028 execsql {SELECT rtrim(' hi ');}
1031 execsql {SELECT trim(' hi ','xyz');}
1034 execsql {SELECT ltrim(' hi ','xyz');}
1037 execsql {SELECT rtrim(' hi ','xyz');}
1039 do_test func-22.10 {
1040 execsql {SELECT trim('xyxzy hi zzzy','xyz');}
1042 do_test func-22.11 {
1043 execsql {SELECT ltrim('xyxzy hi zzzy','xyz');}
1045 do_test func-22.12 {
1046 execsql {SELECT rtrim('xyxzy hi zzzy','xyz');}
1048 do_test func-22.13 {
1049 execsql {SELECT trim(' hi ','');}
1051 if {[db one {PRAGMA encoding}]=="UTF-8"} {
1052 do_test func-22.14 {
1053 execsql {SELECT hex(trim(x'c280e1bfbff48fbfbf6869',x'6162e1bfbfc280'))}
1055 do_test func-22.15 {
1056 execsql {SELECT hex(trim(x'6869c280e1bfbff48fbfbf61',
1057 x'6162e1bfbfc280f48fbfbf'))}
1059 do_test func-22.16 {
1060 execsql {SELECT hex(trim(x'ceb1ceb2ceb3',x'ceb1'));}
1063 do_test func-22.20 {
1064 execsql {SELECT typeof(trim(NULL));}
1066 do_test func-22.21 {
1067 execsql {SELECT typeof(trim(NULL,'xyz'));}
1069 do_test func-22.22 {
1070 execsql {SELECT typeof(trim('hello',NULL));}
1073 # This is to test the deprecated sqlite3_aggregate_count() API.
1075 ifcapable deprecated {
1077 sqlite3_create_aggregate db
1079 SELECT legacy_count() FROM t6;
1084 # The group_concat() function.
1088 SELECT group_concat(t1) FROM tbl1
1090 } {this,program,is,free,software}
1093 SELECT group_concat(t1,' ') FROM tbl1
1095 } {{this program is free software}}
1098 SELECT group_concat(t1,' ' || rowid || ' ') FROM tbl1
1100 } {{this 2 program 3 is 4 free 5 software}}
1103 SELECT group_concat(NULL,t1) FROM tbl1
1108 SELECT group_concat(t1,NULL) FROM tbl1
1110 } {thisprogramisfreesoftware}
1113 SELECT 'BEGIN-'||group_concat(t1) FROM tbl1
1115 } {BEGIN-this,program,is,free,software}
1117 # Ticket #3179: Make sure aggregate functions can take many arguments.
1118 # None of the built-in aggregates do this, so use the md5sum() from the
1121 unset -nocomplain midargs
1123 unset -nocomplain midres
1125 unset -nocomplain result
1126 for {set i 1} {$i<[sqlite3_limit db SQLITE_LIMIT_FUNCTION_ARG -1]} {incr i} {
1127 append midargs ,'/$i'
1130 "this${midres}program${midres}is${midres}free${midres}software${midres}"]
1131 set sql "SELECT md5sum(t1$midargs) FROM tbl1"
1132 do_test func-24.7.$i {
1137 # Ticket #3806. If the initial string in a group_concat is an empty
1138 # string, the separator that follows should still be present.
1142 SELECT group_concat(CASE t1 WHEN 'this' THEN '' ELSE t1 END) FROM tbl1
1144 } {,program,is,free,software}
1147 SELECT group_concat(CASE WHEN t1!='software' THEN '' ELSE t1 END) FROM tbl1
1151 # Ticket #3923. Initial empty strings have a separator. But initial
1154 do_test func-24.10 {
1156 SELECT group_concat(CASE t1 WHEN 'this' THEN null ELSE t1 END) FROM tbl1
1158 } {program,is,free,software}
1159 do_test func-24.11 {
1161 SELECT group_concat(CASE WHEN t1!='software' THEN null ELSE t1 END) FROM tbl1
1164 do_test func-24.12 {
1166 SELECT group_concat(CASE t1 WHEN 'this' THEN ''
1167 WHEN 'program' THEN null ELSE t1 END) FROM tbl1
1169 } {,is,free,software}
1172 # Use the test_isolation function to make sure that type conversions
1173 # on function arguments do not effect subsequent arguments.
1176 execsql {SELECT test_isolation(t1,t1) FROM tbl1}
1177 } {this program is free software}
1179 # Try to misuse the sqlite3_create_function() interface. Verify that
1180 # errors are returned.
1183 abuse_create_function db
1186 # The previous test (func-26.1) registered a function with a very long
1187 # function name that takes many arguments and always returns NULL. Verify
1188 # that this function works correctly.
1192 for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG} {incr i} {
1196 SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789([join $a ,]);
1201 for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG+1} {incr i} {
1205 SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789([join $a ,]);
1207 } {1 {too many arguments on function nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789}}
1210 for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG-1} {incr i} {
1214 SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789([join $a ,]);
1216 } {1 {wrong number of arguments to function nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789()}}
1219 SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678a(0);
1221 } {1 {no such function: nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678a}}
1224 SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789a(0);
1226 } {1 {no such function: nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789a}}
1229 catchsql {SELECT coalesce()}
1230 } {1 {wrong number of arguments to function coalesce()}}
1232 catchsql {SELECT coalesce(1)}
1233 } {1 {wrong number of arguments to function coalesce()}}
1235 catchsql {SELECT coalesce(1,2)}
1238 # Ticket 2d401a94287b5
1239 # Unknown function in a DEFAULT expression causes a segfault.
1243 CREATE TABLE t28(x, y DEFAULT(nosuchfunc(1)));
1246 INSERT INTO t28(x) VALUES(1);
1248 } {1 {unknown function: nosuchfunc()}}