vibesql-storage 0.2.0

Storage engine for vibesql SQL database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Spatial Index - R-tree implementation for spatial queries
//!
//! This module provides R-tree spatial indexing for efficient spatial queries.
//! Uses the `rstar` crate for R*-tree (enhanced R-tree variant) implementation.

use rstar::{RTree, RTreeObject, AABB};
use vibesql_types::SqlValue;

/// A spatial index entry that can be stored in an R-tree
#[derive(Debug, Clone)]
pub struct SpatialIndexEntry {
    /// Row ID in the table
    pub row_id: usize,
    /// Minimum Bounding Rectangle (envelope) for the geometry
    pub mbr: AABB<[f64; 2]>,
}

impl RTreeObject for SpatialIndexEntry {
    type Envelope = AABB<[f64; 2]>;

    fn envelope(&self) -> Self::Envelope {
        self.mbr
    }
}

impl PartialEq for SpatialIndexEntry {
    fn eq(&self, other: &Self) -> bool {
        self.row_id == other.row_id
    }
}

/// Spatial index using R-tree for efficient spatial queries
#[derive(Debug, Clone)]
pub struct SpatialIndex {
    /// R-tree data structure
    rtree: RTree<SpatialIndexEntry>,
    /// Column name this index is on
    pub column_name: String,
}

impl SpatialIndex {
    /// Create a new empty spatial index
    pub fn new(column_name: String) -> Self {
        SpatialIndex { rtree: RTree::new(), column_name }
    }

    /// Create a spatial index from a bulk load of entries
    /// This is more efficient than inserting entries one by one
    pub fn bulk_load(column_name: String, entries: Vec<SpatialIndexEntry>) -> Self {
        SpatialIndex { rtree: RTree::bulk_load(entries), column_name }
    }

    /// Insert a geometry into the spatial index
    pub fn insert(&mut self, row_id: usize, mbr: AABB<[f64; 2]>) {
        let entry = SpatialIndexEntry { row_id, mbr };
        self.rtree.insert(entry);
    }

    /// Remove a geometry from the spatial index
    pub fn remove(&mut self, row_id: usize, mbr: &AABB<[f64; 2]>) -> bool {
        let entry = SpatialIndexEntry { row_id, mbr: *mbr };
        self.rtree.remove(&entry).is_some()
    }

    /// Query the spatial index for geometries intersecting the given MBR
    /// Returns a vector of row IDs that potentially match
    pub fn locate_in_envelope(&self, mbr: &AABB<[f64; 2]>) -> Vec<usize> {
        self.rtree.locate_in_envelope(*mbr).map(|entry| entry.row_id).collect()
    }

    /// Query the spatial index for geometries containing the given point
    /// This is done by querying for a point MBR (zero-size envelope)
    pub fn locate_at_point(&self, point: &[f64; 2]) -> Vec<usize> {
        let point_mbr = AABB::from_point(*point);
        self.locate_in_envelope(&point_mbr)
    }

    /// Get all row IDs in the index (for testing/debugging)
    pub fn all_row_ids(&self) -> Vec<usize> {
        self.rtree.iter().map(|entry| entry.row_id).collect()
    }

    /// Get the number of entries in the index
    pub fn len(&self) -> usize {
        self.rtree.size()
    }

    /// Check if the index is empty
    pub fn is_empty(&self) -> bool {
        self.rtree.size() == 0
    }

    /// Clear all entries from the index
    pub fn clear(&mut self) {
        self.rtree = RTree::new();
    }
}

