1
0
mirror of https://github.com/sqlite/sqlite.git synced 2026-10-25 09:59:22 +03:00
Files
sqlite/test/delete4.test
2015-12-12 17:31:40 +00:00

231 lines
6.5 KiB
Plaintext

# 2005 August 24
#
# 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 script is a test of the DELETE command.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix delete4
do_execsql_test 1.1 {
CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
INSERT INTO t1 VALUES(1, 0);
INSERT INTO t1 VALUES(2, 1);
INSERT INTO t1 VALUES(3, 0);
INSERT INTO t1 VALUES(4, 1);
INSERT INTO t1 VALUES(5, 0);
INSERT INTO t1 VALUES(6, 1);
INSERT INTO t1 VALUES(7, 0);
INSERT INTO t1 VALUES(8, 1);
}
do_execsql_test 1.2 {
DELETE FROM t1 WHERE y=1;
}
do_execsql_test 1.3 {
SELECT x FROM t1;
} {1 3 5 7}
#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 2.1 {
CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z);
INSERT INTO t1 VALUES(1, 0, randomblob(200));
INSERT INTO t1 VALUES(2, 1, randomblob(200));
INSERT INTO t1 VALUES(3, 0, randomblob(200));
INSERT INTO t1 VALUES(4, 1, randomblob(200));
INSERT INTO t1 VALUES(5, 0, randomblob(200));
INSERT INTO t1 VALUES(6, 1, randomblob(200));
INSERT INTO t1 VALUES(7, 0, randomblob(200));
INSERT INTO t1 VALUES(8, 1, randomblob(200));
}
do_execsql_test 2.2 {
DELETE FROM t1 WHERE y=1;
}
do_execsql_test 2.3 {
SELECT x FROM t1;
} {1 3 5 7}
#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 3.1 {
CREATE TABLE t1(a, b, PRIMARY KEY(a, b)) WITHOUT ROWID;
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(2, 4);
INSERT INTO t1 VALUES(1, 5);
DELETE FROM t1 WHERE a=1;
SELECT * FROM t1;
} {2 4}
#-------------------------------------------------------------------------
# DELETE statement that uses the OR optimization
#
reset_db
do_execsql_test 3.1 {
CREATE TABLE t1(i INTEGER PRIMARY KEY, a, b);
CREATE INDEX i1a ON t1(a);
CREATE INDEX i1b ON t1(b);
INSERT INTO t1 VALUES(1, 'one', 'i');
INSERT INTO t1 VALUES(2, 'two', 'ii');
INSERT INTO t1 VALUES(3, 'three', 'iii');
INSERT INTO t1 VALUES(4, 'four', 'iv');
INSERT INTO t1 VALUES(5, 'one', 'i');
INSERT INTO t1 VALUES(6, 'two', 'ii');
INSERT INTO t1 VALUES(7, 'three', 'iii');
INSERT INTO t1 VALUES(8, 'four', 'iv');
} {}
do_execsql_test 3.2 {
DELETE FROM t1 WHERE a='two' OR b='iv';
}
do_execsql_test 3.3 {
SELECT i FROM t1 ORDER BY i;
} {1 3 5 7}
do_execsql_test 3.4 {
PRAGMA integrity_check;
} {ok}
# Between 2015-09-14 and 2015-09-28, the following test cases would result
# in corruption (wrong # of entries in index) due to a bug in the ONEPASS
# optimization.
#
do_execsql_test 4.1 {
DROP TABLE IF EXISTS t4;
CREATE TABLE t4(col0, col1);
INSERT INTO "t4" VALUES(14, 'abcde');
CREATE INDEX idx_t4_0 ON t4 (col1, col0);
CREATE INDEX idx_t4_3 ON t4 (col0);
DELETE FROM t4 WHERE col0=69 OR col0>7;
PRAGMA integrity_check;
} {ok}
do_execsql_test 4.2 {
DROP TABLE IF EXISTS t4;
CREATE TABLE t4(col0, col1);
INSERT INTO "t4" VALUES(14, 'abcde');
CREATE INDEX idx_t4_3 ON t4 (col0);
CREATE INDEX idx_t4_0 ON t4 (col1, col0);
DELETE FROM t4 WHERE col0=69 OR col0>7;
PRAGMA integrity_check;
} {ok}
do_execsql_test 4.11 {
DROP TABLE IF EXISTS t4;
CREATE TABLE t4(col0, col1, pk PRIMARY KEY) WITHOUT ROWID;
INSERT INTO t4 VALUES(14, 'abcde','xyzzy');
CREATE INDEX idx_t4_0 ON t4 (col1, col0);
CREATE INDEX idx_t4_3 ON t4 (col0);
DELETE FROM t4 WHERE col0=69 OR col0>7;
PRAGMA integrity_check;
} {ok}
do_execsql_test 4.12 {
DROP TABLE IF EXISTS t4;
CREATE TABLE t4(col0, col1, pk PRIMARY KEY) WITHOUT ROWID;
INSERT INTO t4 VALUES(14, 'abcde','xyzzy');
CREATE INDEX idx_t4_3 ON t4 (col0);
CREATE INDEX idx_t4_0 ON t4 (col1, col0);
DELETE FROM t4 WHERE col0=69 OR col0>7;
PRAGMA integrity_check;
} {ok}
#-------------------------------------------------------------------------
# Further tests for DELETE statements that use the OR optimization.
#
array unset -nocomplain res
foreach {tn tbl} {
0 { CREATE TABLE t5(i INTEGER, a, b, c) }
1 {
CREATE TABLE t5(i INTEGER PRIMARY KEY, a, b, c);
CREATE INDEX t5a ON t5(a);
CREATE INDEX t5bc ON t5(b, c);
CREATE INDEX t5c ON t5(c);
}
2 {
CREATE TABLE t5(i INT PRIMARY KEY, a, b, c) WITHOUT ROWID;
CREATE INDEX t5a ON t5(a);
CREATE INDEX t5bc ON t5(b, c);
CREATE INDEX t5c ON t5(c);
}
} {
do_test 5.$tn.0 {
execsql { DROP TABLE IF EXISTS t5 }
execsql $tbl
for {set i 1} {$i <= 100} {incr i} {
set a [expr $i*2]
set b [expr ($i*7 % 100)]
set c [expr 100-$i]
execsql { INSERT INTO t5 VALUES($i, $a, $b, $c) }
}
} {}
foreach {tn2 sql} {
1 { DELETE FROM t5 WHERE a=4 OR b=10 OR c=17 }
2 { DELETE FROM t5 WHERE (a=4 OR b=22) AND c=98 }
3 { DELETE FROM t5 WHERE (b=49 AND c=93) OR a=14 }
4 { DELETE FROM t5 WHERE (b=70 AND c=90) OR a=150 }
5 { DELETE FROM t5 WHERE (a BETWEEN 2 AND 20) OR (b BETWEEN 1 AND 10) }
6 { DELETE FROM t5 WHERE a IN (2, 4, 6, 8) OR b IN (63, 70, 77) }
} {
execsql BEGIN
execsql $sql
do_execsql_test 5.$tn.$tn2.1 { PRAGMA integrity_check } ok
if {$tn==0} {
set res($tn2) [db eval { SELECT count(*), md5sum(i, a, b, c) FROM t5 }]
}
do_execsql_test 5.$tn.$tn2.2 {
SELECT count(*), md5sum(i, a, b, c) FROM t5
} $res($tn2)
execsql ROLLBACK
}
}
#-------------------------------------------------------------------------
# Test that nested OR optimizations work.
#
do_execsql_test 6.0 {
CREATE TABLE t6(a, b, c, d);
CREATE INDEX t6a ON t6(a);
CREATE INDEX t6b ON t6(b);
CREATE INDEX t6c ON t6(c);
WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 )
INSERT INTO t6 SELECT i, i*2, i*3, i%2 FROM s;
}
proc do_61_test {tn sql lDel} {
uplevel [list do_execsql_test $tn "
BEGIN;
$sql;
WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 )
SELECT i FROM s EXCEPT SELECT a FROM t6 ORDER BY 1;
ROLLBACK;
" $lDel]
}
do_61_test 6.1 {
DELETE FROM t6 WHERE a=22 OR b=90 OR (d=0 AND (b=400 OR c=303));
} {22 45 200}
do_61_test 6.2 {
DELETE FROM t6 WHERE a=22 OR b=90 OR (d=1 AND (b=400 OR c=303));
} {22 45 101}
do_61_test 6.3 {
DELETE FROM t6 WHERE (d=0 AND (a=100 OR b=150)) OR (d=1 AND (b=50 OR c=603));
} {25 100 201}
finish_test