blob: 593b7aadc5618995cc02e5d87e0b43f244f349a7 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhbed86902000-06-02 13:27:59 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhbed86902000-06-02 13:27:59 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drhbed86902000-06-02 13:27:59 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the SELECT statement.
13#
drhdd579122002-04-02 01:58:57 +000014# $Id: select2.test,v 1.17 2002/04/02 01:58:58 drh Exp $
drhbed86902000-06-02 13:27:59 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
drhd75f54e2000-06-02 15:05:33 +000019# Create a table with some data
20#
drhbed86902000-06-02 13:27:59 +000021execsql {CREATE TABLE tbl1(f1 int, f2 int)}
drhdf16aed2000-06-02 14:27:22 +000022set f [open ./testdata1.txt w]
drhbed86902000-06-02 13:27:59 +000023for {set i 0} {$i<=30} {incr i} {
drh1b07be52000-06-02 14:38:46 +000024 puts $f "[expr {$i%9}]\t[expr {$i%10}]"
drhbed86902000-06-02 13:27:59 +000025}
drhdf16aed2000-06-02 14:27:22 +000026close $f
27execsql {COPY tbl1 FROM './testdata1.txt'}
drhd75f54e2000-06-02 15:05:33 +000028file delete -force ./testdata1.txt
drhf5bf0a72001-11-23 00:24:12 +000029catch {unset data}
drhbed86902000-06-02 13:27:59 +000030
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
drhbed86902000-06-02 13:27:59 +000047do_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
drhd75f54e2000-06-02 15:05:33 +000061# Create a largish table
62#
drh19a775c2000-06-05 18:54:46 +000063do_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
drh3fc190c2001-09-14 03:24:23 +000070 # execsql {--vdbe-trace-on--}
drh19a775c2000-06-05 18:54:46 +000071 execsql {COPY tbl2 FROM './testdata1.txt'}
72 file delete -force ./testdata1.txt
73} {}
drhd75f54e2000-06-02 15:05:33 +000074
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 {
drhe8409722000-06-08 16:54:40 +000083 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
drhd75f54e2000-06-02 15:05:33 +000084} {500}
85
drh19a775c2000-06-05 18:54:46 +000086do_test select2-3.2a {
87 execsql {CREATE INDEX idx1 ON tbl2(f2)}
88} {}
drhd75f54e2000-06-02 15:05:33 +000089
drh19a775c2000-06-05 18:54:46 +000090do_test select2-3.2b {
drhe8409722000-06-08 16:54:40 +000091 execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
drhd75f54e2000-06-02 15:05:33 +000092} {500}
drhe8409722000-06-08 16:54:40 +000093do_test select2-3.2c {
94 execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
95} {500}
96do_test select2-3.2d {
drh487ab3c2001-11-08 00:45:21 +000097 set sqlite_search_count 0
98 execsql {SELECT * FROM tbl2 WHERE 1000=f2}
99 set sqlite_search_count
100} {3}
drh767c2002000-10-19 14:10:08 +0000101do_test select2-3.2e {
drh487ab3c2001-11-08 00:45:21 +0000102 set sqlite_search_count 0
103 execsql {SELECT * FROM tbl2 WHERE f2=1000}
104 set sqlite_search_count
105} {3}
drhd75f54e2000-06-02 15:05:33 +0000106
107# Make sure queries run faster with an index than without
108#
drh487ab3c2001-11-08 00:45:21 +0000109do_test select2-3.3 {
drhd75f54e2000-06-02 15:05:33 +0000110 execsql {DROP INDEX idx1}
drh487ab3c2001-11-08 00:45:21 +0000111 set sqlite_search_count 0
112 execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
113 set sqlite_search_count
114} {29999}
drhd75f54e2000-06-02 15:05:33 +0000115
drhdd579122002-04-02 01:58:57 +0000116# Make sure we can optimize functions in the WHERE clause that
117# use fields from two or more different table. (Bug #6)
118#
119do_test select2-4.1 {
120 execsql {
121 CREATE TABLE aa(a);
122 CREATE TABLE bb(b);
123 INSERT INTO aa VALUES(1);
124 INSERT INTO aa VALUES(3);
125 INSERT INTO bb VALUES(2);
126 INSERT INTO bb VALUES(4);
127 SELECT * FROM aa, bb WHERE max(a,b)>2;
128 }
129} {1 4 3 2 3 4}
130
drhbed86902000-06-02 13:27:59 +0000131finish_test