kglite 0.16.6

Pure-Rust embedded Cypher knowledge graph engine with in-memory, mmap, and disk storage, and agent-facing schema introspection
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! SQL-dump export — the no-lock-in exit to a relational database.
//!
//! Emits a deterministic SQLite-dialect script: node types become tables, one
//! per type, and connection types become link tables. Ingest it with the
//! stock `sqlite3` CLI, which every platform already has:
//!
//! ```text
//! kglite export-sqlite graph.kgl dump.sql
//! sqlite3 out.db < dump.sql
//! ```
//!
//! **Why a script and not a `.db` file.** Writing a real SQLite database would
//! mean linking `rusqlite`/`libsqlite3-sys` into the engine or CLI. A text
//! dump reaches the same destination — a genuine, queryable SQLite database
//! the user owns — with **zero new dependencies**, and it is additionally
//! readable, diffable, greppable, and ingestible by Postgres/DuckDB/MySQL
//! after trivial edits. The dependency-free form is strictly the smaller
//! honest surface, so that is what we ship.
//!
//! **Deliberate schema choices**, each of which a careless exporter gets
//! wrong:
//!
//! - **No `PRIMARY KEY` on `id`.** kglite detects duplicate ids at index-build
//!   time and warns rather than rejecting them (see `warn_on_duplicate_ids`),
//!   so a graph legitimately *may* hold two nodes of a type sharing an id.
//!   Declaring a primary key would make such a dump fail to ingest halfway
//!   through. A plain index gives the join performance without the abort.
//! - **No foreign keys on the link tables.** Same reason, plus link tables
//!   reference `(type, id)` pairs across many node tables, which SQL FKs
//!   cannot express.
//! - **Link tables carry `source_type`/`target_type`.** One connection type
//!   can join several type pairs in kglite, so the endpoint type is data, not
//!   schema.
//! - **Booleans become `INTEGER` 0/1** — SQLite has no boolean type.
//! - **Non-finite floats become `NULL`.** SQLite cannot store NaN/±Inf, and
//!   silently writing the string "NaN" into a REAL column is worse than a
//!   null.
//! - **Reserved provenance keys** (`updated_at`/`git_sha`/`modified_by`) are
//!   omitted, consistent with the canonical node projection used everywhere
//!   else; they are engine metadata, not user data.

use std::collections::{BTreeMap, BTreeSet};

use petgraph::graph::NodeIndex;

use crate::datatypes::values::{raw_string, Value};
use crate::graph::schema::{is_reserved_provenance_key, CurrentSelection, DirGraph};
use crate::graph::storage::GraphRead;

/// The two identity columns every node table leads with. They come from the
/// node's canonical identity rather than from its property bag.
const NODE_IDENTITY_COLUMNS: [&str; 2] = ["id", "title"];

/// The four endpoint columns every link table leads with.
const EDGE_ENDPOINT_COLUMNS: [&str; 4] = ["source_type", "source_id", "target_type", "target_id"];

/// SQLite column affinity inferred from the values a column actually holds.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SqlAffinity {
    /// No non-null value seen yet.
    Unknown,
    Integer,
    Real,
    Text,
}

impl SqlAffinity {
    /// Widen to accommodate one more observed value. Integer+Real → Real;
    /// anything mixed with Text → Text. Nulls carry no type information.
    fn observe(self, value: &Value) -> Self {
        let seen = match value {
            Value::Null => return self,
            Value::Int64(_) | Value::UniqueId(_) | Value::Boolean(_) | Value::NodeRef(_) => {
                Self::Integer
            }
            Value::Float64(_) => Self::Real,
            _ => Self::Text,
        };
        match (self, seen) {
            (Self::Unknown, other) => other,
            (current, other) if current == other => current,
            (Self::Integer, Self::Real) | (Self::Real, Self::Integer) => Self::Real,
            _ => Self::Text,
        }
    }

    /// The declared type. An all-null column gets `TEXT`: it is the most
    /// permissive affinity, so re-populating the table later never fails.
    fn declaration(self) -> &'static str {
        match self {
            Self::Integer => "INTEGER",
            Self::Real => "REAL",
            Self::Text | Self::Unknown => "TEXT",
        }
    }
}

