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