190b1b3a3Sdan# 2010 September 21 290b1b3a3Sdan# 390b1b3a3Sdan# The author disclaims copyright to this source code. In place of 490b1b3a3Sdan# a legal notice, here is a blessing: 590b1b3a3Sdan# 690b1b3a3Sdan# May you do good and not evil. 790b1b3a3Sdan# May you find forgiveness for yourself and forgive others. 890b1b3a3Sdan# May you share freely, never taking more than you give. 990b1b3a3Sdan# 1090b1b3a3Sdan#*********************************************************************** 1190b1b3a3Sdan# 1290b1b3a3Sdan# This file implements tests to verify that the "testable statements" in 1390b1b3a3Sdan# the lang_delete.html document are correct. 1490b1b3a3Sdan# 1590b1b3a3Sdanset testdir [file dirname $argv0] 1690b1b3a3Sdansource $testdir/tester.tcl 1790b1b3a3Sdan 182f56da3fSdanifcapable !compound { 192f56da3fSdan finish_test 202f56da3fSdan return 212f56da3fSdan} 222f56da3fSdan 2390b1b3a3Sdanproc do_delete_tests {args} { 2490b1b3a3Sdan uplevel do_select_tests $args 2590b1b3a3Sdan} 2690b1b3a3Sdan 2790b1b3a3Sdando_execsql_test e_delete-0.0 { 2890b1b3a3Sdan CREATE TABLE t1(a, b); 2990b1b3a3Sdan CREATE INDEX i1 ON t1(a); 3090b1b3a3Sdan} {} 3190b1b3a3Sdan 3239759747Sdrh# -- syntax diagram delete-stmt 3339759747Sdrh# -- syntax diagram qualified-table-name 3490b1b3a3Sdan# 3590b1b3a3Sdando_delete_tests e_delete-0.1 { 3690b1b3a3Sdan 1 "DELETE FROM t1" {} 3790b1b3a3Sdan 2 "DELETE FROM t1 INDEXED BY i1" {} 3890b1b3a3Sdan 3 "DELETE FROM t1 NOT INDEXED" {} 3990b1b3a3Sdan 4 "DELETE FROM main.t1" {} 4090b1b3a3Sdan 5 "DELETE FROM main.t1 INDEXED BY i1" {} 4190b1b3a3Sdan 6 "DELETE FROM main.t1 NOT INDEXED" {} 4290b1b3a3Sdan 7 "DELETE FROM t1 WHERE a>2" {} 4390b1b3a3Sdan 8 "DELETE FROM t1 INDEXED BY i1 WHERE a>2" {} 4490b1b3a3Sdan 9 "DELETE FROM t1 NOT INDEXED WHERE a>2" {} 4590b1b3a3Sdan 10 "DELETE FROM main.t1 WHERE a>2" {} 4690b1b3a3Sdan 11 "DELETE FROM main.t1 INDEXED BY i1 WHERE a>2" {} 4790b1b3a3Sdan 12 "DELETE FROM main.t1 NOT INDEXED WHERE a>2" {} 4890b1b3a3Sdan} 4990b1b3a3Sdan 50af7626f5Sdan# EVIDENCE-OF: R-20205-17349 If the WHERE clause is not present, all 51af7626f5Sdan# records in the table are deleted. 52af7626f5Sdan# 53af7626f5Sdandrop_all_tables 54af7626f5Sdando_test e_delete-1.0 { 55af7626f5Sdan db transaction { 56af7626f5Sdan foreach t {t1 t2 t3 t4 t5 t6} { 57af7626f5Sdan execsql [string map [list %T% $t] { 58af7626f5Sdan CREATE TABLE %T%(x, y); 59af7626f5Sdan INSERT INTO %T% VALUES(1, 'one'); 60af7626f5Sdan INSERT INTO %T% VALUES(2, 'two'); 61af7626f5Sdan INSERT INTO %T% VALUES(3, 'three'); 62af7626f5Sdan INSERT INTO %T% VALUES(4, 'four'); 63af7626f5Sdan INSERT INTO %T% VALUES(5, 'five'); 64af7626f5Sdan }] 65af7626f5Sdan } 66af7626f5Sdan } 67af7626f5Sdan} {} 68af7626f5Sdando_delete_tests e_delete-1.1 { 69af7626f5Sdan 1 "DELETE FROM t1 ; SELECT * FROM t1" {} 70af7626f5Sdan 2 "DELETE FROM main.t2 ; SELECT * FROM t2" {} 71af7626f5Sdan} 7290b1b3a3Sdan 73*b3366b99Sdrh# EVIDENCE-OF: R-26300-50198 If a WHERE clause is supplied, then only 74*b3366b99Sdrh# those rows for which the WHERE clause boolean expression is true are 75*b3366b99Sdrh# deleted. 76*b3366b99Sdrh# 77*b3366b99Sdrh# EVIDENCE-OF: R-23360-48280 Rows for which the expression is false or 78*b3366b99Sdrh# NULL are retained. 79af7626f5Sdan# 80af7626f5Sdando_delete_tests e_delete-1.2 { 81af7626f5Sdan 1 "DELETE FROM t3 WHERE 1 ; SELECT x FROM t3" {} 82af7626f5Sdan 2 "DELETE FROM main.t4 WHERE 0 ; SELECT x FROM t4" {1 2 3 4 5} 83af7626f5Sdan 3 "DELETE FROM t4 WHERE 0.0 ; SELECT x FROM t4" {1 2 3 4 5} 84af7626f5Sdan 4 "DELETE FROM t4 WHERE NULL ; SELECT x FROM t4" {1 2 3 4 5} 85af7626f5Sdan 5 "DELETE FROM t4 WHERE y!='two'; SELECT x FROM t4" {2} 86af7626f5Sdan 6 "DELETE FROM t4 WHERE y='two' ; SELECT x FROM t4" {} 87af7626f5Sdan 7 "DELETE FROM t5 WHERE x=(SELECT max(x) FROM t5);SELECT x FROM t5" {1 2 3 4} 88af7626f5Sdan 8 "DELETE FROM t5 WHERE (SELECT max(x) FROM t4) ;SELECT x FROM t5" {1 2 3 4} 89af7626f5Sdan 9 "DELETE FROM t5 WHERE (SELECT max(x) FROM t6) ;SELECT x FROM t5" {} 90af7626f5Sdan 10 "DELETE FROM t6 WHERE y>'seven' ; SELECT y FROM t6" {one four five} 91af7626f5Sdan} 92af7626f5Sdan 93af7626f5Sdan 94af7626f5Sdan#------------------------------------------------------------------------- 95af7626f5Sdan# Tests for restrictions on DELETE statements that appear within trigger 96af7626f5Sdan# programs. 97af7626f5Sdan# 98af7626f5Sdanforcedelete test.db2 99af7626f5Sdanforcedelete test.db3 100af7626f5Sdando_execsql_test e_delete-2.0 { 101af7626f5Sdan ATTACH 'test.db2' AS aux; 102af7626f5Sdan ATTACH 'test.db3' AS aux2; 103af7626f5Sdan 104af7626f5Sdan CREATE TABLE temp.t7(a, b); INSERT INTO temp.t7 VALUES(1, 2); 105af7626f5Sdan CREATE TABLE main.t7(a, b); INSERT INTO main.t7 VALUES(3, 4); 106af7626f5Sdan CREATE TABLE aux.t7(a, b); INSERT INTO aux.t7 VALUES(5, 6); 107af7626f5Sdan CREATE TABLE aux2.t7(a, b); INSERT INTO aux2.t7 VALUES(7, 8); 108af7626f5Sdan 109af7626f5Sdan CREATE TABLE main.t8(a, b); INSERT INTO main.t8 VALUES(1, 2); 110af7626f5Sdan CREATE TABLE aux.t8(a, b); INSERT INTO aux.t8 VALUES(3, 4); 111af7626f5Sdan CREATE TABLE aux2.t8(a, b); INSERT INTO aux2.t8 VALUES(5, 6); 112af7626f5Sdan 113af7626f5Sdan CREATE TABLE aux.t9(a, b); INSERT INTO aux.t9 VALUES(1, 2); 114af7626f5Sdan CREATE TABLE aux2.t9(a, b); INSERT INTO aux2.t9 VALUES(3, 4); 115af7626f5Sdan 116af7626f5Sdan CREATE TABLE aux2.t10(a, b); INSERT INTO aux2.t10 VALUES(1, 2); 117af7626f5Sdan} {} 118af7626f5Sdan 119af7626f5Sdan 120af7626f5Sdan# EVIDENCE-OF: R-09681-58560 The table-name specified as part of a 121af7626f5Sdan# DELETE statement within a trigger body must be unqualified. 122af7626f5Sdan# 123*b3366b99Sdrh# EVIDENCE-OF: R-12275-20298 In other words, the schema-name. prefix on 124*b3366b99Sdrh# the table name is not allowed within triggers. 125af7626f5Sdan# 126af7626f5Sdando_delete_tests e_delete-2.1 -error { 127af7626f5Sdan qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers 128af7626f5Sdan} { 129af7626f5Sdan 1 { 130af7626f5Sdan CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN 131af7626f5Sdan DELETE FROM main.t2; 132af7626f5Sdan END; 133af7626f5Sdan } {} 134af7626f5Sdan 135af7626f5Sdan 2 { 136af7626f5Sdan CREATE TRIGGER tr1 BEFORE UPDATE ON t2 BEGIN 137af7626f5Sdan DELETE FROM temp.t7 WHERE a=new.a; 138af7626f5Sdan END; 139af7626f5Sdan } {} 140af7626f5Sdan 141af7626f5Sdan 3 { 142af7626f5Sdan CREATE TRIGGER tr1 AFTER UPDATE ON t8 BEGIN 143af7626f5Sdan DELETE FROM aux2.t8 WHERE b!=a; 144af7626f5Sdan END; 145af7626f5Sdan } {} 146af7626f5Sdan} 147af7626f5Sdan 148af7626f5Sdan# EVIDENCE-OF: R-28818-63526 If the table to which the trigger is 149af7626f5Sdan# attached is not in the temp database, then DELETE statements within 150af7626f5Sdan# the trigger body must operate on tables within the same database as 151af7626f5Sdan# it. 152af7626f5Sdan# 153af7626f5Sdan# This is tested in two parts. First, check that if a table of the 154af7626f5Sdan# specified name does not exist, an error is raised. Secondly, test 155af7626f5Sdan# that if tables with the specified name exist in multiple databases, 156af7626f5Sdan# the local database table is used. 157af7626f5Sdan# 158af7626f5Sdando_delete_tests e_delete-2.2.1 -error { no such table: %s } { 159af7626f5Sdan 1 { 160af7626f5Sdan CREATE TRIGGER main.tr1 AFTER INSERT ON main.t7 BEGIN 161af7626f5Sdan DELETE FROM t9; 162af7626f5Sdan END; 163af7626f5Sdan INSERT INTO main.t7 VALUES(1, 2); 164af7626f5Sdan } {main.t9} 165af7626f5Sdan 166af7626f5Sdan 2 { 167af7626f5Sdan CREATE TRIGGER aux.tr2 BEFORE UPDATE ON t9 BEGIN 168af7626f5Sdan DELETE FROM t10; 169af7626f5Sdan END; 170af7626f5Sdan UPDATE t9 SET a=1; 171af7626f5Sdan } {aux.t10} 172af7626f5Sdan} 173af7626f5Sdando_execsql_test e_delete-2.2.X { 174af7626f5Sdan DROP TRIGGER main.tr1; 175af7626f5Sdan DROP TRIGGER aux.tr2; 176af7626f5Sdan} {} 177af7626f5Sdan 178af7626f5Sdando_delete_tests e_delete-2.2.2 { 179af7626f5Sdan 1 { 180af7626f5Sdan CREATE TRIGGER aux.tr1 AFTER INSERT ON t8 BEGIN 181af7626f5Sdan DELETE FROM t9; 182af7626f5Sdan END; 183af7626f5Sdan INSERT INTO aux.t8 VALUES(1, 2); 184af7626f5Sdan 185af7626f5Sdan SELECT count(*) FROM aux.t9 186af7626f5Sdan UNION ALL 187af7626f5Sdan SELECT count(*) FROM aux2.t9; 188af7626f5Sdan } {0 1} 189af7626f5Sdan 190af7626f5Sdan 2 { 191af7626f5Sdan CREATE TRIGGER main.tr1 AFTER INSERT ON t8 BEGIN 192af7626f5Sdan DELETE FROM t7; 193af7626f5Sdan END; 194af7626f5Sdan INSERT INTO main.t8 VALUES(1, 2); 195af7626f5Sdan 196af7626f5Sdan SELECT count(*) FROM temp.t7 197af7626f5Sdan UNION ALL 198af7626f5Sdan SELECT count(*) FROM main.t7 199af7626f5Sdan UNION ALL 200af7626f5Sdan SELECT count(*) FROM aux.t7 201af7626f5Sdan UNION ALL 202af7626f5Sdan SELECT count(*) FROM aux2.t7; 203af7626f5Sdan } {1 0 1 1} 204af7626f5Sdan} 205af7626f5Sdan 206af7626f5Sdan# EVIDENCE-OF: R-31567-38587 If the table to which the trigger is 207af7626f5Sdan# attached is in the TEMP database, then the unqualified name of the 208af7626f5Sdan# table being deleted is resolved in the same way as it is for a 209af7626f5Sdan# top-level statement (by searching first the TEMP database, then the 210af7626f5Sdan# main database, then any other databases in the order they were 211af7626f5Sdan# attached). 212af7626f5Sdan# 213af7626f5Sdando_execsql_test e_delete-2.3.0 { 214af7626f5Sdan DROP TRIGGER aux.tr1; 215af7626f5Sdan DROP TRIGGER main.tr1; 216af7626f5Sdan DELETE FROM main.t8 WHERE oid>1; 217af7626f5Sdan DELETE FROM aux.t8 WHERE oid>1; 218af7626f5Sdan INSERT INTO aux.t9 VALUES(1, 2); 219af7626f5Sdan INSERT INTO main.t7 VALUES(3, 4); 220af7626f5Sdan} {} 221af7626f5Sdando_execsql_test e_delete-2.3.1 { 222af7626f5Sdan SELECT count(*) FROM temp.t7 UNION ALL SELECT count(*) FROM main.t7 UNION ALL 223af7626f5Sdan SELECT count(*) FROM aux.t7 UNION ALL SELECT count(*) FROM aux2.t7; 224af7626f5Sdan 225af7626f5Sdan SELECT count(*) FROM main.t8 UNION ALL SELECT count(*) FROM aux.t8 226af7626f5Sdan UNION ALL SELECT count(*) FROM aux2.t8; 227af7626f5Sdan 228af7626f5Sdan SELECT count(*) FROM aux.t9 UNION ALL SELECT count(*) FROM aux2.t9; 229af7626f5Sdan 230af7626f5Sdan SELECT count(*) FROM aux2.t10; 231af7626f5Sdan} {1 1 1 1 1 1 1 1 1 1} 232af7626f5Sdando_execsql_test e_delete-2.3.2 { 233af7626f5Sdan CREATE TRIGGER temp.tr1 AFTER INSERT ON t7 BEGIN 234af7626f5Sdan DELETE FROM t7; 235af7626f5Sdan DELETE FROM t8; 236af7626f5Sdan DELETE FROM t9; 237af7626f5Sdan DELETE FROM t10; 238af7626f5Sdan END; 239af7626f5Sdan INSERT INTO temp.t7 VALUES('hello', 'world'); 240af7626f5Sdan} {} 241af7626f5Sdando_execsql_test e_delete-2.3.3 { 242af7626f5Sdan SELECT count(*) FROM temp.t7 UNION ALL SELECT count(*) FROM main.t7 UNION ALL 243af7626f5Sdan SELECT count(*) FROM aux.t7 UNION ALL SELECT count(*) FROM aux2.t7; 244af7626f5Sdan 245af7626f5Sdan SELECT count(*) FROM main.t8 UNION ALL SELECT count(*) FROM aux.t8 246af7626f5Sdan UNION ALL SELECT count(*) FROM aux2.t8; 247af7626f5Sdan 248af7626f5Sdan SELECT count(*) FROM aux.t9 UNION ALL SELECT count(*) FROM aux2.t9; 249af7626f5Sdan 250af7626f5Sdan SELECT count(*) FROM aux2.t10; 251af7626f5Sdan} {0 1 1 1 0 1 1 0 1 0} 252af7626f5Sdan 253af7626f5Sdan# EVIDENCE-OF: R-28691-49464 The INDEXED BY and NOT INDEXED clauses are 254af7626f5Sdan# not allowed on DELETE statements within triggers. 255af7626f5Sdan# 256af7626f5Sdando_execsql_test e_delete-2.4.0 { 257af7626f5Sdan CREATE INDEX i8 ON t8(a, b); 258af7626f5Sdan} {} 259af7626f5Sdando_delete_tests e_delete-2.4 -error { 260af7626f5Sdan the %s %s clause is not allowed on UPDATE or DELETE statements within triggers 261af7626f5Sdan} { 262af7626f5Sdan 1 { 263af7626f5Sdan CREATE TRIGGER tr3 AFTER INSERT ON t8 BEGIN 264af7626f5Sdan DELETE FROM t8 INDEXED BY i8 WHERE a=5; 265af7626f5Sdan END; 266af7626f5Sdan } {INDEXED BY} 267af7626f5Sdan 2 { 268af7626f5Sdan CREATE TRIGGER tr3 AFTER INSERT ON t8 BEGIN 269af7626f5Sdan DELETE FROM t8 NOT INDEXED WHERE a=5; 270af7626f5Sdan END; 271af7626f5Sdan } {NOT INDEXED} 272af7626f5Sdan} 273af7626f5Sdan 274af7626f5Sdanifcapable update_delete_limit { 275af7626f5Sdan 276af7626f5Sdan# EVIDENCE-OF: R-64942-06615 The LIMIT and ORDER BY clauses (described 277af7626f5Sdan# below) are unsupported for DELETE statements within triggers. 278af7626f5Sdan# 279af7626f5Sdando_delete_tests e_delete-2.5 -error { near "%s": syntax error } { 280af7626f5Sdan 1 { 281af7626f5Sdan CREATE TRIGGER tr3 AFTER INSERT ON t8 BEGIN 282af7626f5Sdan DELETE FROM t8 LIMIT 10; 283af7626f5Sdan END; 284af7626f5Sdan } {LIMIT} 285af7626f5Sdan 2 { 286af7626f5Sdan CREATE TRIGGER tr3 AFTER INSERT ON t8 BEGIN 287af7626f5Sdan DELETE FROM t8 ORDER BY a LIMIT 5; 288af7626f5Sdan END; 289af7626f5Sdan } {ORDER} 290af7626f5Sdan} 291af7626f5Sdan 292af7626f5Sdan# EVIDENCE-OF: R-40026-10531 If SQLite is compiled with the 293af7626f5Sdan# SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option, then the syntax 294af7626f5Sdan# of the DELETE statement is extended by the addition of optional ORDER 295af7626f5Sdan# BY and LIMIT clauses: 296af7626f5Sdan# 29739759747Sdrh# -- syntax diagram delete-stmt-limited 298af7626f5Sdan# 299af7626f5Sdando_delete_tests e_delete-3.1 { 300af7626f5Sdan 1 "DELETE FROM t1 LIMIT 5" {} 301af7626f5Sdan 2 "DELETE FROM t1 LIMIT 5-1 OFFSET 2+2" {} 302af7626f5Sdan 3 "DELETE FROM t1 LIMIT 2+2, 16/4" {} 303af7626f5Sdan 4 "DELETE FROM t1 ORDER BY x LIMIT 5" {} 304af7626f5Sdan 5 "DELETE FROM t1 ORDER BY x LIMIT 5-1 OFFSET 2+2" {} 305af7626f5Sdan 6 "DELETE FROM t1 ORDER BY x LIMIT 2+2, 16/4" {} 306af7626f5Sdan 7 "DELETE FROM t1 WHERE x>2 LIMIT 5" {} 307af7626f5Sdan 8 "DELETE FROM t1 WHERE x>2 LIMIT 5-1 OFFSET 2+2" {} 308af7626f5Sdan 9 "DELETE FROM t1 WHERE x>2 LIMIT 2+2, 16/4" {} 309af7626f5Sdan 10 "DELETE FROM t1 WHERE x>2 ORDER BY x LIMIT 5" {} 310af7626f5Sdan 11 "DELETE FROM t1 WHERE x>2 ORDER BY x LIMIT 5-1 OFFSET 2+2" {} 311af7626f5Sdan 12 "DELETE FROM t1 WHERE x>2 ORDER BY x LIMIT 2+2, 16/4" {} 312af7626f5Sdan} 313af7626f5Sdan 314af7626f5Sdandrop_all_tables 315af7626f5Sdanproc rebuild_t1 {} { 316af7626f5Sdan catchsql { DROP TABLE t1 } 317af7626f5Sdan execsql { 318af7626f5Sdan CREATE TABLE t1(a, b); 319af7626f5Sdan INSERT INTO t1 VALUES(1, 'one'); 320af7626f5Sdan INSERT INTO t1 VALUES(2, 'two'); 321af7626f5Sdan INSERT INTO t1 VALUES(3, 'three'); 322af7626f5Sdan INSERT INTO t1 VALUES(4, 'four'); 323af7626f5Sdan INSERT INTO t1 VALUES(5, 'five'); 324af7626f5Sdan } 325af7626f5Sdan} 326af7626f5Sdan 327af7626f5Sdan# EVIDENCE-OF: R-44062-08550 If a DELETE statement has a LIMIT clause, 328af7626f5Sdan# the maximum number of rows that will be deleted is found by evaluating 329af7626f5Sdan# the accompanying expression and casting it to an integer value. 330af7626f5Sdan# 331af7626f5Sdanrebuild_t1 332af7626f5Sdando_delete_tests e_delete-3.2 -repair rebuild_t1 -query { 333af7626f5Sdan SELECT a FROM t1 334af7626f5Sdan} { 335af7626f5Sdan 1 "DELETE FROM t1 LIMIT 3" {4 5} 336af7626f5Sdan 2 "DELETE FROM t1 LIMIT 1+1" {3 4 5} 337af7626f5Sdan 3 "DELETE FROM t1 LIMIT '4'" {5} 338af7626f5Sdan 4 "DELETE FROM t1 LIMIT '1.0'" {2 3 4 5} 339af7626f5Sdan} 340af7626f5Sdan 341af7626f5Sdan# EVIDENCE-OF: R-02661-56399 If the result of the evaluating the LIMIT 342af7626f5Sdan# clause cannot be losslessly converted to an integer value, it is an 343af7626f5Sdan# error. 344af7626f5Sdan# 345af7626f5Sdando_delete_tests e_delete-3.3 -error { datatype mismatch } { 346af7626f5Sdan 1 "DELETE FROM t1 LIMIT 'abc'" {} 347af7626f5Sdan 2 "DELETE FROM t1 LIMIT NULL" {} 348af7626f5Sdan 3 "DELETE FROM t1 LIMIT X'ABCD'" {} 349af7626f5Sdan 4 "DELETE FROM t1 LIMIT 1.2" {} 350af7626f5Sdan} 351af7626f5Sdan 352af7626f5Sdan# EVIDENCE-OF: R-00598-03741 A negative LIMIT value is interpreted as 353af7626f5Sdan# "no limit". 354af7626f5Sdan# 355af7626f5Sdando_delete_tests e_delete-3.4 -repair rebuild_t1 -query { 356af7626f5Sdan SELECT a FROM t1 357af7626f5Sdan} { 358af7626f5Sdan 1 "DELETE FROM t1 LIMIT -1" {} 359af7626f5Sdan 2 "DELETE FROM t1 LIMIT 2-4" {} 360af7626f5Sdan 3 "DELETE FROM t1 LIMIT -4.0" {} 361af7626f5Sdan 4 "DELETE FROM t1 LIMIT 5*-1" {} 362af7626f5Sdan} 363af7626f5Sdan 364af7626f5Sdan# EVIDENCE-OF: R-26377-49195 If the DELETE statement also has an OFFSET 365af7626f5Sdan# clause, then it is similarly evaluated and cast to an integer value. 366af7626f5Sdan# Again, it is an error if the value cannot be losslessly converted to 367af7626f5Sdan# an integer. 368af7626f5Sdan# 369af7626f5Sdando_delete_tests e_delete-3.5 -error { datatype mismatch } { 370af7626f5Sdan 1 "DELETE FROM t1 LIMIT 1 OFFSET 'abc'" {} 371af7626f5Sdan 2 "DELETE FROM t1 LIMIT 1 OFFSET NULL" {} 372af7626f5Sdan 3 "DELETE FROM t1 LIMIT 1 OFFSET X'ABCD'" {} 373af7626f5Sdan 4 "DELETE FROM t1 LIMIT 1 OFFSET 1.2" {} 374af7626f5Sdan 5 "DELETE FROM t1 LIMIT 'abc', 1" {} 375af7626f5Sdan 6 "DELETE FROM t1 LIMIT NULL, 1" {} 376af7626f5Sdan 7 "DELETE FROM t1 LIMIT X'ABCD', 1" {} 377af7626f5Sdan 8 "DELETE FROM t1 LIMIT 1.2, 1" {} 378af7626f5Sdan} 379af7626f5Sdan 380af7626f5Sdan 381af7626f5Sdan# EVIDENCE-OF: R-64004-53814 If there is no OFFSET clause, or the 382af7626f5Sdan# calculated integer value is negative, the effective OFFSET value is 383af7626f5Sdan# zero. 384af7626f5Sdan# 385af7626f5Sdando_delete_tests e_delete-3.6 -repair rebuild_t1 -query { 386af7626f5Sdan SELECT a FROM t1 387af7626f5Sdan} { 388af7626f5Sdan 1a "DELETE FROM t1 LIMIT 3 OFFSET 0" {4 5} 389af7626f5Sdan 1b "DELETE FROM t1 LIMIT 3" {4 5} 390af7626f5Sdan 1c "DELETE FROM t1 LIMIT 3 OFFSET -1" {4 5} 391af7626f5Sdan 2a "DELETE FROM t1 LIMIT 1+1 OFFSET 0" {3 4 5} 392af7626f5Sdan 2b "DELETE FROM t1 LIMIT 1+1" {3 4 5} 393af7626f5Sdan 2c "DELETE FROM t1 LIMIT 1+1 OFFSET 2-5" {3 4 5} 394af7626f5Sdan 3a "DELETE FROM t1 LIMIT '4' OFFSET 0" {5} 395af7626f5Sdan 3b "DELETE FROM t1 LIMIT '4'" {5} 396af7626f5Sdan 3c "DELETE FROM t1 LIMIT '4' OFFSET -1.0" {5} 397af7626f5Sdan 4a "DELETE FROM t1 LIMIT '1.0' OFFSET 0" {2 3 4 5} 398af7626f5Sdan 4b "DELETE FROM t1 LIMIT '1.0'" {2 3 4 5} 399af7626f5Sdan 4c "DELETE FROM t1 LIMIT '1.0' OFFSET -11" {2 3 4 5} 400af7626f5Sdan} 401af7626f5Sdan 402af7626f5Sdan# EVIDENCE-OF: R-48141-52334 If the DELETE statement has an ORDER BY 403af7626f5Sdan# clause, then all rows that would be deleted in the absence of the 404af7626f5Sdan# LIMIT clause are sorted according to the ORDER BY. The first M rows, 405af7626f5Sdan# where M is the value found by evaluating the OFFSET clause expression, 406af7626f5Sdan# are skipped, and the following N, where N is the value of the LIMIT 407af7626f5Sdan# expression, are deleted. 408af7626f5Sdan# 409af7626f5Sdando_delete_tests e_delete-3.7 -repair rebuild_t1 -query { 410af7626f5Sdan SELECT a FROM t1 411af7626f5Sdan} { 412af7626f5Sdan 1 "DELETE FROM t1 ORDER BY b LIMIT 2" {1 2 3} 413af7626f5Sdan 2 "DELETE FROM t1 ORDER BY length(b), a LIMIT 3" {3 5} 414af7626f5Sdan 3 "DELETE FROM t1 ORDER BY a DESC LIMIT 1 OFFSET 0" {1 2 3 4} 415af7626f5Sdan 4 "DELETE FROM t1 ORDER BY a DESC LIMIT 1 OFFSET 1" {1 2 3 5} 416af7626f5Sdan 5 "DELETE FROM t1 ORDER BY a DESC LIMIT 1 OFFSET 2" {1 2 4 5} 417af7626f5Sdan} 418af7626f5Sdan 419af7626f5Sdan# EVIDENCE-OF: R-64535-08414 If there are less than N rows remaining 420af7626f5Sdan# after taking the OFFSET clause into account, or if the LIMIT clause 421af7626f5Sdan# evaluated to a negative value, then all remaining rows are deleted. 422af7626f5Sdan# 423af7626f5Sdando_delete_tests e_delete-3.8 -repair rebuild_t1 -query { 424af7626f5Sdan SELECT a FROM t1 425af7626f5Sdan} { 426af7626f5Sdan 1 "DELETE FROM t1 ORDER BY a ASC LIMIT 10" {} 427af7626f5Sdan 2 "DELETE FROM t1 ORDER BY a ASC LIMIT -1" {} 428af7626f5Sdan 3 "DELETE FROM t1 ORDER BY a ASC LIMIT 4 OFFSET 2" {1 2} 429af7626f5Sdan} 430af7626f5Sdan 431af7626f5Sdan# EVIDENCE-OF: R-37284-06965 If the DELETE statement has no ORDER BY 432af7626f5Sdan# clause, then all rows that would be deleted in the absence of the 433af7626f5Sdan# LIMIT clause are assembled in an arbitrary order before applying the 434af7626f5Sdan# LIMIT and OFFSET clauses to determine the subset that are actually 435af7626f5Sdan# deleted. 436af7626f5Sdan# 437af7626f5Sdan# In practice, the "arbitrary order" is rowid order. 438af7626f5Sdan# 439af7626f5Sdando_delete_tests e_delete-3.9 -repair rebuild_t1 -query { 440af7626f5Sdan SELECT a FROM t1 441af7626f5Sdan} { 442af7626f5Sdan 1 "DELETE FROM t1 LIMIT 2" {3 4 5} 443af7626f5Sdan 2 "DELETE FROM t1 LIMIT 3" {4 5} 444af7626f5Sdan 3 "DELETE FROM t1 LIMIT 1 OFFSET 0" {2 3 4 5} 445af7626f5Sdan 4 "DELETE FROM t1 LIMIT 1 OFFSET 1" {1 3 4 5} 446af7626f5Sdan 5 "DELETE FROM t1 LIMIT 1 OFFSET 2" {1 2 4 5} 447af7626f5Sdan} 448af7626f5Sdan 449af7626f5Sdan 4501a475194Sdrh# EVIDENCE-OF: R-07548-13422 The ORDER BY clause on a DELETE statement 451af7626f5Sdan# is used only to determine which rows fall within the LIMIT. The order 452af7626f5Sdan# in which rows are deleted is arbitrary and is not influenced by the 453af7626f5Sdan# ORDER BY clause. 454af7626f5Sdan# 455af7626f5Sdan# In practice, rows are always deleted in rowid order. 456af7626f5Sdan# 457af7626f5Sdando_delete_tests e_delete-3.10 -repair { 458af7626f5Sdan rebuild_t1 459af7626f5Sdan catchsql { DROP TABLE t1log } 460af7626f5Sdan execsql { 461af7626f5Sdan CREATE TABLE t1log(x); 462af7626f5Sdan CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN 463af7626f5Sdan INSERT INTO t1log VALUES(old.a); 464af7626f5Sdan END; 465af7626f5Sdan } 466af7626f5Sdan} -query { 467af7626f5Sdan SELECT x FROM t1log 468af7626f5Sdan} { 469af7626f5Sdan 1 "DELETE FROM t1 ORDER BY a DESC LIMIT 2" {4 5} 470af7626f5Sdan 2 "DELETE FROM t1 ORDER BY a DESC LIMIT -1" {1 2 3 4 5} 471af7626f5Sdan 3 "DELETE FROM t1 ORDER BY a ASC LIMIT 2" {1 2} 472af7626f5Sdan 4 "DELETE FROM t1 ORDER BY a ASC LIMIT -1" {1 2 3 4 5} 473af7626f5Sdan} 474af7626f5Sdan 475af7626f5Sdan} 476af7626f5Sdan 477af7626f5Sdanfinish_test 478