blob: 836e15aba983660d7bf93eb6be9385481351c1b9 [file] [log] [blame]
drh652fbf52008-04-01 01:42:41 +00001# 2008 April 1
2#
3# The author disclaims copyright to this source code. In place of
4# 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# Test cases designed to exercise and verify the logic for
13# factoring constant expressions out of loops and for
14# common subexpression eliminations.
15#
drhc5499be2008-04-01 15:06:33 +000016# $Id: cse.test,v 1.4 2008/04/01 15:06:34 drh Exp $
drh652fbf52008-04-01 01:42:41 +000017#
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22do_test cse-1.1 {
23 execsql {
24 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d, e, f);
25 INSERT INTO t1 VALUES(1,11,12,13,14,15);
26 INSERT INTO t1 VALUES(2,21,22,23,24,25);
27 }
28 execsql {
29 SELECT b, -b, ~b, NOT b, NOT NOT b, b-b, b+b, b*b, b/b, b FROM t1
30 }
31} {11 -11 -12 0 1 0 22 121 1 11 21 -21 -22 0 1 0 42 441 1 21}
32do_test cse-1.2 {
33 execsql {
34 SELECT b, b%b, b==b, b!=b, b<b, b<=b, b IS NULL, b NOT NULL, b FROM t1
35 }
36} {11 0 1 0 0 1 0 1 11 21 0 1 0 0 1 0 1 21}
37do_test cse-1.3 {
38 execsql {
39 SELECT b, abs(b), coalesce(b,-b,NOT b,c,NOT c), c, -c FROM t1;
40 }
41} {11 11 11 12 -12 21 21 21 22 -22}
42do_test cse-1.4 {
43 execsql {
44 SELECT CASE WHEN a==1 THEN b ELSE c END, b, c FROM t1
45 }
46} {11 11 12 22 21 22}
47do_test cse-1.5 {
48 execsql {
49 SELECT CASE a WHEN 1 THEN b WHEN 2 THEN c ELSE d END, b, c, d FROM t1
50 }
51} {11 11 12 13 22 21 22 23}
52do_test cse-1.6 {
53 execsql {
54 SELECT CASE b WHEN 11 THEN -b WHEN 21 THEN -c ELSE -d END, b, c, d FROM t1
55 }
56} {-11 11 12 13 -22 21 22 23}
57do_test cse-1.7 {
58 execsql {
59 SELECT a, -a, ~a, NOT a, NOT NOT a, a-a, a+a, a*a, a/a, a FROM t1
60 }
61} {1 -1 -2 0 1 0 2 1 1 1 2 -2 -3 0 1 0 4 4 1 2}
62do_test cse-1.8 {
63 execsql {
64 SELECT a, a%a, a==a, a!=a, a<a, a<=a, a IS NULL, a NOT NULL, a FROM t1
65 }
66} {1 0 1 0 0 1 0 1 1 2 0 1 0 0 1 0 1 2}
drhb3843a82008-04-01 12:24:11 +000067do_test cse-1.9 {
68 execsql {
69 SELECT NOT b, ~b, NOT NOT b, b FROM t1
70 }
71} {0 -12 1 11 0 -22 1 21}
72do_test cse-1.10 {
drhb3843a82008-04-01 12:24:11 +000073 execsql {
74 SELECT CAST(b AS integer), typeof(b), CAST(b AS text), typeof(b) FROM t1
75 }
76} {11 integer 11 integer 21 integer 21 integer}
drhc5499be2008-04-01 15:06:33 +000077do_test cse-1.11 {
78 execsql {
79 SELECT *,* FROM t1 WHERE a=2
80 UNION ALL
81 SELECT *,* FROM t1 WHERE a=1
82 }
83} {2 21 22 23 24 25 2 21 22 23 24 25 1 11 12 13 14 15 1 11 12 13 14 15}
84do_test cse-1.12 {
85 execsql {
86 SELECT coalesce(b,c,d,e), a, b, c, d, e FROM t1 WHERE a=2
87 UNION ALL
88 SELECT coalesce(e,d,c,b), e, d, c, b, a FROM t1 WHERE a=1
89 }
90} {21 2 21 22 23 24 14 14 13 12 11 1}
91do_test cse-1.13 {
92 explain {
93 SELECT upper(b), typeof(b), b FROM t1
94 }
95 execsql {
96 SELECT upper(b), typeof(b), b FROM t1
97 }
98} {11 integer 11 21 integer 21}
99do_test cse-1.14 {
100 explain {
101 SELECT b, typeof(b), upper(b), typeof(b), b FROM t1
102 }
103 execsql {
104 SELECT b, typeof(b), upper(b), typeof(b), b FROM t1
105 }
106} {11 integer 11 integer 11 21 integer 21 integer 21}
drh652fbf52008-04-01 01:42:41 +0000107
drh2f7794c2008-04-01 03:27:39 +0000108# Overflow the column cache. Create queries involving more and more
109# columns until the cache overflows. Verify correct operation throughout.
110#
111do_test cse-2.1 {
112 execsql {
113 CREATE TABLE t2(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,
114 a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,
115 a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,
116 a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,
117 a40,a41,a42,a43,a44,a45,a46,a47,a48,a49);
118 INSERT INTO t2 VALUES(0,1,2,3,4,5,6,7,8,9,
119 10,11,12,13,14,15,16,17,18,19,
120 20,21,22,23,24,25,26,27,28,29,
121 30,31,32,33,34,35,36,37,38,39,
122 40,41,42,43,44,45,46,47,48,49);
123 SELECT * FROM t2;
124 }
125} {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}
126
127for {set i 1} {$i<100} {incr i} {
128 set n [expr {int(rand()*44)+5}]
129 set colset {}
130 set answer {}
131 for {set j 0} {$j<$n} {incr j} {
132 set r [expr {$j+int(rand()*5)}]
133 if {$r>49} {set r [expr {99-$r}]}
134 lappend colset a$j a$r
135 lappend answer $j $r
136 }
137 set sql "SELECT [join $colset ,] FROM t2"
138 do_test cse-2.2.$i {
139 # explain $::sql
140 execsql $::sql
141 } $answer
142}
drh652fbf52008-04-01 01:42:41 +0000143
144finish_test