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 # Correctly handle function error messages that include %. Ticket #1354
792 proc testfunc1 args {error "Error %d with %s percents %p"}
793 db function testfunc1 ::testfunc1
795 SELECT testfunc1(1,2,3);
797 } {1 {Error %d with %s percents %p}}
799 # The SUM function should return integer results when all inputs are integer.
804 INSERT INTO t5 VALUES(1);
805 INSERT INTO t5 VALUES(-99);
806 INSERT INTO t5 VALUES(10000);
807 SELECT sum(x) FROM t5;
810 ifcapable floatingpoint {
813 INSERT INTO t5 VALUES(0.0);
814 SELECT sum(x) FROM t5;
819 # The sum of nothing is NULL. But the sum of all NULLs is NULL.
821 # The TOTAL of nothing is 0.0.
826 SELECT sum(x), total(x) FROM t5;
831 INSERT INTO t5 VALUES(NULL);
832 SELECT sum(x), total(x) FROM t5
837 INSERT INTO t5 VALUES(NULL);
838 SELECT sum(x), total(x) FROM t5
843 INSERT INTO t5 VALUES(123);
844 SELECT sum(x), total(x) FROM t5
848 # Ticket #1664, #1669, #1670, #1674: An integer overflow on SUM causes
849 # an error. The non-standard TOTAL() function continues to give a helpful
854 CREATE TABLE t6(x INTEGER);
855 INSERT INTO t6 VALUES(1);
856 INSERT INTO t6 VALUES(1<<62);
857 SELECT sum(x) - ((1<<62)+1) from t6;
862 SELECT typeof(sum(x)) FROM t6
865 ifcapable floatingpoint {
866 do_catchsql_test func-18.12 {
867 INSERT INTO t6 VALUES(1<<62);
868 SELECT sum(x) - ((1<<62)*2.0+1) from t6;
869 } {1 {integer overflow}}
870 do_catchsql_test func-18.13 {
871 SELECT total(x) - ((1<<62)*2.0+1) FROM t6
874 if {[working_64bit_int]} {
877 SELECT sum(-9223372036854775805);
879 } -9223372036854775805
881 ifcapable compound&&subquery {
886 (SELECT 9223372036854775807 AS x UNION ALL
889 } {1 {integer overflow}}
890 if {[working_64bit_int]} {
894 (SELECT 9223372036854775807 AS x UNION ALL
897 } {0 9223372036854775797}
901 (SELECT -9223372036854775807 AS x UNION ALL
904 } {0 -9223372036854775797}
909 (SELECT -9223372036854775807 AS x UNION ALL
912 } {1 {integer overflow}}
915 SELECT sum(x) FROM (SELECT 9 AS x UNION ALL SELECT -10 AS x);
920 SELECT sum(x) FROM (SELECT -9 AS x UNION ALL SELECT 10 AS x);
925 SELECT sum(x) FROM (SELECT -10 AS x UNION ALL SELECT 9 AS x);
930 SELECT sum(x) FROM (SELECT 10 AS x UNION ALL SELECT -9 AS x);
934 } ;# ifcapable compound&&subquery
936 # Integer overflow on abs()
938 if {[working_64bit_int]} {
941 SELECT abs(-9223372036854775807);
943 } {0 9223372036854775807}
947 SELECT abs(-9223372036854775807-1);
949 } {1 {integer overflow}}
951 # The MATCH function exists but is only a stub and always throws an error.
955 SELECT match(a,b) FROM t1 WHERE 0;
960 SELECT 'abc' MATCH 'xyz';
962 } {1 {unable to use function MATCH in the requested context}}
965 SELECT 'abc' NOT MATCH 'xyz';
967 } {1 {unable to use function MATCH in the requested context}}
972 } {1 {wrong number of arguments to function match()}}
976 if {![catch {db eval {SELECT soundex('hello')}}]} {
998 execsql {SELECT soundex($name)}
1003 # Tests of the REPLACE function.
1007 SELECT replace(1,2);
1009 } {1 {wrong number of arguments to function replace()}}
1012 SELECT replace(1,2,3,4);
1014 } {1 {wrong number of arguments to function replace()}}
1017 SELECT typeof(replace('This is the main test string', NULL, 'ALT'));
1022 SELECT typeof(replace(NULL, 'main', 'ALT'));
1027 SELECT typeof(replace('This is the main test string', 'main', NULL));
1032 SELECT replace('This is the main test string', 'main', 'ALT');
1034 } {{This is the ALT test string}}
1037 SELECT replace('This is the main test string', 'main', 'larger-main');
1039 } {{This is the larger-main test string}}
1042 SELECT replace('aaaaaaa', 'a', '0123456789');
1044 } {0123456789012345678901234567890123456789012345678901234567890123456789}
1048 # Attempt to exploit a buffer-overflow that at one time existed
1049 # in the REPLACE function.
1050 set ::str "[string repeat A 29998]CC[string repeat A 35537]"
1051 set ::rep [string repeat B 65536]
1053 SELECT LENGTH(REPLACE($::str, 'C', $::rep));
1055 } [expr 29998 + 2*65536 + 35537]
1058 # Tests for the TRIM, LTRIM and RTRIM functions.
1061 catchsql {SELECT trim(1,2,3)}
1062 } {1 {wrong number of arguments to function trim()}}
1064 catchsql {SELECT ltrim(1,2,3)}
1065 } {1 {wrong number of arguments to function ltrim()}}
1067 catchsql {SELECT rtrim(1,2,3)}
1068 } {1 {wrong number of arguments to function rtrim()}}
1070 execsql {SELECT trim(' hi ');}
1073 execsql {SELECT ltrim(' hi ');}
1076 execsql {SELECT rtrim(' hi ');}
1079 execsql {SELECT trim(' hi ','xyz');}
1082 execsql {SELECT ltrim(' hi ','xyz');}
1085 execsql {SELECT rtrim(' hi ','xyz');}
1087 do_test func-22.10 {
1088 execsql {SELECT trim('xyxzy hi zzzy','xyz');}
1090 do_test func-22.11 {
1091 execsql {SELECT ltrim('xyxzy hi zzzy','xyz');}
1093 do_test func-22.12 {
1094 execsql {SELECT rtrim('xyxzy hi zzzy','xyz');}
1096 do_test func-22.13 {
1097 execsql {SELECT trim(' hi ','');}
1099 if {[db one {PRAGMA encoding}]=="UTF-8"} {
1100 do_test func-22.14 {
1101 execsql {SELECT hex(trim(x'c280e1bfbff48fbfbf6869',x'6162e1bfbfc280'))}
1103 do_test func-22.15 {
1104 execsql {SELECT hex(trim(x'6869c280e1bfbff48fbfbf61',
1105 x'6162e1bfbfc280f48fbfbf'))}
1107 do_test func-22.16 {
1108 execsql {SELECT hex(trim(x'ceb1ceb2ceb3',x'ceb1'));}
1111 do_test func-22.20 {
1112 execsql {SELECT typeof(trim(NULL));}
1114 do_test func-22.21 {
1115 execsql {SELECT typeof(trim(NULL,'xyz'));}
1117 do_test func-22.22 {
1118 execsql {SELECT typeof(trim('hello',NULL));}
1121 # 2021-06-15 - infinite loop due to unsigned character counter
1122 # overflow, reported by Zimuzo Ezeozue
1124 do_execsql_test func-22.23 {
1125 SELECT trim('xyzzy',x'c0808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080');
1128 # This is to test the deprecated sqlite3_aggregate_count() API.
1130 ifcapable deprecated {
1132 sqlite3_create_aggregate db
1134 SELECT legacy_count() FROM t6;
1139 # The group_concat() function.
1143 SELECT group_concat(t1) FROM tbl1
1145 } {this,program,is,free,software}
1148 SELECT group_concat(t1,' ') FROM tbl1
1150 } {{this program is free software}}
1153 SELECT group_concat(t1,' ' || rowid || ' ') FROM tbl1
1155 } {{this 2 program 3 is 4 free 5 software}}
1158 SELECT group_concat(NULL,t1) FROM tbl1
1163 SELECT group_concat(t1,NULL) FROM tbl1
1165 } {thisprogramisfreesoftware}
1168 SELECT 'BEGIN-'||group_concat(t1) FROM tbl1
1170 } {BEGIN-this,program,is,free,software}
1172 # Ticket #3179: Make sure aggregate functions can take many arguments.
1173 # None of the built-in aggregates do this, so use the md5sum() from the
1176 unset -nocomplain midargs
1178 unset -nocomplain midres
1180 unset -nocomplain result
1181 for {set i 1} {$i<[sqlite3_limit db SQLITE_LIMIT_FUNCTION_ARG -1]} {incr i} {
1182 append midargs ,'/$i'
1185 "this${midres}program${midres}is${midres}free${midres}software${midres}"]
1186 set sql "SELECT md5sum(t1$midargs) FROM tbl1"
1187 do_test func-24.7.$i {
1192 # Ticket #3806. If the initial string in a group_concat is an empty
1193 # string, the separator that follows should still be present.
1197 SELECT group_concat(CASE t1 WHEN 'this' THEN '' ELSE t1 END) FROM tbl1
1199 } {,program,is,free,software}
1202 SELECT group_concat(CASE WHEN t1!='software' THEN '' ELSE t1 END) FROM tbl1
1206 # Ticket #3923. Initial empty strings have a separator. But initial
1209 do_test func-24.10 {
1211 SELECT group_concat(CASE t1 WHEN 'this' THEN null ELSE t1 END) FROM tbl1
1213 } {program,is,free,software}
1214 do_test func-24.11 {
1216 SELECT group_concat(CASE WHEN t1!='software' THEN null ELSE t1 END) FROM tbl1
1219 do_test func-24.12 {
1221 SELECT group_concat(CASE t1 WHEN 'this' THEN ''
1222 WHEN 'program' THEN null ELSE t1 END) FROM tbl1
1224 } {,is,free,software}
1225 # Tests to verify ticket http://www.sqlite.org/src/tktview/55746f9e65f8587c0
1226 do_test func-24.13 {
1228 SELECT typeof(group_concat(x)) FROM (SELECT '' AS x);
1231 do_test func-24.14 {
1233 SELECT typeof(group_concat(x,''))
1234 FROM (SELECT '' AS x UNION ALL SELECT '');
1239 # Use the test_isolation function to make sure that type conversions
1240 # on function arguments do not effect subsequent arguments.
1243 execsql {SELECT test_isolation(t1,t1) FROM tbl1}
1244 } {this program is free software}
1246 # Try to misuse the sqlite3_create_function() interface. Verify that
1247 # errors are returned.
1250 abuse_create_function db
1253 # The previous test (func-26.1) registered a function with a very long
1254 # function name that takes many arguments and always returns NULL. Verify
1255 # that this function works correctly.
1259 for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG} {incr i} {
1263 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 ,]);
1268 for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG+1} {incr i} {
1272 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 ,]);
1274 } {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}}
1277 for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG-1} {incr i} {
1281 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 ,]);
1283 } {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()}}
1286 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);
1288 } {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}}
1291 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);
1293 } {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}}
1296 catchsql {SELECT coalesce()}
1297 } {1 {wrong number of arguments to function coalesce()}}
1299 catchsql {SELECT coalesce(1)}
1300 } {1 {wrong number of arguments to function coalesce()}}
1302 catchsql {SELECT coalesce(1,2)}
1305 # Ticket 2d401a94287b5
1306 # Unknown function in a DEFAULT expression causes a segfault.
1310 CREATE TABLE t28(x, y DEFAULT(nosuchfunc(1)));
1313 INSERT INTO t28(x) VALUES(1);
1315 } {1 {unknown function: nosuchfunc()}}
1317 # Verify that the length() and typeof() functions do not actually load
1318 # the content of their argument.
1322 CREATE TABLE t29(id INTEGER PRIMARY KEY, x, y);
1323 INSERT INTO t29 VALUES(1, 2, 3), (2, NULL, 4), (3, 4.5, 5);
1324 INSERT INTO t29 VALUES(4, randomblob(1000000), 6);
1325 INSERT INTO t29 VALUES(5, 'hello', 7);
1329 sqlite3_db_status db CACHE_MISS 1
1330 db eval {SELECT typeof(x), length(x), typeof(y) FROM t29 ORDER BY id}
1331 } {integer 1 integer null {} integer real 3 integer blob 1000000 integer text 5 integer}
1333 set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
1340 sqlite3_db_status db CACHE_MISS 1
1341 db eval {SELECT typeof(+x) FROM t29 ORDER BY id}
1342 } {integer null real blob text}
1343 if {[permutation] != "mmap"} {
1344 ifcapable !direct_read {
1346 set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
1347 if {$x>100} {set x many}
1355 sqlite3_db_status db CACHE_MISS 1
1356 db eval {SELECT sum(length(x)) FROM t29}
1359 set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
1364 # The OP_Column opcode has an optimization that avoids loading content
1365 # for fields with content-length=0 when the content offset is on an overflow
1366 # page. Make sure the optimization works.
1368 do_execsql_test func-29.10 {
1369 CREATE TABLE t29b(a,b,c,d,e,f,g,h,i);
1371 VALUES(1, hex(randomblob(2000)), null, 0, 1, '', zeroblob(0),'x',x'01');
1372 SELECT typeof(c), typeof(d), typeof(e), typeof(f),
1373 typeof(g), typeof(h), typeof(i) FROM t29b;
1374 } {null integer integer text blob text blob}
1375 do_execsql_test func-29.11 {
1376 SELECT length(f), length(g), length(h), length(i) FROM t29b;
1378 do_execsql_test func-29.12 {
1379 SELECT quote(f), quote(g), quote(h), quote(i) FROM t29b;
1380 } {'' X'' 'x' X'01'}
1382 # EVIDENCE-OF: R-29701-50711 The unicode(X) function returns the numeric
1383 # unicode code point corresponding to the first character of the string
1386 # EVIDENCE-OF: R-55469-62130 The char(X1,X2,...,XN) function returns a
1387 # string composed of characters having the unicode code point values of
1388 # integers X1 through XN, respectively.
1390 do_execsql_test func-30.1 {SELECT unicode('$');} 36
1391 do_execsql_test func-30.2 [subst {SELECT unicode('\u00A2');}] 162
1392 do_execsql_test func-30.3 [subst {SELECT unicode('\u20AC');}] 8364
1393 do_execsql_test func-30.4 {SELECT char(36,162,8364);} [subst {$\u00A2\u20AC}]
1395 for {set i 1} {$i<0xd800} {incr i 13} {
1396 do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
1398 for {set i 57344} {$i<=0xfffd} {incr i 17} {
1399 if {$i==0xfeff} continue
1400 do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
1402 for {set i 65536} {$i<=0x10ffff} {incr i 139} {
1403 do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
1408 do_execsql_test func-31.1 {
1409 SELECT char(), length(char()), typeof(char())
1412 # sqlite3_value_frombind()
1414 do_execsql_test func-32.100 {
1415 SELECT test_frombind(1,2,3,4);
1417 do_execsql_test func-32.110 {
1418 SELECT test_frombind(1,2,?,4);
1420 do_execsql_test func-32.120 {
1421 SELECT test_frombind(1,(?),4,?+7);
1423 do_execsql_test func-32.130 {
1424 DROP TABLE IF EXISTS t1;
1425 CREATE TABLE t1(a,b,c,e,f);
1426 INSERT INTO t1 VALUES(1,2.5,'xyz',x'e0c1b2a3',null);
1427 SELECT test_frombind(a,b,c,e,f,$xyz) FROM t1;
1429 do_execsql_test func-32.140 {
1430 SELECT test_frombind(a,b,c,e,f,$xyz+f) FROM t1;
1432 do_execsql_test func-32.150 {
1433 SELECT test_frombind(x.a,y.b,x.c,:123,y.e,x.f,$xyz+y.f) FROM t1 x, t1 y;
1437 # Direct-only functions.
1439 proc testdirectonly {x} {return [expr {$x*2}]}
1441 db func testdirectonly -directonly testdirectonly
1442 db eval {SELECT testdirectonly(15)}
1444 do_catchsql_test func-33.2 {
1445 CREATE VIEW v33(y) AS SELECT testdirectonly(15);
1447 } {1 {unsafe use of testdirectonly()}}
1448 do_execsql_test func-33.3 {
1449 SELECT * FROM (SELECT testdirectonly(15)) AS v33;
1451 do_execsql_test func-33.4 {
1452 WITH c(x) AS (SELECT testdirectonly(15))
1455 do_catchsql_test func-33.5 {
1456 WITH c(x) AS (SELECT * FROM v33)
1458 } {1 {unsafe use of testdirectonly()}}
1459 do_execsql_test func-33.10 {
1460 CREATE TABLE t33a(a,b);
1461 CREATE TABLE t33b(x,y);
1462 CREATE TRIGGER r1 AFTER INSERT ON t33a BEGIN
1463 INSERT INTO t33b(x,y) VALUES(testdirectonly(new.a),new.b);
1466 do_catchsql_test func-33.11 {
1467 INSERT INTO t33a VALUES(1,2);
1468 } {1 {unsafe use of testdirectonly()}}
1470 ifcapable altertable {
1471 do_execsql_test func-33.20 {
1472 ALTER TABLE t33a RENAME COLUMN a TO aaa;
1473 SELECT sql FROM sqlite_master WHERE name='r1';
1474 } {{CREATE TRIGGER r1 AFTER INSERT ON t33a BEGIN
1475 INSERT INTO t33b(x,y) VALUES(testdirectonly(new.aaa),new.b);
1479 # 2020-01-09 Yongheng fuzzer find
1480 # The bug is in the register-validity debug logic, not in the SQLite core
1481 # and as such it only impacts debug builds. Release builds work fine.
1484 do_execsql_test func-34.10 {
1485 CREATE TABLE t1(a INT CHECK(
1486 datetime( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1487 10,11,12,13,14,15,16,17,18,19,
1488 20,21,22,23,24,25,26,27,28,29,
1489 30,31,32,33,34,35,36,37,38,39,
1490 40,41,42,43,44,45,46,47,48,a)
1493 INSERT INTO t1(a) VALUES(1),(2);
1497 # 2020-03-11 COALESCE() should short-circuit
1498 # See also ticket 3c9eadd2a6ba0aa5
1499 # Both issues stem from the fact that functions that could
1500 # throw exceptions were being factored out into initialization
1501 # code. The fix was to put those function calls inside of
1505 do_execsql_test func-35.100 {
1507 SELECT coalesce(x, abs(-9223372036854775808)) FROM t1;
1509 do_execsql_test func-35.110 {
1510 SELECT coalesce(x, 'xyz' LIKE printf('%.1000000c','y')) FROM t1;
1512 do_execsql_test func-35.200 {
1513 CREATE TABLE t0(c0 CHECK(ABS(-9223372036854775808)));
1514 PRAGMA integrity_check;
1517 # 2021-01-07: The -> and ->> operators.
1519 proc ptr1 {a b} { return "$a->$b" }
1521 proc ptr2 {a b} { return "$a->>$b" }
1523 do_execsql_test func-36.100 {
1526 do_execsql_test func-36.110 {
1531 # Enhanced precision of SUM().
1534 do_catchsql_test func-37.100 {
1535 WITH c(x) AS (VALUES(9223372036854775807),(9223372036854775807),
1536 (123),(-9223372036854775807),(-9223372036854775807))
1537 SELECT sum(x) FROM c;
1538 } {1 {integer overflow}}
1539 do_catchsql_test func-37.110 {
1540 WITH c(x) AS (VALUES(9223372036854775807),(1))
1541 SELECT sum(x) FROM c;
1542 } {1 {integer overflow}}
1543 do_catchsql_test func-37.120 {
1544 WITH c(x) AS (VALUES(9223372036854775807),(10000),(-10010))
1545 SELECT sum(x) FROM c;
1546 } {1 {integer overflow}}
1548 # 2023-08-28 forum post https://sqlite.org/forum/forumpost/1c06ddcacc86032a
1549 # Incorrect handling of infinity by SUM().
1551 do_execsql_test func-38.100 {
1552 WITH t1(x) AS (VALUES(9e+999)) SELECT sum(x), avg(x), total(x) FROM t1;
1553 WITH t1(x) AS (VALUES(-9e+999)) SELECT sum(x), avg(x), total(x) FROM t1;
1554 } {Inf Inf Inf -Inf -Inf -Inf}