/// A table about to be emitted: its ordered columns and their affinities.
struct TableSpec {
    /// Column names in emission order — leading fixed columns, then the
    /// sorted union of property names.
    columns: Vec<String>,
    affinities: Vec<SqlAffinity>,
}

impl TableSpec {
    fn new(leading: &[&str]) -> Self {
        TableSpec {
            columns: leading.iter().map(|c| c.to_string()).collect(),
            affinities: vec![SqlAffinity::Unknown; leading.len()],
        }
    }

    /// Append the sorted property columns and seed their affinities.
    fn with_property_columns(mut self, names: BTreeSet<String>) -> Self {
        for name in names {
            self.columns.push(name);
            self.affinities.push(SqlAffinity::Unknown);
        }
        self
    }

    /// Widen the affinity of `column` by one observed value.
    fn observe(&mut self, column: usize, value: &Value) {
        self.affinities[column] = self.affinities[column].observe(value);
    }

    /// Position of a property column, or `None` if it isn't in this table.
    fn position(&self, column: &str) -> Option<usize> {
        self.columns.iter().position(|c| c == column)
    }

    fn ddl(&self, table: &str) -> String {
        let body: Vec<String> = self
            .columns
            .iter()
            .zip(&self.affinities)
            .map(|(name, affinity)| format!("  {} {}", quote_ident(name), affinity.declaration()))
            .collect();
        format!(
            "CREATE TABLE {} (\n{}\n);\n",
            quote_ident(table),
            body.join(",\n")
        )
    }

    /// Column list for the `INSERT INTO … (…)` prefix.
    fn insert_prefix(&self, table: &str) -> String {
        let cols: Vec<String> = self.columns.iter().map(|c| quote_ident(c)).collect();
        format!("INSERT INTO {} ({})", quote_ident(table), cols.join(", "))
    }
}

/// One materialized row, aligned to a [`TableSpec`]'s columns.
type Row = Vec<Value>;

// ── SQL text helpers ────────────────────────────────────────────────────────

/// Quote an identifier with SQL's standard double quotes, doubling any
/// embedded quote. Node/connection type names come from user data, so they
/// cannot be interpolated raw.
fn quote_ident(name: &str) -> String {
    format!("\"{}\"", name.replace('"', "\"\""))
}

/// Quote a string literal with single quotes, doubling any embedded quote.
fn quote_text(text: &str) -> String {
    format!("'{}'", text.replace('\'', "''"))
}

/// Render a float so SQLite reads it back as the same `f64`. Rust's `f64`
/// `Display` already emits the shortest round-tripping form; the only extra
/// work is forcing a decimal point so a whole number lands in a REAL column
/// as a float rather than an integer literal.
fn float_literal(value: f64) -> String {
    if !value.is_finite() {
        return "NULL".to_string();
    }
    let rendered = value.to_string();
    if rendered.contains('.') || rendered.contains('e') || rendered.contains('E') {
        rendered
    } else {
        format!("{rendered}.0")
    }
}

/// Render one property value as a SQL literal.
///
/// Scalars keep their type (integers stay integers, floats keep full
/// round-trip precision). Structured values — spatial, temporal-interval,
/// collection, and graph-entity variants — have no relational counterpart, so
/// they become JSON text: lossless enough to be useful, and honest about not
/// being a native column type.
fn sql_literal(value: &Value) -> String {
    match value {
        Value::Null => "NULL".to_string(),
        Value::Boolean(flag) => if *flag { "1" } else { "0" }.to_string(),
        Value::Int64(number) => number.to_string(),
        Value::UniqueId(id) => id.to_string(),
        Value::NodeRef(idx) => idx.to_string(),
        Value::Float64(number) => float_literal(*number),
        Value::String(text) => quote_text(text),
        Value::DateTime(date) => quote_text(&date.to_string()),
        Value::Timestamp(stamp) => quote_text(&stamp.format("%Y-%m-%dT%H:%M:%S").to_string()),
        Value::Point { lat, lon } => quote_text(&format!("{{\"lat\":{lat},\"lon\":{lon}}}")),
        Value::Duration {
            months,
            days,
            seconds,
        } => quote_text(&format!(
            "{{\"months\":{months},\"days\":{days},\"seconds\":{seconds}}}"
        )),
        Value::List(_)
        | Value::Map(_)
        | Value::Node(_)
        | Value::Relationship(_)
        | Value::Path(_) => {
            // All five derive Serialize and hold no cycles, so this can't
            // realistically fail; fall back to the display form if it does.
            match serde_json::to_string(value) {
                Ok(json) => quote_text(&json),
                Err(_) => quote_text(&raw_string(value)),
            }
        }
    }
}

