xref: /sqlite-3.40.0/test/select2.test (revision 487ab3ca)
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.15 2001/11/08 00:45:22 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)}
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
29
30# Do a second query inside a first.
31#
32do_test select2-1.1 {
33  set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
34  set r {}
35  db eval $sql data {
36    set f1 $data(f1)
37    lappend r $f1:
38    set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
39    db eval $sql2 d2 {
40      lappend r $d2(f2)
41    }
42  }
43  set r
44} {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}
45
46do_test select2-1.2 {
47  set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
48  set r {}
49  db eval $sql data {
50    set f1 $data(f1)
51    lappend r $f1:
52    set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
53    db eval $sql2 d2 {
54      lappend r $d2(f2)
55    }
56  }
57  set r
58} {4: 2 3 4}
59
60# Create a largish table
61#
62do_test select2-2.0 {
63  execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int)}
64  set f [open ./testdata1.txt w]
65  for {set i 1} {$i<=30000} {incr i} {
66    puts $f "$i\t[expr {$i*2}]\t[expr {$i*3}]"
67  }
68  close $f
69  # execsql {--vdbe-trace-on--}
70  execsql {COPY tbl2 FROM './testdata1.txt'}
71  file delete -force ./testdata1.txt
72} {}
73
74do_test select2-2.1 {
75  execsql {SELECT count(*) FROM tbl2}
76} {30000}
77do_test select2-2.2 {
78  execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
79} {29500}
80
81do_test select2-3.1 {
82  execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
83} {500}
84
85do_test select2-3.2a {
86  execsql {CREATE INDEX idx1 ON tbl2(f2)}
87} {}
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
115finish_test
116