interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / rtree / rtree1.test

1195 lines · 37605 bytes Raw Blame History
1# 2008 Feb 19
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# The focus of this file is testing the r-tree extension.
13#
14
15if {![info exists testdir]} {
16 set testdir [file join [file dirname [info script]] .. .. test]
17}
18source [file join [file dirname [info script]] rtree_util.tcl]
19source $testdir/tester.tcl
20set testprefix rtree1
21
22# Test plan:
23#
24# rtree-1.*: Creating/destroying r-tree tables.
25# rtree-2.*: Test the implicit constraints - unique rowid and
26# (coord[N]<=coord[N+1]) for even values of N. Also
27# automatic assigning of rowid values.
28# rtree-3.*: Linear scans of r-tree data.
29# rtree-4.*: Test INSERT
30# rtree-5.*: Test DELETE
31# rtree-6.*: Test UPDATE
32# rtree-7.*: Test renaming an r-tree table.
33# rtree-8.*: Test constrained scans of r-tree data.
34#
35# rtree-12.*: Test that on-conflict clauses are supported.
36# rtree-13.*: Test that bug [d2889096e7bdeac6d] has been fixed.
37# rtree-14.*: Test if a non-integer is inserted into the PK column of an
38# r-tree table, it is converted to an integer before being
39# inserted. Also that if a non-numeric is inserted into one
40# of the min/max dimension columns, it is converted to the
41# required type before being inserted.
42# rtree-15.*: Check that DROP TABLE works within a transaction that
43# writes to an r-tree table.
44#
45
46ifcapable !rtree {
47 finish_test
48 return
49}
50
51#----------------------------------------------------------------------------
52# Test cases rtree-1.* test CREATE and DROP table statements.
53#
54
55# Test creating and dropping an rtree table.
56#
57do_test rtree-1.1.1 {
58 execsql { CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2) }
59} {}
60do_test rtree-1.1.2a {
61 execsql { SELECT name FROM sqlite_master ORDER BY name }
62} {t1 t1_node t1_parent t1_rowid}
63do_execsql_test rtree-1.1.2b {
64 SELECT name FROM pragma_table_list WHERE type='shadow' ORDER BY name;
65} {t1_node t1_parent t1_rowid}
66do_test rtree-1.1.3 {
67 execsql {
68 DROP TABLE t1;
69 SELECT name FROM sqlite_master ORDER BY name;
70 }
71} {}
72
73# Test creating and dropping an rtree table with an odd name in
74# an attached database.
75#
76do_test rtree-1.2.1 {
77 file delete -force test2.db
78 execsql {
79 ATTACH 'test2.db' AS aux;
80 CREATE VIRTUAL TABLE aux.'a" "b' USING rtree(ii, x1, x2, y1, y2);
81 }
82} {}
83do_test rtree-1.2.2 {
84 execsql { SELECT name FROM sqlite_master ORDER BY name }
85} {}
86do_test rtree-1.2.3 {
87 execsql { SELECT name FROM aux.sqlite_master ORDER BY name }
88} {{a" "b} {a" "b_node} {a" "b_parent} {a" "b_rowid}}
89do_test rtree-1.2.4 {
90 execsql {
91 DROP TABLE aux.'a" "b';
92 SELECT name FROM aux.sqlite_master ORDER BY name;
93 }
94} {}
95
96# Test that the logic for checking the number of columns specified
97# for an rtree table. Acceptable values are odd numbers between 3 and
98# 11, inclusive.
99#
100set cols [list i1 i2 i3 i4 i5 i6 i7 i8 i9 iA iB iC iD iE iF iG iH iI iJ iK]
101for {set nCol 1} {$nCol<[llength $cols]} {incr nCol} {
102
103 set columns [join [lrange $cols 0 [expr {$nCol-1}]] ,]
104
105 set X {0 {}}
106 if {$nCol%2 == 0} { set X {1 {Wrong number of columns for an rtree table}} }
107 if {$nCol < 3} { set X {1 {Too few columns for an rtree table}} }
108 if {$nCol > 11} { set X {1 {Too many columns for an rtree table}} }
109
110 do_test rtree-1.3.$nCol {
111 catchsql "
112 CREATE VIRTUAL TABLE t1 USING rtree($columns);
113 "
114 } $X
115
116 catchsql { DROP TABLE t1 }
117}
118do_catchsql_test rtree-1.3.1000 {
119 CREATE VIRTUAL TABLE t1000 USING rtree;
120} {1 {Too few columns for an rtree table}}
121
122# Like execsql except display output as integer where that can be
123# done without loss of information.
124#
125proc execsql_intout {sql} {
126 set out {}
127 foreach term [execsql $sql] {
128 regsub {\.0$} $term {} term
129 lappend out $term
130 }
131 return $out
132}
133
134# Test that it is possible to open an existing database that contains
135# r-tree tables.
136#
137do_execsql_test rtree-1.4.1a {
138 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2);
139 INSERT INTO t1 VALUES(1, 5.0, 10.0);
140 SELECT substr(hex(data),1,40) FROM t1_node;
141} {00000001000000000000000140A0000041200000}
142do_execsql_test rtree-1.4.1b {
143 INSERT INTO t1 VALUES(2, 15.0, 20.0);
144} {}
145do_test rtree-1.4.2 {
146 db close
147 sqlite3 db test.db
148 execsql_intout { SELECT * FROM t1 ORDER BY ii }
149} {1 5 10 2 15 20}
150do_test rtree-1.4.3 {
151 execsql { DROP TABLE t1 }
152} {}
153
154# Test that it is possible to create an r-tree table with ridiculous
155# column names.
156#
157do_test rtree-1.5.1 {
158 execsql_intout {
159 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
160 INSERT INTO t1 VALUES(1, 2, 3);
161 SELECT "the key", "x dim.", "x2'dim" FROM t1;
162 }
163} {1 2 3}
164do_test rtree-1.5.1 {
165 execsql { DROP TABLE t1 }
166} {}
167
168# Force the r-tree constructor to fail.
169#
170do_test rtree-1.6.1 {
171 execsql { CREATE TABLE t1_rowid(a); }
172 catchsql {
173 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
174 }
175} {1 {table "t1_rowid" already exists}}
176do_test rtree-1.6.1 {
177 execsql { DROP TABLE t1_rowid }
178} {}
179
180#----------------------------------------------------------------------------
181# Test cases rtree-2.*
182#
183do_test rtree-2.1.1 {
184 execsql {
185 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
186 SELECT * FROM t1;
187 }
188} {}
189
190do_test rtree-2.1.2 {
191 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
192 execsql_intout { SELECT * FROM t1 }
193} {1 1 3 2 4}
194do_test rtree-2.1.3 {
195 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
196 execsql { SELECT rowid FROM t1 ORDER BY rowid }
197} {1 2}
198do_test rtree-2.1.3 {
199 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
200 execsql { SELECT ii FROM t1 ORDER BY ii }
201} {1 2 3}
202
203do_test rtree-2.2.1 {
204 catchsql { INSERT INTO t1 VALUES(2, 1, 3, 2, 4) }
205} {1 {UNIQUE constraint failed: t1.ii}}
206do_test rtree-2.2.2 {
207 catchsql { INSERT INTO t1 VALUES(4, 1, 3, 4, 2) }
208} {1 {rtree constraint failed: t1.(y1<=y2)}}
209do_test rtree-2.2.3 {
210 catchsql { INSERT INTO t1 VALUES(4, 3, 1, 2, 4) }
211} {1 {rtree constraint failed: t1.(x1<=x2)}}
212do_test rtree-2.2.4 {
213 execsql { SELECT ii FROM t1 ORDER BY ii }
214} {1 2 3}
215
216do_test rtree-2.X {
217 execsql { DROP TABLE t1 }
218} {}
219
220#----------------------------------------------------------------------------
221# Test cases rtree-3.* test linear scans of r-tree table data. To test
222# this we have to insert some data into an r-tree, but that is not the
223# focus of these tests.
224#
225do_test rtree-3.1.1 {
226 execsql {
227 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
228 SELECT * FROM t1;
229 }
230} {}
231do_test rtree-3.1.2 {
232 execsql_intout {
233 INSERT INTO t1 VALUES(5, 1, 3, 2, 4);
234 SELECT * FROM t1;
235 }
236} {5 1 3 2 4}
237do_test rtree-3.1.3 {
238 execsql_intout {
239 INSERT INTO t1 VALUES(6, 2, 6, 4, 8);
240 SELECT * FROM t1;
241 }
242} {5 1 3 2 4 6 2 6 4 8}
243
244# Test the constraint on the coordinates (c[i]<=c[i+1] where (i%2==0)):
245do_test rtree-3.2.1 {
246 catchsql { INSERT INTO t1 VALUES(7, 2, 6, 4, 3) }
247} {1 {rtree constraint failed: t1.(y1<=y2)}}
248do_test rtree-3.2.2 {
249 catchsql { INSERT INTO t1 VALUES(8, 2, 6, 3, 3) }
250} {0 {}}
251
252#----------------------------------------------------------------------------
253# Test cases rtree-5.* test DELETE operations.
254#
255do_test rtree-5.1.1 {
256 execsql { CREATE VIRTUAL TABLE t2 USING rtree(ii, x1, x2) }
257} {}
258do_test rtree-5.1.2 {
259 execsql_intout {
260 INSERT INTO t2 VALUES(1, 10, 20);
261 INSERT INTO t2 VALUES(2, 30, 40);
262 INSERT INTO t2 VALUES(3, 50, 60);
263 SELECT * FROM t2 ORDER BY ii;
264 }
265} {1 10 20 2 30 40 3 50 60}
266do_test rtree-5.1.3 {
267 execsql_intout {
268 DELETE FROM t2 WHERE ii=2;
269 SELECT * FROM t2 ORDER BY ii;
270 }
271} {1 10 20 3 50 60}
272do_test rtree-5.1.4 {
273 execsql_intout {
274 DELETE FROM t2 WHERE ii=1;
275 SELECT * FROM t2 ORDER BY ii;
276 }
277} {3 50 60}
278do_test rtree-5.1.5 {
279 execsql {
280 DELETE FROM t2 WHERE ii=3;
281 SELECT * FROM t2 ORDER BY ii;
282 }
283} {}
284do_test rtree-5.1.6 {
285 execsql { SELECT * FROM t2_rowid }
286} {}
287
288#----------------------------------------------------------------------------
289# Test cases rtree-5.* test UPDATE operations.
290#
291do_test rtree-6.1.1 {
292 execsql { CREATE VIRTUAL TABLE t3 USING rtree(ii, x1, x2, y1, y2) }
293} {}
294do_test rtree-6.1.2 {
295 execsql_intout {
296 INSERT INTO t3 VALUES(1, 2, 3, 4, 5);
297 UPDATE t3 SET x2=5;
298 SELECT * FROM t3;
299 }
300} {1 2 5 4 5}
301do_test rtree-6.1.3 {
302 execsql { UPDATE t3 SET ii = 2 }
303 execsql_intout { SELECT * FROM t3 }
304} {2 2 5 4 5}
305
306#----------------------------------------------------------------------------
307# Test cases rtree-7.* test rename operations.
308#
309do_test rtree-7.1.1 {
310 execsql {
311 CREATE VIRTUAL TABLE t4 USING rtree(ii, x1, x2, y1, y2, z1, z2);
312 INSERT INTO t4 VALUES(1, 2, 3, 4, 5, 6, 7);
313 }
314} {}
315do_test rtree-7.1.2 {
316 execsql { ALTER TABLE t4 RENAME TO t5 }
317 execsql_intout { SELECT * FROM t5 }
318} {1 2 3 4 5 6 7}
319do_test rtree-7.1.3 {
320 db close
321 sqlite3 db test.db
322 execsql_intout { SELECT * FROM t5 }
323} {1 2 3 4 5 6 7}
324do_test rtree-7.1.4 {
325 execsql { ALTER TABLE t5 RENAME TO 'raisara "one"'''}
326 execsql_intout { SELECT * FROM "raisara ""one""'" }
327} {1 2 3 4 5 6 7}
328do_test rtree-7.1.5 {
329 execsql_intout { SELECT * FROM 'raisara "one"''' }
330} {1 2 3 4 5 6 7}
331do_test rtree-7.1.6 {
332 execsql { ALTER TABLE "raisara ""one""'" RENAME TO "abc 123" }
333 execsql_intout { SELECT * FROM "abc 123" }
334} {1 2 3 4 5 6 7}
335do_test rtree-7.1.7 {
336 db close
337 sqlite3 db test.db
338 execsql_intout { SELECT * FROM "abc 123" }
339} {1 2 3 4 5 6 7}
340
341# An error midway through a rename operation.
342do_test rtree-7.2.1 {
343 execsql {
344 CREATE TABLE t4_node(a);
345 }
346 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
347} {1 {SQL logic error}}
348do_test rtree-7.2.2 {
349 execsql_intout { SELECT * FROM "abc 123" }
350} {1 2 3 4 5 6 7}
351do_test rtree-7.2.3 {
352 execsql {
353 DROP TABLE t4_node;
354 CREATE TABLE t4_rowid(a);
355 }
356 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
357} {1 {SQL logic error}}
358do_test rtree-7.2.4 {
359 db close
360 sqlite3 db test.db
361 execsql_intout { SELECT * FROM "abc 123" }
362} {1 2 3 4 5 6 7}
363do_test rtree-7.2.5 {
364 execsql { DROP TABLE t4_rowid }
365 execsql { ALTER TABLE "abc 123" RENAME TO t4 }
366 execsql_intout { SELECT * FROM t4 }
367} {1 2 3 4 5 6 7}
368
369
370#----------------------------------------------------------------------------
371# Test cases rtree-8.*
372#
373
374# Test that the function to determine if a leaf cell is part of the
375# result set works.
376do_test rtree-8.1.1 {
377 execsql {
378 CREATE VIRTUAL TABLE t6 USING rtree(ii, x1, x2);
379 INSERT INTO t6 VALUES(1, 3, 7);
380 INSERT INTO t6 VALUES(2, 4, 6);
381 }
382} {}
383do_test rtree-8.1.2 { execsql { SELECT ii FROM t6 WHERE x1>2 } } {1 2}
384do_test rtree-8.1.3 { execsql { SELECT ii FROM t6 WHERE x1>3 } } {2}
385do_test rtree-8.1.4 { execsql { SELECT ii FROM t6 WHERE x1>4 } } {}
386do_test rtree-8.1.5 { execsql { SELECT ii FROM t6 WHERE x1>5 } } {}
387do_test rtree-8.1.6 { execsql { SELECT ii FROM t6 WHERE x1>''} } {}
388do_test rtree-8.1.7 { execsql { SELECT ii FROM t6 WHERE x1>null}} {}
389do_test rtree-8.1.8 { execsql { SELECT ii FROM t6 WHERE x1>'2'} } {1 2}
390do_test rtree-8.1.9 { execsql { SELECT ii FROM t6 WHERE x1>'3'} } {2}
391do_test rtree-8.2.2 { execsql { SELECT ii FROM t6 WHERE x1>=2 } } {1 2}
392do_test rtree-8.2.3 { execsql { SELECT ii FROM t6 WHERE x1>=3 } } {1 2}
393do_test rtree-8.2.4 { execsql { SELECT ii FROM t6 WHERE x1>=4 } } {2}
394do_test rtree-8.2.5 { execsql { SELECT ii FROM t6 WHERE x1>=5 } } {}
395do_test rtree-8.2.6 { execsql { SELECT ii FROM t6 WHERE x1>=''} } {}
396do_test rtree-8.2.7 { execsql { SELECT ii FROM t6 WHERE x1>=null}} {}
397do_test rtree-8.2.8 { execsql { SELECT ii FROM t6 WHERE x1>='4'} } {2}
398do_test rtree-8.2.9 { execsql { SELECT ii FROM t6 WHERE x1>='5'} } {}
399do_test rtree-8.3.2 { execsql { SELECT ii FROM t6 WHERE x1<2 } } {}
400do_test rtree-8.3.3 { execsql { SELECT ii FROM t6 WHERE x1<3 } } {}
401do_test rtree-8.3.4 { execsql { SELECT ii FROM t6 WHERE x1<4 } } {1}
402do_test rtree-8.3.5 { execsql { SELECT ii FROM t6 WHERE x1<5 } } {1 2}
403do_test rtree-8.3.6 { execsql { SELECT ii FROM t6 WHERE x1<''} } {1 2}
404do_test rtree-8.3.7 { execsql { SELECT ii FROM t6 WHERE x1<null}} {}
405do_test rtree-8.3.8 { execsql { SELECT ii FROM t6 WHERE x1<'3'} } {}
406do_test rtree-8.3.9 { execsql { SELECT ii FROM t6 WHERE x1<'4'} } {1}
407do_test rtree-8.4.2 { execsql { SELECT ii FROM t6 WHERE x1<=2 } } {}
408do_test rtree-8.4.3 { execsql { SELECT ii FROM t6 WHERE x1<=3 } } {1}
409do_test rtree-8.4.4 { execsql { SELECT ii FROM t6 WHERE x1<=4 } } {1 2}
410do_test rtree-8.4.5 { execsql { SELECT ii FROM t6 WHERE x1<=5 } } {1 2}
411do_test rtree-8.4.6 { execsql { SELECT ii FROM t6 WHERE x1<=''} } {1 2}
412do_test rtree-8.4.7 { execsql { SELECT ii FROM t6 WHERE x1<=null}} {}
413do_test rtree-8.5.2 { execsql { SELECT ii FROM t6 WHERE x1=2 } } {}
414do_test rtree-8.5.3 { execsql { SELECT ii FROM t6 WHERE x1=3 } } {1}
415do_test rtree-8.5.4 { execsql { SELECT ii FROM t6 WHERE x1=4 } } {2}
416do_test rtree-8.5.5 { execsql { SELECT ii FROM t6 WHERE x1=5 } } {}
417do_test rtree-8.5.6 { execsql { SELECT ii FROM t6 WHERE x1=''} } {}
418do_test rtree-8.5.7 { execsql { SELECT ii FROM t6 WHERE x1=null}} {}
419
420
421#----------------------------------------------------------------------------
422# Test cases rtree-9.*
423#
424# Test that ticket #3549 is fixed.
425do_test rtree-9.1 {
426 execsql {
427 CREATE TABLE foo (id INTEGER PRIMARY KEY);
428 CREATE VIRTUAL TABLE bar USING rtree (id, minX, maxX, minY, maxY);
429 INSERT INTO foo VALUES (null);
430 INSERT INTO foo SELECT null FROM foo;
431 INSERT INTO foo SELECT null FROM foo;
432 INSERT INTO foo SELECT null FROM foo;
433 INSERT INTO foo SELECT null FROM foo;
434 INSERT INTO foo SELECT null FROM foo;
435 INSERT INTO foo SELECT null FROM foo;
436 DELETE FROM foo WHERE id > 40;
437 INSERT INTO bar SELECT NULL, 0, 0, 0, 0 FROM foo;
438 }
439} {}
440
441# This used to crash.
442do_test rtree-9.2 {
443 execsql {
444 SELECT count(*) FROM bar b1, bar b2, foo s1 WHERE s1.id = b1.id;
445 }
446} {1600}
447do_test rtree-9.3 {
448 execsql {
449 SELECT count(*) FROM bar b1, bar b2, foo s1
450 WHERE b1.minX <= b2.maxX AND s1.id = b1.id;
451 }
452} {1600}
453
454#-------------------------------------------------------------------------
455# Ticket #3970: Check that the error message is meaningful when a
456# keyword is used as a column name.
457#
458do_test rtree-10.1 {
459 catchsql { CREATE VIRTUAL TABLE t7 USING rtree(index, x1, y1, x2, y2) }
460} {1 {near "index": syntax error}}
461
462#-------------------------------------------------------------------------
463# Test last_insert_rowid().
464#
465do_test rtree-11.1 {
466 execsql {
467 CREATE VIRTUAL TABLE t8 USING rtree(idx, x1, x2, y1, y2);
468 INSERT INTO t8 VALUES(1, 1.0, 1.0, 2.0, 2.0);
469 SELECT last_insert_rowid();
470 }
471} {1}
472do_test rtree-11.2 {
473 execsql {
474 INSERT INTO t8 VALUES(NULL, 1.0, 1.0, 2.0, 2.0);
475 SELECT last_insert_rowid();
476 }
477} {2}
478
479#-------------------------------------------------------------------------
480# Test on-conflict clause handling.
481#
482db_delete_and_reopen
483do_execsql_test 12.0.1 {
484 CREATE VIRTUAL TABLE t1 USING rtree_i32(idx, x1, x2, y1, y2);
485 INSERT INTO t1 VALUES(1, 1, 2, 3, 4);
486 SELECT substr(hex(data),1,56) FROM t1_node;
487} {00000001000000000000000100000001000000020000000300000004}
488do_execsql_test 12.0.2 {
489 INSERT INTO t1 VALUES(2, 2, 3, 4, 5);
490 INSERT INTO t1 VALUES(3, 3, 4, 5, 6);
491
492 CREATE TABLE source(idx, x1, x2, y1, y2);
493 INSERT INTO source VALUES(5, 8, 8, 8, 8);
494 INSERT INTO source VALUES(2, 7, 7, 7, 7);
495}
496db_save_and_close
497foreach {tn sql_template testdata} {
498 1 "INSERT %CONF% INTO t1 VALUES(2, 7, 7, 7, 7)" {
499 ROLLBACK 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
500 ABORT 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
501 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
502 FAIL 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
503 REPLACE 0 0 {1 1 2 3 4 2 7 7 7 7 3 3 4 5 6 4 4 5 6 7}
504 }
505
506 2 "INSERT %CONF% INTO t1 SELECT * FROM source" {
507 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
508 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
509 IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7 5 8 8 8 8}
510 FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7 5 8 8 8 8}
511 REPLACE 1 0 {1 1 2 3 4 2 7 7 7 7 3 3 4 5 6 4 4 5 6 7 5 8 8 8 8}
512 }
513
514 3 "UPDATE %CONF% t1 SET idx = 2 WHERE idx = 4" {
515 ROLLBACK 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
516 ABORT 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
517 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
518 FAIL 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
519 REPLACE 0 0 {1 1 2 3 4 2 4 5 6 7 3 3 4 5 6}
520 }
521
522 3 "UPDATE %CONF% t1 SET idx = ((idx+1)%5)+1 WHERE idx > 2" {
523 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
524 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
525 IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
526 FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
527 REPLACE 1 0 {1 4 5 6 7 2 2 3 4 5 5 3 4 5 6}
528 }
529
530 4 "INSERT %CONF% INTO t1 VALUES(2, 7, 6, 7, 7)" {
531 ROLLBACK 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
532 ABORT 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
533 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
534 FAIL 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
535 REPLACE 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
536 }
537
538} {
539 foreach {mode uses error data} $testdata {
540 db_restore_and_reopen
541
542 set sql [string map [list %CONF% "OR $mode"] $sql_template]
543 set testname "12.$tn.[string tolower $mode]"
544
545 execsql {
546 BEGIN;
547 INSERT INTO t1 VALUES(4, 4, 5, 6, 7);
548 }
549
550 set res(0) {0 {}}
551 set res(1) {1 {UNIQUE constraint failed: t1.idx}}
552 set res(2) {1 {rtree constraint failed: t1.(x1<=x2)}}
553
554 do_catchsql_test $testname.1 $sql $res($error)
555 do_test $testname.2 [list sql_uses_stmt db $sql] $uses
556 do_execsql_test $testname.3 { SELECT * FROM t1 ORDER BY idx } $data
557
558 do_rtree_integrity_test $testname.4 t1
559 db close
560 }
561}
562
563#-------------------------------------------------------------------------
564# Test that bug [d2889096e7bdeac6d] has been fixed.
565#
566reset_db
567do_execsql_test 13.1 {
568 CREATE VIRTUAL TABLE t9 USING rtree(id, xmin, xmax);
569 INSERT INTO t9 VALUES(1,0,0);
570 INSERT INTO t9 VALUES(2,0,0);
571 SELECT * FROM t9 WHERE id IN (1, 2);
572} {1 0.0 0.0 2 0.0 0.0}
573
574do_execsql_test 13.2 {
575 WITH r(x) AS (
576 SELECT 1 UNION ALL
577 SELECT 2 UNION ALL
578 SELECT 3
579 )
580 SELECT * FROM r CROSS JOIN t9 WHERE id=x;
581} {1 1 0.0 0.0 2 2 0.0 0.0}
582
583#-------------------------------------------------------------------------
584# Test if a non-integer is inserted into the PK column of an r-tree
585# table, it is converted to an integer before being inserted. Also
586# that if a non-numeric is inserted into one of the min/max dimension
587# columns, it is converted to the required type before being inserted.
588#
589do_execsql_test 14.1 {
590 CREATE VIRTUAL TABLE t10 USING rtree(ii, x1, x2);
591}
592
593do_execsql_test 14.2 {
594 INSERT INTO t10 VALUES(NULL, 1, 2);
595 INSERT INTO t10 VALUES(NULL, 2, 3);
596 INSERT INTO t10 VALUES('4xxx', 3, 4);
597 INSERT INTO t10 VALUES(5.0, 4, 5);
598 INSERT INTO t10 VALUES(6.4, 5, 6);
599}
600do_execsql_test 14.3 {
601 SELECT * FROM t10;
602} {
603 1 1.0 2.0 2 2.0 3.0 4 3.0 4.0 5 4.0 5.0 6 5.0 6.0
604}
605
606do_execsql_test 14.4 {
607 DELETE FROM t10;
608 INSERT INTO t10 VALUES(1, 'one', 'two');
609 INSERT INTO t10 VALUES(2, '52xyz', '81...');
610}
611do_execsql_test 14.5 {
612 SELECT * FROM t10;
613} {
614 1 0.0 0.0
615 2 52.0 81.0
616}
617do_execsql_test 14.6 {
618 INSERT INTO t10 VALUES(0,10,20);
619 SELECT * FROM t10 WHERE ii=NULL;
620} {}
621do_execsql_test 14.7 {
622 SELECT * FROM t10 WHERE ii='xyz';
623} {}
624do_execsql_test 14.8 {
625 SELECT * FROM t10 WHERE ii='0.0';
626} {0 10.0 20.0}
627do_execsql_test 14.9 {
628 SELECT * FROM t10 WHERE ii=0.0;
629} {0 10.0 20.0}
630
631
632do_execsql_test 14.104 {
633 DROP TABLE t10;
634 CREATE VIRTUAL TABLE t10 USING rtree_i32(ii, x1, x2);
635 INSERT INTO t10 VALUES(1, 'one', 'two');
636 INSERT INTO t10 VALUES(2, '52xyz', '81...');
637 INSERT INTO t10 VALUES(3, 42.3, 49.9);
638}
639do_execsql_test 14.105 {
640 SELECT * FROM t10;
641} {
642 1 0 0
643 2 52 81
644 3 42 49
645}
646
647#-------------------------------------------------------------------------
648#
649do_execsql_test 15.0 {
650 CREATE VIRTUAL TABLE rt USING rtree(id, x1,x2, y1,y2);
651 CREATE TEMP TABLE t13(a, b, c);
652}
653do_execsql_test 15.1 {
654 BEGIN;
655 INSERT INTO rt VALUES(1,2,3,4,5);
656}
657do_execsql_test 15.2 {
658 DROP TABLE t13;
659 COMMIT;
660}
661
662# Test cases for the new auxiliary columns feature
663#
664do_catchsql_test 16.100 {
665 CREATE VIRTUAL TABLE t16 USING rtree(id,x0,x1,y0,+aux1,x1);
666} {1 {Auxiliary rtree columns must be last}}
667do_test 16.110 {
668 set sql {
669 CREATE VIRTUAL TABLE t16 USING rtree(
670 id, x00, x01, x10, x11, x20, x21, x30, x31, x40, x41
671 }
672 for {set i 12} {$i<=100} {incr i} {
673 append sql ", +a$i"
674 }
675 append sql ");"
676 execsql $sql
677} {}
678do_test 16.120 {
679 set sql {
680 CREATE VIRTUAL TABLE t16b USING rtree(
681 id, x00, x01, x10, x11, x20, x21, x30, x31, x40, x41
682 }
683 for {set i 12} {$i<=101} {incr i} {
684 append sql ", +a$i"
685 }
686 append sql ");"
687 catchsql $sql
688} {1 {Too many columns for an rtree table}}
689
690do_execsql_test 16.130 {
691 DROP TABLE IF EXISTS rt1;
692 CREATE VIRTUAL TABLE rt1 USING rtree(id, x1, x2, +aux);
693 INSERT INTO rt1 VALUES(1, 1, 2, 'aux1');
694 INSERT INTO rt1 VALUES(2, 2, 3, 'aux2');
695 INSERT INTO rt1 VALUES(3, 3, 4, 'aux3');
696 INSERT INTO rt1 VALUES(4, 4, 5, 'aux4');
697 SELECT * FROM rt1 WHERE id IN (1, 2, 3, 4);
698} {1 1.0 2.0 aux1 2 2.0 3.0 aux2 3 3.0 4.0 aux3 4 4.0 5.0 aux4}
699
700reset_db
701do_execsql_test 17.0 {
702 CREATE VIRTUAL TABLE t1 USING rtree(id, x1 PRIMARY KEY, x2, y1, y2);
703 CREATE VIRTUAL TABLE t2 USING rtree(id, x1, x2, y1, y2 UNIQUE);
704}
705do_execsql_test 17.1 {
706 REINDEX t1;
707 REINDEX t2;
708} {}
709
710do_execsql_test 17.2 {
711 REINDEX;
712} {}
713
714reset_db
715do_execsql_test 18.0 {
716 CREATE VIRTUAL TABLE rt0 USING rtree(c0, c1, c2);
717 INSERT INTO rt0(c0,c1,c2) VALUES(9,2,3);
718 SELECT c0 FROM rt0 WHERE rt0.c1 > '-1';
719 SELECT rt0.c1 > '-1' FROM rt0;
720} {9 1}
721
722expand_all_sql db
723
724# 2020-02-28 ticket e63b4d1a65546532
725reset_db
726do_execsql_test 19.0 {
727 CREATE VIRTUAL TABLE rt0 USING rtree(a,b,c);
728 INSERT INTO rt0(a,b,c) VALUES(0,0.0,0.0);
729 CREATE VIEW v0(x) AS SELECT DISTINCT rt0.b FROM rt0;
730 SELECT v0.x FROM v0, rt0;
731} {0.0}
732do_execsql_test 19.1 {
733 SELECT v0.x FROM v0, rt0 WHERE v0.x = rt0.b;
734} {0.0}
735
736# 2022-06-20 https://sqlite.org/forum/forumpost/57bdf2217d
737#
738reset_db
739do_execsql_test 20.0 {
740 CREATE VIRTUAL TABLE rt0 USING rtree(id, x0, x1);
741 CREATE TABLE t0(a INT);
742 CREATE TABLE t1(b INT);
743 INSERT INTO rt0 VALUES(0, 0, 0);
744}
745do_catchsql_test 20.1 {
746 SELECT * FROM t1 JOIN t0 ON x0>a RIGHT JOIN rt0 ON true WHERE +x0 = 0;
747} {1 {ON clause references tables to its right}}
748do_catchsql_test 20.2 {
749 SELECT * FROM t1 JOIN t0 ON x0>a RIGHT JOIN rt0 ON true WHERE x0 = 0;
750} {1 {ON clause references tables to its right}}
751db null -
752do_execsql_test 20.3 {
753 SELECT * FROM t1 JOIN t0 ON true RIGHT JOIN rt0 ON x0>a WHERE +x0 = 0;
754} {- - 0 0.0 0.0}
755do_execsql_test 20.4 {
756 SELECT * FROM t1 JOIN t0 ON true RIGHT JOIN rt0 ON x0>a WHERE x0 = 0;
757} {- - 0 0.0 0.0}
758
759# 2023-05-19 https://sqlite.org/forum/forumpost/da61c4a1b5b4af19
760# Do not omit constraints that involve equality comparisons of
761# floating-point values.
762#
763reset_db
764do_execsql_test 21.0 {
765 CREATE VIRTUAL TABLE t1 USING rtree(id, x0, x1);
766 INSERT INTO t1 VALUES(0, 1, 9223372036854775807);
767 SELECT count(*) FROM t1 WHERE x1=9223372036854775807;
768} {0}
769do_execsql_test 21.1 {
770 SELECT x1=9223372036854775807 FROM t1;
771} {0}
772
773# 2023-05-22 https://sqlite.org/forum/forumpost/da70ee0d0d
774# Round-off error associated with using large integer constraints on
775# a rtree search.
776#
777if {$tcl_platform(machine)!="i686" || $tcl_platform(os)!="Linux"} {
778 reset_db
779 do_execsql_test 22.0 {
780 CREATE VIRTUAL TABLE t1 USING rtree ( id, x0, x1 );
781 INSERT INTO t1 VALUES (123, 9223372036854775799, 9223372036854775800);
782 SELECT id FROM t1 WHERE x0 > 9223372036854775807;
783 } {123}
784 do_execsql_test 22.1 {
785 SELECT id, x0 > 9223372036854775807 AS 'a0' FROM t1;
786 } {123 1}
787}
788
789# 2023-10-14 dbsqlfuzz --sql-fuzz find. rtreecheck() should not call
790# BEGIN/COMMIT because that causes problems with statement transactions,
791# and it is unnecessary.
792#
793reset_db
794do_test 23.0 {
795 db eval {CREATE TABLE t1(a,b,c);}
796 catch {db eval {CREATE TABLE t2 AS SELECT rtreecheck('t1') AS y;}}
797 db eval {PRAGMA integrity_check;}
798} {ok}
799
800reset_db
801do_execsql_test 24.0 {
802 CREATE VIRTUAL TABLE rt1 USING rtree_i32(rid, c1, c2);
803 INSERT INTO rt1(rid, c1, c2) VALUES (9223372036854775807, 10, 18);
804}
805
806do_execsql_test 24.1 {
807 SELECT (rid = (CAST (9223372036854775807 AS REAL)))
808 FROM rt1 WHERE
809 (rid = (CAST (9223372036854775807 AS REAL)));
810}
811
812do_execsql_test 24.2 {
813 DELETE FROM rt1;
814 INSERT INTO rt1(rid, c1, c2) VALUES(1,2,3);
815 SELECT * FROM rt1 WHERE rid=1.005;
816} {}
817
818# Bug 2026-09-19T00:36:32Z
819#
820# Rounding double->float should work even for sub-normal or
821# near infinite values.
822#
823do_execsql_test 25.1 {
824 CREATE VIRTUAL TABLE t1 USING rtree(id,a,b);
825 INSERT INTO t1 VALUES(1,1e-39,1e-39);
826 SELECT id FROM t1 WHERE a<=1e-32 AND b>=1e-39;
827} 1
828foreach {id v} {
829 2 1e-40
830 3 -1e-39
831 4 -1e-40
832 5 3.1234e+38
833 6 -3.1234e+38
834 7 5e-324
835 8 -5e-324
836 9 3.5e+38
837 10 -3.5e+38
838} {
839 do_execsql_test 25.$id "
840 DELETE FROM t1;
841 INSERT INTO t1 VALUES(1,$v,$v);
842 SELECT id FROM t1 WHERE a<=$v AND b>=$v;
843 " 1
844}
845
846# Bug 2026-09-23T02:37:39Z
847#
848# The reported is troubled by the long-standing behavior that integer
849# coordinates to rtree_i32 are truncated to 32-bits. So, instead of
850# truncating, cap the values to the minimum and maximum 32-bit signed
851# integers.
852#
853reset_db
854do_execsql_test 26.1 {
855 CREATE VIRTUAL TABLE t1 USING rtree_i32(id,a,b);
856}
857foreach {id a b ok} {
858 2 2147483647 2147483648 1
859 3 2147483000 4294967296 1
860 4 -2147483649 -2147483648 1
861 5 4294967296 10 0
862 6 8589934592 5 0
863 7 2147483648 -2147483647 0
864 8 20 10 0
865} {
866 if {$ok} {
867 do_execsql_test 26.$i "INSERT INTO t1 VALUES($id,$a,$b);" {}
868 } else {
869 do_catchsql_test 26.$i "INSERT INTO t1 VALUES($id,$a,$b);" \
870 {1 {rtree constraint failed: t1.(a<=b)}}
871 }
872}
873
874# Bug 2026-10-20T20:37:14Z
875reset_db
876do_execsql_test 27.0 {
877 CREATE TABLE t1(a);
878 BEGIN;
879 INSERT INTO t1 VALUES (1);
880 SAVEPOINT s0;
881 CREATE VIRTUAL TABLE t2 USING rtree(id,x0,x1);
882 INSERT INTO t2 VALUES (88,-4.6,22.3);
883 ROLLBACK TO s0;
884 SAVEPOINT s1;
885 COMMIT;
886 SELECT count(*) FROM t1;
887} 1
888
889#-------------------------------------------------------------------------
890# Bug 2026-10-21T05:31:28Z. A new rowid assigned using the names "rowid",
891# "oid" or "_rowid_", instead of the name of the first column, was
892# ignored by UPDATE and INSERT statements.
893#
894foreach {tn mod} {1 rtree 2 rtree_i32} {
895 reset_db
896 do_execsql_test 28.$tn.0 "
897 CREATE VIRTUAL TABLE r USING $mod (id, x1, x2, +aux);
898 INSERT INTO r VALUES (1, 0, 1, 'a'), (2, 2, 3, 'b');
899 "
900
901 do_execsql_test 28.$tn.1 {
902 UPDATE r SET rowid = 9 WHERE id = 2;
903 SELECT changes();
904 SELECT id, CAST(x1 AS INT), CAST(x2 AS INT), aux FROM r ORDER BY id;
905 } {1 1 0 1 a 9 2 3 b}
906
907 do_execsql_test 28.$tn.2 {
908 UPDATE r SET oid = 8, x2 = 7 WHERE id = 1;
909 SELECT id, CAST(x1 AS INT), CAST(x2 AS INT), aux FROM r ORDER BY id;
910 } {8 0 7 a 9 2 3 b}
911
912 do_execsql_test 28.$tn.3 {
913 UPDATE r SET _rowid_ = 6, aux = 'c' WHERE id = 9;
914 SELECT id, CAST(x1 AS INT), CAST(x2 AS INT), aux FROM r ORDER BY id;
915 } {6 2 3 c 8 0 7 a}
916
917 do_execsql_test 28.$tn.4 {
918 INSERT INTO r(rowid, x1, x2, aux) VALUES (5, 4, 5, 'd');
919 SELECT last_insert_rowid();
920 SELECT id, CAST(x1 AS INT), CAST(x2 AS INT), aux FROM r ORDER BY id;
921 } {5 5 4 5 d 6 2 3 c 8 0 7 a}
922
923 # Updates that do not assign the rowid do not move the row.
924 do_execsql_test 28.$tn.5 {
925 UPDATE r SET x2 = 6 WHERE id = 5;
926 UPDATE r SET aux = 'e' WHERE id = 5;
927 SELECT id, CAST(x1 AS INT), CAST(x2 AS INT), aux FROM r ORDER BY id;
928 } {5 4 6 e 6 2 3 c 8 0 7 a}
929
930 # A multi-row UPDATE. This uses the two-pass strategy, so the
931 # "no-change" values are stored in an ephemeral table.
932 do_execsql_test 28.$tn.6 {
933 UPDATE r SET rowid = id + 100;
934 SELECT rowid, id, CAST(x1 AS INT), CAST(x2 AS INT), aux FROM r ORDER BY id;
935 } {105 105 4 6 e 106 106 2 3 c 108 108 0 7 a}
936
937 # A new rowid that is already in use.
938 do_catchsql_test 28.$tn.7 {
939 UPDATE r SET rowid = 106 WHERE id = 105;
940 } {1 {UNIQUE constraint failed: r.id}}
941 do_catchsql_test 28.$tn.8 {
942 INSERT INTO r(rowid, x1, x2) VALUES (106, 0, 0);
943 } {1 {UNIQUE constraint failed: r.id}}
944 do_execsql_test 28.$tn.9 {
945 UPDATE OR REPLACE r SET rowid = 106 WHERE id = 105;
946 SELECT id, CAST(x1 AS INT), CAST(x2 AS INT), aux FROM r ORDER BY id;
947 } {106 4 6 e 108 0 7 a}
948 do_execsql_test 28.$tn.10 {
949 INSERT OR REPLACE INTO r(rowid, x1, x2, aux) VALUES (108, 1, 2, 'f');
950 SELECT id, CAST(x1 AS INT), CAST(x2 AS INT), aux FROM r ORDER BY id;
951 } {106 4 6 e 108 1 2 f}
952
953 # UPDATE ... FROM
954 do_execsql_test 28.$tn.11 {
955 CREATE TABLE m(o, n);
956 INSERT INTO m VALUES(106, 16), (108, 18);
957 UPDATE r SET rowid = m.n FROM m WHERE m.o = r.id;
958 SELECT id, CAST(x1 AS INT), CAST(x2 AS INT), aux FROM r ORDER BY id;
959 } {16 4 6 e 18 1 2 f}
960
961 # Assigning the first column still works. If both the first column
962 # and the rowid are assigned, the value of the first column is used.
963 do_execsql_test 28.$tn.12 {
964 UPDATE r SET id = 26 WHERE id = 16;
965 UPDATE r SET id = 28, rowid = 38 WHERE id = 18;
966 INSERT INTO r(id, x1, x2) VALUES(30, 0, 0);
967 INSERT INTO r(rowid, id, x1, x2) VALUES(41, 31, 0, 0);
968 SELECT id FROM r ORDER BY id;
969 } {26 28 30 31}
970
971 do_execsql_test 28.$tn.13 {
972 SELECT rtreecheck('r');
973 } {ok}
974}
975
976#-------------------------------------------------------------------------
977# Bug 2026-10-21T05:30:42Z. In a geopoly table, an UPDATE that changed
978# the rowid without assigning _shape failed with "_shape does not
979# contain a valid polygon".
980#
981ifcapable geopoly {
982 reset_db
983 do_execsql_test 29.0 {
984 CREATE VIRTUAL TABLE g USING geopoly(a);
985 INSERT INTO g(rowid, _shape, a) VALUES
986 (1, '[[0,0],[1,0],[0,1],[0,0]]', 'x'),
987 (2, '[[5,5],[6,5],[5,6],[5,5]]', 'y');
988 }
989
990 do_execsql_test 29.1 {
991 UPDATE g SET rowid = 7 WHERE rowid = 1;
992 UPDATE g SET rowid = 8, _shape = _shape WHERE rowid = 2;
993 SELECT rowid, a, geopoly_json(_shape) FROM g ORDER BY rowid;
994 } {7 x {[[0.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,0.0]]}
995 8 y {[[5.0,5.0],[6.0,5.0],[5.0,6.0],[5.0,5.0]]}}
996
997 # The moved row is found by the r-tree using its new rowid.
998 do_execsql_test 29.2 {
999 SELECT rowid FROM g
1000 WHERE geopoly_overlap(_shape, '[[0,0],[2,0],[0,2],[0,0]]');
1001 SELECT rowid FROM g
1002 WHERE geopoly_within(_shape, '[[-1,-1],[3,-1],[-1,3],[-1,-1]]');
1003 } {7 7}
1004
1005 # Rowid and an auxiliary column.
1006 do_execsql_test 29.3 {
1007 UPDATE g SET rowid = 9, a = 'z' WHERE rowid = 7;
1008 SELECT rowid, a, geopoly_area(_shape) FROM g ORDER BY rowid;
1009 } {8 y 0.5 9 z 0.5}
1010
1011 # Auxiliary column only, and the rowid set to its current value.
1012 do_execsql_test 29.4 {
1013 UPDATE g SET a = 'w' WHERE rowid = 9;
1014 UPDATE g SET rowid = 8 WHERE rowid = 8;
1015 SELECT rowid, a, geopoly_area(_shape) FROM g ORDER BY rowid;
1016 } {8 y 0.5 9 w 0.5}
1017
1018 # A multi-row UPDATE, which uses the two-pass strategy.
1019 do_execsql_test 29.5 {
1020 UPDATE g SET rowid = rowid + 100;
1021 SELECT rowid, a, geopoly_area(_shape) FROM g ORDER BY rowid;
1022 SELECT rowid FROM g
1023 WHERE geopoly_overlap(_shape, '[[4,4],[7,4],[4,7],[4,4]]');
1024 } {108 y 0.5 109 w 0.5 108}
1025
1026 # A new rowid that is already in use. The error names the first
1027 # column, as it does for an UPDATE that also assigns _shape.
1028 do_catchsql_test 29.6 {
1029 UPDATE g SET rowid = 108 WHERE rowid = 109;
1030 } {1 {UNIQUE constraint failed: g._shape}}
1031 do_execsql_test 29.7 {
1032 UPDATE OR IGNORE g SET rowid = 108 WHERE rowid = 109;
1033 SELECT rowid, a FROM g ORDER BY rowid;
1034 } {108 y 109 w}
1035 do_execsql_test 29.8 {
1036 UPDATE OR REPLACE g SET rowid = 108 WHERE rowid = 109;
1037 SELECT rowid, a, geopoly_area(_shape) FROM g ORDER BY rowid;
1038 } {108 w 0.5}
1039
1040 # UPDATE ... FROM, and an UPDATE inside a trigger.
1041 do_execsql_test 29.9 {
1042 INSERT INTO g(rowid, _shape, a) VALUES
1043 (3, '[[5,5],[7,5],[5,7],[5,5]]', 'v');
1044 CREATE TABLE m(o, n);
1045 INSERT INTO m VALUES(108, 18), (3, 13);
1046 UPDATE g SET rowid = m.n FROM m WHERE m.o = g.rowid;
1047 SELECT rowid, a, geopoly_area(_shape) FROM g ORDER BY rowid;
1048 } {13 v 2.0 18 w 0.5}
1049 do_execsql_test 29.10 {
1050 CREATE TABLE t(x);
1051 CREATE TRIGGER tr AFTER INSERT ON t BEGIN
1052 UPDATE g SET rowid = new.x WHERE rowid = 13;
1053 END;
1054 INSERT INTO t VALUES(23);
1055 SELECT rowid, a, geopoly_area(_shape) FROM g ORDER BY rowid;
1056 } {18 w 0.5 23 v 2.0}
1057
1058 do_execsql_test 29.11 {
1059 PRAGMA integrity_check;
1060 } {ok}
1061}
1062
1063#-------------------------------------------------------------------------
1064# Bug 2026-10-21T02:53:43Z. On a geopoly table, "rowid = ?" with a value
1065# that is not equal to any integer found the row whose rowid was the
1066# integer part of the value. R-Tree tables got this right, except that
1067# a text value in exponent form, like '1e1', was converted to an
1068# integer using only the digits before the "e".
1069#
1070# Each lookup is compared with the same lookup on an ordinary table.
1071#
1072reset_db
1073do_execsql_test 30.0 {
1074 CREATE TABLE o(a);
1075 CREATE VIRTUAL TABLE r USING rtree(id, x1, x2);
1076 INSERT INTO o(rowid) VALUES
1077 (0), (1), (6), (7), (10),
1078 (9223372036854775807), (-9223372036854775808);
1079 INSERT INTO r SELECT rowid, 0, 1 FROM o;
1080}
1081ifcapable geopoly {
1082 do_execsql_test 30.0.1 {
1083 CREATE VIRTUAL TABLE g USING geopoly();
1084 INSERT INTO g(rowid, _shape)
1085 SELECT rowid, '[[0,0],[1,0],[0,1],[0,0]]' FROM o;
1086 }
1087}
1088
1089set tn 0
1090foreach v {
1091 6 6.0 -0.0 '6' {' 6 '} '6.0' '6e0' '0.6e1' '1e1' '10.0e0'
1092 6.5 6.000000000000001 '6.5' '6abc' 'x' '0x6' x'36' NULL
1093 1e19 -1e19 9223372036854775807.0 '9223372036854775808'
1094 9223372036854775807 '9223372036854775807' -9223372036854775808
1095} {
1096 incr tn
1097 set expect [db eval "SELECT rowid FROM o WHERE rowid = $v"]
1098 do_execsql_test 30.$tn.1 "
1099 SELECT id FROM r WHERE id = $v
1100 " $expect
1101 do_execsql_test 30.$tn.2 "
1102 SELECT id FROM r WHERE rowid IN ($v, 7)
1103 " [lsort -integer [concat $expect 7]]
1104 ifcapable geopoly {
1105 do_execsql_test 30.$tn.3 "
1106 SELECT rowid FROM g WHERE rowid = $v
1107 " $expect
1108 do_execsql_test 30.$tn.4 "
1109 SELECT rowid FROM g WHERE rowid IN ($v, 7)
1110 " [lsort -integer [concat $expect 7]]
1111 }
1112}
1113
1114ifcapable geopoly {
1115 do_execsql_test 30.100 {
1116 CREATE TABLE t1(x);
1117 INSERT INTO t1 VALUES (6), (6.5), ('7abc'), ('1e1');
1118 SELECT t1.x, g.rowid FROM t1 LEFT JOIN g ON g.rowid = t1.x;
1119 } {6 6 6.5 {} 7abc {} 1e1 10}
1120 do_execsql_test 30.101 {
1121 DELETE FROM g WHERE rowid = 7.5;
1122 UPDATE g SET rowid = 70 WHERE rowid = '7abc';
1123 SELECT rowid FROM g WHERE rowid BETWEEN 0 AND 100;
1124 } {0 1 6 7 10}
1125 do_execsql_test 30.102 {
1126 DELETE FROM g WHERE rowid = 7.0;
1127 SELECT rowid FROM g WHERE rowid BETWEEN 0 AND 100;
1128 } {0 1 6 10}
1129 do_execsql_test 30.103 {
1130 SELECT t1.x, r.id FROM t1 LEFT JOIN r ON r.id = t1.x;
1131 } {6 6 6.5 {} 7abc {} 1e1 10}
1132}
1133
1134#-------------------------------------------------------------------------
1135# Bug 06c54816a3b5b776. Some malformed polygons were accepted as a valid
1136# (0,0) bounding box: a BLOB of 28 or more bytes whose header does not
1137# match its length, and TEXT that does not begin with '['. These must be
1138# rejected, the same as a short blob or text that starts with '['.
1139#
1140ifcapable geopoly {
1141 reset_db
1142 set box {[[10.0,10.0],[11.0,10.0],[11.0,11.0],[10.0,11.0],[10.0,10.0]]}
1143
1144 # geopoly_bbox() already returns NULL for each malformed value.
1145 foreach {tn val} {
1146 1 zeroblob(28)
1147 2 zeroblob(52)
1148 3 'junk'
1149 4 x'00'
1150 5 '[junk'
1151 } {
1152 do_execsql_test 31.1.$tn "SELECT quote(geopoly_bbox($val))" {NULL}
1153 }
1154
1155 # geopoly_group_bbox() must ignore the malformed values, not fold them
1156 # in as the point (0,0), and must return NULL when every value is bad.
1157 do_execsql_test 31.2 {
1158 CREATE TABLE t(x);
1159 INSERT INTO t VALUES('[[10,10],[11,10],[11,11],[10,11],[10,10]]');
1160 INSERT INTO t VALUES(zeroblob(28));
1161 SELECT geopoly_json(geopoly_group_bbox(x)) FROM t;
1162 } $box
1163 do_execsql_test 31.3 {
1164 UPDATE t SET x='junk' WHERE typeof(x)='blob';
1165 SELECT geopoly_json(geopoly_group_bbox(x)) FROM t;
1166 } $box
1167 do_execsql_test 31.4 {
1168 SELECT quote(geopoly_bbox(zeroblob(52))),
1169 quote(geopoly_json(geopoly_group_bbox(zeroblob(52))));
1170 } {NULL NULL}
1171
1172 # A geopoly virtual table must reject the same malformed values.
1173 do_execsql_test 31.5 {
1174 CREATE VIRTUAL TABLE g USING geopoly();
1175 }
1176 foreach {tn val} {
1177 1 x'00'
1178 2 '[junk'
1179 3 'junk'
1180 4 zeroblob(28)
1181 5 zeroblob(52)
1182 } {
1183 do_catchsql_test 31.6.$tn "INSERT INTO g(_shape) VALUES($val)" \
1184 {1 {_shape does not contain a valid polygon}}
1185 }
1186
1187 # A valid polygon is still accepted and found by geopoly_overlap().
1188 do_execsql_test 31.7 {
1189 INSERT INTO g(_shape) VALUES('[[10,10],[11,10],[11,11],[10,11],[10,10]]');
1190 SELECT rowid FROM g
1191 WHERE geopoly_overlap(_shape, '[[10,10],[12,10],[12,12],[10,12],[10,10]]');
1192 } {1}
1193}
1194
1195finish_test