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
19 # Create a table to work with.
22 execsql {CREATE TABLE tbl1(t1 text)}
23 foreach word {this program is free software} {
24 execsql "INSERT INTO tbl1 VALUES('$word')"
26 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
27 } {free is program software this}
31 INSERT INTO t2 VALUES(1);
32 INSERT INTO t2 VALUES(NULL);
33 INSERT INTO t2 VALUES(345);
34 INSERT INTO t2 VALUES(NULL);
35 INSERT INTO t2 VALUES(67890);
40 # Check out the length() function
43 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
45 set isutf16 [regexp 16 [db one {PRAGMA encoding}]]
46 do_execsql_test func-1.0b {
47 SELECT octet_length(t1) FROM tbl1 ORDER BY t1;
48 } [expr {$isutf16?"8 4 14 16 8":"4 2 7 8 4"}]
50 set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
52 } {1 {wrong number of arguments to function length()}}
54 set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
56 } {1 {wrong number of arguments to function length()}}
58 execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
62 execsql {SELECT coalesce(length(a),-1) FROM t2}
64 do_execsql_test func-1.5 {
65 SELECT octet_length(12345);
66 } [expr {(1+($isutf16!=0))*5}]
68 do_execsql_test func-1.6 {
69 SELECT octet_length(NULL);
71 do_execsql_test func-1.7 {
72 SELECT octet_length(7.5);
73 } [expr {(1+($isutf16!=0))*3}]
74 do_execsql_test func-1.8 {
75 SELECT octet_length(x'30313233');
77 do_execsql_test func-1.9 {
78 WITH c(x) AS (VALUES(char(350,351,352,353,354)))
79 SELECT length(x), octet_length(x) FROM c;
84 # Check out the substr() function
88 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
91 execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
94 execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
97 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
100 execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
103 execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
106 execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
109 execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
112 execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
113 } {this software free program is}
115 execsql {SELECT substr(a,1,1) FROM t2}
118 execsql {SELECT substr(a,2,2) FROM t2}
121 # Only do the following tests if TCL has UTF-8 capabilities
123 if {"\u1234"!="u1234"} {
125 # Put some UTF-8 characters in the database
128 execsql {DELETE FROM tbl1}
129 foreach word "contains UTF-8 characters hi\u1234ho" {
130 execsql "INSERT INTO tbl1 VALUES('$word')"
132 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
133 } "UTF-8 characters contains hi\u1234ho"
135 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
138 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
141 execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
142 } "UTF cha con hi\u1234"
144 execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
147 execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
148 } "TF- har ont i\u1234h"
150 execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
153 execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
156 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
159 execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
162 execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
163 } "TF- ter ain i\u1234h"
165 execsql {DELETE FROM tbl1}
166 foreach word {this program is free software} {
167 execsql "INSERT INTO tbl1 VALUES('$word')"
169 execsql {SELECT t1 FROM tbl1}
170 } {this program is free software}
172 } ;# End \u1234!=u1234
174 # Test the abs() and round() functions.
176 ifcapable !floatingpoint {
179 CREATE TABLE t1(a,b,c);
180 INSERT INTO t1 VALUES(1,2,3);
181 INSERT INTO t1 VALUES(2,12345678901234,-1234567890);
182 INSERT INTO t1 VALUES(3,-2,-5);
184 catchsql {SELECT abs(a,b) FROM t1}
185 } {1 {wrong number of arguments to function abs()}}
187 ifcapable floatingpoint {
190 CREATE TABLE t1(a,b,c);
191 INSERT INTO t1 VALUES(1,2,3);
192 INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890);
193 INSERT INTO t1 VALUES(3,-2,-5);
195 catchsql {SELECT abs(a,b) FROM t1}
196 } {1 {wrong number of arguments to function abs()}}
199 catchsql {SELECT abs() FROM t1}
200 } {1 {wrong number of arguments to function abs()}}
201 ifcapable floatingpoint {
203 catchsql {SELECT abs(b) FROM t1 ORDER BY a}
204 } {0 {2 1.2345678901234 2}}
206 catchsql {SELECT abs(c) FROM t1 ORDER BY a}
207 } {0 {3 12345.6789 5}}
209 ifcapable !floatingpoint {
210 if {[working_64bit_int]} {
212 catchsql {SELECT abs(b) FROM t1 ORDER BY a}
213 } {0 {2 12345678901234 2}}
216 catchsql {SELECT abs(c) FROM t1 ORDER BY a}
217 } {0 {3 1234567890 5}}
220 execsql {SELECT abs(a) FROM t2}
221 } {1 {} 345 {} 67890}
223 execsql {SELECT abs(t1) FROM tbl1}
224 } {0.0 0.0 0.0 0.0 0.0}
226 ifcapable floatingpoint {
228 catchsql {SELECT round(a,b,c) FROM t1}
229 } {1 {wrong number of arguments to function round()}}
231 catchsql {SELECT round(b,2) FROM t1 ORDER BY b}
232 } {0 {-2.0 1.23 2.0}}
234 catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
237 catchsql {SELECT round(c) FROM t1 ORDER BY a}
238 } {0 {3.0 -12346.0 -5.0}}
240 catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
241 } {0 {3.0 -12345.68 -5.0}}
243 catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
244 } {0 {x3.0y x-12345.68y x-5.0y}}
246 catchsql {SELECT round() FROM t1 ORDER BY a}
247 } {1 {wrong number of arguments to function round()}}
249 execsql {SELECT coalesce(round(a,2),'nil') FROM t2}
250 } {1.0 nil 345.0 nil 67890.0}
252 execsql {SELECT round(t1,2) FROM tbl1}
253 } {0.0 0.0 0.0 0.0 0.0}
255 execsql {SELECT typeof(round(5.1,1));}
258 execsql {SELECT typeof(round(5.1));}
261 catchsql {SELECT round(b,2.0) FROM t1 ORDER BY b}
262 } {0 {-2.0 1.23 2.0}}
263 # Verify some values reported on the mailing list.
264 # Some of these fail on MSVC builds with 64-bit
265 # long doubles, but not on GCC builds with 80-bit
267 for {set i 1} {$i<999} {incr i} {
268 set x1 [expr 40222.5 + $i]
269 set x2 [expr 40223.0 + $i]
270 do_test func-4.17.$i {
271 execsql {SELECT round($x1);}
274 for {set i 1} {$i<999} {incr i} {
275 set x1 [expr 40222.05 + $i]
276 set x2 [expr 40222.10 + $i]
277 do_test func-4.18.$i {
278 execsql {SELECT round($x1,1);}
282 execsql {SELECT round(40223.4999999999);}
285 execsql {SELECT round(40224.4999999999);}
288 execsql {SELECT round(40225.4999999999);}
290 for {set i 1} {$i<10} {incr i} {
291 do_test func-4.23.$i {
292 execsql {SELECT round(40223.4999999999,$i);}
294 do_test func-4.24.$i {
295 execsql {SELECT round(40224.4999999999,$i);}
297 do_test func-4.25.$i {
298 execsql {SELECT round(40225.4999999999,$i);}
301 for {set i 10} {$i<32} {incr i} {
302 do_test func-4.26.$i {
303 execsql {SELECT round(40223.4999999999,$i);}
305 do_test func-4.27.$i {
306 execsql {SELECT round(40224.4999999999,$i);}
308 do_test func-4.28.$i {
309 execsql {SELECT round(40225.4999999999,$i);}
313 execsql {SELECT round(1234567890.5);}
316 execsql {SELECT round(12345678901.5);}
319 execsql {SELECT round(123456789012.5);}
322 execsql {SELECT round(1234567890123.5);}
325 execsql {SELECT round(12345678901234.5);}
328 execsql {SELECT round(1234567890123.35,1);}
331 execsql {SELECT round(1234567890123.445,2);}
334 execsql {SELECT round(99999999999994.5);}
337 execsql {SELECT round(9999999999999.55,1);}
340 execsql {SELECT round(9999999999999.556,2);}
343 string tolower [db eval {SELECT round(1e500), round(-1e500);}]
347 # Test the upper() and lower() functions
350 execsql {SELECT upper(t1) FROM tbl1}
351 } {THIS PROGRAM IS FREE SOFTWARE}
353 execsql {SELECT lower(upper(t1)) FROM tbl1}
354 } {this program is free software}
356 execsql {SELECT upper(a), lower(a) FROM t2}
357 } {1 1 {} {} 345 345 {} {} 67890 67890}
360 catchsql {SELECT upper(a,5) FROM t2}
361 } {1 {wrong number of arguments to function upper()}}
364 catchsql {SELECT upper(*) FROM t2}
365 } {1 {wrong number of arguments to function upper()}}
367 # Test the coalesce() and nullif() functions
370 execsql {SELECT coalesce(a,'xyz') FROM t2}
371 } {1 xyz 345 xyz 67890}
373 execsql {SELECT coalesce(upper(a),'nil') FROM t2}
374 } {1 nil 345 nil 67890}
376 execsql {SELECT coalesce(nullif(1,1),'nil')}
379 execsql {SELECT coalesce(nullif(1,2),'nil')}
382 execsql {SELECT coalesce(nullif(1,NULL),'nil')}
386 # Test the last_insert_rowid() function
389 execsql {SELECT last_insert_rowid()}
390 } [db last_insert_rowid]
392 # Tests for aggregate functions and how they handle NULLs.
394 ifcapable floatingpoint {
397 execsql {EXPLAIN SELECT sum(a) FROM t2;}
400 SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
402 } {68236 3 22745.33 1 67890 5}
404 ifcapable !floatingpoint {
407 execsql {EXPLAIN SELECT sum(a) FROM t2;}
410 SELECT sum(a), count(a), avg(a), min(a), max(a), count(*) FROM t2;
412 } {68236 3 22745.0 1 67890 5}
416 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2;
418 } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
423 CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
424 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
426 } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
430 CREATE TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
431 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
433 } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
437 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
439 } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
443 SELECT sum(x) FROM (SELECT '9223372036' || '854775807' AS x
444 UNION ALL SELECT -9223372036854775807)
449 SELECT typeof(sum(x)) FROM (SELECT '9223372036' || '854775807' AS x
450 UNION ALL SELECT -9223372036854775807)
455 SELECT typeof(sum(x)) FROM (SELECT '9223372036' || '854775808' AS x
456 UNION ALL SELECT -9223372036854775807)
459 ifcapable floatingpoint {
462 SELECT sum(x)>0.0 FROM (SELECT '9223372036' || '854775808' AS x
463 UNION ALL SELECT -9223372036850000000)
467 ifcapable !floatingpoint {
470 SELECT sum(x)>0 FROM (SELECT '9223372036' || '854775808' AS x
471 UNION ALL SELECT -9223372036850000000)
477 # How do you test the random() function in a meaningful, deterministic way?
481 SELECT random() is not null;
486 SELECT typeof(random());
491 SELECT randomblob(32) is not null;
496 SELECT typeof(randomblob(32));
501 SELECT length(randomblob(32)), length(randomblob(-5)),
502 length(randomblob(2000))
506 # The "hex()" function was added in order to be able to render blobs
507 # generated by randomblob(). So this seems like a good place to test
512 execsql {SELECT hex(x'00112233445566778899aAbBcCdDeEfF')}
513 } {00112233445566778899AABBCCDDEEFF}
515 set encoding [db one {PRAGMA encoding}]
516 if {$encoding=="UTF-16le"} {
517 do_test func-9.11-utf16le {
518 execsql {SELECT hex(replace('abcdefg','ef','12'))}
519 } {6100620063006400310032006700}
520 do_test func-9.12-utf16le {
521 execsql {SELECT hex(replace('abcdefg','','12'))}
522 } {6100620063006400650066006700}
523 do_test func-9.13-utf16le {
524 execsql {SELECT hex(replace('aabcdefg','a','aaa'))}
525 } {610061006100610061006100620063006400650066006700}
526 } elseif {$encoding=="UTF-8"} {
527 do_test func-9.11-utf8 {
528 execsql {SELECT hex(replace('abcdefg','ef','12'))}
530 do_test func-9.12-utf8 {
531 execsql {SELECT hex(replace('abcdefg','','12'))}
533 do_test func-9.13-utf8 {
534 execsql {SELECT hex(replace('aabcdefg','a','aaa'))}
535 } {616161616161626364656667}
537 do_execsql_test func-9.14 {
538 WITH RECURSIVE c(x) AS (
541 SELECT x+1 FROM c WHERE x<1040
545 sum(length(replace(printf('abc%.*cxyz',x,'m'),'m','nnnn'))-(6+x*4))
549 # Use the "sqlite_register_test_function" TCL command which is part of
550 # the text fixture in order to verify correct operation of some of
551 # the user-defined SQL function APIs that are not used by the built-in
554 set ::DB [sqlite3_connection_pointer db]
555 sqlite_register_test_function $::DB testfunc
558 SELECT testfunc(NULL,NULL);
560 } {1 {first argument should be one of: int int64 string double null value}}
564 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
572 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
578 ifcapable floatingpoint {
582 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
590 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
592 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
594 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
596 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
598 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
600 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
607 # Test the built-in sqlite_version(*) SQL function.
611 SELECT sqlite_version(*);
615 # Test that destructors passed to sqlite3 by calls to sqlite3_result_text()
616 # etc. are called. These tests use two special user-defined functions
617 # (implemented in func.c) only available in test builds.
619 # Function test_destructor() takes one argument and returns a copy of the
620 # text form of that argument. A destructor is associated with the return
621 # value. Function test_destructor_count() returns the number of outstanding
622 # destructor calls for values returned by test_destructor().
624 if {[db eval {PRAGMA encoding}]=="UTF-8"} {
625 do_test func-12.1-utf8 {
627 SELECT test_destructor('hello world'), test_destructor_count();
632 do_test func-12.1-utf16 {
634 SELECT test_destructor16('hello world'), test_destructor_count();
641 SELECT test_destructor_count();
646 SELECT test_destructor('hello')||' world'
651 SELECT test_destructor_count();
657 INSERT INTO t4 VALUES(test_destructor('hello'));
658 INSERT INTO t4 VALUES(test_destructor('world'));
659 SELECT min(test_destructor(x)), max(test_destructor(x)) FROM t4;
664 SELECT test_destructor_count();
674 # Test that the auxdata API for scalar functions works. This test uses
675 # a special user-defined function only available in test builds,
676 # test_auxdata(). Function test_auxdata() takes any number of arguments.
679 SELECT test_auxdata('hello world');
685 CREATE TABLE t4(a, b);
686 INSERT INTO t4 VALUES('abc', 'def');
687 INSERT INTO t4 VALUES('ghi', 'jkl');
692 SELECT test_auxdata('hello world') FROM t4;
697 SELECT test_auxdata('hello world', 123) FROM t4;
702 SELECT test_auxdata('hello world', a) FROM t4;
707 SELECT test_auxdata('hello'||'world', a) FROM t4;
711 # Test that auxilary data is preserved between calls for SQL variables.
713 set DB [sqlite3_connection_pointer db]
714 set sql "SELECT test_auxdata( ? , a ) FROM t4;"
715 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
716 sqlite3_bind_text $STMT 1 hello\000 -1
718 while { "SQLITE_ROW"==[sqlite3_step $STMT] } {
719 lappend res [sqlite3_column_text $STMT 0]
721 lappend res [sqlite3_finalize $STMT]
722 } {{0 0} {1 0} SQLITE_OK}
724 # Test that auxiliary data is discarded when a statement is reset.
725 do_execsql_test 13.8.1 {
726 SELECT test_auxdata('constant') FROM t4;
728 do_execsql_test 13.8.2 {
729 SELECT test_auxdata('constant') FROM t4;
732 do_execsql_test 13.8.3 {
733 SELECT test_auxdata('constant') FROM t4;
736 do_execsql_test 13.8.4 {
737 SELECT test_auxdata($V), $V FROM t4;
740 do_execsql_test 13.8.5 {
741 SELECT test_auxdata($V), $V FROM t4;
745 do_execsql_test 13.8.6 {
746 SELECT test_auxdata($V), $V FROM t4;
750 # Make sure that a function with a very long name is rejected
753 db function [string repeat X 254] {return "hello"}
758 db function [string repeat X 256] {return "hello"}
763 catchsql {select test_error(NULL)}
766 catchsql {select test_error('this is the error message')}
767 } {1 {this is the error message}}
769 catchsql {select test_error('this is the error message',12)}
770 } {1 {this is the error message}}
775 # Test the quote function for BLOB and NULL values.
778 CREATE TABLE tbl2(a, b);
780 set STMT [sqlite3_prepare $::DB "INSERT INTO tbl2 VALUES(?, ?)" -1 TAIL]
781 sqlite3_bind_blob $::STMT 1 abc 3
783 sqlite3_finalize $::STMT
785 SELECT quote(a), quote(b) FROM tbl2;
789 # Test the quote function for +Inf and -Inf
790 do_execsql_test func-16.2 {
791 SELECT quote(4.2e+859), quote(-7.8e+904);
792 } {9.0e+999 -9.0e+999}
794 # Correctly handle function error messages that include %. Ticket #1354
797 proc testfunc1 args {error "Error %d with %s percents %p"}
798 db function testfunc1 ::testfunc1
800 SELECT testfunc1(1,2,3);
802 } {1 {Error %d with %s percents %p}}
804 # The SUM function should return integer results when all inputs are integer.
809 INSERT INTO t5 VALUES(1);
810 INSERT INTO t5 VALUES(-99);
811 INSERT INTO t5 VALUES(10000);
812 SELECT sum(x) FROM t5;
815 ifcapable floatingpoint {
818 INSERT INTO t5 VALUES(0.0);
819 SELECT sum(x) FROM t5;
824 # The sum of nothing is NULL. But the sum of all NULLs is NULL.
826 # The TOTAL of nothing is 0.0.
831 SELECT sum(x), total(x) FROM t5;
836 INSERT INTO t5 VALUES(NULL);
837 SELECT sum(x), total(x) FROM t5
842 INSERT INTO t5 VALUES(NULL);
843 SELECT sum(x), total(x) FROM t5
848 INSERT INTO t5 VALUES(123);
849 SELECT sum(x), total(x) FROM t5
853 # Ticket #1664, #1669, #1670, #1674: An integer overflow on SUM causes
854 # an error. The non-standard TOTAL() function continues to give a helpful
859 CREATE TABLE t6(x INTEGER);
860 INSERT INTO t6 VALUES(1);
861 INSERT INTO t6 VALUES(1<<62);
862 SELECT sum(x) - ((1<<62)+1) from t6;
867 SELECT typeof(sum(x)) FROM t6
870 ifcapable floatingpoint {
871 do_catchsql_test func-18.12 {
872 INSERT INTO t6 VALUES(1<<62);
873 SELECT sum(x) - ((1<<62)*2.0+1) from t6;
874 } {1 {integer overflow}}
875 do_catchsql_test func-18.13 {
876 SELECT total(x) - ((1<<62)*2.0+1) FROM t6
879 if {[working_64bit_int]} {
882 SELECT sum(-9223372036854775805);
884 } -9223372036854775805
886 ifcapable compound&&subquery {
891 (SELECT 9223372036854775807 AS x UNION ALL
894 } {1 {integer overflow}}
895 if {[working_64bit_int]} {
899 (SELECT 9223372036854775807 AS x UNION ALL
902 } {0 9223372036854775797}
906 (SELECT -9223372036854775807 AS x UNION ALL
909 } {0 -9223372036854775797}
914 (SELECT -9223372036854775807 AS x UNION ALL
917 } {1 {integer overflow}}
920 SELECT sum(x) FROM (SELECT 9 AS x UNION ALL SELECT -10 AS x);
925 SELECT sum(x) FROM (SELECT -9 AS x UNION ALL SELECT 10 AS x);
930 SELECT sum(x) FROM (SELECT -10 AS x UNION ALL SELECT 9 AS x);
935 SELECT sum(x) FROM (SELECT 10 AS x UNION ALL SELECT -9 AS x);
939 } ;# ifcapable compound&&subquery
941 # Integer overflow on abs()
943 if {[working_64bit_int]} {
946 SELECT abs(-9223372036854775807);
948 } {0 9223372036854775807}
952 SELECT abs(-9223372036854775807-1);
954 } {1 {integer overflow}}
956 # The MATCH function exists but is only a stub and always throws an error.
960 SELECT match(a,b) FROM t1 WHERE 0;
965 SELECT 'abc' MATCH 'xyz';
967 } {1 {unable to use function MATCH in the requested context}}
970 SELECT 'abc' NOT MATCH 'xyz';
972 } {1 {unable to use function MATCH in the requested context}}
977 } {1 {wrong number of arguments to function match()}}
981 if {![catch {db eval {SELECT soundex('hello')}}]} {
1002 do_test func-20.$i {
1003 execsql {SELECT soundex($name)}
1008 # Tests of the REPLACE function.
1012 SELECT replace(1,2);
1014 } {1 {wrong number of arguments to function replace()}}
1017 SELECT replace(1,2,3,4);
1019 } {1 {wrong number of arguments to function replace()}}
1022 SELECT typeof(replace('This is the main test string', NULL, 'ALT'));
1027 SELECT typeof(replace(NULL, 'main', 'ALT'));
1032 SELECT typeof(replace('This is the main test string', 'main', NULL));
1037 SELECT replace('This is the main test string', 'main', 'ALT');
1039 } {{This is the ALT test string}}
1042 SELECT replace('This is the main test string', 'main', 'larger-main');
1044 } {{This is the larger-main test string}}
1047 SELECT replace('aaaaaaa', 'a', '0123456789');
1049 } {0123456789012345678901234567890123456789012345678901234567890123456789}
1050 do_execsql_test func-21.9 {
1051 SELECT typeof(replace(1,'',0));
1056 # Attempt to exploit a buffer-overflow that at one time existed
1057 # in the REPLACE function.
1058 set ::str "[string repeat A 29998]CC[string repeat A 35537]"
1059 set ::rep [string repeat B 65536]
1061 SELECT LENGTH(REPLACE($::str, 'C', $::rep));
1063 } [expr 29998 + 2*65536 + 35537]
1066 # Tests for the TRIM, LTRIM and RTRIM functions.
1069 catchsql {SELECT trim(1,2,3)}
1070 } {1 {wrong number of arguments to function trim()}}
1072 catchsql {SELECT ltrim(1,2,3)}
1073 } {1 {wrong number of arguments to function ltrim()}}
1075 catchsql {SELECT rtrim(1,2,3)}
1076 } {1 {wrong number of arguments to function rtrim()}}
1078 execsql {SELECT trim(' hi ');}
1081 execsql {SELECT ltrim(' hi ');}
1084 execsql {SELECT rtrim(' hi ');}
1087 execsql {SELECT trim(' hi ','xyz');}
1090 execsql {SELECT ltrim(' hi ','xyz');}
1093 execsql {SELECT rtrim(' hi ','xyz');}
1095 do_test func-22.10 {
1096 execsql {SELECT trim('xyxzy hi zzzy','xyz');}
1098 do_test func-22.11 {
1099 execsql {SELECT ltrim('xyxzy hi zzzy','xyz');}
1101 do_test func-22.12 {
1102 execsql {SELECT rtrim('xyxzy hi zzzy','xyz');}
1104 do_test func-22.13 {
1105 execsql {SELECT trim(' hi ','');}
1107 if {[db one {PRAGMA encoding}]=="UTF-8"} {
1108 do_test func-22.14 {
1109 execsql {SELECT hex(trim(x'c280e1bfbff48fbfbf6869',x'6162e1bfbfc280'))}
1111 do_test func-22.15 {
1112 execsql {SELECT hex(trim(x'6869c280e1bfbff48fbfbf61',
1113 x'6162e1bfbfc280f48fbfbf'))}
1115 do_test func-22.16 {
1116 execsql {SELECT hex(trim(x'ceb1ceb2ceb3',x'ceb1'));}
1119 do_test func-22.20 {
1120 execsql {SELECT typeof(trim(NULL));}
1122 do_test func-22.21 {
1123 execsql {SELECT typeof(trim(NULL,'xyz'));}
1125 do_test func-22.22 {
1126 execsql {SELECT typeof(trim('hello',NULL));}
1129 # 2021-06-15 - infinite loop due to unsigned character counter
1130 # overflow, reported by Zimuzo Ezeozue
1132 do_execsql_test func-22.23 {
1133 SELECT trim('xyzzy',x'c0808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080');
1136 # This is to test the deprecated sqlite3_aggregate_count() API.
1138 ifcapable deprecated {
1140 sqlite3_create_aggregate db
1142 SELECT legacy_count() FROM t6;
1147 # The group_concat() and string_agg() functions.
1151 SELECT group_concat(t1), string_agg(t1,',') FROM tbl1
1153 } {this,program,is,free,software this,program,is,free,software}
1156 SELECT group_concat(t1,' '), string_agg(t1,' ') FROM tbl1
1158 } {{this program is free software} {this program is free software}}
1161 SELECT group_concat(t1,' ' || rowid || ' ') FROM tbl1
1163 } {{this 2 program 3 is 4 free 5 software}}
1166 SELECT group_concat(NULL,t1) FROM tbl1
1171 SELECT group_concat(t1,NULL), string_agg(t1,NULL) FROM tbl1
1173 } {thisprogramisfreesoftware thisprogramisfreesoftware}
1176 SELECT 'BEGIN-'||group_concat(t1) FROM tbl1
1178 } {BEGIN-this,program,is,free,software}
1180 # Ticket #3179: Make sure aggregate functions can take many arguments.
1181 # None of the built-in aggregates do this, so use the md5sum() from the
1184 unset -nocomplain midargs
1186 unset -nocomplain midres
1188 unset -nocomplain result
1189 for {set i 1} {$i<[sqlite3_limit db SQLITE_LIMIT_FUNCTION_ARG -1]} {incr i} {
1190 append midargs ,'/$i'
1193 "this${midres}program${midres}is${midres}free${midres}software${midres}"]
1194 set sql "SELECT md5sum(t1$midargs) FROM tbl1"
1195 do_test func-24.7.$i {
1200 # Ticket #3806. If the initial string in a group_concat is an empty
1201 # string, the separator that follows should still be present.
1205 SELECT group_concat(CASE t1 WHEN 'this' THEN '' ELSE t1 END) FROM tbl1
1207 } {,program,is,free,software}
1210 SELECT group_concat(CASE WHEN t1!='software' THEN '' ELSE t1 END) FROM tbl1
1214 # Ticket #3923. Initial empty strings have a separator. But initial
1217 do_test func-24.10 {
1219 SELECT group_concat(CASE t1 WHEN 'this' THEN null ELSE t1 END) FROM tbl1
1221 } {program,is,free,software}
1222 do_test func-24.11 {
1224 SELECT group_concat(CASE WHEN t1!='software' THEN null ELSE t1 END) FROM tbl1
1227 do_test func-24.12 {
1229 SELECT group_concat(CASE t1 WHEN 'this' THEN ''
1230 WHEN 'program' THEN null ELSE t1 END) FROM tbl1
1232 } {,is,free,software}
1233 # Tests to verify ticket http://www.sqlite.org/src/tktview/55746f9e65f8587c0
1234 do_test func-24.13 {
1236 SELECT typeof(group_concat(x)) FROM (SELECT '' AS x);
1239 do_test func-24.14 {
1241 SELECT typeof(group_concat(x,''))
1242 FROM (SELECT '' AS x UNION ALL SELECT '');
1247 # Use the test_isolation function to make sure that type conversions
1248 # on function arguments do not effect subsequent arguments.
1251 execsql {SELECT test_isolation(t1,t1) FROM tbl1}
1252 } {this program is free software}
1254 # Try to misuse the sqlite3_create_function() interface. Verify that
1255 # errors are returned.
1258 abuse_create_function db
1261 # The previous test (func-26.1) registered a function with a very long
1262 # function name that takes many arguments and always returns NULL. Verify
1263 # that this function works correctly.
1267 for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG} {incr i} {
1271 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 ,]);
1276 for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG+1} {incr i} {
1280 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 ,]);
1282 } {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}}
1285 for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG-1} {incr i} {
1289 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 ,]);
1291 } {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()}}
1294 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);
1296 } {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}}
1299 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);
1301 } {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}}
1304 catchsql {SELECT coalesce()}
1305 } {1 {wrong number of arguments to function coalesce()}}
1307 catchsql {SELECT coalesce(1)}
1308 } {1 {wrong number of arguments to function coalesce()}}
1310 catchsql {SELECT coalesce(1,2)}
1313 # Ticket 2d401a94287b5
1314 # Unknown function in a DEFAULT expression causes a segfault.
1318 CREATE TABLE t28(x, y DEFAULT(nosuchfunc(1)));
1321 INSERT INTO t28(x) VALUES(1);
1323 } {1 {unknown function: nosuchfunc()}}
1325 # Verify that the length() and typeof() functions do not actually load
1326 # the content of their argument.
1330 CREATE TABLE t29(id INTEGER PRIMARY KEY, x, y);
1331 INSERT INTO t29 VALUES(1, 2, 3), (2, NULL, 4), (3, 4.5, 5);
1332 INSERT INTO t29 VALUES(4, randomblob(1000000), 6);
1333 INSERT INTO t29 VALUES(5, 'hello', 7);
1337 sqlite3_db_status db CACHE_MISS 1
1338 db eval {SELECT typeof(x), length(x), typeof(y) FROM t29 ORDER BY id}
1339 } {integer 1 integer null {} integer real 3 integer blob 1000000 integer text 5 integer}
1341 set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
1348 sqlite3_db_status db CACHE_MISS 1
1349 db eval {SELECT typeof(+x) FROM t29 ORDER BY id}
1350 } {integer null real blob text}
1351 if {[permutation] != "mmap"} {
1352 ifcapable !direct_read {
1354 set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
1355 if {$x>100} {set x many}
1363 sqlite3_db_status db CACHE_MISS 1
1364 db eval {SELECT sum(length(x)) FROM t29}
1367 set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
1372 # The OP_Column opcode has an optimization that avoids loading content
1373 # for fields with content-length=0 when the content offset is on an overflow
1374 # page. Make sure the optimization works.
1376 do_execsql_test func-29.10 {
1377 CREATE TABLE t29b(a,b,c,d,e,f,g,h,i);
1379 VALUES(1, hex(randomblob(2000)), null, 0, 1, '', zeroblob(0),'x',x'01');
1380 SELECT typeof(c), typeof(d), typeof(e), typeof(f),
1381 typeof(g), typeof(h), typeof(i) FROM t29b;
1382 } {null integer integer text blob text blob}
1383 do_execsql_test func-29.11 {
1384 SELECT length(f), length(g), length(h), length(i) FROM t29b;
1386 do_execsql_test func-29.12 {
1387 SELECT quote(f), quote(g), quote(h), quote(i) FROM t29b;
1388 } {'' X'' 'x' X'01'}
1390 # EVIDENCE-OF: R-29701-50711 The unicode(X) function returns the numeric
1391 # unicode code point corresponding to the first character of the string
1394 # EVIDENCE-OF: R-55469-62130 The char(X1,X2,...,XN) function returns a
1395 # string composed of characters having the unicode code point values of
1396 # integers X1 through XN, respectively.
1398 do_execsql_test func-30.1 {SELECT unicode('$');} 36
1399 do_execsql_test func-30.2 [subst {SELECT unicode('\u00A2');}] 162
1400 do_execsql_test func-30.3 [subst {SELECT unicode('\u20AC');}] 8364
1401 do_execsql_test func-30.4 {SELECT char(36,162,8364);} [subst {$\u00A2\u20AC}]
1403 for {set i 1} {$i<0xd800} {incr i 13} {
1404 do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
1406 for {set i 57344} {$i<=0xfffd} {incr i 17} {
1407 if {$i==0xfeff} continue
1408 do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
1410 for {set i 65536} {$i<=0x10ffff} {incr i 139} {
1411 do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
1416 do_execsql_test func-31.1 {
1417 SELECT char(), length(char()), typeof(char())
1420 # sqlite3_value_frombind()
1422 do_execsql_test func-32.100 {
1423 SELECT test_frombind(1,2,3,4);
1425 do_execsql_test func-32.110 {
1426 SELECT test_frombind(1,2,?,4);
1428 do_execsql_test func-32.120 {
1429 SELECT test_frombind(1,(?),4,?+7);
1431 do_execsql_test func-32.130 {
1432 DROP TABLE IF EXISTS t1;
1433 CREATE TABLE t1(a,b,c,e,f);
1434 INSERT INTO t1 VALUES(1,2.5,'xyz',x'e0c1b2a3',null);
1435 SELECT test_frombind(a,b,c,e,f,$xyz) FROM t1;
1437 do_execsql_test func-32.140 {
1438 SELECT test_frombind(a,b,c,e,f,$xyz+f) FROM t1;
1440 do_execsql_test func-32.150 {
1441 SELECT test_frombind(x.a,y.b,x.c,:123,y.e,x.f,$xyz+y.f) FROM t1 x, t1 y;
1445 # Direct-only functions.
1447 proc testdirectonly {x} {return [expr {$x*2}]}
1449 db func testdirectonly -directonly testdirectonly
1450 db eval {SELECT testdirectonly(15)}
1452 do_catchsql_test func-33.2 {
1453 CREATE VIEW v33(y) AS SELECT testdirectonly(15);
1455 } {1 {unsafe use of testdirectonly()}}
1456 do_execsql_test func-33.3 {
1457 SELECT * FROM (SELECT testdirectonly(15)) AS v33;
1459 do_execsql_test func-33.4 {
1460 WITH c(x) AS (SELECT testdirectonly(15))
1463 do_catchsql_test func-33.5 {
1464 WITH c(x) AS (SELECT * FROM v33)
1466 } {1 {unsafe use of testdirectonly()}}
1467 do_execsql_test func-33.10 {
1468 CREATE TABLE t33a(a,b);
1469 CREATE TABLE t33b(x,y);
1470 CREATE TRIGGER r1 AFTER INSERT ON t33a BEGIN
1471 INSERT INTO t33b(x,y) VALUES(testdirectonly(new.a),new.b);
1474 do_catchsql_test func-33.11 {
1475 INSERT INTO t33a VALUES(1,2);
1476 } {1 {unsafe use of testdirectonly()}}
1478 ifcapable altertable {
1479 do_execsql_test func-33.20 {
1480 ALTER TABLE t33a RENAME COLUMN a TO aaa;
1481 SELECT sql FROM sqlite_master WHERE name='r1';
1482 } {{CREATE TRIGGER r1 AFTER INSERT ON t33a BEGIN
1483 INSERT INTO t33b(x,y) VALUES(testdirectonly(new.aaa),new.b);
1487 # 2020-01-09 Yongheng fuzzer find
1488 # The bug is in the register-validity debug logic, not in the SQLite core
1489 # and as such it only impacts debug builds. Release builds work fine.
1492 do_execsql_test func-34.10 {
1493 CREATE TABLE t1(a INT CHECK(
1494 datetime( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1495 10,11,12,13,14,15,16,17,18,19,
1496 20,21,22,23,24,25,26,27,28,29,
1497 30,31,32,33,34,35,36,37,38,39,
1498 40,41,42,43,44,45,46,47,48,a)
1501 INSERT INTO t1(a) VALUES(1),(2);
1505 # 2020-03-11 COALESCE() should short-circuit
1506 # See also ticket 3c9eadd2a6ba0aa5
1507 # Both issues stem from the fact that functions that could
1508 # throw exceptions were being factored out into initialization
1509 # code. The fix was to put those function calls inside of
1513 do_execsql_test func-35.100 {
1515 SELECT coalesce(x, abs(-9223372036854775808)) FROM t1;
1517 do_execsql_test func-35.110 {
1518 SELECT coalesce(x, 'xyz' LIKE printf('%.1000000c','y')) FROM t1;
1520 do_execsql_test func-35.200 {
1521 CREATE TABLE t0(c0 CHECK(ABS(-9223372036854775808)));
1522 PRAGMA integrity_check;
1525 # 2021-01-07: The -> and ->> operators.
1527 proc ptr1 {a b} { return "$a->$b" }
1529 proc ptr2 {a b} { return "$a->>$b" }
1531 do_execsql_test func-36.100 {
1534 do_execsql_test func-36.110 {
1539 # Enhanced precision of SUM().
1542 do_catchsql_test func-37.100 {
1543 WITH c(x) AS (VALUES(9223372036854775807),(9223372036854775807),
1544 (123),(-9223372036854775807),(-9223372036854775807))
1545 SELECT sum(x) FROM c;
1546 } {1 {integer overflow}}
1547 do_catchsql_test func-37.110 {
1548 WITH c(x) AS (VALUES(9223372036854775807),(1))
1549 SELECT sum(x) FROM c;
1550 } {1 {integer overflow}}
1551 do_catchsql_test func-37.120 {
1552 WITH c(x) AS (VALUES(9223372036854775807),(10000),(-10010))
1553 SELECT sum(x) FROM c;
1554 } {1 {integer overflow}}
1556 # 2023-08-28 forum post https://sqlite.org/forum/forumpost/1c06ddcacc86032a
1557 # Incorrect handling of infinity by SUM().
1559 do_execsql_test func-38.100 {
1560 WITH t1(x) AS (VALUES(9e+999)) SELECT sum(x), avg(x), total(x) FROM t1;
1561 WITH t1(x) AS (VALUES(-9e+999)) SELECT sum(x), avg(x), total(x) FROM t1;
1562 } {Inf Inf Inf -Inf -Inf -Inf}
1564 # 2024-03-21 https://sqlite.org/forum/forumpost/23b8688ef4
1565 # Another problem with Kahan-Babushka-Neumaier summation and
1568 do_execsql_test func-39.101 {
1569 WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<1)
1570 SELECT sum(1.7976931348623157e308),
1571 avg(1.7976931348623157e308),
1572 total(1.7976931348623157e308)
1574 } {1.79769313486232e+308 1.79769313486232e+308 1.79769313486232e+308}
1575 for {set i 2} {$i<10} {incr i} {
1576 do_execsql_test func-39.[expr {10*$i+100}] {
1577 WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<$i)
1578 SELECT sum(1.7976931348623157e308),
1579 avg(1.7976931348623157e308),
1580 total(1.7976931348623157e308)