oxgraph-postgres 0.3.2

Postgres-backed OxGraph engine: catalog, build, artifact I/O, query, sync.
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
//! Registration model for relational source tables and edges.

use core::fmt;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Stable identifier for a registered source table.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TableId(pub u32);

impl fmt::Display for TableId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "table:{}", self.0)
    }
}

/// Stable identifier for a registered edge mapping.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EdgeId(pub u32);

impl fmt::Display for EdgeId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "edge:{}", self.0)
    }
}

/// External node key supplied by a registered table row.
///
/// Encodes `(table_id, primary_key)` as `(table_id << 32) | primary_key` so keys from
/// different registered tables remain distinct at build time.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NodeKey(pub u64);

impl NodeKey {
    /// Builds a node key from a registered table id and SQL primary-key value.
    ///
    /// # Performance
    ///
    /// This method is `O(1)`.
    #[must_use]
    pub const fn registered(table: TableId, primary_key: u64) -> Self {
        Self(((table.0 as u64) << 32) | primary_key)
    }

    /// Returns the registered table id embedded in this key.
    #[must_use]
    pub const fn table_id(self) -> TableId {
        TableId((self.0 >> 32) as u32)
    }

    /// Returns the SQL primary-key component embedded in this key.
    #[must_use]
    pub const fn primary_key(self) -> u64 {
        self.0 & 0xFFFF_FFFF
    }
}

impl fmt::Display for NodeKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "node:{}:{}", self.table_id(), self.primary_key())
    }
}

/// Column used for filter/search indexing at query time.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct FilterColumn {
    /// Owning registered table.
    pub table: TableId,
    /// SQL column name (semantic boundary — interpreted only by the extension).
    pub column: String,
}

/// Registered node table metadata.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RegisteredTable {
    /// Stable table id assigned at registration time.
    pub id: TableId,
    /// SQL schema name.
    pub schema: String,
    /// SQL table name.
    pub name: String,
    /// Primary-key column used to derive [`NodeKey`] values.
    pub primary_key_column: String,
}

/// Registered directed edge between two node tables.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RegisteredEdge {
    /// Stable edge id assigned at registration time.
    pub id: EdgeId,
    /// Source endpoint table.
    pub source_table: TableId,
    /// Target endpoint table.
    pub target_table: TableId,
    /// Source foreign-key column on the edge table.
    pub source_column: String,
    /// Target foreign-key column on the edge table.
    pub target_column: String,
    /// Edge table schema.
    pub schema: String,
    /// Edge table name.
    pub name: String,
}

/// In-memory catalog of registered tables, edges, and filter columns.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Catalog {
    /// Registered node tables.
    pub tables: Vec<RegisteredTable>,
    /// Registered edge mappings.
    pub edges: Vec<RegisteredEdge>,
    /// Optional filter columns indexed at query time.
    pub filter_columns: Vec<FilterColumn>,
}

impl Catalog {
    /// Creates an empty catalog.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns whether registration is sufficient to run a snapshot rebuild.
    ///
    /// # Errors
    ///
    /// Returns [`CatalogError::EmptyCatalog`] when no tables are registered.
    ///
    /// # Performance
    ///
    /// This method is `O(1)`.
    pub const fn validate_for_build(&self) -> Result<(), CatalogError> {
        if self.tables.is_empty() {
            return Err(CatalogError::EmptyCatalog);
        }
        Ok(())
    }

    /// Registers a node table after validating uniqueness.
    ///
    /// # Errors
    ///
    /// Returns [`CatalogError`] when ids or names collide.
    ///
    /// # Performance
    ///
    /// This method is `O(t)` where `t` is the number of registered tables.
    pub fn add_table(&mut self, table: RegisteredTable) -> Result<(), CatalogError> {
        if self.tables.iter().any(|existing| existing.id == table.id) {
            return Err(CatalogError::DuplicateTableId(table.id));
        }
        if self
            .tables
            .iter()
            .any(|existing| existing.schema == table.schema && existing.name == table.name)
        {
            return Err(CatalogError::DuplicateTableName {
                schema: table.schema,
                name: table.name,
            });
        }
        self.tables.push(table);
        Ok(())
    }