/// Extract minimum bounding rectangle from a geometry SqlValue
/// Returns None if the value is not a valid geometry
pub fn extract_mbr_from_sql_value(value: &SqlValue) -> Option<AABB<[f64; 2]>> {
    // Parse the geometry from SqlValue
    // Format: "__GEOMETRY__|SRID|wkt_content"
    const GEOMETRY_TYPE_MARKER: &str = "__GEOMETRY__";

    match value {
        SqlValue::Varchar(s) | SqlValue::Character(s) => {
            if !s.starts_with(GEOMETRY_TYPE_MARKER) {
                // Try parsing as WKT directly
                return parse_wkt_mbr(s);
            }

            // Extract WKT from marker format
            let parts: Vec<&str> = s.splitn(3, '|').collect();
            if parts.len() >= 3 {
                let wkt = parts[2];
                parse_wkt_mbr(wkt)
            } else {
                None
            }
        }
        SqlValue::Null => None,
        _ => None,
    }
}

/// Parse WKT and extract minimum bounding rectangle
fn parse_wkt_mbr(wkt: &str) -> Option<AABB<[f64; 2]>> {
    // Strip SRID prefix if present (SRID=4326;POINT(1 2))
    let wkt = if wkt.starts_with("SRID=") {
        wkt.split_once(';').map(|(_, w)| w).unwrap_or(wkt)
    } else {
        wkt
    };

    let wkt = wkt.trim();

    // Extract coordinates based on geometry type
    if wkt.starts_with("POINT") {
        parse_point_mbr(wkt)
    } else if wkt.starts_with("LINESTRING") {
        parse_linestring_mbr(wkt)
    } else if wkt.starts_with("POLYGON") {
        parse_polygon_mbr(wkt)
    } else if wkt.starts_with("MULTIPOINT") {
        parse_multipoint_mbr(wkt)
    } else if wkt.starts_with("MULTILINESTRING") {
        parse_multilinestring_mbr(wkt)
    } else if wkt.starts_with("MULTIPOLYGON") {
        parse_multipolygon_mbr(wkt)
    } else {
        None
    }
}

/// Parse POINT WKT and return MBR
fn parse_point_mbr(wkt: &str) -> Option<AABB<[f64; 2]>> {
    // POINT(x y)
    let coords_str = wkt.strip_prefix("POINT")?.trim();
    let coords_str = coords_str.strip_prefix('(')?.strip_suffix(')')?;
    let parts: Vec<&str> = coords_str.split_whitespace().collect();

    if parts.len() >= 2 {
        let x: f64 = parts[0].parse().ok()?;
        let y: f64 = parts[1].parse().ok()?;
        Some(AABB::from_point([x, y]))
    } else {
        None
    }
}

/// Parse LINESTRING WKT and return MBR
fn parse_linestring_mbr(wkt: &str) -> Option<AABB<[f64; 2]>> {
    // LINESTRING(x1 y1, x2 y2, ...)
    let coords_str = wkt.strip_prefix("LINESTRING")?.trim();
    let coords_str = coords_str.strip_prefix('(')?.strip_suffix(')')?;

    let points = parse_coordinate_list(coords_str)?;
    calculate_mbr_from_points(&points)
}

/// Parse POLYGON WKT and return MBR
fn parse_polygon_mbr(wkt: &str) -> Option<AABB<[f64; 2]>> {
    // POLYGON((x1 y1, x2 y2, ...), (...))
    let coords_str = wkt.strip_prefix("POLYGON")?.trim();
    let coords_str = coords_str.strip_prefix('(')?.strip_suffix(')')?;

    // Extract exterior ring (first ring)
    // For single ring: "(0 0, 10 0, 10 10, 0 10, 0 0)"
    // For multiple rings: "(0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1)"
    let exterior = if coords_str.contains("),") {
        // Multiple rings - split and take first
        let rings: Vec<&str> = coords_str.split("),").collect();
        rings[0].trim().strip_prefix('(').unwrap_or(rings[0])
    } else {
        // Single ring
        coords_str.strip_prefix('(')?.strip_suffix(')')?
    };

    let points = parse_coordinate_list(exterior)?;
    calculate_mbr_from_points(&points)
}

