AryaWu/sqlite
0
1# 2013-11-212#3# The author disclaims copyright to this source code. In place of4# a legal notice, here is a blessing:5#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.9#10#*************************************************************************11#12# Testing of function factoring and the SQLITE_DETERMINISTIC flag.13#14set testdir [file dirname $argv0]15source $testdir/tester.tcl16 17# Verify that constant string expressions that get factored into initializing18# code are not reused between function parameters and other values in the19# VDBE program, as the function might have changed the encoding.20#21do_execsql_test func5-1.1 {22 PRAGMA encoding=UTF16le;23 CREATE TABLE t1(x,a,b,c);24 INSERT INTO t1 VALUES(1,'ab','cd',1);25 INSERT INTO t1 VALUES(2,'gh','ef',5);26 INSERT INTO t1 VALUES(3,'pqr','fuzzy',99);27 INSERT INTO t1 VALUES(4,'abcdefg','xy',22);28 INSERT INTO t1 VALUES(5,'shoe','mayer',2953);29 SELECT x FROM t1 WHERE c=instr('abcdefg',b) OR a='abcdefg' ORDER BY +x;30} {2 4}31do_execsql_test func5-1.2 {32 SELECT x FROM t1 WHERE a='abcdefg' OR c=instr('abcdefg',b) ORDER BY +x;33} {2 4}34 35# Verify that SQLITE_DETERMINISTIC functions get factored out of the36# evaluation loop whereas non-deterministic functions do not. counter1()37# is marked as non-deterministic and so is not factored out of the loop,38# and it really is non-deterministic, returning a different result each39# time. But counter2() is marked as deterministic, so it does get factored40# out of the loop. counter2() has the same implementation as counter1(),41# returning a different result on each invocation, but because it is 42# only invoked once outside of the loop, it appears to return the same43# result multiple times.44#45do_execsql_test func5-2.1 {46 CREATE TABLE t2(x,y);47 INSERT INTO t2 VALUES(1,2),(3,4),(5,6),(7,8);48 SELECT x, y FROM t2 WHERE x+5=5+x ORDER BY +x;49} {1 2 3 4 5 6 7 8}50sqlite3_create_function db51do_execsql_test func5-2.2 {52 SELECT x, y FROM t253 WHERE x+counter1('hello')=counter1('hello')+x54 ORDER BY +x;55} {}56set cvalue [db one {SELECT counter2('hello')+1}]57do_execsql_test func5-2.3 {58 SELECT x, y FROM t259 WHERE x+counter2('hello')=$cvalue+x60 ORDER BY +x;61} {1 2 3 4 5 6 7 8}62 63 64finish_test65 