xref: /illumos-gate/usr/src/lib/libsqlite/test/select2.test (revision e3ae4b35c024af1196582063ecee3ab79367227d)
1#
2# 2001 September 15
3#
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
7#    May you do good and not evil.
8#    May you find forgiveness for yourself and forgive others.
9#    May you share freely, never taking more than you give.
10#
11#***********************************************************************
12# This file implements regression tests for SQLite library.  The
13# focus of this file is testing the SELECT statement.
14#
15# $Id: select2.test,v 1.18 2002/04/02 13:26:11 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Create a table with some data
21#
22execsql {CREATE TABLE tbl1(f1 int, f2 int)}
23set f [open ./testdata1.txt w]
24for {set i 0} {$i<=30} {incr i} {
25  puts $f "[expr {$i%9}]\t[expr {$i%10}]"
26}
27close $f
28execsql {COPY tbl1 FROM './testdata1.txt'}
29file delete -force ./testdata1.txt
30catch {unset data}
31
32# Do a second query inside a first.
33#
34do_test select2-1.1 {
35  set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
36  set r {}
37  db eval $sql data {
38    set f1 $data(f1)
39    lappend r $f1:
40    set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
41    db eval $sql2 d2 {
42      lappend r $d2(f2)
43    }
44  }
45  set r
46} {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}
47
48do_test select2-1.2 {
49  set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
50  set r {}
51  db eval $sql data {
52    set f1 $data(f1)
53    lappend r $f1:
54    set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
55    db eval $sql2 d2 {
56      lappend r $d2(f2)
57    }
58  }
59  set r
60} {4: 2 3 4}
61
62# Create a largish table
63#
64do_test select2-2.0 {
65  execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int)}
66  set f [open ./testdata1.txt w]
67  for {set i 1} {$i<=30000} {incr i} {
68    puts $f "$i\t[expr {$i*2}]\t[expr {$i*3}]"
69  }
70  close $f
71  # execsql {--vdbe-trace-on--}
72  execsql {COPY tbl2 FROM './testdata1.txt'}
73  file delete -force ./testdata1.txt
74} {}
75
76do_test select2-2.1 {
77  execsql {SELECT count(*) FROM tbl2}
78} {30000}
79do_test select2-2.2 {
80  execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
81} {29500}
82
83do_test select2-3.1 {
84  execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
85} {500}
86
87do_test select2-3.2a {
88  execsql {CREATE INDEX idx1 ON tbl2(f2)}
89} {}
90
91do_test select2-3.2b {
92  execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
93} {500}
94do_test select2-3.2c {
95  execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
96} {500}
97do_test select2-3.2d {
98  set sqlite_search_count 0
99  execsql {SELECT * FROM tbl2 WHERE 1000=f2}
100  set sqlite_search_count
101} {3}
102do_test select2-3.2e {
103  set sqlite_search_count 0
104  execsql {SELECT * FROM tbl2 WHERE f2=1000}
105  set sqlite_search_count
106} {3}
107
108# Make sure queries run faster with an index than without
109#
110do_test select2-3.3 {
111  execsql {DROP INDEX idx1}
112  set sqlite_search_count 0
113  execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
114  set sqlite_search_count
115} {29999}
116
117# Make sure we can optimize functions in the WHERE clause that
118# use fields from two or more different table.  (Bug #6)
119#
120do_test select2-4.1 {
121  execsql {
122    CREATE TABLE aa(a);
123    CREATE TABLE bb(b);
124    INSERT INTO aa VALUES(1);
125    INSERT INTO aa VALUES(3);
126    INSERT INTO bb VALUES(2);
127    INSERT INTO bb VALUES(4);
128    SELECT * FROM aa, bb WHERE max(a,b)>2;
129  }
130} {1 4 3 2 3 4}
131do_test select2-4.2 {
132  execsql {
133    INSERT INTO bb VALUES(0);
134    SELECT * FROM aa, bb WHERE b;
135  }
136} {1 2 1 4 3 2 3 4}
137do_test select2-4.3 {
138  execsql {
139    SELECT * FROM aa, bb WHERE NOT b;
140  }
141} {1 0 3 0}
142do_test select2-4.4 {
143  execsql {
144    SELECT * FROM aa, bb WHERE min(a,b);
145  }
146} {1 2 1 4 3 2 3 4}
147do_test select2-4.5 {
148  execsql {
149    SELECT * FROM aa, bb WHERE NOT min(a,b);
150  }
151} {1 0 3 0}
152do_test select2-4.6 {
153  execsql {
154    SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END;
155  }
156} {1 2 3 4}
157do_test select2-4.7 {
158  execsql {
159    SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END;
160  }
161} {1 4 1 0 3 2 3 0}
162
163finish_test
164