nodedb 0.2.1

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// SPDX-License-Identifier: BUSL-1.1

//! Canonical engine-option parsing for `CREATE COLLECTION` and `CREATE TABLE`.
//!
//! The single accepted syntax is `WITH (engine='<name>')`. All legacy axes —
//! `TYPE <keyword>`, `WITH (profile='...')`, or bare `WITH (vector_field='...')`
//! without an explicit engine — are rejected hard with a helpful SQLSTATE error.

use pgwire::error::PgWireResult;

use super::super::super::super::types::sqlstate_error;

/// The seven canonical engine names. Anything else is `42601`.
pub const CANONICAL_ENGINES: &[&str] = &[
    "document_schemaless",
    "document_strict",
    "kv",
    "columnar",
    "timeseries",
    "spatial",
    "vector",
];

/// Validate a pre-parsed `engine: Option<&str>` from the typed AST.
///
/// Also checks for deprecated axes (profile=, vector_field= without engine=)
/// from the parsed `options` slice.
///
/// Returns the canonical engine static str (`Some`) or `None` (caller applies default).
pub fn validate_engine_name(
    engine: Option<&str>,
    options: &[(String, String)],
) -> PgWireResult<Option<&'static str>> {
    // ── Axis B: profile= present ──────────────────────────────────
    if let Some((_, profile_val)) = options
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case("profile"))
    {
        let profile_up = profile_val.to_uppercase();
        let suggestion = match profile_up.as_str() {
            "TIMESERIES" => "engine='timeseries'",
            "SPATIAL" => "engine='spatial'",
            other => {
                return Err(sqlstate_error(
                    "0A000",
                    &format!(
                        "NodeDB has canonicalized engine selection; 'WITH (profile='{other}')' \
                         is no longer accepted. Use WITH (engine='timeseries') or \
                         WITH (engine='spatial')"
                    ),
                ));
            }
        };
        return Err(sqlstate_error(
            "0A000",
            &format!(
                "NodeDB has canonicalized engine selection; 'WITH (profile=...)' is no longer \
                 accepted. Use: CREATE COLLECTION foo (...) WITH ({suggestion})"
            ),
        ));
    }

    // ── Axis C: vector_field= without engine= ─────────────────────
    if engine.is_none()
        && options
            .iter()
            .any(|(k, _)| k.eq_ignore_ascii_case("vector_field"))
    {
        return Err(sqlstate_error(
            "0A000",
            "NodeDB has canonicalized engine selection; 'WITH (vector_field=...)' without \
             'engine=...' is no longer accepted. Use: CREATE COLLECTION foo (...) WITH \
             (engine='vector', vector_field='embedding')",
        ));
    }

    let engine_lower = match engine {
        None => return Ok(None),
        Some(e) => e.to_lowercase(),
    };

    let canonical = match engine_lower.as_str() {
        "document_schemaless" => "document_schemaless",
        "document_strict" => "document_strict",
        "kv" => "kv",
        "columnar" => "columnar",
        "timeseries" => "timeseries",
        "spatial" => "spatial",
        "vector" => "vector",
        "strict" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'strict'; did you mean 'document_strict'? Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        "document" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'document'; did you mean 'document_schemaless' or \
                     'document_strict'? Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        "key_value" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'key_value'; did you mean 'kv'? Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        "fts" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'fts'; FTS uses CREATE FULLTEXT INDEX (separate DDL); \
                     graph operations work against existing collections via MATCH. \
                     Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        "graph" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'graph'; graph operations work against existing collections \
                     via MATCH / GRAPH INSERT/DELETE — there is no engine='graph'. \
                     Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        other => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine '{other}'; supported: {}. \
                     (FTS uses CREATE FULLTEXT INDEX; graph operations work against existing \
                     collections via MATCH.)",
                    canonical_list()
                ),
            ));
        }
    };

    Ok(Some(canonical))
}

