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 #***********************************************************************
12 # Test cases designed to exercise and verify the logic for
13 # factoring constant expressions out of loops and for
14 # common subexpression eliminations.
16 # $Id: cse.test,v 1.6 2008/08/04 03:51:24 danielk1977 Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
25 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d, e, f);
26 INSERT INTO t1 VALUES(1,11,12,13,14,15);
27 INSERT INTO t1 VALUES(2,21,22,23,24,25);
30 SELECT b, -b, ~b, NOT b, NOT NOT b, b-b, b+b, b*b, b/b, b FROM t1
32 } {11 -11 -12 0 1 0 22 121 1 11 21 -21 -22 0 1 0 42 441 1 21}
35 SELECT b, b%b, b==b, b!=b, b<b, b<=b, b IS NULL, b NOT NULL, b FROM t1
37 } {11 0 1 0 0 1 0 1 11 21 0 1 0 0 1 0 1 21}
40 SELECT b, abs(b), coalesce(b,-b,NOT b,c,NOT c), c, -c FROM t1;
42 } {11 11 11 12 -12 21 21 21 22 -22}
45 SELECT CASE WHEN a==1 THEN b ELSE c END, b, c FROM t1
50 SELECT CASE a WHEN 1 THEN b WHEN 2 THEN c ELSE d END, b, c, d FROM t1
52 } {11 11 12 13 22 21 22 23}
55 SELECT CASE b WHEN 11 THEN -b WHEN 21 THEN -c ELSE -d END, b, c, d FROM t1
57 } {-11 11 12 13 -22 21 22 23}
60 SELECT CASE b+1 WHEN c THEN d WHEN e THEN f ELSE 999 END, b, c, d FROM t1
62 } {13 11 12 13 23 21 22 23}
65 SELECT CASE WHEN b THEN d WHEN e THEN f ELSE 999 END, b, c, d FROM t1
67 } {13 11 12 13 23 21 22 23}
70 SELECT b, c, d, CASE WHEN b THEN d WHEN e THEN f ELSE 999 END FROM t1
72 } {11 12 13 13 21 22 23 23}
75 SELECT b, c, d, CASE WHEN 0 THEN d WHEN e THEN f ELSE 999 END FROM t1
77 } {11 12 13 15 21 22 23 25}
80 SELECT a, -a, ~a, NOT a, NOT NOT a, a-a, a+a, a*a, a/a, a FROM t1
82 } {1 -1 -2 0 1 0 2 1 1 1 2 -2 -3 0 1 0 4 4 1 2}
85 SELECT a, a%a, a==a, a!=a, a<a, a<=a, a IS NULL, a NOT NULL, a FROM t1
87 } {1 0 1 0 0 1 0 1 1 2 0 1 0 0 1 0 1 2}
90 SELECT NOT b, ~b, NOT NOT b, b FROM t1
92 } {0 -12 1 11 0 -22 1 21}
95 SELECT CAST(b AS integer), typeof(b), CAST(b AS text), typeof(b) FROM t1
97 } {11 integer 11 integer 21 integer 21 integer}
101 SELECT *,* FROM t1 WHERE a=2
103 SELECT *,* FROM t1 WHERE a=1
105 } {2 21 22 23 24 25 2 21 22 23 24 25 1 11 12 13 14 15 1 11 12 13 14 15}
108 SELECT coalesce(b,c,d,e), a, b, c, d, e FROM t1 WHERE a=2
110 SELECT coalesce(e,d,c,b), e, d, c, b, a FROM t1 WHERE a=1
112 } {21 2 21 22 23 24 14 14 13 12 11 1}
116 SELECT upper(b), typeof(b), b FROM t1
118 } {11 integer 11 21 integer 21}
121 SELECT b, typeof(b), upper(b), typeof(b), b FROM t1
123 } {11 integer 11 integer 11 21 integer 21 integer 21}
125 # Overflow the column cache. Create queries involving more and more
126 # columns until the cache overflows. Verify correct operation throughout.
130 CREATE TABLE t2(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,
131 a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,
132 a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,
133 a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,
134 a40,a41,a42,a43,a44,a45,a46,a47,a48,a49);
135 INSERT INTO t2 VALUES(0,1,2,3,4,5,6,7,8,9,
136 10,11,12,13,14,15,16,17,18,19,
137 20,21,22,23,24,25,26,27,28,29,
138 30,31,32,33,34,35,36,37,38,39,
139 40,41,42,43,44,45,46,47,48,49);
142 } {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49}
144 for {set i 1} {$i<100} {incr i} {
145 set n [expr {int(rand()*44)+5}]
148 for {set j 0} {$j<$n} {incr j} {
149 set r [expr {$j+int(rand()*5)}]
150 if {$r>49} {set r [expr {99-$r}]}
151 lappend colset a$j a$r
154 set sql "SELECT [join $colset ,] FROM t2"
161 #-------------------------------------------------------------------------
162 # Ticket fd1bda016d1a
165 do_execsql_test 3.0 {
166 CREATE TABLE t1(a TEXT, b);
167 INSERT INTO t1 VALUES('hello', 0);
168 INSERT INTO t1 VALUES('world', 0);
170 CREATE TABLE t2(x TEXT);
171 INSERT INTO t2 VALUES('hello');
172 INSERT INTO t2 VALUES('world');
175 INSERT INTO t3 VALUES(1000);
178 do_execsql_test 3.1 {
179 SELECT 1000 = y FROM t3
182 do_execsql_test 3.2 {
183 SELECT 1000 IN (SELECT x FROM t2), 1000 = y FROM t3
186 do_execsql_test 3.3 {
187 SELECT 0 IN (SELECT a), (SELECT a LIMIT 0) FROM t1
190 do_execsql_test 3.4 {
191 SELECT 0 IN (SELECT a) FROM t1 WHERE a = 'hello' OR (SELECT a LIMIT 0);
194 do_execsql_test 3.5 {
195 CREATE TABLE v0(v1 VARCHAR0);
196 INSERT INTO v0 VALUES(2), (3);
197 SELECT 0 IN(SELECT v1) FROM v0 WHERE v1 = 2 OR(SELECT v1 LIMIT 0);