/// Parse MULTIPOINT WKT and return MBR
fn parse_multipoint_mbr(wkt: &str) -> Option<AABB<[f64; 2]>> {
    // MULTIPOINT((x1 y1), (x2 y2), ...)
    let coords_str = wkt.strip_prefix("MULTIPOINT")?.trim();
    let coords_str = coords_str.strip_prefix('(')?.strip_suffix(')')?;

    // Remove parentheses around individual points
    let coords_str = coords_str.replace("(", "").replace(")", "");
    let points = parse_coordinate_list(&coords_str)?;
    calculate_mbr_from_points(&points)
}

/// Parse MULTILINESTRING WKT and return MBR
fn parse_multilinestring_mbr(wkt: &str) -> Option<AABB<[f64; 2]>> {
    // MULTILINESTRING((x1 y1, x2 y2), (x3 y3, x4 y4))
    let coords_str = wkt.strip_prefix("MULTILINESTRING")?.trim();
    let coords_str = coords_str.strip_prefix('(')?.strip_suffix(')')?;

    let mut all_points = Vec::new();
    let lines: Vec<&str> = coords_str.split("),").collect();

    for line in lines {
        let line = line.trim().strip_prefix('(').unwrap_or(line);
        let line = line.trim().strip_suffix(')').unwrap_or(line);
        if let Some(mut points) = parse_coordinate_list(line) {
            all_points.append(&mut points);
        }
    }

    calculate_mbr_from_points(&all_points)
}

/// Parse MULTIPOLYGON WKT and return MBR
fn parse_multipolygon_mbr(wkt: &str) -> Option<AABB<[f64; 2]>> {
    // MULTIPOLYGON(((x1 y1, ...)), ((x2 y2, ...)))
    let coords_str = wkt.strip_prefix("MULTIPOLYGON")?.trim();
    let coords_str = coords_str.strip_prefix('(')?.strip_suffix(')')?;

    let mut all_points = Vec::new();

    // Split into polygons
    let polygons: Vec<&str> = coords_str.split(")),").collect();

    for polygon in polygons {
        let polygon = polygon.trim().strip_prefix('(').unwrap_or(polygon);
        let polygon = polygon.trim().strip_suffix(')').unwrap_or(polygon);

        // Extract exterior ring (first ring)
        let rings: Vec<&str> = polygon.split("),").collect();
        if !rings.is_empty() {
            let exterior = rings[0].trim().strip_prefix('(').unwrap_or(rings[0]);
            if let Some(mut points) = parse_coordinate_list(exterior) {
                all_points.append(&mut points);
            }
        }
    }

    calculate_mbr_from_points(&all_points)
}

/// Parse coordinate list "x1 y1, x2 y2, ..."
fn parse_coordinate_list(coords_str: &str) -> Option<Vec<[f64; 2]>> {
    let mut points = Vec::new();

    for coord_pair in coords_str.split(',') {
        let parts: Vec<&str> = coord_pair.split_whitespace().collect();
        if parts.len() >= 2 {
            let x: f64 = parts[0].parse().ok()?;
            let y: f64 = parts[1].parse().ok()?;
            points.push([x, y]);
        }
    }

    if points.is_empty() {
        None
    } else {
        Some(points)
    }
}

