vibesql-executor 0.2.0

Query execution 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! WKB (Well-Known Binary) format parser and serializer
//!
//! Implements parsing and serialization of geometries in WKB format.
//! References:
//! - ISO/IEC 13249-3:2016 (SQL/MM Part 3)
//! - OGC Simple Features for SQL v1.1.1

use std::io::{Cursor, Read};

use super::Geometry;
use crate::errors::ExecutorError;

// WKB Geometry Type Codes
const WKB_POINT: u32 = 1;
const WKB_LINESTRING: u32 = 2;
const WKB_POLYGON: u32 = 3;
const WKB_MULTIPOINT: u32 = 4;
const WKB_MULTILINESTRING: u32 = 5;
const WKB_MULTIPOLYGON: u32 = 6;
const WKB_GEOMETRYCOLLECTION: u32 = 7;

// EWKB SRID flag (PostGIS extension)
const EWKB_SRID_FLAG: u32 = 0x20000000;

/// Byte order marker
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ByteOrder {
    LittleEndian = 1,
    BigEndian = 0,
}

impl ByteOrder {
    fn from_u8(val: u8) -> Result<Self, ExecutorError> {
        match val {
            0 => Ok(ByteOrder::BigEndian),
            1 => Ok(ByteOrder::LittleEndian),
            _ => Err(ExecutorError::UnsupportedFeature(format!("Invalid WKB byte order: {}", val))),
        }
    }
}

/// Parse WKB (Well-Known Binary) data
pub fn wkb_to_geometry(data: &[u8]) -> Result<Geometry, ExecutorError> {
    let mut cursor = Cursor::new(data);
    parse_geometry(&mut cursor)
}

fn parse_geometry(cursor: &mut Cursor<&[u8]>) -> Result<Geometry, ExecutorError> {
    let byte_order = read_byte_order(cursor)?;
    let geom_type = read_u32(cursor, byte_order)?;

    // Remove SRID flag if present (EWKB extension)
    let has_srid = (geom_type & EWKB_SRID_FLAG) != 0;
    let pure_type = geom_type & !EWKB_SRID_FLAG;

    // Skip SRID if present
    if has_srid {
        let _ = read_i32(cursor, byte_order)?;
    }

    match pure_type {
        WKB_POINT => parse_point(cursor, byte_order),
        WKB_LINESTRING => parse_linestring(cursor, byte_order),
        WKB_POLYGON => parse_polygon(cursor, byte_order),
        WKB_MULTIPOINT => parse_multipoint(cursor, byte_order),
        WKB_MULTILINESTRING => parse_multilinestring(cursor, byte_order),
        WKB_MULTIPOLYGON => parse_multipolygon(cursor, byte_order),
        WKB_GEOMETRYCOLLECTION => parse_geometrycollection(cursor, byte_order),
        _ => Err(ExecutorError::UnsupportedFeature(format!(
            "Unknown WKB geometry type: {}",
            pure_type
        ))),
    }
}

fn parse_point(
    cursor: &mut Cursor<&[u8]>,
    byte_order: ByteOrder,
) -> Result<Geometry, ExecutorError> {
    let x = read_f64(cursor, byte_order)?;
    let y = read_f64(cursor, byte_order)?;
    Ok(Geometry::Point { x, y })
}

fn parse_linestring(
    cursor: &mut Cursor<&[u8]>,
    byte_order: ByteOrder,
) -> Result<Geometry, ExecutorError> {
    let num_points = read_u32(cursor, byte_order)? as usize;
    let mut points = Vec::with_capacity(num_points);

    for _ in 0..num_points {
        let x = read_f64(cursor, byte_order)?;
        let y = read_f64(cursor, byte_order)?;
        points.push((x, y));
    }

    if points.len() < 2 {
        return Err(ExecutorError::UnsupportedFeature(
            "LineString must have at least 2 points".to_string(),
        ));
    }

    Ok(Geometry::LineString { points })
}

