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