// ── Collection ──────────────────────────────────────────────────────────────

/// A node's exportable properties: the canonical backend-consistent
/// projection, minus the identity columns that get their own slots.
///
/// Going through `materialize_node_value` (rather than `property_iter`) is
/// mandatory, not stylistic: on the columnar disk/mapped backends the
/// properties live in a per-type column store and `property_iter` yields
/// nothing at all. It also recovers aliased id/title columns and already
/// drops reserved provenance keys.
fn node_properties(graph: &DirGraph, idx: NodeIndex) -> BTreeMap<String, Value> {
    let Some(materialized) =
        crate::graph::languages::cypher::executor::helpers::materialize_node_value(idx, graph)
    else {
        return BTreeMap::new();
    };
    // The two edits below are the only in-place mutations of a materialised
    // node's property map in the engine. They run against a map this function
    // solely owns, so `PropMap`'s copy-on-write mutation does not copy.
    let structural_type = materialized
        .properties
        .get("type")
        .map(raw_string)
        .unwrap_or_default();
    let mut properties = materialized.properties;
    for identity in NODE_IDENTITY_COLUMNS {
        properties.remove(identity);
    }
    // `type` is a soft alias: usually it is just the structural type name,
    // which the table name already carries, so drop it. If a node stores its
    // own `type` property it shadows the structural one and must survive —
    // dropping it would be silent data loss.
    if graph
        .graph
        .node_view(idx)
        .and_then(|node| node.get_property_value("type"))
        .is_none()
    {
        properties.remove("type");
    } else {
        properties.insert("type", Value::String(structural_type));
    }
    // SQL export speaks `BTreeMap` to `build_table`; this is a cold,
    // whole-graph dump path, so the re-key is not worth threading a second
    // container type through the table builder.
    properties.into()
}

/// Group the exported nodes by type, sorted by type then by id, mirroring
/// `to_text`'s ordering so two saves of the same graph produce the same dump.
fn nodes_by_type(
    graph: &DirGraph,
    indices: &[NodeIndex],
) -> BTreeMap<String, Vec<(String, NodeIndex)>> {
    let mut grouped: BTreeMap<String, Vec<(String, NodeIndex)>> = BTreeMap::new();
    for &idx in indices {
        if let Some(node) = graph.graph.node_view(idx) {
            let type_name = node.node_type_str(&graph.interner).to_string();
            grouped
                .entry(type_name)
                .or_default()
                .push((raw_string(&node.id()), idx));
        }
    }
    for rows in grouped.values_mut() {
        rows.sort_by(|a, b| a.0.cmp(&b.0));
    }
    grouped
}

/// One collected edge, endpoints already resolved to `(type, id)` pairs.
struct EdgeRow {
    source_type: String,
    source_id: String,
    target_type: String,
    target_id: String,
    properties: BTreeMap<String, Value>,
}

