xref: /sqlite-3.40.0/ext/rtree/rtree1.test (revision e99cb2da)
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.2 {
61  execsql { SELECT name FROM sqlite_master ORDER BY name }
62} {t1 t1_node t1_parent t1_rowid}
63do_test rtree-1.1.3 {
64  execsql {
65    DROP TABLE t1;
66    SELECT name FROM sqlite_master ORDER BY name;
67  }
68} {}
69
70# Test creating and dropping an rtree table with an odd name in
71# an attached database.
72#
73do_test rtree-1.2.1 {
74  file delete -force test2.db
75  execsql {
76    ATTACH 'test2.db' AS aux;
77    CREATE VIRTUAL TABLE aux.'a" "b' USING rtree(ii, x1, x2, y1, y2);
78  }
79} {}
80do_test rtree-1.2.2 {
81  execsql { SELECT name FROM sqlite_master ORDER BY name }
82} {}
83do_test rtree-1.2.3 {
84  execsql { SELECT name FROM aux.sqlite_master ORDER BY name }
85} {{a" "b} {a" "b_node} {a" "b_parent} {a" "b_rowid}}
86do_test rtree-1.2.4 {
87  execsql {
88    DROP TABLE aux.'a" "b';
89    SELECT name FROM aux.sqlite_master ORDER BY name;
90  }
91} {}
92
93# Test that the logic for checking the number of columns specified
94# for an rtree table. Acceptable values are odd numbers between 3 and
95# 11, inclusive.
96#
97set cols [list i1 i2 i3 i4 i5 i6 i7 i8 i9 iA iB iC iD iE iF iG iH iI iJ iK]
98for {set nCol 1} {$nCol<[llength $cols]} {incr nCol} {
99
100  set columns [join [lrange $cols 0 [expr {$nCol-1}]] ,]
101
102  set X {0 {}}
103  if {$nCol%2 == 0}  { set X {1 {Wrong number of columns for an rtree table}} }
104  if {$nCol < 3}     { set X {1 {Too few columns for an rtree table}} }
105  if {$nCol > 11}    { set X {1 {Too many columns for an rtree table}} }
106
107  do_test rtree-1.3.$nCol {
108    catchsql "
109      CREATE VIRTUAL TABLE t1 USING rtree($columns);
110    "
111  } $X
112
113  catchsql { DROP TABLE t1 }
114}
115do_catchsql_test rtree-1.3.1000 {
116  CREATE VIRTUAL TABLE t1000 USING rtree;
117} {1 {Too few columns for an rtree table}}
118
119# Like execsql except display output as integer where that can be
120# done without loss of information.
121#
122proc execsql_intout {sql} {
123  set out {}
124  foreach term [execsql $sql] {
125    regsub {\.0$} $term {} term
126    lappend out $term
127  }
128  return $out
129}
130
131# Test that it is possible to open an existing database that contains
132# r-tree tables.
133#
134do_execsql_test rtree-1.4.1a {
135  CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2);
136  INSERT INTO t1 VALUES(1, 5.0, 10.0);
137  SELECT substr(hex(data),1,40) FROM t1_node;
138} {00000001000000000000000140A0000041200000}
139do_execsql_test rtree-1.4.1b {
140  INSERT INTO t1 VALUES(2, 15.0, 20.0);
141} {}
142do_test rtree-1.4.2 {
143  db close
144  sqlite3 db test.db
145  execsql_intout { SELECT * FROM t1 ORDER BY ii }
146} {1 5 10 2 15 20}
147do_test rtree-1.4.3 {
148  execsql { DROP TABLE t1 }
149} {}
150
151# Test that it is possible to create an r-tree table with ridiculous
152# column names.
153#
154do_test rtree-1.5.1 {
155  execsql_intout {
156    CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
157    INSERT INTO t1 VALUES(1, 2, 3);
158    SELECT "the key", "x dim.", "x2'dim" FROM t1;
159  }
160} {1 2 3}
161do_test rtree-1.5.1 {
162  execsql { DROP TABLE t1 }
163} {}
164
165# Force the r-tree constructor to fail.
166#
167do_test rtree-1.6.1 {
168  execsql { CREATE TABLE t1_rowid(a); }
169  catchsql {
170    CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
171  }
172} {1 {table "t1_rowid" already exists}}
173do_test rtree-1.6.1 {
174  execsql { DROP TABLE t1_rowid }
175} {}
176
177#----------------------------------------------------------------------------
178# Test cases rtree-2.*
179#
180do_test rtree-2.1.1 {
181  execsql {
182    CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
183    SELECT * FROM t1;
184  }
185} {}
186
187do_test rtree-2.1.2 {
188  execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
189  execsql_intout { SELECT * FROM t1 }
190} {1 1 3 2 4}
191do_test rtree-2.1.3 {
192  execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
193  execsql { SELECT rowid FROM t1 ORDER BY rowid }
194} {1 2}
195do_test rtree-2.1.3 {
196  execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
197  execsql { SELECT ii FROM t1 ORDER BY ii }
198} {1 2 3}
199
200do_test rtree-2.2.1 {
201  catchsql { INSERT INTO t1 VALUES(2, 1, 3, 2, 4) }
202} {1 {UNIQUE constraint failed: t1.ii}}
203do_test rtree-2.2.2 {
204  catchsql { INSERT INTO t1 VALUES(4, 1, 3, 4, 2) }
205} {1 {rtree constraint failed: t1.(y1<=y2)}}
206do_test rtree-2.2.3 {
207  catchsql { INSERT INTO t1 VALUES(4, 3, 1, 2, 4) }
208} {1 {rtree constraint failed: t1.(x1<=x2)}}
209do_test rtree-2.2.4 {
210  execsql { SELECT ii FROM t1 ORDER BY ii }
211} {1 2 3}
212
213do_test rtree-2.X {
214  execsql { DROP TABLE t1 }
215} {}
216
217#----------------------------------------------------------------------------
218# Test cases rtree-3.* test linear scans of r-tree table data. To test
219# this we have to insert some data into an r-tree, but that is not the
220# focus of these tests.
221#
222do_test rtree-3.1.1 {
223  execsql {
224    CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
225    SELECT * FROM t1;
226  }
227} {}
228do_test rtree-3.1.2 {
229  execsql_intout {
230    INSERT INTO t1 VALUES(5, 1, 3, 2, 4);
231    SELECT * FROM t1;
232  }
233} {5 1 3 2 4}
234do_test rtree-3.1.3 {
235  execsql_intout {
236    INSERT INTO t1 VALUES(6, 2, 6, 4, 8);
237    SELECT * FROM t1;
238  }
239} {5 1 3 2 4 6 2 6 4 8}
240
241# Test the constraint on the coordinates (c[i]<=c[i+1] where (i%2==0)):
242do_test rtree-3.2.1 {
243  catchsql { INSERT INTO t1 VALUES(7, 2, 6, 4, 3) }
244} {1 {rtree constraint failed: t1.(y1<=y2)}}
245do_test rtree-3.2.2 {
246  catchsql { INSERT INTO t1 VALUES(8, 2, 6, 3, 3) }
247} {0 {}}
248
249#----------------------------------------------------------------------------
250# Test cases rtree-5.* test DELETE operations.
251#
252do_test rtree-5.1.1 {
253  execsql { CREATE VIRTUAL TABLE t2 USING rtree(ii, x1, x2) }
254} {}
255do_test rtree-5.1.2 {
256  execsql_intout {
257    INSERT INTO t2 VALUES(1, 10, 20);
258    INSERT INTO t2 VALUES(2, 30, 40);
259    INSERT INTO t2 VALUES(3, 50, 60);
260    SELECT * FROM t2 ORDER BY ii;
261  }
262} {1 10 20 2 30 40 3 50 60}
263do_test rtree-5.1.3 {
264  execsql_intout {
265    DELETE FROM t2 WHERE ii=2;
266    SELECT * FROM t2 ORDER BY ii;
267  }
268} {1 10 20 3 50 60}
269do_test rtree-5.1.4 {
270  execsql_intout {
271    DELETE FROM t2 WHERE ii=1;
272    SELECT * FROM t2 ORDER BY ii;
273  }
274} {3 50 60}
275do_test rtree-5.1.5 {
276  execsql {
277    DELETE FROM t2 WHERE ii=3;
278    SELECT * FROM t2 ORDER BY ii;
279  }
280} {}
281do_test rtree-5.1.6 {
282  execsql { SELECT * FROM t2_rowid }
283} {}
284
285#----------------------------------------------------------------------------
286# Test cases rtree-5.* test UPDATE operations.
287#
288do_test rtree-6.1.1 {
289  execsql { CREATE VIRTUAL TABLE t3 USING rtree(ii, x1, x2, y1, y2) }
290} {}
291do_test rtree-6.1.2 {
292  execsql_intout {
293    INSERT INTO t3 VALUES(1, 2, 3, 4, 5);
294    UPDATE t3 SET x2=5;
295    SELECT * FROM t3;
296  }
297} {1 2 5 4 5}
298do_test rtree-6.1.3 {
299  execsql { UPDATE t3 SET ii = 2 }
300  execsql_intout { SELECT * FROM t3 }
301} {2 2 5 4 5}
302
303#----------------------------------------------------------------------------
304# Test cases rtree-7.* test rename operations.
305#
306do_test rtree-7.1.1 {
307  execsql {
308    CREATE VIRTUAL TABLE t4 USING rtree(ii, x1, x2, y1, y2, z1, z2);
309    INSERT INTO t4 VALUES(1, 2, 3, 4, 5, 6, 7);
310  }
311} {}
312do_test rtree-7.1.2 {
313  execsql { ALTER TABLE t4 RENAME TO t5 }
314  execsql_intout { SELECT * FROM t5 }
315} {1 2 3 4 5 6 7}
316do_test rtree-7.1.3 {
317  db close
318  sqlite3 db test.db
319  execsql_intout { SELECT * FROM t5 }
320} {1 2 3 4 5 6 7}
321do_test rtree-7.1.4 {
322  execsql { ALTER TABLE t5 RENAME TO 'raisara "one"'''}
323  execsql_intout { SELECT * FROM "raisara ""one""'" }
324} {1 2 3 4 5 6 7}
325do_test rtree-7.1.5 {
326  execsql_intout { SELECT * FROM 'raisara "one"''' }
327} {1 2 3 4 5 6 7}
328do_test rtree-7.1.6 {
329  execsql { ALTER TABLE "raisara ""one""'" RENAME TO "abc 123" }
330  execsql_intout { SELECT * FROM "abc 123" }
331} {1 2 3 4 5 6 7}
332do_test rtree-7.1.7 {
333  db close
334  sqlite3 db test.db
335  execsql_intout { SELECT * FROM "abc 123" }
336} {1 2 3 4 5 6 7}
337
338# An error midway through a rename operation.
339do_test rtree-7.2.1 {
340  execsql {
341    CREATE TABLE t4_node(a);
342  }
343  catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
344} {1 {SQL logic error}}
345do_test rtree-7.2.2 {
346  execsql_intout { SELECT * FROM "abc 123" }
347} {1 2 3 4 5 6 7}
348do_test rtree-7.2.3 {
349  execsql {
350    DROP TABLE t4_node;
351    CREATE TABLE t4_rowid(a);
352  }
353  catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
354} {1 {SQL logic error}}
355do_test rtree-7.2.4 {
356  db close
357  sqlite3 db test.db
358  execsql_intout { SELECT * FROM "abc 123" }
359} {1 2 3 4 5 6 7}
360do_test rtree-7.2.5 {
361  execsql { DROP TABLE t4_rowid }
362  execsql { ALTER TABLE "abc 123" RENAME TO t4 }
363  execsql_intout { SELECT * FROM t4 }
364} {1 2 3 4 5 6 7}
365
366
367#----------------------------------------------------------------------------
368# Test cases rtree-8.*
369#
370
371# Test that the function to determine if a leaf cell is part of the
372# result set works.
373do_test rtree-8.1.1 {
374  execsql {
375    CREATE VIRTUAL TABLE t6 USING rtree(ii, x1, x2);
376    INSERT INTO t6 VALUES(1, 3, 7);
377    INSERT INTO t6 VALUES(2, 4, 6);
378  }
379} {}
380do_test rtree-8.1.2 { execsql { SELECT ii FROM t6 WHERE x1>2 } }   {1 2}
381do_test rtree-8.1.3 { execsql { SELECT ii FROM t6 WHERE x1>3 } }   {2}
382do_test rtree-8.1.4 { execsql { SELECT ii FROM t6 WHERE x1>4 } }   {}
383do_test rtree-8.1.5 { execsql { SELECT ii FROM t6 WHERE x1>5 } }   {}
384do_test rtree-8.1.6 { execsql { SELECT ii FROM t6 WHERE x1>''} }   {}
385do_test rtree-8.1.7 { execsql { SELECT ii FROM t6 WHERE x1>null}}  {}
386do_test rtree-8.1.8 { execsql { SELECT ii FROM t6 WHERE x1>'2'} }   {1 2}
387do_test rtree-8.1.9 { execsql { SELECT ii FROM t6 WHERE x1>'3'} }   {2}
388do_test rtree-8.2.2 { execsql { SELECT ii FROM t6 WHERE x1>=2 } }  {1 2}
389do_test rtree-8.2.3 { execsql { SELECT ii FROM t6 WHERE x1>=3 } }  {1 2}
390do_test rtree-8.2.4 { execsql { SELECT ii FROM t6 WHERE x1>=4 } }  {2}
391do_test rtree-8.2.5 { execsql { SELECT ii FROM t6 WHERE x1>=5 } }  {}
392do_test rtree-8.2.6 { execsql { SELECT ii FROM t6 WHERE x1>=''} }  {}
393do_test rtree-8.2.7 { execsql { SELECT ii FROM t6 WHERE x1>=null}} {}
394do_test rtree-8.2.8 { execsql { SELECT ii FROM t6 WHERE x1>='4'} } {2}
395do_test rtree-8.2.9 { execsql { SELECT ii FROM t6 WHERE x1>='5'} } {}
396do_test rtree-8.3.2 { execsql { SELECT ii FROM t6 WHERE x1<2 } }   {}
397do_test rtree-8.3.3 { execsql { SELECT ii FROM t6 WHERE x1<3 } }   {}
398do_test rtree-8.3.4 { execsql { SELECT ii FROM t6 WHERE x1<4 } }   {1}
399do_test rtree-8.3.5 { execsql { SELECT ii FROM t6 WHERE x1<5 } }   {1 2}
400do_test rtree-8.3.6 { execsql { SELECT ii FROM t6 WHERE x1<''} }   {1 2}
401do_test rtree-8.3.7 { execsql { SELECT ii FROM t6 WHERE x1<null}}  {}
402do_test rtree-8.3.8 { execsql { SELECT ii FROM t6 WHERE x1<'3'} }  {}
403do_test rtree-8.3.9 { execsql { SELECT ii FROM t6 WHERE x1<'4'} }  {1}
404do_test rtree-8.4.2 { execsql { SELECT ii FROM t6 WHERE x1<=2 } }  {}
405do_test rtree-8.4.3 { execsql { SELECT ii FROM t6 WHERE x1<=3 } }  {1}
406do_test rtree-8.4.4 { execsql { SELECT ii FROM t6 WHERE x1<=4 } }  {1 2}
407do_test rtree-8.4.5 { execsql { SELECT ii FROM t6 WHERE x1<=5 } }  {1 2}
408do_test rtree-8.4.6 { execsql { SELECT ii FROM t6 WHERE x1<=''} }  {1 2}
409do_test rtree-8.4.7 { execsql { SELECT ii FROM t6 WHERE x1<=null}} {}
410do_test rtree-8.5.2 { execsql { SELECT ii FROM t6 WHERE x1=2 } }   {}
411do_test rtree-8.5.3 { execsql { SELECT ii FROM t6 WHERE x1=3 } }   {1}
412do_test rtree-8.5.4 { execsql { SELECT ii FROM t6 WHERE x1=4 } }   {2}
413do_test rtree-8.5.5 { execsql { SELECT ii FROM t6 WHERE x1=5 } }   {}
414do_test rtree-8.5.6 { execsql { SELECT ii FROM t6 WHERE x1=''} }   {}
415do_test rtree-8.5.7 { execsql { SELECT ii FROM t6 WHERE x1=null}}  {}
416
417
418#----------------------------------------------------------------------------
419# Test cases rtree-9.*
420#
421# Test that ticket #3549 is fixed.
422do_test rtree-9.1 {
423  execsql {
424    CREATE TABLE foo (id INTEGER PRIMARY KEY);
425    CREATE VIRTUAL TABLE bar USING rtree (id, minX, maxX, minY, maxY);
426    INSERT INTO foo VALUES (null);
427    INSERT INTO foo SELECT null FROM foo;
428    INSERT INTO foo SELECT null FROM foo;
429    INSERT INTO foo SELECT null FROM foo;
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    DELETE FROM foo WHERE id > 40;
434    INSERT INTO bar SELECT NULL, 0, 0, 0, 0 FROM foo;
435  }
436} {}
437
438# This used to crash.
439do_test rtree-9.2 {
440  execsql {
441    SELECT count(*) FROM bar b1, bar b2, foo s1 WHERE s1.id = b1.id;
442  }
443} {1600}
444do_test rtree-9.3 {
445  execsql {
446    SELECT count(*) FROM bar b1, bar b2, foo s1
447    WHERE b1.minX <= b2.maxX AND s1.id = b1.id;
448  }
449} {1600}
450
451#-------------------------------------------------------------------------
452# Ticket #3970: Check that the error message is meaningful when a
453# keyword is used as a column name.
454#
455do_test rtree-10.1 {
456  catchsql { CREATE VIRTUAL TABLE t7 USING rtree(index, x1, y1, x2, y2) }
457} {1 {near "index": syntax error}}
458
459#-------------------------------------------------------------------------
460# Test last_insert_rowid().
461#
462do_test rtree-11.1 {
463  execsql {
464    CREATE VIRTUAL TABLE t8 USING rtree(idx, x1, x2, y1, y2);
465    INSERT INTO t8 VALUES(1, 1.0, 1.0, 2.0, 2.0);
466    SELECT last_insert_rowid();
467  }
468} {1}
469do_test rtree-11.2 {
470  execsql {
471    INSERT INTO t8 VALUES(NULL, 1.0, 1.0, 2.0, 2.0);
472    SELECT last_insert_rowid();
473  }
474} {2}
475
476#-------------------------------------------------------------------------
477# Test on-conflict clause handling.
478#
479db_delete_and_reopen
480do_execsql_test 12.0.1 {
481  CREATE VIRTUAL TABLE t1 USING rtree_i32(idx, x1, x2, y1, y2);
482  INSERT INTO t1 VALUES(1,   1, 2, 3, 4);
483  SELECT substr(hex(data),1,56) FROM t1_node;
484} {00000001000000000000000100000001000000020000000300000004}
485do_execsql_test 12.0.2 {
486  INSERT INTO t1 VALUES(2,   2, 3, 4, 5);
487  INSERT INTO t1 VALUES(3,   3, 4, 5, 6);
488
489  CREATE TABLE source(idx, x1, x2, y1, y2);
490  INSERT INTO source VALUES(5, 8, 8, 8, 8);
491  INSERT INTO source VALUES(2, 7, 7, 7, 7);
492}
493db_save_and_close
494foreach {tn sql_template testdata} {
495  1    "INSERT %CONF% INTO t1 VALUES(2, 7, 7, 7, 7)" {
496    ROLLBACK 0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6}
497    ABORT    0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
498    IGNORE   0 0 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
499    FAIL     0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
500    REPLACE  0 0 {1 1 2 3 4   2 7 7 7 7   3 3 4 5 6   4 4 5 6 7}
501  }
502
503  2    "INSERT %CONF% INTO t1 SELECT * FROM source" {
504    ROLLBACK 1 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6}
505    ABORT    1 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
506    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}
507    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}
508    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}
509  }
510
511  3    "UPDATE %CONF% t1 SET idx = 2 WHERE idx = 4" {
512    ROLLBACK 0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6}
513    ABORT    0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
514    IGNORE   0 0 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
515    FAIL     0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
516    REPLACE  0 0 {1 1 2 3 4   2 4 5 6 7   3 3 4 5 6}
517  }
518
519  3    "UPDATE %CONF% t1 SET idx = ((idx+1)%5)+1 WHERE idx > 2" {
520    ROLLBACK 1 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6}
521    ABORT    1 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
522    IGNORE   1 0 {1 1 2 3 4   2 2 3 4 5               4 4 5 6 7   5 3 4 5 6}
523    FAIL     1 1 {1 1 2 3 4   2 2 3 4 5               4 4 5 6 7   5 3 4 5 6}
524    REPLACE  1 0 {1 4 5 6 7   2 2 3 4 5                           5 3 4 5 6}
525  }
526
527  4    "INSERT %CONF% INTO t1 VALUES(2, 7, 6, 7, 7)" {
528    ROLLBACK 0 2 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6}
529    ABORT    0 2 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
530    IGNORE   0 0 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
531    FAIL     0 2 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
532    REPLACE  0 2 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
533  }
534
535} {
536  foreach {mode uses error data} $testdata {
537    db_restore_and_reopen
538
539    set sql [string map [list %CONF% "OR $mode"] $sql_template]
540    set testname "12.$tn.[string tolower $mode]"
541
542    execsql {
543      BEGIN;
544        INSERT INTO t1 VALUES(4,   4, 5, 6, 7);
545    }
546
547    set res(0) {0 {}}
548    set res(1) {1 {UNIQUE constraint failed: t1.idx}}
549    set res(2) {1 {rtree constraint failed: t1.(x1<=x2)}}
550
551    do_catchsql_test $testname.1 $sql $res($error)
552    do_test $testname.2 [list sql_uses_stmt db $sql] $uses
553    do_execsql_test $testname.3 { SELECT * FROM t1 ORDER BY idx } $data
554
555    do_rtree_integrity_test $testname.4 t1
556    db close
557  }
558}
559
560#-------------------------------------------------------------------------
561# Test that bug [d2889096e7bdeac6d] has been fixed.
562#
563reset_db
564do_execsql_test 13.1 {
565  CREATE VIRTUAL TABLE t9 USING rtree(id, xmin, xmax);
566  INSERT INTO t9 VALUES(1,0,0);
567  INSERT INTO t9 VALUES(2,0,0);
568  SELECT * FROM t9 WHERE id IN (1, 2);
569} {1 0.0 0.0 2 0.0 0.0}
570
571do_execsql_test 13.2 {
572  WITH r(x) AS (
573    SELECT 1 UNION ALL
574    SELECT 2 UNION ALL
575    SELECT 3
576  )
577  SELECT * FROM r CROSS JOIN t9 WHERE id=x;
578} {1 1 0.0 0.0 2 2 0.0 0.0}
579
580#-------------------------------------------------------------------------
581# Test if a non-integer is inserted into the PK column of an r-tree
582# table, it is converted to an integer before being inserted. Also
583# that if a non-numeric is inserted into one of the min/max dimension
584# columns, it is converted to the required type before being inserted.
585#
586do_execsql_test 14.1 {
587  CREATE VIRTUAL TABLE t10 USING rtree(ii, x1, x2);
588}
589
590do_execsql_test 14.2 {
591  INSERT INTO t10 VALUES(NULL,   1, 2);
592  INSERT INTO t10 VALUES(NULL,   2, 3);
593  INSERT INTO t10 VALUES('4xxx', 3, 4);
594  INSERT INTO t10 VALUES(5.0,    4, 5);
595  INSERT INTO t10 VALUES(6.4,    5, 6);
596}
597do_execsql_test 14.3 {
598  SELECT * FROM t10;
599} {
600  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
601}
602
603do_execsql_test 14.4 {
604  DELETE FROM t10;
605  INSERT INTO t10 VALUES(1, 'one', 'two');
606  INSERT INTO t10 VALUES(2, '52xyz', '81...');
607}
608do_execsql_test 14.5 {
609  SELECT * FROM t10;
610} {
611  1 0.0 0.0
612  2 52.0 81.0
613}
614do_execsql_test 14.6 {
615  INSERT INTO t10 VALUES(0,10,20);
616  SELECT * FROM t10 WHERE ii=NULL;
617} {}
618do_execsql_test 14.7 {
619  SELECT * FROM t10 WHERE ii='xyz';
620} {}
621do_execsql_test 14.8 {
622  SELECT * FROM t10 WHERE ii='0.0';
623} {0 10.0 20.0}
624do_execsql_test 14.9 {
625  SELECT * FROM t10 WHERE ii=0.0;
626} {0 10.0 20.0}
627
628
629do_execsql_test 14.104 {
630  DROP TABLE t10;
631  CREATE VIRTUAL TABLE t10 USING rtree_i32(ii, x1, x2);
632  INSERT INTO t10 VALUES(1, 'one', 'two');
633  INSERT INTO t10 VALUES(2, '52xyz', '81...');
634  INSERT INTO t10 VALUES(3, 42.3, 49.9);
635}
636do_execsql_test 14.105 {
637  SELECT * FROM t10;
638} {
639  1 0 0
640  2 52 81
641  3 42 49
642}
643
644#-------------------------------------------------------------------------
645#
646do_execsql_test 15.0 {
647  CREATE VIRTUAL TABLE rt USING rtree(id, x1,x2, y1,y2);
648  CREATE TEMP TABLE t13(a, b, c);
649}
650do_execsql_test 15.1 {
651  BEGIN;
652  INSERT INTO rt VALUES(1,2,3,4,5);
653}
654do_execsql_test 15.2 {
655  DROP TABLE t13;
656  COMMIT;
657}
658
659# Test cases for the new auxiliary columns feature
660#
661do_catchsql_test 16.100 {
662  CREATE VIRTUAL TABLE t16 USING rtree(id,x0,x1,y0,+aux1,x1);
663} {1 {Auxiliary rtree columns must be last}}
664do_test 16.110 {
665  set sql {
666    CREATE VIRTUAL TABLE t16 USING rtree(
667      id, x00, x01, x10, x11, x20, x21, x30, x31, x40, x41
668  }
669  for {set i 12} {$i<=100} {incr i} {
670     append sql ", +a$i"
671  }
672  append sql ");"
673  execsql $sql
674} {}
675do_test 16.120 {
676  set sql {
677    CREATE VIRTUAL TABLE t16b USING rtree(
678      id, x00, x01, x10, x11, x20, x21, x30, x31, x40, x41
679  }
680  for {set i 12} {$i<=101} {incr i} {
681     append sql ", +a$i"
682  }
683  append sql ");"
684  catchsql $sql
685} {1 {Too many columns for an rtree table}}
686
687do_execsql_test 16.130 {
688  DROP TABLE IF EXISTS rt1;
689  CREATE VIRTUAL TABLE rt1 USING rtree(id, x1, x2, +aux);
690  INSERT INTO rt1 VALUES(1, 1, 2, 'aux1');
691  INSERT INTO rt1 VALUES(2, 2, 3, 'aux2');
692  INSERT INTO rt1 VALUES(3, 3, 4, 'aux3');
693  INSERT INTO rt1 VALUES(4, 4, 5, 'aux4');
694  SELECT * FROM rt1 WHERE id IN (1, 2, 3, 4);
695} {1 1.0 2.0 aux1 2 2.0 3.0 aux2 3 3.0 4.0 aux3 4 4.0 5.0 aux4}
696
697reset_db
698do_execsql_test 17.0 {
699  CREATE VIRTUAL TABLE t1 USING rtree(id, x1 PRIMARY KEY, x2, y1, y2);
700  CREATE VIRTUAL TABLE t2 USING rtree(id, x1, x2, y1, y2 UNIQUE);
701}
702do_execsql_test 17.1 {
703  REINDEX t1;
704  REINDEX t2;
705} {}
706
707do_execsql_test 17.2 {
708  REINDEX;
709} {}
710
711reset_db
712do_execsql_test 18.0 {
713  CREATE VIRTUAL TABLE rt0 USING rtree(c0, c1, c2);
714  INSERT INTO rt0(c0,c1,c2) VALUES(9,2,3);
715  SELECT c0 FROM rt0 WHERE rt0.c1 > '-1';
716  SELECT rt0.c1 > '-1' FROM rt0;
717} {9 1}
718
719
720expand_all_sql db
721finish_test
722