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 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is syntax errors involving row-value constructors
13 # and sub-selects that return multiple arguments.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 set ::testprefix rowvalue4
20 #-------------------------------------------------------------------------
21 # Test some error conditions:
23 # * row values used where they are not supported,
24 # * row values or sub-selects that contain/return the wrong number
28 CREATE TABLE t1(a, b, c);
29 CREATE INDEX t1bac ON t1(b, a, c);
37 do_catchsql_test 1.$tn "SELECT $e" {1 {row value misused}}
40 foreach {tn s error} {
41 1 "SELECT * FROM t1 WHERE a = (1, 2)" {row value misused}
42 2 "SELECT * FROM t1 WHERE b = (1, 2)" {row value misused}
43 3 "SELECT * FROM t1 WHERE NOT (b = (1, 2))" {row value misused}
44 4 "SELECT * FROM t1 LIMIT (1, 2)" {row value misused}
45 5 "SELECT (a, b) IN (SELECT * FROM t1) FROM t1"
46 {sub-select returns 3 columns - expected 2}
48 6 "SELECT * FROM t1 WHERE (a, b) IN (SELECT * FROM t1)"
49 {sub-select returns 3 columns - expected 2}
50 7 "SELECT * FROM t1 WHERE (c, c) <= 1" {row value misused}
51 8 "SELECT * FROM t1 WHERE (b, b) <= 1" {row value misused}
53 do_catchsql_test 2.$tn "$s" [list 1 $error]
56 #-------------------------------------------------------------------------
58 CREATE TABLE t2(a, b, c, d);
59 INSERT INTO t2 VALUES(1, 1, 1, 1);
60 INSERT INTO t2 VALUES(1, 1, 2, 2);
61 INSERT INTO t2 VALUES(1, 1, 3, 3);
62 INSERT INTO t2 VALUES(1, 2, 1, 4);
63 INSERT INTO t2 VALUES(1, 2, 2, 5);
64 INSERT INTO t2 VALUES(1, 2, 3, 6);
65 INSERT INTO t2 VALUES(1, 3, 1, 7);
66 INSERT INTO t2 VALUES(1, 3, 2, 8);
67 INSERT INTO t2 VALUES(1, 3, 3, 9);
69 INSERT INTO t2 VALUES(2, 1, 1, 10);
70 INSERT INTO t2 VALUES(2, 1, 2, 11);
71 INSERT INTO t2 VALUES(2, 1, 3, 12);
72 INSERT INTO t2 VALUES(2, 2, 1, 13);
73 INSERT INTO t2 VALUES(2, 2, 2, 14);
74 INSERT INTO t2 VALUES(2, 2, 3, 15);
75 INSERT INTO t2 VALUES(2, 3, 1, 16);
76 INSERT INTO t2 VALUES(2, 3, 2, 17);
77 INSERT INTO t2 VALUES(2, 3, 3, 18);
79 INSERT INTO t2 VALUES(3, 1, 1, 19);
80 INSERT INTO t2 VALUES(3, 1, 2, 20);
81 INSERT INTO t2 VALUES(3, 1, 3, 21);
82 INSERT INTO t2 VALUES(3, 2, 1, 22);
83 INSERT INTO t2 VALUES(3, 2, 2, 23);
84 INSERT INTO t2 VALUES(3, 2, 3, 24);
85 INSERT INTO t2 VALUES(3, 3, 1, 25);
86 INSERT INTO t2 VALUES(3, 3, 2, 26);
87 INSERT INTO t2 VALUES(3, 3, 3, 27);
92 idx2 { CREATE INDEX t2abc ON t2(a, b, c); }
93 idx3 { CREATE INDEX t2abc ON t2(a, b DESC, c); }
94 idx4 { CREATE INDEX t2abc ON t2(a DESC, b DESC, c DESC); }
95 idx5 { CREATE INDEX t2abc ON t2(a ASC, b ASC, c ASC); }
96 idx6 { CREATE INDEX t2abc ON t2(a DESC, b, c); }
97 idx7 { CREATE INDEX t2abc ON t2(a DESC, b DESC) }
98 idx8 { CREATE INDEX t2abc ON t2(c, b, a); }
99 idx9 { CREATE INDEX t2d ON t2(d); }
100 idx10 { CREATE INDEX t2abc ON t2(a DESC, b, c DESC); }
105 foreach {tn where res} {
106 1 "(a, b, c) < (2, 2, 2)" {1 2 3 4 5 6 7 8 9 10 11 12 13}
107 2 "(a, b, c) <= (2, 2, 2)" {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
108 3 "(a, b, c) > (2, 2, 2)" {15 16 17 18 19 20 21 22 23 24 25 26 27}
109 4 "(a, b, c) >= (2, 2, 2)" {14 15 16 17 18 19 20 21 22 23 24 25 26 27}
110 5 "(a, b, c) >= (2, 2, NULL)" {16 17 18 19 20 21 22 23 24 25 26 27}
111 6 "(a, b, c) <= (2, 2, NULL)" {1 2 3 4 5 6 7 8 9 10 11 12}
112 7 "(a, b, c) >= (2, NULL, NULL)" {19 20 21 22 23 24 25 26 27}
113 8 "(a, b, c) <= (2, NULL, NULL)" {1 2 3 4 5 6 7 8 9}
115 9 "(a, b, c) < (SELECT a, b, c FROM t2 WHERE d=14)"
116 {1 2 3 4 5 6 7 8 9 10 11 12 13}
118 10 "(a, b, c) = (SELECT a, b, c FROM t2 WHERE d=14)" 14
120 11 "a = 2 AND (b, c) > (2, 2)" {15 16 17 18}
121 12 "a = 2 AND (b, c) < (3, 3) AND (b, c) > (1, 1)" {11 12 13 14 15 16 17}
123 set result [db eval "SELECT d FROM t2 WHERE $where"]
124 do_test 2.1.$nm.$tn { lsort -integer $result } $res
128 1 "(2, 1) IN (SELECT a, b FROM t2)" 1
129 2 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d)" 1
130 3 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d LIMIT 9)" 0
131 4 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d LIMIT 10)" 1
133 5 "(3, 3) = (SELECT a, b FROM t2 ORDER BY d DESC LIMIT 1)" 1
134 6 "(3, 3) = (SELECT a, b FROM t2 ORDER BY d ASC LIMIT 1)" 0
135 7 "(1, NULL) = (SELECT a, b FROM t2 ORDER BY d ASC LIMIT 1)" {{}}
137 8 "(3, 1) = (SELECT b, c FROM t2 ORDER BY d DESC LIMIT 1 OFFSET 2)" 1
138 9 "(3, 1) = (SELECT b, c FROM t2 ORDER BY d ASC LIMIT 1 OFFSET 2)" 0
139 10 "(1, NULL) = (SELECT b, c FROM t2 ORDER BY d ASC LIMIT 1 OFFSET 2)" {{}}
141 11 "(3, 3) = (SELECT max(a), max(b) FROM t2)" 1
142 12 "(3, 1) = (SELECT max(a), min(b) FROM t2)" 1
143 13 "(NULL, NULL) = (SELECT max(a), min(b) FROM t2)" {{}}
145 14 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d LIMIT 5 OFFSET 11)" 1
146 15 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d LIMIT 5 OFFSET 12)" 0
148 do_execsql_test 2.2.$nm.$tn "SELECT $e" $res
153 do_execsql_test 3.0 {
154 CREATE TABLE c1(a, b, c, d);
155 INSERT INTO c1(a, b) VALUES(1, 'a');
156 INSERT INTO c1(a, b) VALUES(1, 'b');
157 INSERT INTO c1(a, b) VALUES(1, 'c');
158 INSERT INTO c1(a, b) VALUES(1, 'd');
159 INSERT INTO c1(a, b) VALUES(1, 'e');
160 INSERT INTO c1(a, b) VALUES(1, 'f');
161 INSERT INTO c1(a, b) VALUES(1, 'g');
162 INSERT INTO c1(a, b) VALUES(1, 'h');
163 INSERT INTO c1(a, b) VALUES(1, 'i');
164 INSERT INTO c1(a, b) VALUES(1, 'j');
165 INSERT INTO c1(a, b) VALUES(1, 'k');
166 INSERT INTO c1(a, b) VALUES(1, 'l');
167 INSERT INTO c1(a, b) VALUES(1, 'm');
168 INSERT INTO c1(a, b) VALUES(1, 'n');
169 INSERT INTO c1(a, b) VALUES(1, 'o');
170 INSERT INTO c1(a, b) VALUES(1, 'p');
171 INSERT INTO c1(a, b) VALUES(2, 'a');
172 INSERT INTO c1(a, b) VALUES(2, 'b');
173 INSERT INTO c1(a, b) VALUES(2, 'c');
174 INSERT INTO c1(a, b) VALUES(2, 'd');
175 INSERT INTO c1(a, b) VALUES(2, 'e');
176 INSERT INTO c1(a, b) VALUES(2, 'f');
177 INSERT INTO c1(a, b) VALUES(2, 'g');
178 INSERT INTO c1(a, b) VALUES(2, 'h');
180 INSERT INTO c1(c, d) SELECT a, b FROM c1;
182 CREATE INDEX c1ab ON c1(a, b);
183 CREATE INDEX c1cd ON c1(c, d);
187 do_eqp_test 3.1.1 { SELECT * FROM c1 WHERE a=1 AND c=2 } {
188 0 0 0 {SEARCH TABLE c1 USING INDEX c1cd (c=?)}
190 do_eqp_test 3.1.2 { SELECT * FROM c1 WHERE a=1 AND b>'d' AND c=2 } {
191 0 0 0 {SEARCH TABLE c1 USING INDEX c1cd (c=?)}
193 do_eqp_test 3.1.3 { SELECT * FROM c1 WHERE a=1 AND b>'l' AND c=2 } {
194 0 0 0 {SEARCH TABLE c1 USING INDEX c1ab (a=? AND b>?)}
197 do_eqp_test 3.2.1 { SELECT * FROM c1 WHERE a=1 AND c>1 } {
198 0 0 0 {SEARCH TABLE c1 USING INDEX c1cd (c>?)}
200 do_eqp_test 3.2.2 { SELECT * FROM c1 WHERE a=1 AND c>0 } {
201 0 0 0 {SEARCH TABLE c1 USING INDEX c1ab (a=?)}
203 do_eqp_test 3.2.3 { SELECT * FROM c1 WHERE a=1 AND c>=1 } {
204 0 0 0 {SEARCH TABLE c1 USING INDEX c1ab (a=?)}
206 do_eqp_test 3.2.4 { SELECT * FROM c1 WHERE a=1 AND (c, d)>(1, 'c') } {
207 0 0 0 {SEARCH TABLE c1 USING INDEX c1ab (a=?)}
209 do_eqp_test 3.2.5 { SELECT * FROM c1 WHERE a=1 AND (c, d)>(1, 'o') } {
210 0 0 0 {SEARCH TABLE c1 USING INDEX c1cd ((c,d)>(?,?))}
212 do_eqp_test 3.2.6 { SELECT * FROM c1 WHERE a=1 AND (c, +b)>(1, 'c') } {
213 0 0 0 {SEARCH TABLE c1 USING INDEX c1ab (a=?)}
217 #------------------------------------------------------------------------
219 do_execsql_test 5.0 {
220 CREATE TABLE d1(x, y);
221 CREATE TABLE d2(a, b, c);
222 CREATE INDEX d2ab ON d2(a, b);
223 CREATE INDEX d2c ON d2(c);
226 VALUES(1) UNION ALL SELECT i+1 FROM i WHERE i<1000
228 INSERT INTO d2 SELECT i/3, i%3, i/3 FROM i;
233 SELECT * FROM d2 WHERE
234 (a, b) IN (SELECT x, y FROM d1) AND
235 (c) IN (SELECT y FROM d1)
237 0 0 0 {SEARCH TABLE d2 USING INDEX d2ab (a=? AND b=?)}
238 0 0 0 {EXECUTE LIST SUBQUERY 1}
239 1 0 0 {SCAN TABLE d1}
240 0 0 0 {EXECUTE LIST SUBQUERY 2}
241 2 0 0 {SCAN TABLE d1}
244 do_execsql_test 6.0 {
245 CREATE TABLE e1(a, b, c, d, e);
246 CREATE INDEX e1ab ON e1(a, b);
247 CREATE INDEX e1cde ON e1(c, d, e);
251 SELECT * FROM e1 WHERE (a, b) > (?, ?)
253 0 0 0 {SEARCH TABLE e1 USING INDEX e1ab ((a,b)>(?,?))}
256 SELECT * FROM e1 WHERE (a, b) < (?, ?)
258 0 0 0 {SEARCH TABLE e1 USING INDEX e1ab ((a,b)<(?,?))}
261 SELECT * FROM e1 WHERE c = ? AND (d, e) > (?, ?)
263 0 0 0 {SEARCH TABLE e1 USING INDEX e1cde (c=? AND (d,e)>(?,?))}
266 SELECT * FROM e1 WHERE c = ? AND (d, e) < (?, ?)
268 0 0 0 {SEARCH TABLE e1 USING INDEX e1cde (c=? AND (d,e)<(?,?))}
272 SELECT * FROM e1 WHERE (d, e) BETWEEN (?, ?) AND (?, ?) AND c = ?
275 {SEARCH TABLE e1 USING INDEX e1cde (c=? AND (d,e)>(?,?) AND (d,e)<(?,?))}
278 #-------------------------------------------------------------------------
280 do_execsql_test 7.1 {
281 CREATE TABLE f1(a, b, c);
282 CREATE INDEX f1ab ON f1(a, b);
285 do_catchsql_test 7.2 {
286 SELECT (a COLLATE nocase, b) IN (SELECT a, b FROM f1) FROM f1;
289 do_catchsql_test 7.3 {
290 SELECT (a COLLATE nose, b) IN (SELECT a, b FROM f1) FROM f1;
291 } {1 {no such collation sequence: nose}}
293 do_catchsql_test 7.4 {
294 SELECT * FROM f1 WHERE (?, ? COLLATE nose) > (a, b);
295 } {1 {no such collation sequence: nose}}
297 #-------------------------------------------------------------------------
299 do_execsql_test 8.1 {
300 CREATE TABLE c1(x, y);
301 CREATE TABLE c2(a, b, c);
302 CREATE INDEX c2ab ON c2(a, b);
303 CREATE INDEX c2c ON c2(c);
307 do_catchsql_test 8.2 {
308 SELECT * FROM c2 CROSS JOIN c3 WHERE
309 ( (a, b) == (SELECT x, y FROM c1) AND c3.d = c ) OR
310 ( c == (SELECT x, y FROM c1) AND c3.d = c )
311 } {1 {row value misused}}