89425d5e02
FossilOrigin-Name: b00cf110b1cc671b7200a5ce8b9e704f660763c9
510 lines
14 KiB
Plaintext
510 lines
14 KiB
Plaintext
# 2001 September 15
|
|
#
|
|
# The author disclaims copyright to this source code. In place of
|
|
# a legal notice, here is a blessing:
|
|
#
|
|
# May you do good and not evil.
|
|
# May you find forgiveness for yourself and forgive others.
|
|
# May you share freely, never taking more than you give.
|
|
#
|
|
#***********************************************************************
|
|
# This file implements regression tests for SQLite library. The
|
|
# focus of this file is testing the SELECT statement.
|
|
#
|
|
# $Id: select1.test,v 1.20 2002/02/28 03:04:48 drh Exp $
|
|
|
|
set testdir [file dirname $argv0]
|
|
source $testdir/tester.tcl
|
|
|
|
# Try to select on a non-existant table.
|
|
#
|
|
do_test select1-1.1 {
|
|
set v [catch {execsql {SELECT * FROM test1}} msg]
|
|
lappend v $msg
|
|
} {1 {no such table: test1}}
|
|
|
|
execsql {CREATE TABLE test1(f1 int, f2 int)}
|
|
|
|
do_test select1-1.2 {
|
|
set v [catch {execsql {SELECT * FROM test1, test2}} msg]
|
|
lappend v $msg
|
|
} {1 {no such table: test2}}
|
|
do_test select1-1.3 {
|
|
set v [catch {execsql {SELECT * FROM test2, test1}} msg]
|
|
lappend v $msg
|
|
} {1 {no such table: test2}}
|
|
|
|
execsql {INSERT INTO test1(f1,f2) VALUES(11,22)}
|
|
|
|
|
|
# Make sure the columns are extracted correctly.
|
|
#
|
|
do_test select1-1.4 {
|
|
execsql {SELECT f1 FROM test1}
|
|
} {11}
|
|
do_test select1-1.5 {
|
|
execsql {SELECT f2 FROM test1}
|
|
} {22}
|
|
do_test select1-1.6 {
|
|
execsql {SELECT f2, f1 FROM test1}
|
|
} {22 11}
|
|
do_test select1-1.7 {
|
|
execsql {SELECT f1, f2 FROM test1}
|
|
} {11 22}
|
|
do_test select1-1.8 {
|
|
execsql {SELECT * FROM test1}
|
|
} {11 22}
|
|
do_test select1-1.8.1 {
|
|
execsql {SELECT *, * FROM test1}
|
|
} {11 22 11 22}
|
|
do_test select1-1.8.2 {
|
|
execsql {SELECT *, min(f1,f2), max(f1,f2) FROM test1}
|
|
} {11 22 11 22}
|
|
do_test select1-1.8.3 {
|
|
execsql {SELECT 'one', *, 'two', * FROM test1}
|
|
} {one 11 22 two 11 22}
|
|
|
|
execsql {CREATE TABLE test2(r1 real, r2 real)}
|
|
execsql {INSERT INTO test2(r1,r2) VALUES(1.1,2.2)}
|
|
|
|
do_test select1-1.9 {
|
|
execsql {SELECT * FROM test1, test2}
|
|
} {11 22 1.1 2.2}
|
|
do_test select1-1.9.1 {
|
|
execsql {SELECT *, 'hi' FROM test1, test2}
|
|
} {11 22 1.1 2.2 hi}
|
|
do_test select1-1.9.2 {
|
|
execsql {SELECT 'one', *, 'two', * FROM test1, test2}
|
|
} {one 11 22 1.1 2.2 two 11 22 1.1 2.2}
|
|
do_test select1-1.10 {
|
|
execsql {SELECT test1.f1, test2.r1 FROM test1, test2}
|
|
} {11 1.1}
|
|
do_test select1-1.11 {
|
|
execsql {SELECT test1.f1, test2.r1 FROM test2, test1}
|
|
} {11 1.1}
|
|
do_test select1-1.11.1 {
|
|
execsql {SELECT * FROM test2, test1}
|
|
} {1.1 2.2 11 22}
|
|
do_test select1-1.11.2 {
|
|
execsql {SELECT * FROM test1 AS a, test1 AS b}
|
|
} {11 22 11 22}
|
|
do_test select1-1.12 {
|
|
execsql {SELECT max(test1.f1,test2.r1), min(test1.f2,test2.r2)
|
|
FROM test2, test1}
|
|
} {11 2.2}
|
|
do_test select1-1.13 {
|
|
execsql {SELECT min(test1.f1,test2.r1), max(test1.f2,test2.r2)
|
|
FROM test1, test2}
|
|
} {1.1 22}
|
|
|
|
execsql {DROP TABLE test2}
|
|
execsql {DELETE FROM test1}
|
|
execsql {INSERT INTO test1 VALUES(11,22)}
|
|
execsql {INSERT INTO test1 VALUES(33,44)}
|
|
|
|
# Error messges from sqliteExprCheck
|
|
#
|
|
do_test select1-2.1 {
|
|
set v [catch {execsql {SELECT count(f1,f2) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {1 {wrong number of arguments to function count()}}
|
|
do_test select1-2.2 {
|
|
set v [catch {execsql {SELECT count(f1) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 2}
|
|
do_test select1-2.3 {
|
|
set v [catch {execsql {SELECT Count() FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 2}
|
|
do_test select1-2.4 {
|
|
set v [catch {execsql {SELECT COUNT(*) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 2}
|
|
do_test select1-2.5 {
|
|
set v [catch {execsql {SELECT COUNT(*)+1 FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 3}
|
|
do_test select1-2.6 {
|
|
set v [catch {execsql {SELECT min(*) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 {{} {}}}
|
|
do_test select1-2.7 {
|
|
set v [catch {execsql {SELECT Min(f1) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 11}
|
|
do_test select1-2.8 {
|
|
set v [catch {execsql {SELECT MIN(f1,f2) FROM test1}} msg]
|
|
lappend v [lsort $msg]
|
|
} {0 {11 33}}
|
|
do_test select1-2.9 {
|
|
set v [catch {execsql {SELECT MAX(*) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 {{} {}}}
|
|
do_test select1-2.10 {
|
|
set v [catch {execsql {SELECT Max(f1) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 33}
|
|
do_test select1-2.11 {
|
|
set v [catch {execsql {SELECT max(f1,f2) FROM test1}} msg]
|
|
lappend v [lsort $msg]
|
|
} {0 {22 44}}
|
|
do_test select1-2.12 {
|
|
set v [catch {execsql {SELECT MAX(f1,f2)+1 FROM test1}} msg]
|
|
lappend v [lsort $msg]
|
|
} {0 {23 45}}
|
|
do_test select1-2.13 {
|
|
set v [catch {execsql {SELECT MAX(f1)+1 FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 34}
|
|
do_test select1-2.14 {
|
|
set v [catch {execsql {SELECT SUM(*) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {1 {wrong number of arguments to function SUM()}}
|
|
do_test select1-2.15 {
|
|
set v [catch {execsql {SELECT Sum(f1) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 44}
|
|
do_test select1-2.16 {
|
|
set v [catch {execsql {SELECT sum(f1,f2) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {1 {wrong number of arguments to function sum()}}
|
|
do_test select1-2.17 {
|
|
set v [catch {execsql {SELECT SUM(f1)+1 FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 45}
|
|
do_test select1-2.18 {
|
|
set v [catch {execsql {SELECT XYZZY(f1) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {1 {no such function: XYZZY}}
|
|
do_test select1-2.19 {
|
|
set v [catch {execsql {SELECT SUM(min(f1,f2)) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {0 44}
|
|
do_test select1-2.20 {
|
|
set v [catch {execsql {SELECT SUM(min(f1)) FROM test1}} msg]
|
|
lappend v $msg
|
|
} {1 {misuse of aggregate function min()}}
|
|
|
|
# WHERE clause expressions
|
|
#
|
|
do_test select1-3.1 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<11}} msg]
|
|
lappend v $msg
|
|
} {0 {}}
|
|
do_test select1-3.2 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<=11}} msg]
|
|
lappend v $msg
|
|
} {0 11}
|
|
do_test select1-3.3 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 WHERE f1=11}} msg]
|
|
lappend v $msg
|
|
} {0 11}
|
|
do_test select1-3.4 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>=11}} msg]
|
|
lappend v [lsort $msg]
|
|
} {0 {11 33}}
|
|
do_test select1-3.5 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>11}} msg]
|
|
lappend v [lsort $msg]
|
|
} {0 33}
|
|
do_test select1-3.6 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 WHERE f1!=11}} msg]
|
|
lappend v [lsort $msg]
|
|
} {0 33}
|
|
do_test select1-3.7 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 WHERE min(f1,f2)!=11}} msg]
|
|
lappend v [lsort $msg]
|
|
} {0 33}
|
|
do_test select1-3.8 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 WHERE max(f1,f2)!=11}} msg]
|
|
lappend v [lsort $msg]
|
|
} {0 {11 33}}
|
|
do_test select1-3.9 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 WHERE count(f1,f2)!=11}} msg]
|
|
lappend v $msg
|
|
} {1 {wrong number of arguments to function count()}}
|
|
|
|
# ORDER BY expressions
|
|
#
|
|
do_test select1-4.1 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 ORDER BY f1}} msg]
|
|
lappend v $msg
|
|
} {0 {11 33}}
|
|
do_test select1-4.2 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 ORDER BY -f1}} msg]
|
|
lappend v $msg
|
|
} {0 {33 11}}
|
|
do_test select1-4.3 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1,f2)}} msg]
|
|
lappend v $msg
|
|
} {0 {11 33}}
|
|
do_test select1-4.4 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1)}} msg]
|
|
lappend v $msg
|
|
} {1 {misuse of aggregate function min()}}
|
|
do_test select1-4.5 {
|
|
catchsql {
|
|
SELECT f1 FROM test1 ORDER BY 8.4;
|
|
}
|
|
} {1 {ORDER BY expressions should not be constant}}
|
|
|
|
# ORDER BY ignored on an aggregate query
|
|
#
|
|
do_test select1-5.1 {
|
|
set v [catch {execsql {SELECT max(f1) FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 33}
|
|
|
|
execsql {CREATE TABLE test2(t1 test, t2 text)}
|
|
execsql {INSERT INTO test2 VALUES('abc','xyz')}
|
|
|
|
# Check for column naming
|
|
#
|
|
do_test select1-6.1 {
|
|
set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {f1 11 f1 33}}
|
|
do_test select1-6.1.1 {
|
|
execsql {PRAGMA full_column_names=on}
|
|
set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {test1.f1 11 test1.f1 33}}
|
|
do_test select1-6.1.2 {
|
|
set v [catch {execsql2 {SELECT f1 as 'f1' FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {f1 11 f1 33}}
|
|
do_test select1-6.1.3 {
|
|
set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
|
|
lappend v $msg
|
|
} {0 {test1.f1 11 test1.f2 22}}
|
|
do_test select1-6.1.4 {
|
|
set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
|
|
execsql {PRAGMA full_column_names=off}
|
|
lappend v $msg
|
|
} {0 {test1.f1 11 test1.f2 22}}
|
|
do_test select1-6.1.5 {
|
|
set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
|
|
lappend v $msg
|
|
} {0 {f1 11 f2 22}}
|
|
do_test select1-6.1.6 {
|
|
set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
|
|
lappend v $msg
|
|
} {0 {f1 11 f2 22}}
|
|
do_test select1-6.2 {
|
|
set v [catch {execsql2 {SELECT f1 as xyzzy FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {xyzzy 11 xyzzy 33}}
|
|
do_test select1-6.3 {
|
|
set v [catch {execsql2 {SELECT f1 as "xyzzy" FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {xyzzy 11 xyzzy 33}}
|
|
do_test select1-6.3.1 {
|
|
set v [catch {execsql2 {SELECT f1 as 'xyzzy ' FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {{xyzzy } 11 {xyzzy } 33}}
|
|
do_test select1-6.4 {
|
|
set v [catch {execsql2 {SELECT f1+F2 as xyzzy FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {xyzzy 33 xyzzy 77}}
|
|
do_test select1-6.4a {
|
|
set v [catch {execsql2 {SELECT f1+F2 FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {f1+F2 33 f1+F2 77}}
|
|
do_test select1-6.5 {
|
|
set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {test1.f1+F2 33 test1.f1+F2 77}}
|
|
do_test select1-6.5.1 {
|
|
execsql2 {PRAGMA full_column_names=on}
|
|
set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
|
|
execsql2 {PRAGMA full_column_names=off}
|
|
lappend v $msg
|
|
} {0 {test1.f1+F2 33 test1.f1+F2 77}}
|
|
do_test select1-6.6 {
|
|
set v [catch {execsql2 {SELECT test1.f1+F2, t1 FROM test1, test2
|
|
ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {test1.f1+F2 33 t1 abc test1.f1+F2 77 t1 abc}}
|
|
do_test select1-6.7 {
|
|
set v [catch {execsql2 {SELECT A.f1, t1 FROM test1 as A, test2
|
|
ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {0 {A.f1 11 t1 abc A.f1 33 t1 abc}}
|
|
do_test select1-6.8 {
|
|
set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as B
|
|
ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {1 {ambiguous column name: f1}}
|
|
do_test select1-6.8b {
|
|
set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
|
|
ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {1 {ambiguous column name: f2}}
|
|
do_test select1-6.8c {
|
|
set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as A
|
|
ORDER BY f2}} msg]
|
|
lappend v $msg
|
|
} {1 {ambiguous column name: A.f1}}
|
|
do_test select1-6.9 {
|
|
set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
|
|
ORDER BY A.f1, B.f1}} msg]
|
|
lappend v $msg
|
|
} {0 {A.f1 11 B.f1 11 A.f1 11 B.f1 33 A.f1 33 B.f1 11 A.f1 33 B.f1 33}}
|
|
do_test select1-6.10 {
|
|
set v [catch {execsql2 {
|
|
SELECT f1 FROM test1 UNION SELECT f2 FROM test1
|
|
ORDER BY f2;
|
|
}} msg]
|
|
lappend v $msg
|
|
} {0 {f2 11 f2 22 f2 33 f2 44}}
|
|
do_test select1-6.11 {
|
|
set v [catch {execsql2 {
|
|
SELECT f1 FROM test1 UNION SELECT f2+100 FROM test1
|
|
ORDER BY f2+100;
|
|
}} msg]
|
|
lappend v $msg
|
|
} {0 {f2+100 11 f2+100 33 f2+100 122 f2+100 144}}
|
|
|
|
do_test select1-7.1 {
|
|
set v [catch {execsql {
|
|
SELECT f1 FROM test1 WHERE f2=;
|
|
}} msg]
|
|
lappend v $msg
|
|
} {1 {near ";": syntax error}}
|
|
do_test select1-7.2 {
|
|
set v [catch {execsql {
|
|
SELECT f1 FROM test1 UNION SELECT WHERE;
|
|
}} msg]
|
|
lappend v $msg
|
|
} {1 {near "WHERE": syntax error}}
|
|
do_test select1-7.3 {
|
|
set v [catch {execsql {SELECT f1 FROM test1 as 'hi', test2 as}} msg]
|
|
lappend v $msg
|
|
} {1 {near "as": syntax error}}
|
|
do_test select1-7.4 {
|
|
set v [catch {execsql {
|
|
SELECT f1 FROM test1 ORDER BY;
|
|
}} msg]
|
|
lappend v $msg
|
|
} {1 {near ";": syntax error}}
|
|
do_test select1-7.5 {
|
|
set v [catch {execsql {
|
|
SELECT f1 FROM test1 ORDER BY f1 desc, f2 where;
|
|
}} msg]
|
|
lappend v $msg
|
|
} {1 {near "where": syntax error}}
|
|
do_test select1-7.6 {
|
|
set v [catch {execsql {
|
|
SELECT count(f1,f2 FROM test1;
|
|
}} msg]
|
|
lappend v $msg
|
|
} {1 {near "FROM": syntax error}}
|
|
do_test select1-7.7 {
|
|
set v [catch {execsql {
|
|
SELECT count(f1,f2+) FROM test1;
|
|
}} msg]
|
|
lappend v $msg
|
|
} {1 {near ")": syntax error}}
|
|
do_test select1-7.8 {
|
|
set v [catch {execsql {
|
|
SELECT f1 FROM test1 ORDER BY f2, f1+;
|
|
}} msg]
|
|
lappend v $msg
|
|
} {1 {near ";": syntax error}}
|
|
|
|
do_test select1-8.1 {
|
|
execsql {SELECT f1 FROM test1 WHERE 4.3+2.4 OR 1 ORDER BY f1}
|
|
} {11 33}
|
|
do_test select1-8.2 {
|
|
execsql {
|
|
SELECT f1 FROM test1 WHERE ('x' || f1) BETWEEN 'x10' AND 'x20'
|
|
ORDER BY f1
|
|
}
|
|
} {11}
|
|
do_test select1-8.3 {
|
|
execsql {
|
|
SELECT f1 FROM test1 WHERE 5-3==2
|
|
ORDER BY f1
|
|
}
|
|
} {11 33}
|
|
do_test select1-8.4 {
|
|
execsql {
|
|
SELECT f1/(f1-11), min(f1/(f1-11),5), max(f1/(f1-33),6)
|
|
FROM test1 ORDER BY f1
|
|
}
|
|
} {{} {} 6 1.5 1.5 6}
|
|
do_test select1-8.5 {
|
|
execsql {
|
|
SELECT min(1,2,3), -max(1,2,3)
|
|
FROM test1 ORDER BY f1
|
|
}
|
|
} {1 -3 1 -3}
|
|
|
|
|
|
# Check the behavior when the result set is empty
|
|
#
|
|
do_test select1-9.1 {
|
|
catch {unset r}
|
|
set r(*) {}
|
|
db eval {SELECT * FROM test1 WHERE f1<0} r {}
|
|
set r(*)
|
|
} {}
|
|
do_test select1-9.2 {
|
|
execsql {PRAGMA empty_result_callbacks=on}
|
|
set r(*) {}
|
|
db eval {SELECT * FROM test1 WHERE f1<0} r {}
|
|
set r(*)
|
|
} {f1 f2}
|
|
do_test select1-9.3 {
|
|
set r(*) {}
|
|
db eval {SELECT * FROM test1 WHERE f1<(select count(*) from test2)} r {}
|
|
set r(*)
|
|
} {f1 f2}
|
|
do_test select1-9.4 {
|
|
set r(*) {}
|
|
db eval {SELECT * FROM test1 ORDER BY f1} r {}
|
|
set r(*)
|
|
} {f1 f2}
|
|
do_test select1-9.5 {
|
|
set r(*) {}
|
|
db eval {SELECT * FROM test1 WHERE f1<0 ORDER BY f1} r {}
|
|
set r(*)
|
|
} {f1 f2}
|
|
unset r
|
|
|
|
# Check for ORDER BY clauses that refer to an AS name in the column list
|
|
#
|
|
do_test select1-10.1 {
|
|
execsql {
|
|
SELECT f1 AS x FROM test1 ORDER BY x
|
|
}
|
|
} {11 33}
|
|
do_test select1-10.2 {
|
|
execsql {
|
|
SELECT f1 AS x FROM test1 ORDER BY -x
|
|
}
|
|
} {33 11}
|
|
do_test select1-10.3 {
|
|
execsql {
|
|
SELECT f1-23 AS x FROM test1 ORDER BY abs(x)
|
|
}
|
|
} {10 -12}
|
|
do_test select1-10.4 {
|
|
execsql {
|
|
SELECT f1-23 AS x FROM test1 ORDER BY -abs(x)
|
|
}
|
|
} {-12 10}
|
|
do_test select1-10.5 {
|
|
execsql {
|
|
SELECT f1-22 AS x, f2-22 as y FROM test1
|
|
}
|
|
} {-11 0 11 22}
|
|
do_test select1-10.6 {
|
|
execsql {
|
|
SELECT f1-22 AS x, f2-22 as y FROM test1 WHERE x>0 AND y<50
|
|
}
|
|
} {11 22}
|
|
|
|
|
|
finish_test
|