/// Build the canonical-list suffix used in unknown-engine errors.
fn canonical_list() -> String {
    CANONICAL_ENGINES.join(", ")
}

/// Parse and validate the `WITH (engine='...')` option from `sql`.
///
/// Returns:
/// - `Ok(Some(name))` — one of the 7 canonical names (static str).
/// - `Ok(None)`       — no `engine=` key present; caller applies its default.
/// - `Err(_)`         — hard rejection (Axis A / B / C / unknown engine).
///
/// All rejection codes follow the spec:
/// - `0A000` — deprecated axis or unsupported feature syntax.
/// - `42601` — unknown engine name.
pub fn parse_engine_option(sql: &str, upper: &str) -> PgWireResult<Option<&'static str>> {
    // ── Axis A: TYPE <keyword> ──────────────────────────────────────────────
    // Detect any `TYPE <keyword>` token sequence in the uppercased SQL.
    if upper.contains("TYPE")
        && let Some(suggestion) = axis_a_suggestion(upper)
    {
        return Err(sqlstate_error(
            "0A000",
            &format!(
                "NodeDB has canonicalized engine selection; the 'TYPE ...' syntax is no \
                     longer accepted. Use: CREATE COLLECTION foo (...) WITH ({suggestion})"
            ),
        ));
    }

    // ── Axis B: WITH (profile='...') ────────────────────────────────────────
    // Reject profile= whether or not engine= is also present.
    if let Some(profile_val) = extract_with_value(sql, "profile") {
        let profile_up = profile_val.to_uppercase();
        let suggestion = match profile_up.as_str() {
            "TIMESERIES" => "engine='timeseries'",
            "SPATIAL" => "engine='spatial'",
            other => {
                return Err(sqlstate_error(
                    "0A000",
                    &format!(
                        "NodeDB has canonicalized engine selection; 'WITH (profile='{other}')' \
                         is no longer accepted. Use WITH (engine='timeseries') or \
                         WITH (engine='spatial')"
                    ),
                ));
            }
        };
        return Err(sqlstate_error(
            "0A000",
            &format!(
                "NodeDB has canonicalized engine selection; 'WITH (profile=...)' is no longer \
                 accepted. Use: CREATE COLLECTION foo (...) WITH ({suggestion})"
            ),
        ));
    }

    // ── Resolve engine= ─────────────────────────────────────────────────────
    let engine_val = match extract_with_value(sql, "engine") {
        Some(v) => v,
        None => {
            // ── Axis C: vector_field= without engine= ──────────────────────
            if extract_with_value(sql, "vector_field").is_some() {
                return Err(sqlstate_error(
                    "0A000",
                    "NodeDB has canonicalized engine selection; 'WITH (vector_field=...)' without \
                     'engine=...' is no longer accepted. Use: CREATE COLLECTION foo (...) WITH \
                     (engine='vector', vector_field='embedding')",
                ));
            }
            return Ok(None);
        }
    };

    let engine_lower = engine_val.to_lowercase();

    // Map to canonical static str, or reject.
    let canonical = match engine_lower.as_str() {
        "document_schemaless" => "document_schemaless",
        "document_strict" => "document_strict",
        "kv" => "kv",
        "columnar" => "columnar",
        "timeseries" => "timeseries",
        "spatial" => "spatial",
        "vector" => "vector",

        // Helpful alias rejections.
        "strict" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'strict'; did you mean 'document_strict'? Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        "document" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'document'; did you mean 'document_schemaless' or \
                     'document_strict'? Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        "key_value" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'key_value'; did you mean 'kv'? Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        "fts" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'fts'; FTS uses CREATE FULLTEXT INDEX (separate DDL); \
                     graph operations work against existing collections via MATCH. \
                     Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        "graph" => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine 'graph'; graph operations work against existing collections \
                     via MATCH / GRAPH INSERT/DELETE — there is no engine='graph'. \
                     Supported engines: {}",
                    canonical_list()
                ),
            ));
        }
        other => {
            return Err(sqlstate_error(
                "42601",
                &format!(
                    "unknown engine '{other}'; supported: {}. \
                     (FTS uses CREATE FULLTEXT INDEX; graph operations work against existing \
                     collections via MATCH.)",
                    canonical_list()
                ),
            ));
        }
    };

    Ok(Some(canonical))
}