/// Calculate MBR from a list of points
fn calculate_mbr_from_points(points: &[[f64; 2]]) -> Option<AABB<[f64; 2]>> {
    if points.is_empty() {
        return None;
    }

    let mut min_x = f64::INFINITY;
    let mut min_y = f64::INFINITY;
    let mut max_x = f64::NEG_INFINITY;
    let mut max_y = f64::NEG_INFINITY;

    for &[x, y] in points {
        min_x = min_x.min(x);
        min_y = min_y.min(y);
        max_x = max_x.max(x);
        max_y = max_y.max(y);
    }

    Some(AABB::from_corners([min_x, min_y], [max_x, max_y]))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_spatial_index_insert_and_query() {
        let mut index = SpatialIndex::new("location".to_string());

        // Insert some points
        index.insert(0, AABB::from_point([0.0, 0.0]));
        index.insert(1, AABB::from_point([10.0, 10.0]));
        index.insert(2, AABB::from_point([20.0, 20.0]));

        assert_eq!(index.len(), 3);

        // Query for points in a region
        let query_mbr = AABB::from_corners([5.0, 5.0], [15.0, 15.0]);
        let results = index.locate_in_envelope(&query_mbr);

        // Should find row 1 (10, 10) which is within the query MBR
        assert!(results.contains(&1));
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_spatial_index_remove() {
        let mut index = SpatialIndex::new("location".to_string());

        let mbr = AABB::from_point([10.0, 10.0]);
        index.insert(0, mbr);
        assert_eq!(index.len(), 1);

        let removed = index.remove(0, &mbr);
        assert!(removed);
        assert_eq!(index.len(), 0);
    }

    #[test]
    fn test_extract_mbr_from_point() {
        let sql_value = SqlValue::Varchar(arcstr::ArcStr::from("__GEOMETRY__|0|POINT(10 20)"));
        let mbr = extract_mbr_from_sql_value(&sql_value);

        assert!(mbr.is_some());
        let mbr = mbr.unwrap();
        assert_eq!(mbr.lower(), [10.0, 20.0]);
        assert_eq!(mbr.upper(), [10.0, 20.0]);
    }

    #[test]
    fn test_extract_mbr_from_polygon() {
        let sql_value = SqlValue::Varchar(arcstr::ArcStr::from(
            "__GEOMETRY__|0|POLYGON((0 0, 100 0, 100 100, 0 100, 0 0))",
        ));
        let mbr = extract_mbr_from_sql_value(&sql_value);

        assert!(mbr.is_some());
        let mbr = mbr.unwrap();
        assert_eq!(mbr.lower(), [0.0, 0.0]);
        assert_eq!(mbr.upper(), [100.0, 100.0]);
    }

    #[test]
    fn test_parse_point_mbr() {
        let mbr = parse_point_mbr("POINT(5 10)");
        assert!(mbr.is_some());
        let mbr = mbr.unwrap();
        assert_eq!(mbr.lower(), [5.0, 10.0]);
        assert_eq!(mbr.upper(), [5.0, 10.0]);
    }

    #[test]
    fn test_parse_linestring_mbr() {
        let mbr = parse_linestring_mbr("LINESTRING(0 0, 10 10, 20 5)");
        assert!(mbr.is_some());
        let mbr = mbr.unwrap();
        assert_eq!(mbr.lower(), [0.0, 0.0]);
        assert_eq!(mbr.upper(), [20.0, 10.0]);
    }

    #[test]
    fn test_spatial_index_bulk_load() {
        let entries = vec![
            SpatialIndexEntry { row_id: 0, mbr: AABB::from_point([0.0, 0.0]) },
            SpatialIndexEntry { row_id: 1, mbr: AABB::from_point([10.0, 10.0]) },
            SpatialIndexEntry { row_id: 2, mbr: AABB::from_point([20.0, 20.0]) },
        ];

        let index = SpatialIndex::bulk_load("location".to_string(), entries);
        assert_eq!(index.len(), 3);

        // Verify query works
        let query_mbr = AABB::from_corners([5.0, 5.0], [15.0, 15.0]);
        let results = index.locate_in_envelope(&query_mbr);
        assert!(results.contains(&1));
    }

    #[test]
    fn test_extract_mbr_null_value() {
        let sql_value = SqlValue::Null;
        let mbr = extract_mbr_from_sql_value(&sql_value);
        assert!(mbr.is_none());
    }

    #[test]
    fn test_extract_mbr_with_srid() {
        let sql_value =
            SqlValue::Varchar(arcstr::ArcStr::from("__GEOMETRY__|4326|SRID=4326;POINT(10 20)"));
        let mbr = extract_mbr_from_sql_value(&sql_value);

        assert!(mbr.is_some());
        let mbr = mbr.unwrap();
        assert_eq!(mbr.lower(), [10.0, 20.0]);
    }
}