xref: /sqlite-3.40.0/test/select2.test (revision f5bf0a78)
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.16 2001/11/23 00:24:13 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
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} {}
89
90do_test select2-3.2b {
91  execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
92} {500}
93do_test select2-3.2c {
94  execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
95} {500}
96do_test select2-3.2d {
97  set sqlite_search_count 0
98  execsql {SELECT * FROM tbl2 WHERE 1000=f2}
99  set sqlite_search_count
100} {3}
101do_test select2-3.2e {
102  set sqlite_search_count 0
103  execsql {SELECT * FROM tbl2 WHERE f2=1000}
104  set sqlite_search_count
105} {3}
106
107# Make sure queries run faster with an index than without
108#
109do_test select2-3.3 {
110  execsql {DROP INDEX idx1}
111  set sqlite_search_count 0
112  execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
113  set sqlite_search_count
114} {29999}
115
116finish_test
117