/// Extract a value from a `WITH` clause: `key = 'value'` or `key = "value"`.
///
/// Duplicated from `collection::helpers` to avoid `pub(super)` visibility reach.
fn extract_with_value(sql: &str, key: &str) -> Option<String> {
    let upper = sql.to_uppercase();
    let key_upper = key.to_uppercase();
    let pos = upper.find(&key_upper)?;
    let after = sql[pos + key.len()..].trim_start();
    let after = after.strip_prefix('=')?;
    let after = after.trim_start();
    let val = after.trim_start_matches('\'').trim_start_matches('"');
    let end = val
        .find('\'')
        .or_else(|| val.find('"'))
        .or_else(|| val.find(','))
        .or_else(|| val.find(')'))
        .unwrap_or(val.len());
    let result = val[..end].trim().to_string();
    if result.is_empty() {
        None
    } else {
        Some(result)
    }
}

/// Map a `TYPE <keyword>` token pair to the corrective `WITH (engine=...)` suggestion.
///
/// Returns `None` when the SQL contains TYPE but we cannot identify a known
/// keyword following it (i.e., not one of our legacy axes — let the caller
/// fall through to normal parsing, which will surface a different error).
fn axis_a_suggestion(upper: &str) -> Option<&'static str> {
    // Walk the uppercased token stream looking for TYPE followed by a known keyword.
    let tokens: Vec<&str> = upper.split_whitespace().collect();
    for window in tokens.windows(2) {
        if window[0] == "TYPE" {
            return match window[1] {
                "STRICT" => Some("engine='document_strict'"),
                "KEY_VALUE" => Some("engine='kv'"),
                "COLUMNAR" => Some("engine='columnar'"),
                "VECTOR" => Some("engine='vector', vector_field='...'"),
                _ => None,
            };
        }
    }
    None
}

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

    fn ok_engine(sql: &str, upper: &str) -> Option<&'static str> {
        parse_engine_option(sql, upper).expect("should succeed")
    }

    fn err_code(sql: &str, upper: &str) -> String {
        match parse_engine_option(sql, upper) {
            Ok(_) => panic!("expected error"),
            Err(e) => e.to_string(),
        }
    }

    // ── Axis A ────────────────────────────────────────────────────────────

    #[test]
    fn axis_a_type_strict() {
        let sql = "CREATE COLLECTION foo TYPE STRICT (id BIGINT)";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(
            e.contains("0A000") || e.contains("engine='document_strict'"),
            "{e}"
        );
    }

    #[test]
    fn axis_a_type_key_value() {
        let sql = "CREATE COLLECTION foo TYPE KEY_VALUE (id TEXT)";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("engine='kv'"), "{e}");
    }

    #[test]
    fn axis_a_type_columnar() {
        let sql = "CREATE COLLECTION foo TYPE COLUMNAR (ts TIMESTAMP)";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("engine='columnar'"), "{e}");
    }

    #[test]
    fn axis_a_type_vector() {
        let sql = "CREATE COLLECTION foo TYPE VECTOR (emb VECTOR(128))";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("engine='vector'"), "{e}");
    }

    // ── Axis B ────────────────────────────────────────────────────────────

    #[test]
    fn axis_b_profile_timeseries() {
        let sql = "CREATE COLLECTION foo (ts TIMESTAMP) WITH (profile='timeseries')";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("engine='timeseries'"), "{e}");
    }

    #[test]
    fn axis_b_profile_spatial() {
        let sql = "CREATE COLLECTION foo (ts TIMESTAMP) WITH (profile='spatial')";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("engine='spatial'"), "{e}");
    }

    #[test]
    fn axis_b_engine_columnar_with_profile() {
        let sql =
            "CREATE COLLECTION foo (ts TIMESTAMP) WITH (engine='columnar', profile='timeseries')";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("engine='timeseries'"), "{e}");
    }

    // ── Axis C ────────────────────────────────────────────────────────────

    #[test]
    fn axis_c_vector_field_without_engine() {
        let sql = "CREATE COLLECTION foo (emb VECTOR(128)) WITH (vector_field='emb')";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("engine='vector'"), "{e}");
    }

    // ── Unknown engine 42601 ──────────────────────────────────────────────

    #[test]
    fn unknown_engine_fts() {
        let sql = "CREATE COLLECTION foo WITH (engine='fts')";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("42601") || e.contains("fts"), "{e}");
        assert!(e.contains("FULLTEXT") || e.contains("fts"), "{e}");
    }

    #[test]
    fn unknown_engine_graph() {
        let sql = "CREATE COLLECTION foo WITH (engine='graph')";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("graph"), "{e}");
        assert!(e.contains("MATCH"), "{e}");
    }

    #[test]
    fn unknown_engine_strict_alias() {
        let sql = "CREATE COLLECTION foo WITH (engine='strict')";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("document_strict"), "{e}");
    }

    #[test]
    fn unknown_engine_nonsense() {
        let sql = "CREATE COLLECTION foo WITH (engine='nonsense')";
        let upper = sql.to_uppercase();
        let e = err_code(sql, &upper);
        assert!(e.contains("nonsense"), "{e}");
        assert!(e.contains("document_schemaless"), "{e}");
    }

    // ── Canonical success ─────────────────────────────────────────────────

    #[test]
    fn canonical_document_schemaless() {
        let sql = "CREATE COLLECTION foo WITH (engine='document_schemaless')";
        let upper = sql.to_uppercase();
        assert_eq!(ok_engine(sql, &upper), Some("document_schemaless"));
    }

    #[test]
    fn canonical_document_strict() {
        let sql = "CREATE COLLECTION foo WITH (engine='document_strict')";
        let upper = sql.to_uppercase();
        assert_eq!(ok_engine(sql, &upper), Some("document_strict"));
    }

    #[test]
    fn canonical_kv() {
        let sql = "CREATE COLLECTION foo WITH (engine='kv')";
        let upper = sql.to_uppercase();
        assert_eq!(ok_engine(sql, &upper), Some("kv"));
    }

    #[test]
    fn canonical_columnar() {
        let sql = "CREATE COLLECTION foo WITH (engine='columnar')";
        let upper = sql.to_uppercase();
        assert_eq!(ok_engine(sql, &upper), Some("columnar"));
    }

    #[test]
    fn canonical_timeseries() {
        let sql = "CREATE COLLECTION foo WITH (engine='timeseries')";
        let upper = sql.to_uppercase();
        assert_eq!(ok_engine(sql, &upper), Some("timeseries"));
    }

    #[test]
    fn canonical_spatial() {
        let sql = "CREATE COLLECTION foo WITH (engine='spatial')";
        let upper = sql.to_uppercase();
        assert_eq!(ok_engine(sql, &upper), Some("spatial"));
    }

    #[test]
    fn canonical_vector() {
        let sql = "CREATE COLLECTION foo WITH (engine='vector', vector_field='emb')";
        let upper = sql.to_uppercase();
        assert_eq!(ok_engine(sql, &upper), Some("vector"));
    }

    #[test]
    fn no_engine_returns_none() {
        let sql = "CREATE COLLECTION foo";
        let upper = sql.to_uppercase();
        assert_eq!(ok_engine(sql, &upper), None);
    }

    #[test]
    fn no_engine_with_columns_returns_none() {
        let sql = "CREATE COLLECTION foo (id INT)";
        let upper = sql.to_uppercase();
        assert_eq!(ok_engine(sql, &upper), None);
    }
}