fn parse_polygon(
    cursor: &mut Cursor<&[u8]>,
    byte_order: ByteOrder,
) -> Result<Geometry, ExecutorError> {
    let num_rings = read_u32(cursor, byte_order)? as usize;
    let mut rings = Vec::with_capacity(num_rings);

    for _ in 0..num_rings {
        let num_points = read_u32(cursor, byte_order)? as usize;
        let mut ring = Vec::with_capacity(num_points);

        for _ in 0..num_points {
            let x = read_f64(cursor, byte_order)?;
            let y = read_f64(cursor, byte_order)?;
            ring.push((x, y));
        }

        rings.push(ring);
    }

    if rings.is_empty() {
        return Err(ExecutorError::UnsupportedFeature(
            "Polygon must have at least one ring".to_string(),
        ));
    }

    Ok(Geometry::Polygon { rings })
}

fn parse_multipoint(
    cursor: &mut Cursor<&[u8]>,
    byte_order: ByteOrder,
) -> Result<Geometry, ExecutorError> {
    let num_points = read_u32(cursor, byte_order)? as usize;
    let mut points = Vec::with_capacity(num_points);

    for _ in 0..num_points {
        // Each point in multipoint has its own header
        let _ = read_byte_order(cursor)?;
        let geom_type = read_u32(cursor, byte_order)?;

        if (geom_type & !EWKB_SRID_FLAG) != WKB_POINT {
            return Err(ExecutorError::UnsupportedFeature(
                "Invalid MULTIPOINT geometry".to_string(),
            ));
        }

        let x = read_f64(cursor, byte_order)?;
        let y = read_f64(cursor, byte_order)?;
        points.push((x, y));
    }

    if points.is_empty() {
        return Err(ExecutorError::UnsupportedFeature(
            "MultiPoint must have at least one point".to_string(),
        ));
    }

    Ok(Geometry::MultiPoint { points })
}

fn parse_multilinestring(
    cursor: &mut Cursor<&[u8]>,
    byte_order: ByteOrder,
) -> Result<Geometry, ExecutorError> {
    let num_lines = read_u32(cursor, byte_order)? as usize;
    let mut lines = Vec::with_capacity(num_lines);

    for _ in 0..num_lines {
        // Each linestring in multilinestring has its own header
        let _ = read_byte_order(cursor)?;
        let geom_type = read_u32(cursor, byte_order)?;

        if (geom_type & !EWKB_SRID_FLAG) != WKB_LINESTRING {
            return Err(ExecutorError::UnsupportedFeature(
                "Invalid MULTILINESTRING geometry".to_string(),
            ));
        }

        let num_points = read_u32(cursor, byte_order)? as usize;
        let mut line = Vec::with_capacity(num_points);

        for _ in 0..num_points {
            let x = read_f64(cursor, byte_order)?;
            let y = read_f64(cursor, byte_order)?;
            line.push((x, y));
        }

        lines.push(line);
    }

    if lines.is_empty() {
        return Err(ExecutorError::UnsupportedFeature(
            "MultiLineString must have at least one linestring".to_string(),
        ));
    }

    Ok(Geometry::MultiLineString { lines })
}

fn parse_multipolygon(
    cursor: &mut Cursor<&[u8]>,
    byte_order: ByteOrder,
) -> Result<Geometry, ExecutorError> {
    let num_polygons = read_u32(cursor, byte_order)? as usize;
    let mut polygons = Vec::with_capacity(num_polygons);

    for _ in 0..num_polygons {
        // Each polygon in multipolygon has its own header
        let _ = read_byte_order(cursor)?;
        let geom_type = read_u32(cursor, byte_order)?;

        if (geom_type & !EWKB_SRID_FLAG) != WKB_POLYGON {
            return Err(ExecutorError::UnsupportedFeature(
                "Invalid MULTIPOLYGON geometry".to_string(),
            ));
        }

        let num_rings = read_u32(cursor, byte_order)? as usize;
        let mut rings = Vec::with_capacity(num_rings);

        for _ in 0..num_rings {
            let num_points = read_u32(cursor, byte_order)? as usize;
            let mut ring = Vec::with_capacity(num_points);

            for _ in 0..num_points {
                let x = read_f64(cursor, byte_order)?;
                let y = read_f64(cursor, byte_order)?;
                ring.push((x, y));
            }

            rings.push(ring);
        }

        polygons.push(rings);
    }

    if polygons.is_empty() {
        return Err(ExecutorError::UnsupportedFeature(
            "MultiPolygon must have at least one polygon".to_string(),
        ));
    }

    Ok(Geometry::MultiPolygon { polygons })
}