    /// Registers an edge mapping after validating endpoint tables exist.
    ///
    /// # Errors
    ///
    /// Returns [`CatalogError`] when endpoints are missing or ids collide.
    ///
    /// # Performance
    ///
    /// This method is `O(t + e)`.
    pub fn add_edge(&mut self, edge: RegisteredEdge) -> Result<(), CatalogError> {
        if self.edges.iter().any(|existing| existing.id == edge.id) {
            return Err(CatalogError::DuplicateEdgeId(edge.id));
        }
        if !self
            .tables
            .iter()
            .any(|table| table.id == edge.source_table)
        {
            return Err(CatalogError::MissingTable(edge.source_table));
        }
        if !self
            .tables
            .iter()
            .any(|table| table.id == edge.target_table)
        {
            return Err(CatalogError::MissingTable(edge.target_table));
        }
        self.edges.push(edge);
        Ok(())
    }

    /// Registers a filter column for search indexing.
    ///
    /// # Errors
    ///
    /// Returns [`CatalogError`] when the table id is unknown.
    ///
    /// # Performance
    ///
    /// This method is `O(t + f)`.
    pub fn add_filter_column(&mut self, column: FilterColumn) -> Result<(), CatalogError> {
        if !self.tables.iter().any(|table| table.id == column.table) {
            return Err(CatalogError::MissingTable(column.table));
        }
        self.filter_columns.push(column);
        Ok(())
    }

    /// Looks up a registered table by id.
    #[must_use]
    pub fn table(&self, id: TableId) -> Option<&RegisteredTable> {
        self.tables.iter().find(|table| table.id == id)
    }

    /// Hydrates a catalog from registration rows (SPI adapters collect rows first).
    ///
    /// # Errors
    ///
    /// Returns [`CatalogError`] when any registration row violates catalog invariants.
    ///
    /// # Performance
    ///
    /// This method is `O(t + e + f)` for table, edge, and filter row counts.
    pub fn from_registration_rows(
        tables: impl IntoIterator<Item = RegisteredTable>,
        edges: impl IntoIterator<Item = RegisteredEdge>,
        filter_columns: impl IntoIterator<Item = FilterColumn>,
    ) -> Result<Self, CatalogError> {
        let mut catalog = Self::new();
        for table in tables {
            catalog.add_table(table)?;
        }
        for edge in edges {
            catalog.add_edge(edge)?;
        }
        for column in filter_columns {
            catalog.add_filter_column(column)?;
        }
        Ok(catalog)
    }
}

impl RegisteredEdge {
    /// Builds a validated `SELECT` that resolves endpoint [`NodeKey`] primary-key values.
    ///
    /// When endpoints reference different registered tables, the scan joins each node
    /// table on the edge foreign-key columns. When both endpoints use the same registered
    /// table and edge columns store that table's primary-key values directly, a single-table
    /// scan is used.
    ///
    /// # Errors
    ///
    /// Returns [`CatalogError::InvalidSqlIdent`] when any identifier is unsafe, or
    /// [`CatalogError::MissingTable`] when endpoint tables are not registered.
    ///
    /// # Performance
    ///
    /// This method is `O(i)` where `i` is total identifier length.
    pub fn edge_scan_sql(&self, catalog: &Catalog) -> Result<alloc::string::String, CatalogError> {
        validate_sql_ident(&self.schema)?;
        validate_sql_ident(&self.name)?;
        validate_sql_ident(&self.source_column)?;
        validate_sql_ident(&self.target_column)?;

        let source = catalog
            .table(self.source_table)
            .ok_or(CatalogError::MissingTable(self.source_table))?;
        let target = catalog
            .table(self.target_table)
            .ok_or(CatalogError::MissingTable(self.target_table))?;
        validate_sql_ident(&source.schema)?;
        validate_sql_ident(&source.name)?;
        validate_sql_ident(&target.schema)?;
        validate_sql_ident(&target.name)?;
        validate_sql_ident(&source.primary_key_column)?;
        validate_sql_ident(&target.primary_key_column)?;

        if self.source_table == self.target_table {
            return Ok(alloc::format!(
                "SELECT \"{}\"::bigint, \"{}\"::bigint FROM \"{}\".\"{}\"",
                self.source_column,
                self.target_column,
                self.schema,
                self.name
            ));
        }

        Ok(alloc::format!(
            "SELECT src.\"{}\"::bigint, tgt.\"{}\"::bigint \
             FROM \"{}\".\"{}\" e \
             JOIN \"{}\".\"{}\" src ON e.\"{}\" = src.\"{}\" \
             JOIN \"{}\".\"{}\" tgt ON e.\"{}\" = tgt.\"{}\"",
            source.primary_key_column,
            target.primary_key_column,
            self.schema,
            self.name,
            source.schema,
            source.name,
            self.source_column,
            source.primary_key_column,
            target.schema,
            target.name,
            self.target_column,
            target.primary_key_column,
        ))
    }
}

