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