1d673cddaSdrh# 2013-11-21 2d673cddaSdrh# 3d673cddaSdrh# The author disclaims copyright to this source code. In place of 4d673cddaSdrh# a legal notice, here is a blessing: 5d673cddaSdrh# 6d673cddaSdrh# May you do good and not evil. 7d673cddaSdrh# May you find forgiveness for yourself and forgive others. 8d673cddaSdrh# May you share freely, never taking more than you give. 9d673cddaSdrh# 10d673cddaSdrh#************************************************************************* 11d673cddaSdrh# 124a8ee3dfSdrh# Testing of function factoring and the SQLITE_DETERMINISTIC flag. 13d673cddaSdrh# 14d673cddaSdrhset testdir [file dirname $argv0] 15d673cddaSdrhsource $testdir/tester.tcl 16d673cddaSdrh 174a8ee3dfSdrh# Verify that constant string expressions that get factored into initializing 184a8ee3dfSdrh# code are not reused between function parameters and other values in the 194a8ee3dfSdrh# VDBE program, as the function might have changed the encoding. 204a8ee3dfSdrh# 21d673cddaSdrhdo_execsql_test func5-1.1 { 22d673cddaSdrh PRAGMA encoding=UTF16le; 23d673cddaSdrh CREATE TABLE t1(x,a,b,c); 24d673cddaSdrh INSERT INTO t1 VALUES(1,'ab','cd',1); 25d673cddaSdrh INSERT INTO t1 VALUES(2,'gh','ef',5); 26d673cddaSdrh INSERT INTO t1 VALUES(3,'pqr','fuzzy',99); 27d673cddaSdrh INSERT INTO t1 VALUES(4,'abcdefg','xy',22); 28d673cddaSdrh INSERT INTO t1 VALUES(5,'shoe','mayer',2953); 29d673cddaSdrh SELECT x FROM t1 WHERE c=instr('abcdefg',b) OR a='abcdefg' ORDER BY +x; 30d673cddaSdrh} {2 4} 31d673cddaSdrhdo_execsql_test func5-1.2 { 32d673cddaSdrh SELECT x FROM t1 WHERE a='abcdefg' OR c=instr('abcdefg',b) ORDER BY +x; 33d673cddaSdrh} {2 4} 34d673cddaSdrh 354a8ee3dfSdrh# Verify that SQLITE_DETERMINISTIC functions get factored out of the 364a8ee3dfSdrh# evaluation loop whereas non-deterministic functions do not. counter1() 374a8ee3dfSdrh# is marked as non-deterministic and so is not factored out of the loop, 384a8ee3dfSdrh# and it really is non-deterministic, returning a different result each 394a8ee3dfSdrh# time. But counter2() is marked as deterministic, so it does get factored 404a8ee3dfSdrh# out of the loop. counter2() has the same implementation as counter1(), 414a8ee3dfSdrh# returning a different result on each invocation, but because it is 424a8ee3dfSdrh# only invoked once outside of the loop, it appears to return the same 434a8ee3dfSdrh# result multiple times. 444a8ee3dfSdrh# 454a8ee3dfSdrhdo_execsql_test func5-2.1 { 464a8ee3dfSdrh CREATE TABLE t2(x,y); 474a8ee3dfSdrh INSERT INTO t2 VALUES(1,2),(3,4),(5,6),(7,8); 484a8ee3dfSdrh SELECT x, y FROM t2 WHERE x+5=5+x ORDER BY +x; 494a8ee3dfSdrh} {1 2 3 4 5 6 7 8} 504a8ee3dfSdrhsqlite3_create_function db 514a8ee3dfSdrhdo_execsql_test func5-2.2 { 524a8ee3dfSdrh SELECT x, y FROM t2 534a8ee3dfSdrh WHERE x+counter1('hello')=counter1('hello')+x 544a8ee3dfSdrh ORDER BY +x; 554a8ee3dfSdrh} {} 56*38dfbdaeSdrhset cvalue [db one {SELECT counter2('hello')+1}] 574a8ee3dfSdrhdo_execsql_test func5-2.3 { 584a8ee3dfSdrh SELECT x, y FROM t2 59*38dfbdaeSdrh WHERE x+counter2('hello')=$cvalue+x 604a8ee3dfSdrh ORDER BY +x; 614a8ee3dfSdrh} {1 2 3 4 5 6 7 8} 624a8ee3dfSdrh 634a8ee3dfSdrh 64d673cddaSdrhfinish_test 65