/// Group the exported edges by connection type, sorted by
/// `(source_id, target_id)` for determinism. Only edges whose *both*
/// endpoints are exported are included — a link table must never reference a
/// row that isn't in the dump.
fn edges_by_type(graph: &DirGraph, indices: &[NodeIndex]) -> BTreeMap<String, Vec<EdgeRow>> {
    let exported: BTreeSet<NodeIndex> = indices.iter().copied().collect();
    let mut grouped: BTreeMap<String, Vec<EdgeRow>> = BTreeMap::new();
    for &source in indices {
        let Some(source_node) = graph.graph.node_view(source) else {
            continue;
        };
        let source_type = source_node.node_type_str(&graph.interner).to_string();
        let source_id = raw_string(&source_node.id());
        for edge in graph.graph.edges(source) {
            let target = edge.target();
            if !exported.contains(&target) {
                continue;
            }
            let Some(target_node) = graph.graph.node_view(target) else {
                continue;
            };
            let weight = edge.weight();
            let mut properties = BTreeMap::new();
            for (key, value) in weight.property_iter(&graph.interner) {
                if !is_reserved_provenance_key(key) {
                    properties.insert(key.to_string(), value.clone());
                }
            }
            grouped
                .entry(weight.connection_type_str(&graph.interner).to_string())
                .or_default()
                .push(EdgeRow {
                    source_type: source_type.clone(),
                    source_id: source_id.clone(),
                    target_type: target_node.node_type_str(&graph.interner).to_string(),
                    target_id: raw_string(&target_node.id()),
                    properties,
                });
        }
    }
    for rows in grouped.values_mut() {
        rows.sort_by(|a, b| {
            (&a.source_id, &a.target_id, &a.source_type, &a.target_type).cmp(&(
                &b.source_id,
                &b.target_id,
                &b.source_type,
                &b.target_type,
            ))
        });
    }
    grouped
}

// ── Emission ────────────────────────────────────────────────────────────────

/// Build the spec + aligned rows for one table from its per-row property maps
/// and the values of its leading fixed columns.
fn build_table(
    leading: &[&str],
    rows: Vec<(Vec<Value>, BTreeMap<String, Value>)>,
) -> (TableSpec, Vec<Row>) {
    let property_names: BTreeSet<String> = rows
        .iter()
        .flat_map(|(_, properties)| properties.keys().cloned())
        .collect();
    let mut spec = TableSpec::new(leading).with_property_columns(property_names);

    let mut aligned = Vec::with_capacity(rows.len());
    for (fixed, properties) in rows {
        let mut row: Row = vec![Value::Null; spec.columns.len()];
        for (position, value) in fixed.into_iter().enumerate() {
            row[position] = value;
        }
        for (name, value) in properties {
            if let Some(position) = spec.position(&name) {
                row[position] = value;
            }
        }
        for (position, value) in row.iter().enumerate() {
            spec.observe(position, value);
        }
        aligned.push(row);
    }
    (spec, aligned)
}

/// Append a table's DDL, its rows, and its indexes to the script.
fn emit_table(
    out: &mut String,
    table: &str,
    spec: &TableSpec,
    rows: &[Row],
    indexed_columns: &[&str],
) {
    out.push_str(&spec.ddl(table));
    let prefix = spec.insert_prefix(table);
    for row in rows {
        let literals: Vec<String> = row.iter().map(sql_literal).collect();
        out.push_str(&format!("{prefix} VALUES ({});\n", literals.join(", ")));
    }
    for column in indexed_columns {
        // Index names are derived from user type names, so they need the same
        // quoting treatment as the table itself.
        let name = format!("idx_{table}_{column}");
        out.push_str(&format!(
            "CREATE INDEX {} ON {} ({});\n",
            quote_ident(&name),
            quote_ident(table),
            quote_ident(column)
        ));
    }
    out.push('\n');
}

/// Pick the link table's name. A connection type and a node type may share a
/// name (nothing in kglite forbids `Person` nodes and a `Person` edge type),
/// but SQL table names must be unique — so a colliding link table takes an
/// `_edge` suffix. Deterministic, and it keeps the common case clean.
fn link_table_name(connection: &str, node_tables: &BTreeSet<String>) -> String {
    if node_tables.contains(connection) {
        format!("{connection}_edge")
    } else {
        connection.to_string()
    }
}