fn parse_geometrycollection(
    cursor: &mut Cursor<&[u8]>,
    byte_order: ByteOrder,
) -> Result<Geometry, ExecutorError> {
    let num_geoms = read_u32(cursor, byte_order)? as usize;
    let mut geometries = Vec::with_capacity(num_geoms);

    for _ in 0..num_geoms {
        // Each geometry in the collection has its own full WKB
        geometries.push(parse_geometry(cursor)?);
    }

    if geometries.is_empty() {
        return Err(ExecutorError::UnsupportedFeature(
            "GeometryCollection must have at least one geometry".to_string(),
        ));
    }

    Ok(Geometry::Collection { geometries })
}

// Helper functions for reading WKB data

fn read_byte_order(cursor: &mut Cursor<&[u8]>) -> Result<ByteOrder, ExecutorError> {
    let mut buf = [0u8; 1];
    cursor
        .read_exact(&mut buf)
        .map_err(|_| ExecutorError::UnsupportedFeature("Unexpected end of WKB data".to_string()))?;
    ByteOrder::from_u8(buf[0])
}

fn read_u32(cursor: &mut Cursor<&[u8]>, byte_order: ByteOrder) -> Result<u32, ExecutorError> {
    let mut buf = [0u8; 4];
    cursor
        .read_exact(&mut buf)
        .map_err(|_| ExecutorError::UnsupportedFeature("Unexpected end of WKB data".to_string()))?;

    Ok(match byte_order {
        ByteOrder::LittleEndian => u32::from_le_bytes(buf),
        ByteOrder::BigEndian => u32::from_be_bytes(buf),
    })
}

fn read_i32(cursor: &mut Cursor<&[u8]>, byte_order: ByteOrder) -> Result<i32, ExecutorError> {
    let mut buf = [0u8; 4];
    cursor
        .read_exact(&mut buf)
        .map_err(|_| ExecutorError::UnsupportedFeature("Unexpected end of WKB data".to_string()))?;

    Ok(match byte_order {
        ByteOrder::LittleEndian => i32::from_le_bytes(buf),
        ByteOrder::BigEndian => i32::from_be_bytes(buf),
    })
}

fn read_f64(cursor: &mut Cursor<&[u8]>, byte_order: ByteOrder) -> Result<f64, ExecutorError> {
    let mut buf = [0u8; 8];
    cursor
        .read_exact(&mut buf)
        .map_err(|_| ExecutorError::UnsupportedFeature("Unexpected end of WKB data".to_string()))?;

    Ok(match byte_order {
        ByteOrder::LittleEndian => f64::from_le_bytes(buf),
        ByteOrder::BigEndian => f64::from_be_bytes(buf),
    })
}

// WKB Serialization

/// Convert a geometry to WKB (Well-Known Binary) format
#[allow(dead_code)]
pub fn geometry_to_wkb(geom: &Geometry) -> Vec<u8> {
    let mut buf = Vec::new();
    buf.push(ByteOrder::LittleEndian as u8); // Use little-endian
    serialize_geometry(geom, &mut buf, None);
    buf
}

/// Convert a geometry to EWKB (Extended WKB) format with SRID
pub fn geometry_to_ewkb(geom: &Geometry, srid: i32) -> Vec<u8> {
    let mut buf = Vec::new();
    buf.push(ByteOrder::LittleEndian as u8); // Use little-endian

    // If SRID is specified, include it
    if srid > 0 {
        serialize_geometry(geom, &mut buf, Some(srid));
    } else {
        serialize_geometry(geom, &mut buf, None);
    }

    buf
}

