xref: /sqlite-3.40.0/ext/rtree/README (revision ebaecc14)
1*ebaecc14Sdanielk1977
2*ebaecc14Sdanielk1977This directory contains an SQLite extension that implements a virtual
3*ebaecc14Sdanielk1977table type that allows users to create, query and manipulate r-tree[1]
4*ebaecc14Sdanielk1977data structures inside of SQLite databases. Users create, populate
5*ebaecc14Sdanielk1977and query r-tree structures using ordinary SQL statements.
6*ebaecc14Sdanielk1977
7*ebaecc14Sdanielk1977    1.  SQL Interface
8*ebaecc14Sdanielk1977
9*ebaecc14Sdanielk1977        1.1  Table Creation
10*ebaecc14Sdanielk1977        1.2  Data Manipulation
11*ebaecc14Sdanielk1977        1.3  Data Querying
12*ebaecc14Sdanielk1977        1.4  Introspection and Analysis
13*ebaecc14Sdanielk1977
14*ebaecc14Sdanielk1977    2.  Compilation and Deployment
15*ebaecc14Sdanielk1977
16*ebaecc14Sdanielk1977    3.  References
17*ebaecc14Sdanielk1977
18*ebaecc14Sdanielk1977
19*ebaecc14Sdanielk19771. SQL INTERFACE
20*ebaecc14Sdanielk1977
21*ebaecc14Sdanielk1977  1.1 Table Creation.
22*ebaecc14Sdanielk1977
23*ebaecc14Sdanielk1977    All r-tree virtual tables have an odd number of columns between
24*ebaecc14Sdanielk1977    3 and 11. Unlike regular SQLite tables, r-tree tables are strongly
25*ebaecc14Sdanielk1977    typed.
26*ebaecc14Sdanielk1977
27*ebaecc14Sdanielk1977    The leftmost column is always the pimary key and contains 64-bit
28*ebaecc14Sdanielk1977    integer values. Each subsequent column contains a 32-bit real
29*ebaecc14Sdanielk1977    value. For each pair of real values, the first (leftmost) must be
30*ebaecc14Sdanielk1977    less than or greater than the second. R-tree tables may be
31*ebaecc14Sdanielk1977    constructed using the following syntax:
32*ebaecc14Sdanielk1977
33*ebaecc14Sdanielk1977      CREATE VIRTUAL TABLE <name> USING rtree(<column-names>)
34*ebaecc14Sdanielk1977
35*ebaecc14Sdanielk1977    For example:
36*ebaecc14Sdanielk1977
37*ebaecc14Sdanielk1977      CREATE VIRTUAL TABLE boxes USING rtree(boxno, xmin, xmax, ymin, ymax);
38*ebaecc14Sdanielk1977      CREATE VIRTUAL TABLE boxes USING rtree(1, 1.0, 3.0, 2.0, 4.0);
39*ebaecc14Sdanielk1977
40*ebaecc14Sdanielk1977    Constructing a virtual r-tree table <name> creates the following three
41*ebaecc14Sdanielk1977    real tables in the database to store the data structure:
42*ebaecc14Sdanielk1977
43*ebaecc14Sdanielk1977      <name>_node
44*ebaecc14Sdanielk1977      <name>_rowid
45*ebaecc14Sdanielk1977      <name>_parent
46*ebaecc14Sdanielk1977
47*ebaecc14Sdanielk1977    Dropping or modifying the contents of these tables directly will
48*ebaecc14Sdanielk1977    corrupt the r-tree structure. To delete an r-tree from a database,
49*ebaecc14Sdanielk1977    use a regular DROP TABLE statement:
50*ebaecc14Sdanielk1977
51*ebaecc14Sdanielk1977      DROP TABLE <name>;
52*ebaecc14Sdanielk1977
53*ebaecc14Sdanielk1977    Dropping the main r-tree table automatically drops the automatically
54*ebaecc14Sdanielk1977    created tables.
55*ebaecc14Sdanielk1977
56*ebaecc14Sdanielk1977  1.2 Data Manipulation (INSERT, UPDATE, DELETE).
57*ebaecc14Sdanielk1977
58*ebaecc14Sdanielk1977    The usual INSERT, UPDATE or DELETE syntax is used to manipulate data
59*ebaecc14Sdanielk1977    stored in an r-tree table. Please note the following:
60*ebaecc14Sdanielk1977
61*ebaecc14Sdanielk1977      * Inserting a NULL value into the primary key column has the
62*ebaecc14Sdanielk1977        same effect as inserting a NULL into an INTEGER PRIMARY KEY
63*ebaecc14Sdanielk1977        column of a regular table. The system automatically assigns
64*ebaecc14Sdanielk1977        an unused integer key value to the new record. Usually, this
65*ebaecc14Sdanielk1977        is one greater than the largest primary key value currently
66*ebaecc14Sdanielk1977        present in the table.
67*ebaecc14Sdanielk1977
68*ebaecc14Sdanielk1977      * Attempting to insert a duplicate primary key value fails with
69*ebaecc14Sdanielk1977        an SQLITE_CONSTRAINT error.
70*ebaecc14Sdanielk1977
71*ebaecc14Sdanielk1977      * Attempting to insert or modify a record such that the value
72*ebaecc14Sdanielk1977        stored in the (N*2)th column is greater than that stored in
73*ebaecc14Sdanielk1977        the (N*2+1)th column fails with an SQLITE_CONSTRAINT error.
74*ebaecc14Sdanielk1977
75*ebaecc14Sdanielk1977      * When a record is inserted, values are always converted to
76*ebaecc14Sdanielk1977        the required type (64-bit integer or 32-bit real) as if they
77*ebaecc14Sdanielk1977        were part of an SQL CAST expression. Non-numeric strings are
78*ebaecc14Sdanielk1977        converted to zero.
79*ebaecc14Sdanielk1977
80*ebaecc14Sdanielk1977  1.3 Queries.
81*ebaecc14Sdanielk1977
82*ebaecc14Sdanielk1977    R-tree tables may be queried using all of the same SQL syntax supported
83*ebaecc14Sdanielk1977    by regular tables. However, some query patterns are more efficient faster
84*ebaecc14Sdanielk1977    than others.
85*ebaecc14Sdanielk1977
86*ebaecc14Sdanielk1977    R-trees support fast lookup by primary key value (O(logN), like
87*ebaecc14Sdanielk1977    regular tables).
88*ebaecc14Sdanielk1977
89*ebaecc14Sdanielk1977    Any combination of equality and range (<, <=, >, >=) constraints
90*ebaecc14Sdanielk1977    on spatial data columns may be used to optimize other queries. This
91*ebaecc14Sdanielk1977    is the key advantage to using r-tree tables instead of creating
92*ebaecc14Sdanielk1977    indices on regular tables.
93*ebaecc14Sdanielk1977
94*ebaecc14Sdanielk1977  1.4 Introspection and Analysis.
95*ebaecc14Sdanielk1977
96*ebaecc14Sdanielk1977    TODO: Describe rtreenode() and rtreedepth() functions.
97*ebaecc14Sdanielk1977
98*ebaecc14Sdanielk1977
99*ebaecc14Sdanielk19772. COMPILATION AND USAGE
100*ebaecc14Sdanielk1977
101*ebaecc14Sdanielk1977  The easiest way to compile and use the ICU extension is to build
102*ebaecc14Sdanielk1977  and use it as a dynamically loadable SQLite extension. To do this
103*ebaecc14Sdanielk1977  using gcc on *nix:
104*ebaecc14Sdanielk1977
105*ebaecc14Sdanielk1977    gcc -shared rtree.c -o libSqliteRtree.so
106*ebaecc14Sdanielk1977
107*ebaecc14Sdanielk1977  You may need to add "-I" flags so that gcc can find sqlite3ext.h
108*ebaecc14Sdanielk1977  and sqlite3.h. The resulting shared lib, libSqliteIcu.so, may be
109*ebaecc14Sdanielk1977  loaded into sqlite in the same way as any other dynamicly loadable
110*ebaecc14Sdanielk1977  extension.
111*ebaecc14Sdanielk1977
112*ebaecc14Sdanielk1977
113*ebaecc14Sdanielk19773. REFERENCES
114*ebaecc14Sdanielk1977
115*ebaecc14Sdanielk1977  [1]  Atonin Guttman, "R-trees - A Dynamic Index Structure For Spatial
116*ebaecc14Sdanielk1977       Searching", University of California Berkeley, 1984.
117*ebaecc14Sdanielk1977
118*ebaecc14Sdanielk1977  [2]  Norbert Beckmann, Hans-Peter Kriegel, Ralf Schneider, Bernhard Seeger,
119*ebaecc14Sdanielk1977       "The R*-tree: An Efficient and Robust Access Method for Points and
120*ebaecc14Sdanielk1977       Rectangles", Universitaet Bremen, 1990.
121*ebaecc14Sdanielk1977
122*ebaecc14Sdanielk1977
123*ebaecc14Sdanielk1977
124