xref: /sqlite-3.40.0/test/select2.test (revision 5f3b4ab5)
1# 2001 September 15
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 of this file is testing the SELECT statement.
13#
14# $Id: select2.test,v 1.21 2004/05/27 17:22:56 drh Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Create a table with some data
20#
21execsql {CREATE TABLE tbl1(f1 int, f2 int)}
22execsql {BEGIN}
23for {set i 0} {$i<=30} {incr i} {
24  execsql "INSERT INTO tbl1 VALUES([expr {$i%9}],[expr {$i%10}])"
25}
26execsql {COMMIT}
27
28# Do a second query inside a first.
29#
30do_test select2-1.1 {
31  set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
32  set r {}
33  db eval $sql data {
34    set f1 $data(f1)
35    lappend r $f1:
36    set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
37    db eval $sql2 d2 {
38      lappend r $d2(f2)
39    }
40  }
41  set r
42} {0: 0 7 8 9 1: 0 1 8 9 2: 0 1 2 9 3: 0 1 2 3 4: 2 3 4 5: 3 4 5 6: 4 5 6 7: 5 6 7 8: 6 7 8}
43
44do_test select2-1.2 {
45  set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
46  set r {}
47  db eval $sql data {
48    set f1 $data(f1)
49    lappend r $f1:
50    set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
51    db eval $sql2 d2 {
52      lappend r $d2(f2)
53    }
54  }
55  set r
56} {4: 2 3 4}
57
58# Create a largish table
59#
60do_test select2-2.0 {
61  execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;}
62  for {set i 1} {$i<=30000} {incr i} {
63    execsql "INSERT INTO tbl2 VALUES($i,[expr {$i*2}],[expr {$i*3}])"
64  }
65  execsql {COMMIT}
66} {}
67
68do_test select2-2.1 {
69  execsql {SELECT count(*) FROM tbl2}
70} {30000}
71do_test select2-2.2 {
72  execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
73} {29500}
74
75do_test select2-3.1 {
76  execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
77} {500}
78
79do_test select2-3.2a {
80  execsql {CREATE INDEX idx1 ON tbl2(f2)}
81} {}
82do_test select2-3.2b {
83  execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
84} {500}
85do_test select2-3.2c {
86  execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
87} {500}
88do_test select2-3.2d {
89  set sqlite_search_count 0
90  execsql {SELECT * FROM tbl2 WHERE 1000=f2}
91  set sqlite_search_count
92} {3}
93do_test select2-3.2e {
94  set sqlite_search_count 0
95  execsql {SELECT * FROM tbl2 WHERE f2=1000}
96  set sqlite_search_count
97} {3}
98
99# Make sure queries run faster with an index than without
100#
101do_test select2-3.3 {
102  execsql {DROP INDEX idx1}
103  set sqlite_search_count 0
104  execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
105  set sqlite_search_count
106} {29999}
107
108# Make sure we can optimize functions in the WHERE clause that
109# use fields from two or more different table.  (Bug #6)
110#
111do_test select2-4.1 {
112  execsql {
113    CREATE TABLE aa(a);
114    CREATE TABLE bb(b);
115    INSERT INTO aa VALUES(1);
116    INSERT INTO aa VALUES(3);
117    INSERT INTO bb VALUES(2);
118    INSERT INTO bb VALUES(4);
119    SELECT * FROM aa, bb WHERE max(a,b)>2;
120  }
121} {1 4 3 2 3 4}
122do_test select2-4.2 {
123  execsql {
124    INSERT INTO bb VALUES(0);
125    SELECT * FROM aa, bb WHERE b;
126  }
127} {1 2 1 4 3 2 3 4}
128do_test select2-4.3 {
129  execsql {
130    SELECT * FROM aa, bb WHERE NOT b;
131  }
132} {1 0 3 0}
133do_test select2-4.4 {
134  execsql {
135    SELECT * FROM aa, bb WHERE min(a,b);
136  }
137} {1 2 1 4 3 2 3 4}
138do_test select2-4.5 {
139  execsql {
140    SELECT * FROM aa, bb WHERE NOT min(a,b);
141  }
142} {1 0 3 0}
143do_test select2-4.6 {
144  execsql {
145    SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END;
146  }
147} {1 2 3 4}
148do_test select2-4.7 {
149  execsql {
150    SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END;
151  }
152} {1 4 1 0 3 2 3 0}
153
154finish_test
155