fn serialize_geometry(geom: &Geometry, buf: &mut Vec<u8>, srid: Option<i32>) {
    match geom {
        Geometry::Point { x, y } => {
            if let Some(s) = srid {
                write_u32(buf, WKB_POINT | EWKB_SRID_FLAG);
                write_i32(buf, s);
            } else {
                write_u32(buf, WKB_POINT);
            }
            write_f64(buf, *x);
            write_f64(buf, *y);
        }
        Geometry::LineString { points } => {
            if let Some(s) = srid {
                write_u32(buf, WKB_LINESTRING | EWKB_SRID_FLAG);
                write_i32(buf, s);
            } else {
                write_u32(buf, WKB_LINESTRING);
            }
            write_u32(buf, points.len() as u32);
            for (x, y) in points {
                write_f64(buf, *x);
                write_f64(buf, *y);
            }
        }
        Geometry::Polygon { rings } => {
            if let Some(s) = srid {
                write_u32(buf, WKB_POLYGON | EWKB_SRID_FLAG);
                write_i32(buf, s);
            } else {
                write_u32(buf, WKB_POLYGON);
            }
            write_u32(buf, rings.len() as u32);
            for ring in rings {
                write_u32(buf, ring.len() as u32);
                for (x, y) in ring {
                    write_f64(buf, *x);
                    write_f64(buf, *y);
                }
            }
        }
        Geometry::MultiPoint { points } => {
            if let Some(s) = srid {
                write_u32(buf, WKB_MULTIPOINT | EWKB_SRID_FLAG);
                write_i32(buf, s);
            } else {
                write_u32(buf, WKB_MULTIPOINT);
            }
            write_u32(buf, points.len() as u32);
            for (x, y) in points {
                buf.push(ByteOrder::LittleEndian as u8);
                write_u32(buf, WKB_POINT);
                write_f64(buf, *x);
                write_f64(buf, *y);
            }
        }
        Geometry::MultiLineString { lines } => {
            if let Some(s) = srid {
                write_u32(buf, WKB_MULTILINESTRING | EWKB_SRID_FLAG);
                write_i32(buf, s);
            } else {
                write_u32(buf, WKB_MULTILINESTRING);
            }
            write_u32(buf, lines.len() as u32);
            for line in lines {
                buf.push(ByteOrder::LittleEndian as u8);
                write_u32(buf, WKB_LINESTRING);
                write_u32(buf, line.len() as u32);
                for (x, y) in line {
                    write_f64(buf, *x);
                    write_f64(buf, *y);
                }
            }
        }
        Geometry::MultiPolygon { polygons } => {
            if let Some(s) = srid {
                write_u32(buf, WKB_MULTIPOLYGON | EWKB_SRID_FLAG);
                write_i32(buf, s);
            } else {
                write_u32(buf, WKB_MULTIPOLYGON);
            }
            write_u32(buf, polygons.len() as u32);
            for rings_list in polygons {
                buf.push(ByteOrder::LittleEndian as u8);
                write_u32(buf, WKB_POLYGON);
                write_u32(buf, rings_list.len() as u32);
                for ring in rings_list {
                    write_u32(buf, ring.len() as u32);
                    for (x, y) in ring {
                        write_f64(buf, *x);
                        write_f64(buf, *y);
                    }
                }
            }
        }
        Geometry::Collection { geometries } => {
            if let Some(s) = srid {
                write_u32(buf, WKB_GEOMETRYCOLLECTION | EWKB_SRID_FLAG);
                write_i32(buf, s);
            } else {
                write_u32(buf, WKB_GEOMETRYCOLLECTION);
            }
            write_u32(buf, geometries.len() as u32);
            for geom in geometries {
                buf.push(ByteOrder::LittleEndian as u8);
                serialize_geometry(geom, buf, None);
            }
        }
    }
}

fn write_u32(buf: &mut Vec<u8>, val: u32) {
    buf.extend_from_slice(&val.to_le_bytes());
}

fn write_i32(buf: &mut Vec<u8>, val: i32) {
    buf.extend_from_slice(&val.to_le_bytes());
}

fn write_f64(buf: &mut Vec<u8>, val: f64) {
    buf.extend_from_slice(&val.to_le_bytes());
}