2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 September 15
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing the SELECT statement.
2N/A#
2N/A# $Id: select2.test,v 1.18 2002/04/02 13:26:11 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Create a table with some data
2N/A#
2N/Aexecsql {CREATE TABLE tbl1(f1 int, f2 int)}
2N/Aset f [open ./testdata1.txt w]
2N/Afor {set i 0} {$i<=30} {incr i} {
2N/A puts $f "[expr {$i%9}]\t[expr {$i%10}]"
2N/A}
2N/Aclose $f
2N/Aexecsql {COPY tbl1 FROM './testdata1.txt'}
2N/Afile delete -force ./testdata1.txt
2N/Acatch {unset data}
2N/A
2N/A# Do a second query inside a first.
2N/A#
2N/Ado_test select2-1.1 {
2N/A set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
2N/A set r {}
2N/A db eval $sql data {
2N/A set f1 $data(f1)
2N/A lappend r $f1:
2N/A set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
2N/A db eval $sql2 d2 {
2N/A lappend r $d2(f2)
2N/A }
2N/A }
2N/A set r
2N/A} {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}
2N/A
2N/Ado_test select2-1.2 {
2N/A set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
2N/A set r {}
2N/A db eval $sql data {
2N/A set f1 $data(f1)
2N/A lappend r $f1:
2N/A set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
2N/A db eval $sql2 d2 {
2N/A lappend r $d2(f2)
2N/A }
2N/A }
2N/A set r
2N/A} {4: 2 3 4}
2N/A
2N/A# Create a largish table
2N/A#
2N/Ado_test select2-2.0 {
2N/A execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int)}
2N/A set f [open ./testdata1.txt w]
2N/A for {set i 1} {$i<=30000} {incr i} {
2N/A puts $f "$i\t[expr {$i*2}]\t[expr {$i*3}]"
2N/A }
2N/A close $f
2N/A # execsql {--vdbe-trace-on--}
2N/A execsql {COPY tbl2 FROM './testdata1.txt'}
2N/A file delete -force ./testdata1.txt
2N/A} {}
2N/A
2N/Ado_test select2-2.1 {
2N/A execsql {SELECT count(*) FROM tbl2}
2N/A} {30000}
2N/Ado_test select2-2.2 {
2N/A execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
2N/A} {29500}
2N/A
2N/Ado_test select2-3.1 {
2N/A execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
2N/A} {500}
2N/A
2N/Ado_test select2-3.2a {
2N/A execsql {CREATE INDEX idx1 ON tbl2(f2)}
2N/A} {}
2N/A
2N/Ado_test select2-3.2b {
2N/A execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
2N/A} {500}
2N/Ado_test select2-3.2c {
2N/A execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
2N/A} {500}
2N/Ado_test select2-3.2d {
2N/A set sqlite_search_count 0
2N/A execsql {SELECT * FROM tbl2 WHERE 1000=f2}
2N/A set sqlite_search_count
2N/A} {3}
2N/Ado_test select2-3.2e {
2N/A set sqlite_search_count 0
2N/A execsql {SELECT * FROM tbl2 WHERE f2=1000}
2N/A set sqlite_search_count
2N/A} {3}
2N/A
2N/A# Make sure queries run faster with an index than without
2N/A#
2N/Ado_test select2-3.3 {
2N/A execsql {DROP INDEX idx1}
2N/A set sqlite_search_count 0
2N/A execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
2N/A set sqlite_search_count
2N/A} {29999}
2N/A
2N/A# Make sure we can optimize functions in the WHERE clause that
2N/A# use fields from two or more different table. (Bug #6)
2N/A#
2N/Ado_test select2-4.1 {
2N/A execsql {
2N/A CREATE TABLE aa(a);
2N/A CREATE TABLE bb(b);
2N/A INSERT INTO aa VALUES(1);
2N/A INSERT INTO aa VALUES(3);
2N/A INSERT INTO bb VALUES(2);
2N/A INSERT INTO bb VALUES(4);
2N/A SELECT * FROM aa, bb WHERE max(a,b)>2;
2N/A }
2N/A} {1 4 3 2 3 4}
2N/Ado_test select2-4.2 {
2N/A execsql {
2N/A INSERT INTO bb VALUES(0);
2N/A SELECT * FROM aa, bb WHERE b;
2N/A }
2N/A} {1 2 1 4 3 2 3 4}
2N/Ado_test select2-4.3 {
2N/A execsql {
2N/A SELECT * FROM aa, bb WHERE NOT b;
2N/A }
2N/A} {1 0 3 0}
2N/Ado_test select2-4.4 {
2N/A execsql {
2N/A SELECT * FROM aa, bb WHERE min(a,b);
2N/A }
2N/A} {1 2 1 4 3 2 3 4}
2N/Ado_test select2-4.5 {
2N/A execsql {
2N/A SELECT * FROM aa, bb WHERE NOT min(a,b);
2N/A }
2N/A} {1 0 3 0}
2N/Ado_test select2-4.6 {
2N/A execsql {
2N/A SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END;
2N/A }
2N/A} {1 2 3 4}
2N/Ado_test select2-4.7 {
2N/A execsql {
2N/A SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END;
2N/A }
2N/A} {1 4 1 0 3 2 3 0}
2N/A
2N/Afinish_test