/// Validates a SQL primary-key value for [`NodeKey::registered`].
///
/// # Errors
///
/// Returns [`CatalogError::InvalidPrimaryKey`] for negative values and
/// [`CatalogError::PrimaryKeyOutOfRange`] when the key does not fit the lower
/// 32 bits of a [`NodeKey`].
///
/// # Performance
///
/// This function is `O(1)`.
pub fn validate_primary_key(value: i64) -> Result<u64, CatalogError> {
    if value.is_negative() {
        return Err(CatalogError::InvalidPrimaryKey);
    }
    let primary_key = u64::try_from(value).map_err(|_| CatalogError::PrimaryKeyOutOfRange)?;
    if primary_key > u64::from(u32::MAX) {
        return Err(CatalogError::PrimaryKeyOutOfRange);
    }
    Ok(primary_key)
}

/// Parses a registered table id from SPI/catalog storage.
///
/// # Errors
///
/// Returns [`CatalogError::InvalidTableId`] when `value` is negative or overflows `u32`.
///
/// # Performance
///
/// This function is `O(1)`.
pub fn table_id_from_i32(value: i32) -> Result<TableId, CatalogError> {
    if value.is_negative() {
        return Err(CatalogError::InvalidTableId);
    }
    u32::try_from(value)
        .map(TableId)
        .map_err(|_| CatalogError::InvalidTableId)
}

/// Parses a registered edge id from SPI/catalog storage.
///
/// # Errors
///
/// Returns [`CatalogError::InvalidEdgeId`] when `value` is negative or overflows `u32`.
///
/// # Performance
///
/// This function is `O(1)`.
pub fn edge_id_from_i32(value: i32) -> Result<EdgeId, CatalogError> {
    if value.is_negative() {
        return Err(CatalogError::InvalidEdgeId);
    }
    u32::try_from(value)
        .map(EdgeId)
        .map_err(|_| CatalogError::InvalidEdgeId)
}

/// Returns whether `ident` is safe for double-quoted SQL identifiers.
///
/// # Errors
///
/// Returns [`CatalogError::InvalidSqlIdent`] when the identifier is empty,
/// starts with an unsafe character, or contains non-identifier characters.
///
/// # Performance
///
/// This function is `O(i)` where `i` is identifier length.
pub fn validate_sql_ident(ident: &str) -> Result<(), CatalogError> {
    let mut chars = ident.chars();
    let Some(first) = chars.next() else {
        return Err(CatalogError::InvalidSqlIdent);
    };
    if !first.is_ascii_alphabetic() && first != '_' {
        return Err(CatalogError::InvalidSqlIdent);
    }
    if !chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '_') {
        return Err(CatalogError::InvalidSqlIdent);
    }
    Ok(())
}

/// Catalog validation failures.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CatalogError {
    /// No tables were registered.
    EmptyCatalog,
    /// Duplicate registered table id.
    DuplicateTableId(TableId),
    /// Duplicate registered edge id.
    DuplicateEdgeId(EdgeId),
    /// Duplicate schema-qualified table name.
    DuplicateTableName {
        /// SQL schema name.
        schema: String,
        /// SQL table name.
        name: String,
    },
    /// Referenced table id was not registered.
    MissingTable(TableId),
    /// SQL identifier failed validation.
    InvalidSqlIdent,
    /// SQL primary key was negative.
    InvalidPrimaryKey,
    /// SQL primary key does not fit in a [`NodeKey`] payload.
    PrimaryKeyOutOfRange,
    /// Registered table id was negative or overflowed `u32`.
    InvalidTableId,
    /// Registered edge id was negative or overflowed `u32`.
    InvalidEdgeId,
}

impl fmt::Display for CatalogError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyCatalog => f.write_str("catalog must register at least one table"),
            Self::DuplicateTableId(id) => write!(f, "duplicate table id {id}"),
            Self::DuplicateEdgeId(id) => write!(f, "duplicate edge id {id}"),
            Self::DuplicateTableName { schema, name } => {
                write!(f, "duplicate table name {schema}.{name}")
            }
            Self::MissingTable(id) => write!(f, "missing catalog table {id}"),
            Self::InvalidSqlIdent => f.write_str("invalid SQL identifier"),
            Self::InvalidPrimaryKey => f.write_str("primary key must be non-negative"),
            Self::PrimaryKeyOutOfRange => {
                f.write_str("primary key must fit in u32 for NodeKey encoding")
            }
            Self::InvalidTableId => f.write_str("invalid registered table id"),
            Self::InvalidEdgeId => f.write_str("invalid registered edge id"),
        }
    }
}

impl core::error::Error for CatalogError {}