interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / cursorhint2.test

206 lines · 4455 bytes Raw Blame History
1# 2016 June 17
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# This file implements regression tests for SQLite library. The
12# focus is on testing that cursor-hints are correct for queries
13# involving LEFT JOIN.
14#
15
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19set ::testprefix cursorhint2
20
21ifcapable !cursorhints {
22 finish_test
23 return
24}
25
26proc extract_hints {sql} {
27
28 db eval "SELECT tbl_name, rootpage FROM sqlite_master where rootpage" {
29 set lookup($rootpage) $tbl_name
30 }
31
32 set ret [list]
33 db eval "EXPLAIN $sql" a {
34 switch -- $a(opcode) {
35 OpenRead {
36 set csr($a(p1)) $lookup($a(p2))
37 }
38 CursorHint {
39 if {$a(p4)!=""} {
40 lappend ret $csr($a(p1)) $a(p4)
41 }
42 }
43 }
44 }
45
46 set ret
47}
48
49proc do_extract_hints_test {tn sql ret} {
50 uplevel [list do_test $tn [list extract_hints $sql] [list {*}$ret]]
51}
52
53do_execsql_test 1.0 {
54 PRAGMA automatic_index = 0;
55 CREATE TABLE t1(a, b);
56 CREATE TABLE t2(c, d);
57 CREATE TABLE t3(e, f);
58}
59
60do_extract_hints_test 1.1 {
61 SELECT * FROM t1 WHERE a=1;
62} {
63 t1 EQ(c0,1)
64}
65
66do_extract_hints_test 1.2 {
67 SELECT * FROM t1 CROSS JOIN t2 ON (a=c) WHERE d IS NULL;
68} {
69 t2 {AND(ISNULL(c1),EQ(r[1],c0))}
70}
71
72do_extract_hints_test 1.3 {
73 SELECT * FROM t1 LEFT JOIN t2 ON (a=c) WHERE d IS NULL;
74} {
75 t2 {EQ(r[2],c0)}
76}
77
78do_extract_hints_test 1.4 {
79 SELECT * FROM t1 LEFT JOIN t2 ON (a=c AND a=10) WHERE d IS NULL;
80} {
81 t2 {AND(EQ(r[2],c0),EQ(r[3],10))}
82}
83
84do_extract_hints_test 1.5 {
85 SELECT * FROM t1 CROSS JOIN t2 ON (a=c AND a=10) WHERE d IS NULL;
86} {
87 t1 EQ(c0,10) t2 {AND(ISNULL(c1),EQ(r[3],c0))}
88}
89
90do_extract_hints_test 1.6 {
91 SELECT * FROM t1 LEFT JOIN t2 ON (a=c) LEFT JOIN t3 ON (d=f);
92} {
93 t2 {EQ(r[2],c0)} t3 {EQ(r[6],c1)}
94}
95
96if 0 {
97 do_extract_hints_test 1.7 {
98 SELECT * FROM t1 LEFT JOIN t2 ON (a=c AND d=e) LEFT JOIN t3 ON (d=f);
99 } {
100 t2 {EQ(r[2],c0)} t3 {AND(EQ(r[6],c0),EQ(r[7],c1))}
101 }
102}
103
104#-------------------------------------------------------------------------
105#
106do_execsql_test 2.0 {
107 CREATE TABLE x1(x, y);
108 CREATE TABLE x2(a, b);
109}
110
111do_extract_hints_test 2.1 {
112 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS NULL;
113} {
114 x2 {EQ(c0,r[2])}
115}
116
117do_extract_hints_test 2.2 {
118 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS +NULL;
119} {
120 x2 {EQ(c0,r[2])}
121}
122
123do_extract_hints_test 2.3 {
124 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = (b IS NULL)
125} {
126 x2 {EQ(c0,r[2])}
127}
128
129do_extract_hints_test 2.4 {
130 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = coalesce(b, 1)
131} {
132 x2 {EQ(c0,r[2])}
133}
134
135do_extract_hints_test 2.5 {
136 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = coalesce(b, 1)
137} {
138 x2 {EQ(c0,r[2])}
139}
140
141if {0} {
142 # These tests no longer work due to the LEFT-JOIN strength reduction
143 # optimization
144 do_extract_hints_test 2.6 {
145 SELECT * FROM x1 CROSS JOIN x2 ON (a=x) WHERE 0 = (b IS NOT NULL)
146 } {
147 x2 {EQ(c0,r[2])}
148 }
149
150 do_extract_hints_test 2.7 {
151 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 0 = (b IS NOT +NULL)
152 } {
153 x2 {EQ(c0,r[2])}
154 }
155
156 do_extract_hints_test 2.8 {
157 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS NOT +NULL
158 } {
159 x2 {EQ(c0,r[2])}
160 }
161
162 do_extract_hints_test 2.9 {
163 SELECT * FROM x1 LEFT JOIN x2 ON (a=x)
164 WHERE CASE b WHEN 0 THEN 0 ELSE 1 END;
165 } {
166 x2 {EQ(c0,r[2])}
167 }
168
169 do_extract_hints_test 2.10 {
170 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE x2.b = 32+32
171 } {
172 x2 {AND(EQ(c1,ADD(32,32)),EQ(c0,r[2]))}
173 }
174
175 ifcapable !icu {
176 # This test only works using the built-in LIKE, not the ICU LIKE extension.
177 do_extract_hints_test 2.11 {
178 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE x2.b LIKE 'abc%'
179 } {
180 x2 {AND(expr,EQ(c0,r[2]))}
181 }
182 }
183}
184
185do_extract_hints_test 2.12 {
186 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE coalesce(x2.b, 1)
187} {
188 x2 {EQ(c0,r[2])}
189}
190
191reset_db
192do_execsql_test 3.0 {
193 CREATE TABLE t1 (i1 TEXT);
194 CREATE TABLE t2 (i2 TEXT UNIQUE);
195 INSERT INTO t1 VALUES('0');
196 INSERT INTO t2 VALUES('0');
197}
198
199do_extract_hints_test 3.1 {
200 SELECT * FROM t1 CROSS JOIN t2 WHERE (t1.i1 = t2.i2) AND t2.i2 = 1;
201} {
202 t1 {EQ(c0,r[1])} t2 EQ(c0,1)
203}
204
205
206finish_test