2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the SELECT statement.
17 # $Id: select1.test,v 1.30.2.3 2004/07/20 01:45:49 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Try to select on a non-existant table.
25 set v [catch {execsql {SELECT * FROM test1}} msg]
27 } {1 {no such table: test1}}
29 execsql {CREATE TABLE test1(f1 int, f2 int)}
32 set v [catch {execsql {SELECT * FROM test1, test2}} msg]
34 } {1 {no such table: test2}}
36 set v [catch {execsql {SELECT * FROM test2, test1}} msg]
38 } {1 {no such table: test2}}
40 execsql {INSERT INTO test1(f1,f2) VALUES(11,22)}
43 # Make sure the columns are extracted correctly.
46 execsql {SELECT f1 FROM test1}
49 execsql {SELECT f2 FROM test1}
52 execsql {SELECT f2, f1 FROM test1}
55 execsql {SELECT f1, f2 FROM test1}
58 execsql {SELECT * FROM test1}
60 do_test select1-1.8.1 {
61 execsql {SELECT *, * FROM test1}
63 do_test select1-1.8.2 {
64 execsql {SELECT *, min(f1,f2), max(f1,f2) FROM test1}
66 do_test select1-1.8.3 {
67 execsql {SELECT 'one', *, 'two', * FROM test1}
68 } {one 11 22 two 11 22}
70 execsql {CREATE TABLE test2(r1 real, r2 real)}
71 execsql {INSERT INTO test2(r1,r2) VALUES(1.1,2.2)}
74 execsql {SELECT * FROM test1, test2}
76 do_test select1-1.9.1 {
77 execsql {SELECT *, 'hi' FROM test1, test2}
79 do_test select1-1.9.2 {
80 execsql {SELECT 'one', *, 'two', * FROM test1, test2}
81 } {one 11 22 1.1 2.2 two 11 22 1.1 2.2}
82 do_test select1-1.10 {
83 execsql {SELECT test1.f1, test2.r1 FROM test1, test2}
85 do_test select1-1.11 {
86 execsql {SELECT test1.f1, test2.r1 FROM test2, test1}
88 do_test select1-1.11.1 {
89 execsql {SELECT * FROM test2, test1}
91 do_test select1-1.11.2 {
92 execsql {SELECT * FROM test1 AS a, test1 AS b}
94 do_test select1-1.12 {
95 execsql {SELECT max(test1.f1,test2.r1), min(test1.f2,test2.r2)
98 do_test select1-1.13 {
99 execsql {SELECT min(test1.f1,test2.r1), max(test1.f2,test2.r2)
103 set long {This is a string that is too big to fit inside a NBFS buffer}
104 do_test select1-2.0 {
108 INSERT INTO test1 VALUES(11,22);
109 INSERT INTO test1 VALUES(33,44);
110 CREATE TABLE t3(a,b);
111 INSERT INTO t3 VALUES('abc',NULL);
112 INSERT INTO t3 VALUES(NULL,'xyz');
113 INSERT INTO t3 SELECT * FROM test1;
114 CREATE TABLE t4(a,b);
115 INSERT INTO t4 VALUES(NULL,'$long');
118 } {abc {} {} xyz 11 22 33 44}
120 # Error messges from sqliteExprCheck
122 do_test select1-2.1 {
123 set v [catch {execsql {SELECT count(f1,f2) FROM test1}} msg]
125 } {1 {wrong number of arguments to function count()}}
126 do_test select1-2.2 {
127 set v [catch {execsql {SELECT count(f1) FROM test1}} msg]
130 do_test select1-2.3 {
131 set v [catch {execsql {SELECT Count() FROM test1}} msg]
134 do_test select1-2.4 {
135 set v [catch {execsql {SELECT COUNT(*) FROM test1}} msg]
138 do_test select1-2.5 {
139 set v [catch {execsql {SELECT COUNT(*)+1 FROM test1}} msg]
142 do_test select1-2.5.1 {
143 execsql {SELECT count(*),count(a),count(b) FROM t3}
145 do_test select1-2.5.2 {
146 execsql {SELECT count(*),count(a),count(b) FROM t4}
148 do_test select1-2.5.3 {
149 execsql {SELECT count(*),count(a),count(b) FROM t4 WHERE b=5}
151 do_test select1-2.6 {
152 set v [catch {execsql {SELECT min(*) FROM test1}} msg]
154 } {1 {wrong number of arguments to function min()}}
155 do_test select1-2.7 {
156 set v [catch {execsql {SELECT Min(f1) FROM test1}} msg]
159 do_test select1-2.8 {
160 set v [catch {execsql {SELECT MIN(f1,f2) FROM test1}} msg]
161 lappend v [lsort $msg]
163 do_test select1-2.8.1 {
164 execsql {SELECT coalesce(min(a),'xyzzy') FROM t3}
166 do_test select1-2.8.2 {
167 execsql {SELECT min(coalesce(a,'xyzzy')) FROM t3}
169 do_test select1-2.8.3 {
170 execsql {SELECT min(b), min(b) FROM t4}
172 do_test select1-2.9 {
173 set v [catch {execsql {SELECT MAX(*) FROM test1}} msg]
175 } {1 {wrong number of arguments to function MAX()}}
176 do_test select1-2.10 {
177 set v [catch {execsql {SELECT Max(f1) FROM test1}} msg]
180 do_test select1-2.11 {
181 set v [catch {execsql {SELECT max(f1,f2) FROM test1}} msg]
182 lappend v [lsort $msg]
184 do_test select1-2.12 {
185 set v [catch {execsql {SELECT MAX(f1,f2)+1 FROM test1}} msg]
186 lappend v [lsort $msg]
188 do_test select1-2.13 {
189 set v [catch {execsql {SELECT MAX(f1)+1 FROM test1}} msg]
192 do_test select1-2.13.1 {
193 execsql {SELECT coalesce(max(a),'xyzzy') FROM t3}
195 do_test select1-2.13.2 {
196 execsql {SELECT max(coalesce(a,'xyzzy')) FROM t3}
198 do_test select1-2.14 {
199 set v [catch {execsql {SELECT SUM(*) FROM test1}} msg]
201 } {1 {wrong number of arguments to function SUM()}}
202 do_test select1-2.15 {
203 set v [catch {execsql {SELECT Sum(f1) FROM test1}} msg]
206 do_test select1-2.16 {
207 set v [catch {execsql {SELECT sum(f1,f2) FROM test1}} msg]
209 } {1 {wrong number of arguments to function sum()}}
210 do_test select1-2.17 {
211 set v [catch {execsql {SELECT SUM(f1)+1 FROM test1}} msg]
214 do_test select1-2.17.1 {
215 execsql {SELECT sum(a) FROM t3}
217 do_test select1-2.18 {
218 set v [catch {execsql {SELECT XYZZY(f1) FROM test1}} msg]
220 } {1 {no such function: XYZZY}}
221 do_test select1-2.19 {
222 set v [catch {execsql {SELECT SUM(min(f1,f2)) FROM test1}} msg]
225 do_test select1-2.20 {
226 set v [catch {execsql {SELECT SUM(min(f1)) FROM test1}} msg]
228 } {1 {misuse of aggregate function min()}}
230 # WHERE clause expressions
232 do_test select1-3.1 {
233 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<11}} msg]
236 do_test select1-3.2 {
237 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<=11}} msg]
240 do_test select1-3.3 {
241 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1=11}} msg]
244 do_test select1-3.4 {
245 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>=11}} msg]
246 lappend v [lsort $msg]
248 do_test select1-3.5 {
249 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>11}} msg]
250 lappend v [lsort $msg]
252 do_test select1-3.6 {
253 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1!=11}} msg]
254 lappend v [lsort $msg]
256 do_test select1-3.7 {
257 set v [catch {execsql {SELECT f1 FROM test1 WHERE min(f1,f2)!=11}} msg]
258 lappend v [lsort $msg]
260 do_test select1-3.8 {
261 set v [catch {execsql {SELECT f1 FROM test1 WHERE max(f1,f2)!=11}} msg]
262 lappend v [lsort $msg]
264 do_test select1-3.9 {
265 set v [catch {execsql {SELECT f1 FROM test1 WHERE count(f1,f2)!=11}} msg]
267 } {1 {wrong number of arguments to function count()}}
269 # ORDER BY expressions
271 do_test select1-4.1 {
272 set v [catch {execsql {SELECT f1 FROM test1 ORDER BY f1}} msg]
275 do_test select1-4.2 {
276 set v [catch {execsql {SELECT f1 FROM test1 ORDER BY -f1}} msg]
279 do_test select1-4.3 {
280 set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1,f2)}} msg]
283 do_test select1-4.4 {
284 set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1)}} msg]
286 } {1 {misuse of aggregate function min()}}
287 do_test select1-4.5 {
289 SELECT f1 FROM test1 ORDER BY 8.4;
291 } {1 {ORDER BY terms must not be non-integer constants}}
292 do_test select1-4.6 {
294 SELECT f1 FROM test1 ORDER BY '8.4';
296 } {1 {ORDER BY terms must not be non-integer constants}}
297 do_test select1-4.7 {
299 SELECT f1 FROM test1 ORDER BY 'xyz';
301 } {1 {ORDER BY terms must not be non-integer constants}}
302 do_test select1-4.8 {
304 CREATE TABLE t5(a,b);
305 INSERT INTO t5 VALUES(1,10);
306 INSERT INTO t5 VALUES(2,9);
307 SELECT * FROM t5 ORDER BY 1;
310 do_test select1-4.9 {
312 SELECT * FROM t5 ORDER BY 2;
315 do_test select1-4.10 {
317 SELECT * FROM t5 ORDER BY 3;
319 } {1 {ORDER BY column number 3 out of range - should be between 1 and 2}}
320 do_test select1-4.11 {
322 INSERT INTO t5 VALUES(3,10);
323 SELECT * FROM t5 ORDER BY 2, 1 DESC;
326 do_test select1-4.12 {
328 SELECT * FROM t5 ORDER BY 1 DESC, b;
331 do_test select1-4.13 {
333 SELECT * FROM t5 ORDER BY b DESC, 1;
338 # ORDER BY ignored on an aggregate query
340 do_test select1-5.1 {
341 set v [catch {execsql {SELECT max(f1) FROM test1 ORDER BY f2}} msg]
345 execsql {CREATE TABLE test2(t1 test, t2 text)}
346 execsql {INSERT INTO test2 VALUES('abc','xyz')}
348 # Check for column naming
350 do_test select1-6.1 {
351 set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
354 do_test select1-6.1.1 {
355 execsql {PRAGMA full_column_names=on}
356 set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
358 } {0 {test1.f1 11 test1.f1 33}}
359 do_test select1-6.1.2 {
360 set v [catch {execsql2 {SELECT f1 as 'f1' FROM test1 ORDER BY f2}} msg]
363 do_test select1-6.1.3 {
364 set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
366 } {0 {test1.f1 11 test1.f2 22}}
367 do_test select1-6.1.4 {
368 set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
369 execsql {PRAGMA full_column_names=off}
371 } {0 {test1.f1 11 test1.f2 22}}
372 do_test select1-6.1.5 {
373 set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
376 do_test select1-6.1.6 {
377 set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
380 do_test select1-6.2 {
381 set v [catch {execsql2 {SELECT f1 as xyzzy FROM test1 ORDER BY f2}} msg]
383 } {0 {xyzzy 11 xyzzy 33}}
384 do_test select1-6.3 {
385 set v [catch {execsql2 {SELECT f1 as "xyzzy" FROM test1 ORDER BY f2}} msg]
387 } {0 {xyzzy 11 xyzzy 33}}
388 do_test select1-6.3.1 {
389 set v [catch {execsql2 {SELECT f1 as 'xyzzy ' FROM test1 ORDER BY f2}} msg]
391 } {0 {{xyzzy } 11 {xyzzy } 33}}
392 do_test select1-6.4 {
393 set v [catch {execsql2 {SELECT f1+F2 as xyzzy FROM test1 ORDER BY f2}} msg]
395 } {0 {xyzzy 33 xyzzy 77}}
396 do_test select1-6.4a {
397 set v [catch {execsql2 {SELECT f1+F2 FROM test1 ORDER BY f2}} msg]
399 } {0 {f1+F2 33 f1+F2 77}}
400 do_test select1-6.5 {
401 set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
403 } {0 {test1.f1+F2 33 test1.f1+F2 77}}
404 do_test select1-6.5.1 {
405 execsql2 {PRAGMA full_column_names=on}
406 set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
407 execsql2 {PRAGMA full_column_names=off}
409 } {0 {test1.f1+F2 33 test1.f1+F2 77}}
410 do_test select1-6.6 {
411 set v [catch {execsql2 {SELECT test1.f1+F2, t1 FROM test1, test2
414 } {0 {test1.f1+F2 33 t1 abc test1.f1+F2 77 t1 abc}}
415 do_test select1-6.7 {
416 set v [catch {execsql2 {SELECT A.f1, t1 FROM test1 as A, test2
419 } {0 {A.f1 11 t1 abc A.f1 33 t1 abc}}
420 do_test select1-6.8 {
421 set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as B
424 } {1 {ambiguous column name: f1}}
425 do_test select1-6.8b {
426 set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
429 } {1 {ambiguous column name: f2}}
430 do_test select1-6.8c {
431 set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as A
434 } {1 {ambiguous column name: A.f1}}
435 do_test select1-6.9 {
436 set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
437 ORDER BY A.f1, B.f1}} msg]
439 } {0 {A.f1 11 B.f1 11 A.f1 11 B.f1 33 A.f1 33 B.f1 11 A.f1 33 B.f1 33}}
440 do_test select1-6.10 {
441 set v [catch {execsql2 {
442 SELECT f1 FROM test1 UNION SELECT f2 FROM test1
446 } {0 {f2 11 f2 22 f2 33 f2 44}}
447 do_test select1-6.11 {
448 set v [catch {execsql2 {
449 SELECT f1 FROM test1 UNION SELECT f2+100 FROM test1
453 } {0 {f2+100 11 f2+100 33 f2+100 122 f2+100 144}}
455 do_test select1-7.1 {
456 set v [catch {execsql {
457 SELECT f1 FROM test1 WHERE f2=;
460 } {1 {near ";": syntax error}}
461 do_test select1-7.2 {
462 set v [catch {execsql {
463 SELECT f1 FROM test1 UNION SELECT WHERE;
466 } {1 {near "WHERE": syntax error}}
467 do_test select1-7.3 {
468 set v [catch {execsql {SELECT f1 FROM test1 as 'hi', test2 as}} msg]
470 } {1 {near "as": syntax error}}
471 do_test select1-7.4 {
472 set v [catch {execsql {
473 SELECT f1 FROM test1 ORDER BY;
476 } {1 {near ";": syntax error}}
477 do_test select1-7.5 {
478 set v [catch {execsql {
479 SELECT f1 FROM test1 ORDER BY f1 desc, f2 where;
482 } {1 {near "where": syntax error}}
483 do_test select1-7.6 {
484 set v [catch {execsql {
485 SELECT count(f1,f2 FROM test1;
488 } {1 {near "FROM": syntax error}}
489 do_test select1-7.7 {
490 set v [catch {execsql {
491 SELECT count(f1,f2+) FROM test1;
494 } {1 {near ")": syntax error}}
495 do_test select1-7.8 {
496 set v [catch {execsql {
497 SELECT f1 FROM test1 ORDER BY f2, f1+;
500 } {1 {near ";": syntax error}}
502 do_test select1-8.1 {
503 execsql {SELECT f1 FROM test1 WHERE 4.3+2.4 OR 1 ORDER BY f1}
505 do_test select1-8.2 {
507 SELECT f1 FROM test1 WHERE ('x' || f1) BETWEEN 'x10' AND 'x20'
511 do_test select1-8.3 {
513 SELECT f1 FROM test1 WHERE 5-3==2
517 do_test select1-8.4 {
519 SELECT coalesce(f1/(f1-11),'x'),
520 coalesce(min(f1/(f1-11),5),'y'),
521 coalesce(max(f1/(f1-33),6),'z')
522 FROM test1 ORDER BY f1
525 do_test select1-8.5 {
527 SELECT min(1,2,3), -max(1,2,3)
528 FROM test1 ORDER BY f1
533 # Check the behavior when the result set is empty
535 do_test select1-9.1 {
538 db eval {SELECT * FROM test1 WHERE f1<0} r {}
541 do_test select1-9.2 {
542 execsql {PRAGMA empty_result_callbacks=on}
544 db eval {SELECT * FROM test1 WHERE f1<0} r {}
547 do_test select1-9.3 {
549 db eval {SELECT * FROM test1 WHERE f1<(select count(*) from test2)} r {}
552 do_test select1-9.4 {
554 db eval {SELECT * FROM test1 ORDER BY f1} r {}
557 do_test select1-9.5 {
559 db eval {SELECT * FROM test1 WHERE f1<0 ORDER BY f1} r {}
564 # Check for ORDER BY clauses that refer to an AS name in the column list
566 do_test select1-10.1 {
568 SELECT f1 AS x FROM test1 ORDER BY x
571 do_test select1-10.2 {
573 SELECT f1 AS x FROM test1 ORDER BY -x
576 do_test select1-10.3 {
578 SELECT f1-23 AS x FROM test1 ORDER BY abs(x)
581 do_test select1-10.4 {
583 SELECT f1-23 AS x FROM test1 ORDER BY -abs(x)
586 do_test select1-10.5 {
588 SELECT f1-22 AS x, f2-22 as y FROM test1
591 do_test select1-10.6 {
593 SELECT f1-22 AS x, f2-22 as y FROM test1 WHERE x>0 AND y<50
597 # Check the ability to specify "TABLE.*" in the result set of a SELECT
599 do_test select1-11.1 {
603 INSERT INTO t3 VALUES(1,2);
604 INSERT INTO t4 VALUES(3,4);
605 SELECT * FROM t3, t4;
608 do_test select1-11.2 {
610 SELECT * FROM t3, t4;
612 } {t3.a 1 t3.b 2 t4.a 3 t4.b 4}
613 do_test select1-11.3 {
615 SELECT * FROM t3 AS x, t4 AS y;
617 } {x.a 1 x.b 2 y.a 3 y.b 4}
618 do_test select1-11.4.1 {
620 SELECT t3.*, t4.b FROM t3, t4;
623 do_test select1-11.4.2 {
625 SELECT "t3".*, t4.b FROM t3, t4;
628 do_test select1-11.5 {
630 SELECT t3.*, t4.b FROM t3, t4;
632 } {t3.a 1 t3.b 2 t4.b 4}
633 do_test select1-11.6 {
635 SELECT x.*, y.b FROM t3 AS x, t4 AS y;
637 } {x.a 1 x.b 2 y.b 4}
638 do_test select1-11.7 {
640 SELECT t3.b, t4.* FROM t3, t4;
643 do_test select1-11.8 {
645 SELECT t3.b, t4.* FROM t3, t4;
647 } {t3.b 2 t4.a 3 t4.b 4}
648 do_test select1-11.9 {
650 SELECT x.b, y.* FROM t3 AS x, t4 AS y;
652 } {x.b 2 y.a 3 y.b 4}
653 do_test select1-11.10 {
655 SELECT t5.* FROM t3, t4;
657 } {1 {no such table: t5}}
658 do_test select1-11.11 {
660 SELECT t3.* FROM t3 AS x, t4;
662 } {1 {no such table: t3}}
663 do_test select1-11.12 {
665 SELECT t3.* FROM t3, (SELECT max(a), max(b) FROM t4)
668 do_test select1-11.13 {
670 SELECT t3.* FROM (SELECT max(a), max(b) FROM t4), t3
673 do_test select1-11.14 {
675 SELECT * FROM t3, (SELECT max(a), max(b) FROM t4) AS 'tx'
677 } {t3.a 1 t3.b 2 tx.max(a) 3 tx.max(b) 4}
678 do_test select1-11.15 {
680 SELECT y.*, t3.* FROM t3, (SELECT max(a), max(b) FROM t4) AS y
682 } {y.max(a) 3 y.max(b) 4 t3.a 1 t3.b 2}
683 do_test select1-11.16 {
685 SELECT y.* FROM t3 as y, t4 as z
689 # Tests of SELECT statements without a FROM clause.
691 do_test select1-12.1 {
696 do_test select1-12.2 {
700 } {1 1 'hello' hello 2 2}
701 do_test select1-12.3 {
703 SELECT 1 AS 'a','hello' AS 'b',2 AS 'c'
706 do_test select1-12.4 {
709 INSERT INTO t3 VALUES(1,2);
710 SELECT * FROM t3 UNION SELECT 3 AS 'a', 4 ORDER BY a;
713 do_test select1-12.5 {
715 SELECT 3, 4 UNION SELECT * FROM t3;
718 do_test select1-12.6 {
720 SELECT * FROM t3 WHERE a=(SELECT 1);
723 do_test select1-12.7 {
725 SELECT * FROM t3 WHERE a=(SELECT 2);
728 do_test select1-12.8 {
731 SELECT a,b FROM t3 UNION SELECT a AS 'x', b AS 'y' FROM t4 ORDER BY a,b
735 do_test select1-12.9 {
738 SELECT a,b FROM t3 UNION SELECT a AS 'x', b AS 'y' FROM t4 ORDER BY a,b