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 {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 {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 {SEARCH TABLE c1 USING INDEX c1ab (a=? AND b>?)}
196 do_eqp_test 3.2.1 { SELECT * FROM c1 WHERE a=1 AND c>1 } \
197 {SEARCH TABLE c1 USING INDEX c1cd (c>?)}
199 do_eqp_test 3.2.2 { SELECT * FROM c1 WHERE a=1 AND c>0 } \
200 {SEARCH TABLE c1 USING INDEX c1ab (a=?)}
202 do_eqp_test 3.2.3 { SELECT * FROM c1 WHERE a=1 AND c>=1 } \
203 {SEARCH TABLE c1 USING INDEX c1ab (a=?)}
205 do_eqp_test 3.2.4 { SELECT * FROM c1 WHERE a=1 AND (c, d)>(1, 'c') } \
206 {SEARCH TABLE c1 USING INDEX c1ab (a=?)}
208 do_eqp_test 3.2.5 { SELECT * FROM c1 WHERE a=1 AND (c, d)>(1, 'o') } \
209 {SEARCH TABLE c1 USING INDEX c1cd ((c,d)>(?,?))}
211 do_eqp_test 3.2.6 { SELECT * FROM c1 WHERE a=1 AND (c, +b)>(1, 'c') } \
212 {SEARCH TABLE c1 USING INDEX c1ab (a=?)}
216 #------------------------------------------------------------------------
218 do_execsql_test 5.0 {
219 CREATE TABLE d1(x, y);
220 CREATE TABLE d2(a, b, c);
221 CREATE INDEX d2ab ON d2(a, b);
222 CREATE INDEX d2c ON d2(c);
225 VALUES(1) UNION ALL SELECT i+1 FROM i WHERE i<1000
227 INSERT INTO d2 SELECT i/100, i%100, i/100 FROM i;
232 SELECT * FROM d2 WHERE
233 (a, b) IN (SELECT x, y FROM d1) AND
234 (c) IN (SELECT y FROM d1)
237 |--SEARCH TABLE d2 USING INDEX d2ab (a=? AND b=?)
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) > (?, ?)
252 } {SEARCH TABLE e1 USING INDEX e1ab ((a,b)>(?,?))}
255 SELECT * FROM e1 WHERE (a, b) < (?, ?)
256 } {SEARCH TABLE e1 USING INDEX e1ab ((a,b)<(?,?))}
259 SELECT * FROM e1 WHERE c = ? AND (d, e) > (?, ?)
260 } {SEARCH TABLE e1 USING INDEX e1cde (c=? AND (d,e)>(?,?))}
263 SELECT * FROM e1 WHERE c = ? AND (d, e) < (?, ?)
264 } {SEARCH TABLE e1 USING INDEX e1cde (c=? AND (d,e)<(?,?))}
267 SELECT * FROM e1 WHERE (d, e) BETWEEN (?, ?) AND (?, ?) AND c = ?
268 } {SEARCH TABLE e1 USING INDEX e1cde (c=? AND (d,e)>(?,?) AND (d,e)<(?,?))}
270 #-------------------------------------------------------------------------
272 do_execsql_test 7.1 {
273 CREATE TABLE f1(a, b, c);
274 CREATE INDEX f1ab ON f1(a, b);
277 do_catchsql_test 7.2 {
278 SELECT (a COLLATE nocase, b) IN (SELECT a, b FROM f1) FROM f1;
281 do_catchsql_test 7.3 {
282 SELECT (a COLLATE nose, b) IN (SELECT a, b FROM f1) FROM f1;
283 } {1 {no such collation sequence: nose}}
285 do_catchsql_test 7.4 {
286 SELECT * FROM f1 WHERE (?, ? COLLATE nose) > (a, b);
287 } {1 {no such collation sequence: nose}}
289 #-------------------------------------------------------------------------
291 do_execsql_test 8.1 {
292 CREATE TABLE c1(x, y);
293 CREATE TABLE c2(a, b, c);
294 CREATE INDEX c2ab ON c2(a, b);
295 CREATE INDEX c2c ON c2(c);
299 do_catchsql_test 8.2 {
300 SELECT * FROM c2 CROSS JOIN c3 WHERE
301 ( (a, b) == (SELECT x, y FROM c1) AND c3.d = c ) OR
302 ( c == (SELECT x, y FROM c1) AND c3.d = c )
303 } {1 {row value misused}}