xref: /onnv-gate/usr/src/lib/libsqlite/test/subselect.test (revision 4520:7dbeadedd7fe)
1*4520Snw141292
2*4520Snw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*4520Snw141292
4*4520Snw141292# 2001 September 15
5*4520Snw141292#
6*4520Snw141292# The author disclaims copyright to this source code.  In place of
7*4520Snw141292# a legal notice, here is a blessing:
8*4520Snw141292#
9*4520Snw141292#    May you do good and not evil.
10*4520Snw141292#    May you find forgiveness for yourself and forgive others.
11*4520Snw141292#    May you share freely, never taking more than you give.
12*4520Snw141292#
13*4520Snw141292#***********************************************************************
14*4520Snw141292# This file implements regression tests for SQLite library.  The
15*4520Snw141292# focus of this file is testing SELECT statements that are part of
16*4520Snw141292# expressions.
17*4520Snw141292#
18*4520Snw141292# $Id: subselect.test,v 1.7 2002/07/15 18:55:26 drh Exp $
19*4520Snw141292
20*4520Snw141292set testdir [file dirname $argv0]
21*4520Snw141292source $testdir/tester.tcl
22*4520Snw141292
23*4520Snw141292# Basic sanity checking.  Try a simple subselect.
24*4520Snw141292#
25*4520Snw141292do_test subselect-1.1 {
26*4520Snw141292  execsql {
27*4520Snw141292    CREATE TABLE t1(a int, b int);
28*4520Snw141292    INSERT INTO t1 VALUES(1,2);
29*4520Snw141292    INSERT INTO t1 VALUES(3,4);
30*4520Snw141292    INSERT INTO t1 VALUES(5,6);
31*4520Snw141292  }
32*4520Snw141292  execsql {SELECT * FROM t1 WHERE a = (SELECT count(*) FROM t1)}
33*4520Snw141292} {3 4}
34*4520Snw141292
35*4520Snw141292# Try a select with more than one result column.
36*4520Snw141292#
37*4520Snw141292do_test subselect-1.2 {
38*4520Snw141292  set v [catch {execsql {SELECT * FROM t1 WHERE a = (SELECT * FROM t1)}} msg]
39*4520Snw141292  lappend v $msg
40*4520Snw141292} {1 {only a single result allowed for a SELECT that is part of an expression}}
41*4520Snw141292
42*4520Snw141292# A subselect without an aggregate.
43*4520Snw141292#
44*4520Snw141292do_test subselect-1.3a {
45*4520Snw141292  execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=2)}
46*4520Snw141292} {2}
47*4520Snw141292do_test subselect-1.3b {
48*4520Snw141292  execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=4)}
49*4520Snw141292} {4}
50*4520Snw141292do_test subselect-1.3c {
51*4520Snw141292  execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=6)}
52*4520Snw141292} {6}
53*4520Snw141292do_test subselect-1.3c {
54*4520Snw141292  execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=8)}
55*4520Snw141292} {}
56*4520Snw141292
57*4520Snw141292# What if the subselect doesn't return any value.  We should get
58*4520Snw141292# NULL as the result.  Check it out.
59*4520Snw141292#
60*4520Snw141292do_test subselect-1.4 {
61*4520Snw141292  execsql {SELECT b from t1 where a = coalesce((SELECT a FROM t1 WHERE b=5),1)}
62*4520Snw141292} {2}
63*4520Snw141292
64*4520Snw141292# Try multiple subselects within a single expression.
65*4520Snw141292#
66*4520Snw141292do_test subselect-1.5 {
67*4520Snw141292  execsql {
68*4520Snw141292    CREATE TABLE t2(x int, y int);
69*4520Snw141292    INSERT INTO t2 VALUES(1,2);
70*4520Snw141292    INSERT INTO t2 VALUES(2,4);
71*4520Snw141292    INSERT INTO t2 VALUES(3,8);
72*4520Snw141292    INSERT INTO t2 VALUES(4,16);
73*4520Snw141292  }
74*4520Snw141292  execsql {
75*4520Snw141292    SELECT y from t2
76*4520Snw141292    WHERE x = (SELECT sum(b) FROM t1 where a notnull) - (SELECT sum(a) FROM t1)
77*4520Snw141292  }
78*4520Snw141292} {8}
79*4520Snw141292
80*4520Snw141292# Try something useful.  Delete every entry from t2 where the
81*4520Snw141292# x value is less than half of the maximum.
82*4520Snw141292#
83*4520Snw141292do_test subselect-1.6 {
84*4520Snw141292  execsql {DELETE FROM t2 WHERE x < 0.5*(SELECT max(x) FROM t2)}
85*4520Snw141292  execsql {SELECT x FROM t2 ORDER BY x}
86*4520Snw141292} {2 3 4}
87*4520Snw141292
88*4520Snw141292# Make sure sorting works for SELECTs there used as a scalar expression.
89*4520Snw141292#
90*4520Snw141292do_test subselect-2.1 {
91*4520Snw141292  execsql {
92*4520Snw141292    SELECT (SELECT a FROM t1 ORDER BY a), (SELECT a FROM t1 ORDER BY a DESC)
93*4520Snw141292  }
94*4520Snw141292} {1 5}
95*4520Snw141292do_test subselect-2.2 {
96*4520Snw141292  execsql {
97*4520Snw141292    SELECT 1 IN (SELECT a FROM t1 ORDER BY a);
98*4520Snw141292  }
99*4520Snw141292} {1}
100*4520Snw141292do_test subselect-2.3 {
101*4520Snw141292  execsql {
102*4520Snw141292    SELECT 2 IN (SELECT a FROM t1 ORDER BY a DESC);
103*4520Snw141292  }
104*4520Snw141292} {0}
105*4520Snw141292
106*4520Snw141292# Verify that the ORDER BY clause is honored in a subquery.
107*4520Snw141292#
108*4520Snw141292do_test subselect-3.1 {
109*4520Snw141292  execsql {
110*4520Snw141292    CREATE TABLE t3(x int);
111*4520Snw141292    INSERT INTO t3 SELECT a FROM t1 UNION ALL SELECT b FROM t1;
112*4520Snw141292    SELECT * FROM t3 ORDER BY x;
113*4520Snw141292  }
114*4520Snw141292} {1 2 3 4 5 6}
115*4520Snw141292do_test subselect-3.2 {
116*4520Snw141292  execsql {
117*4520Snw141292    SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x LIMIT 2);
118*4520Snw141292  }
119*4520Snw141292} {3}
120*4520Snw141292do_test subselect-3.3 {
121*4520Snw141292  execsql {
122*4520Snw141292    SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x DESC LIMIT 2);
123*4520Snw141292  }
124*4520Snw141292} {11}
125*4520Snw141292do_test subselect-3.4 {
126*4520Snw141292  execsql {
127*4520Snw141292    SELECT (SELECT x FROM t3 ORDER BY x);
128*4520Snw141292  }
129*4520Snw141292} {1}
130*4520Snw141292do_test subselect-3.5 {
131*4520Snw141292  execsql {
132*4520Snw141292    SELECT (SELECT x FROM t3 ORDER BY x DESC);
133*4520Snw141292  }
134*4520Snw141292} {6}
135*4520Snw141292do_test subselect-3.6 {
136*4520Snw141292  execsql {
137*4520Snw141292    SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1);
138*4520Snw141292  }
139*4520Snw141292} {1}
140*4520Snw141292do_test subselect-3.7 {
141*4520Snw141292  execsql {
142*4520Snw141292    SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1);
143*4520Snw141292  }
144*4520Snw141292} {6}
145*4520Snw141292do_test subselect-3.8 {
146*4520Snw141292  execsql {
147*4520Snw141292    SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1 OFFSET 2);
148*4520Snw141292  }
149*4520Snw141292} {3}
150*4520Snw141292do_test subselect-3.9 {
151*4520Snw141292  execsql {
152*4520Snw141292    SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);
153*4520Snw141292  }
154*4520Snw141292} {4}
155*4520Snw141292
156*4520Snw141292
157*4520Snw141292
158*4520Snw141292finish_test
159