/// Export the graph (or the current selection) as a SQLite-dialect SQL script.
///
/// Node types become tables (`id`, `title`, then the sorted union of the
/// type's property names); connection types become link tables
/// (`source_type`, `source_id`, `target_type`, `target_id`, then their
/// property union). The whole script runs inside one transaction, so an
/// interrupted ingest leaves no half-built database.
///
/// Output is deterministic: types, columns, and rows are all sorted, so the
/// same graph always produces byte-identical SQL. See the module header for
/// the schema decisions and their rationale.
pub fn to_sqlite_dump(
    graph: &DirGraph,
    selection: Option<&CurrentSelection>,
) -> Result<String, String> {
    // Arena guard: disk-backed node/edge reads materialize into the query
    // arena (protocol in disk/graph.rs); no-op on memory/mapped.
    let _arena_guard = graph.graph.begin_query();

    let indices = super::export::selected_node_indices(graph, selection);
    let grouped_nodes = nodes_by_type(graph, &indices);
    let node_tables: BTreeSet<String> = grouped_nodes.keys().cloned().collect();

    let mut out = String::with_capacity(64 * 1024);
    out.push_str(&format!(
        "-- kglite {} SQL export\n\
         -- Ingest with: sqlite3 target.db < this-file.sql\n\
         -- {} node type(s), {} node(s) exported.\n",
        env!("CARGO_PKG_VERSION"),
        grouped_nodes.len(),
        indices.len()
    ));
    out.push_str("PRAGMA foreign_keys=OFF;\nBEGIN TRANSACTION;\n\n");

    for (type_name, nodes) in &grouped_nodes {
        let rows: Vec<(Vec<Value>, BTreeMap<String, Value>)> = nodes
            .iter()
            .map(|(_, idx)| {
                let node = graph.graph.node_view(*idx);
                let identity = vec![
                    node.map(|n| n.id().into_owned()).unwrap_or(Value::Null),
                    node.map(|n| n.title().into_owned()).unwrap_or(Value::Null),
                ];
                (identity, node_properties(graph, *idx))
            })
            .collect();
        let (spec, aligned) = build_table(&NODE_IDENTITY_COLUMNS, rows);
        emit_table(&mut out, type_name, &spec, &aligned, &["id"]);
    }

    for (connection, edges) in edges_by_type(graph, &indices) {
        let rows: Vec<(Vec<Value>, BTreeMap<String, Value>)> = edges
            .into_iter()
            .map(|edge| {
                (
                    vec![
                        Value::String(edge.source_type),
                        Value::String(edge.source_id),
                        Value::String(edge.target_type),
                        Value::String(edge.target_id),
                    ],
                    edge.properties,
                )
            })
            .collect();
        let (spec, aligned) = build_table(&EDGE_ENDPOINT_COLUMNS, rows);
        let table = link_table_name(&connection, &node_tables);
        emit_table(
            &mut out,
            &table,
            &spec,
            &aligned,
            &["source_id", "target_id"],
        );
    }

    out.push_str("COMMIT;\n");
    Ok(out)
}

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

    #[test]
    fn identifiers_and_text_are_escaped() {
        assert_eq!(quote_ident("Per\"son"), "\"Per\"\"son\"");
        assert_eq!(quote_text("O'Brien"), "'O''Brien'");
        // The classic injection shape must land inside one quoted literal.
        assert_eq!(
            quote_text("'); DROP TABLE x; --"),
            "'''); DROP TABLE x; --'"
        );
    }

    #[test]
    fn floats_round_trip_and_non_finite_becomes_null() {
        assert_eq!(float_literal(2.0), "2.0");
        assert_eq!(float_literal(0.1), "0.1");
        assert_eq!(float_literal(1.0 / 3.0), "0.3333333333333333");
        assert_eq!(float_literal(f64::NAN), "NULL");
        assert_eq!(float_literal(f64::INFINITY), "NULL");
    }

    #[test]
    fn affinity_widens_int_to_real_and_collapses_mixed_to_text() {
        let ints = SqlAffinity::Unknown
            .observe(&Value::Int64(1))
            .observe(&Value::Null)
            .observe(&Value::Int64(2));
        assert_eq!(ints.declaration(), "INTEGER");

        let mixed_numeric = ints.observe(&Value::Float64(1.5));
        assert_eq!(mixed_numeric.declaration(), "REAL");

        let with_text = mixed_numeric.observe(&Value::String("x".into()));
        assert_eq!(with_text.declaration(), "TEXT");

        // An all-null column is TEXT: the most permissive affinity.
        assert_eq!(
            SqlAffinity::Unknown.observe(&Value::Null).declaration(),
            "TEXT"
        );
    }

    #[test]
    fn booleans_become_integers_and_nulls_stay_null() {
        assert_eq!(sql_literal(&Value::Boolean(true)), "1");
        assert_eq!(sql_literal(&Value::Boolean(false)), "0");
        assert_eq!(sql_literal(&Value::Null), "NULL");
    }

    /// A graph with two node types, a typed edge, and one node that is
    /// missing a property its sibling has (so the column must be NULL).
    fn mixed_graph() -> DirGraph {
        let mut graph = DirGraph::new();
        let params = std::collections::HashMap::new();
        let options = crate::graph::session::execute::ExecuteOptions::eager(&params);
        for statement in [
            "CREATE (:Person {id: 1, title: 'Ada', age: 36, score: 0.5, active: true})",
            "CREATE (:Person {id: 2, title: \"O'Brien\", age: 41})",
            "CREATE (:Company {id: 10, title: 'Acme'})",
            "MATCH (p:Person {id: 1}), (c:Company {id: 10}) \
             CREATE (p)-[:WORKS_AT {since: 2019}]->(c)",
        ] {
            crate::graph::session::execute::execute_mut(&mut graph, statement, &options)
                .unwrap_or_else(|e| panic!("setup statement failed: {statement}: {e}"));
        }
        graph
    }

    #[test]
    fn dump_emits_one_table_per_type_and_a_link_table() {
        let dump = to_sqlite_dump(&mixed_graph(), None).unwrap();

        // Node tables, one per type; the link table for the connection type.
        assert!(dump.contains("CREATE TABLE \"Person\" ("), "{dump}");
        assert!(dump.contains("CREATE TABLE \"Company\" ("), "{dump}");
        assert!(dump.contains("CREATE TABLE \"WORKS_AT\" ("), "{dump}");

        // Inferred affinities: integer stays integer, float stays real.
        assert!(dump.contains("\"age\" INTEGER"), "{dump}");
        assert!(dump.contains("\"score\" REAL"), "{dump}");

        // The whole script is one transaction, and the type column is dropped
        // (the table name carries it).
        assert!(dump.contains("BEGIN TRANSACTION;"), "{dump}");
        assert!(dump.trim_end().ends_with("COMMIT;"), "{dump}");
        assert!(!dump.contains("\"type\" TEXT"), "{dump}");

        // No PRIMARY KEY (duplicate ids are legal in kglite) but an index.
        assert!(!dump.contains("PRIMARY KEY"), "{dump}");
        assert!(dump.contains("CREATE INDEX \"idx_Person_id\""), "{dump}");
    }

    #[test]
    fn dump_preserves_values_types_quotes_and_missing_properties() {
        let dump = to_sqlite_dump(&mixed_graph(), None).unwrap();

        // Ada: every property present. Booleans become 0/1, the float keeps
        // its decimal point so it lands in the REAL column as a float.
        assert!(dump.contains("VALUES (1, 'Ada', 1, 36, 0.5)"), "{dump}");
        // O'Brien: the apostrophe is doubled, and the two properties the node
        // does not carry come out as NULL rather than empty strings.
        assert!(
            dump.contains("VALUES (2, 'O''Brien', NULL, 41, NULL)"),
            "{dump}"
        );
        // Edge properties ride along with the resolved endpoint (type, id).
        assert!(
            dump.contains("VALUES ('Person', '1', 'Company', '10', 2019)"),
            "{dump}"
        );
    }

    #[test]
    fn dump_is_deterministic() {
        let graph = mixed_graph();
        assert_eq!(
            to_sqlite_dump(&graph, None).unwrap(),
            to_sqlite_dump(&graph, None).unwrap(),
            "the same graph must always produce byte-identical SQL"
        );
    }

    #[test]
    fn link_table_name_avoids_colliding_with_a_node_table() {
        let mut node_tables = BTreeSet::new();
        node_tables.insert("Person".to_string());
        assert_eq!(link_table_name("KNOWS", &node_tables), "KNOWS");
        assert_eq!(link_table_name("Person", &node_tables), "Person_edge");
    }
}