2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
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# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing SELECT statements that are part of
2N/Aset testdir [file dirname $argv0]
2N/A# Basic sanity checking. Try a simple subselect.
2N/Ado_test subselect-1.1 {
2N/A CREATE TABLE t1(a int, b int);
2N/A INSERT INTO t1 VALUES(1,2);
2N/A INSERT INTO t1 VALUES(3,4);
2N/A INSERT INTO t1 VALUES(5,6);
2N/A execsql {SELECT * FROM t1 WHERE a = (SELECT count(*) FROM t1)}
2N/A# Try a select with more than one result column.
2N/Ado_test subselect-1.2 {
2N/A set v [catch {execsql {SELECT * FROM t1 WHERE a = (SELECT * FROM t1)}} msg]
2N/A} {1 {only a single result allowed for a SELECT that is part of an expression}}
2N/A# A subselect without an aggregate.
2N/Ado_test subselect-1.3a {
2N/A execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=2)}
2N/Ado_test subselect-1.3b {
2N/A execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=4)}
2N/Ado_test subselect-1.3c {
2N/A execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=6)}
2N/Ado_test subselect-1.3c {
2N/A execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=8)}
2N/A# What if the subselect doesn't return any value. We should get
2N/A# NULL as the result. Check it out.
2N/Ado_test subselect-1.4 {
2N/A execsql {SELECT b from t1 where a = coalesce((SELECT a FROM t1 WHERE b=5),1)}
2N/A# Try multiple subselects within a single expression.
2N/Ado_test subselect-1.5 {
2N/A CREATE TABLE t2(x int, y int);
2N/A INSERT INTO t2 VALUES(1,2);
2N/A INSERT INTO t2 VALUES(2,4);
2N/A INSERT INTO t2 VALUES(3,8);
2N/A INSERT INTO t2 VALUES(4,16);
2N/A WHERE x = (SELECT sum(b) FROM t1 where a notnull) - (SELECT sum(a) FROM t1)
2N/A# Try something useful. Delete every entry from t2 where the
2N/A# x value is less than half of the maximum.
2N/Ado_test subselect-1.6 {
2N/A execsql {DELETE FROM t2 WHERE x < 0.5*(SELECT max(x) FROM t2)}
2N/A execsql {SELECT x FROM t2 ORDER BY x}
2N/A# Make sure sorting works for SELECTs there used as a scalar expression.
2N/Ado_test subselect-2.1 {
2N/A SELECT (SELECT a FROM t1 ORDER BY a), (SELECT a FROM t1 ORDER BY a DESC)
2N/Ado_test subselect-2.2 {
2N/A SELECT 1 IN (SELECT a FROM t1 ORDER BY a);
2N/Ado_test subselect-2.3 {
2N/A SELECT 2 IN (SELECT a FROM t1 ORDER BY a DESC);
2N/A# Verify that the ORDER BY clause is honored in a subquery.
2N/Ado_test subselect-3.1 {
2N/A CREATE TABLE t3(x int);
2N/A INSERT INTO t3 SELECT a FROM t1 UNION ALL SELECT b FROM t1;
2N/A SELECT * FROM t3 ORDER BY x;
2N/Ado_test subselect-3.2 {
2N/A SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x LIMIT 2);
2N/Ado_test subselect-3.3 {
2N/A SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x DESC LIMIT 2);
2N/Ado_test subselect-3.4 {
2N/A SELECT (SELECT x FROM t3 ORDER BY x);
2N/Ado_test subselect-3.5 {
2N/A SELECT (SELECT x FROM t3 ORDER BY x DESC);
2N/Ado_test subselect-3.6 {
2N/A SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1);
2N/Ado_test subselect-3.7 {
2N/A SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1);
2N/Ado_test subselect-3.8 {
2N/A SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1 OFFSET 2);
2N/Ado_test subselect-3.9 {
2N/A SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);