citadeldb-sql 0.16.0

SQL parser, planner, and executor for Citadel encrypted 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
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
//! Built-in virtual tables: rows materialized from Rust iterators instead of
//! the B+ tree. Used for PG system catalog views (`pg_timezone_*`,
//! `information_schema.*`).

use std::sync::Arc;

use citadel::Database;
use rustc_hash::FxHashSet;

use crate::error::Result;
use crate::schema::SchemaManager;
use crate::types::{DataType, QueryResult, Value};

pub trait VirtualTable: Send + Sync {
    fn name(&self) -> &str;
    fn scan(&self, db: &Database, schema: &SchemaManager) -> Result<QueryResult>;
}

pub fn register_builtins(schema: &mut SchemaManager) {
    let entries: [Arc<dyn VirtualTable>; 9] = [
        Arc::new(PgTimezoneNames),
        Arc::new(PgTimezoneAbbrevs),
        Arc::new(InfoSchemaTables),
        Arc::new(InfoSchemaColumns),
        Arc::new(InfoSchemaKeyColumnUsage),
        Arc::new(InfoSchemaTableConstraints),
        Arc::new(InfoSchemaTriggers),
        Arc::new(CitadelTriggersStatus),
        Arc::new(PgMatviews),
    ];
    for vt in entries {
        schema.register_virtual(vt);
    }
}

pub struct PgTimezoneNames;
impl VirtualTable for PgTimezoneNames {
    fn name(&self) -> &str {
        "pg_timezone_names"
    }
    fn scan(&self, _db: &Database, _schema: &SchemaManager) -> Result<QueryResult> {
        let columns = vec![
            "name".to_string(),
            "utc_offset".to_string(),
            "is_dst".to_string(),
        ];
        let now = jiff::Timestamp::now();
        let db = jiff::tz::db();
        let mut rows = Vec::new();
        for name in db.available() {
            if let Ok(tz) = db.get(name.as_str()) {
                let info = tz.to_offset_info(now);
                let utc_offset = Value::Interval {
                    months: 0,
                    days: 0,
                    micros: i64::from(info.offset().seconds()) * 1_000_000,
                };
                rows.push(vec![
                    Value::Text(name.to_string().into()),
                    utc_offset,
                    Value::Boolean(info.dst().is_dst()),
                ]);
            }
        }
        Ok(QueryResult { columns, rows })
    }
}

pub struct PgTimezoneAbbrevs;
impl VirtualTable for PgTimezoneAbbrevs {
    fn name(&self) -> &str {
        "pg_timezone_abbrevs"
    }
    fn scan(&self, _db: &Database, _schema: &SchemaManager) -> Result<QueryResult> {
        let columns = vec![
            "abbrev".to_string(),
            "utc_offset".to_string(),
            "is_dst".to_string(),
        ];
        let now = jiff::Timestamp::now();
        let db = jiff::tz::db();
        let mut seen: FxHashSet<String> = FxHashSet::default();
        let mut rows = Vec::new();
        for name in db.available() {
            if let Ok(tz) = db.get(name.as_str()) {
                let info = tz.to_offset_info(now);
                let abbrev = info.abbreviation().to_string();
                if !seen.insert(abbrev.clone()) {
                    continue;
                }
                let utc_offset = Value::Interval {
                    months: 0,
                    days: 0,
                    micros: i64::from(info.offset().seconds()) * 1_000_000,
                };
                rows.push(vec![
                    Value::Text(abbrev.into()),
                    utc_offset,
                    Value::Boolean(info.dst().is_dst()),
                ]);
            }
        }
        Ok(QueryResult { columns, rows })
    }
}

pub struct InfoSchemaTables;
impl VirtualTable for InfoSchemaTables {
    fn name(&self) -> &str {
        "information_schema.tables"
    }
    fn scan(&self, _db: &Database, schema: &SchemaManager) -> Result<QueryResult> {
        let columns = vec![
            "table_catalog".to_string(),
            "table_schema".to_string(),
            "table_name".to_string(),
            "table_type".to_string(),
        ];
        let mut rows = Vec::new();
        for ts in schema.all_schemas() {
            // Listed separately below as MATERIALIZED VIEW.
            if schema.get_matview(&ts.name).is_some() {
                continue;
            }
            rows.push(vec![
                Value::Text("citadel".into()),
                Value::Text("public".into()),
                Value::Text(ts.name.clone().into()),
                Value::Text("BASE TABLE".into()),
            ]);
        }
        for vn in schema.view_names() {
            rows.push(vec![
                Value::Text("citadel".into()),
                Value::Text("public".into()),
                Value::Text(vn.to_string().into()),
                Value::Text("VIEW".into()),
            ]);
        }
        for mv in schema.all_matviews() {
            rows.push(vec![
                Value::Text("citadel".into()),
                Value::Text("public".into()),
                Value::Text(mv.name.clone().into()),
                Value::Text("MATERIALIZED VIEW".into()),
            ]);
        }
        rows.sort_by(|a, b| match (&a[2], &b[2]) {
            (Value::Text(x), Value::Text(y)) => x.cmp(y),
            _ => std::cmp::Ordering::Equal,
        });
        Ok(QueryResult { columns, rows })
    }
}

pub struct InfoSchemaColumns;
impl VirtualTable for InfoSchemaColumns {
    fn name(&self) -> &str {
        "information_schema.columns"
    }
    fn scan(&self, _db: &Database, schema: &SchemaManager) -> Result<QueryResult> {
        let columns = vec![
            "table_catalog".to_string(),
            "table_schema".to_string(),
            "table_name".to_string(),
            "column_name".to_string(),
            "ordinal_position".to_string(),
            "column_default".to_string(),
            "is_nullable".to_string(),
            "data_type".to_string(),
        ];
        let mut rows = Vec::new();
        let mut schemas: Vec<_> = schema.all_schemas().collect();
        schemas.sort_by(|a, b| a.name.cmp(&b.name));
        for ts in schemas {
            for col in &ts.columns {
                rows.push(vec![
                    Value::Text("citadel".into()),
                    Value::Text("public".into()),
                    Value::Text(ts.name.clone().into()),
                    Value::Text(col.name.clone().into()),
                    Value::Integer(i64::from(col.position) + 1),
                    col.default_sql
                        .as_deref()
                        .map(|s| Value::Text(s.to_string().into()))
                        .unwrap_or(Value::Null),
                    Value::Text(if col.nullable {
                        "YES".into()
                    } else {
                        "NO".into()
                    }),
                    Value::Text(data_type_name(&col.data_type).into()),
                ]);
            }
        }
        Ok(QueryResult { columns, rows })
    }
}

pub struct InfoSchemaKeyColumnUsage;
impl VirtualTable for InfoSchemaKeyColumnUsage {
    fn name(&self) -> &str {
        "information_schema.key_column_usage"
    }
    fn scan(&self, _db: &Database, schema: &SchemaManager) -> Result<QueryResult> {
        let columns = vec![
            "constraint_catalog".to_string(),
            "constraint_schema".to_string(),
            "constraint_name".to_string(),
            "table_catalog".to_string(),
            "table_schema".to_string(),
            "table_name".to_string(),
            "column_name".to_string(),
            "ordinal_position".to_string(),
            "referenced_table_name".to_string(),
            "referenced_column_name".to_string(),
        ];
        let mut rows = Vec::new();
        let mut schemas: Vec<_> = schema.all_schemas().collect();
        schemas.sort_by(|a, b| a.name.cmp(&b.name));
        for ts in schemas {
            for (i, &col_pos) in ts.primary_key_columns.iter().enumerate() {
                let col = &ts.columns[col_pos as usize];
                rows.push(vec![
                    Value::Text("citadel".into()),
                    Value::Text("public".into()),
                    Value::Text(format!("{}_pkey", ts.name).into()),
                    Value::Text("citadel".into()),
                    Value::Text("public".into()),
                    Value::Text(ts.name.clone().into()),
                    Value::Text(col.name.clone().into()),
                    Value::Integer((i + 1) as i64),
                    Value::Null,
                    Value::Null,
                ]);
            }
            for fk in &ts.foreign_keys {
                let cname = fk
                    .name
                    .clone()
                    .unwrap_or_else(|| format!("{}_fkey", ts.name));
                for (i, col_pos) in fk.columns.iter().enumerate() {
                    let col = &ts.columns[*col_pos as usize];
                    let ref_col = fk.referred_columns.get(i).cloned().unwrap_or_default();
                    rows.push(vec![
                        Value::Text("citadel".into()),
                        Value::Text("public".into()),
                        Value::Text(cname.clone().into()),
                        Value::Text("citadel".into()),
                        Value::Text("public".into()),
                        Value::Text(ts.name.clone().into()),
                        Value::Text(col.name.clone().into()),
                        Value::Integer((i + 1) as i64),
                        Value::Text(fk.foreign_table.clone().into()),
                        Value::Text(ref_col.into()),
                    ]);
                }
            }
        }
        Ok(QueryResult { columns, rows })
    }
}

pub struct InfoSchemaTableConstraints;
impl VirtualTable for InfoSchemaTableConstraints {
    fn name(&self) -> &str {
        "information_schema.table_constraints"
    }
    fn scan(&self, _db: &Database, schema: &SchemaManager) -> Result<QueryResult> {
        let columns = vec![
            "constraint_catalog".to_string(),
            "constraint_schema".to_string(),
            "constraint_name".to_string(),
            "table_catalog".to_string(),
            "table_schema".to_string(),
            "table_name".to_string(),
            "constraint_type".to_string(),
        ];
        let mut rows = Vec::new();
        let mut schemas: Vec<_> = schema.all_schemas().collect();
        schemas.sort_by(|a, b| a.name.cmp(&b.name));
        for ts in schemas {
            if !ts.primary_key_columns.is_empty() {
                rows.push(constraint_row(
                    &format!("{}_pkey", ts.name),
                    &ts.name,
                    "PRIMARY KEY",
                ));
            }
            for fk in &ts.foreign_keys {
                let cname = fk
                    .name
                    .clone()
                    .unwrap_or_else(|| format!("{}_fkey", ts.name));
                rows.push(constraint_row(&cname, &ts.name, "FOREIGN KEY"));
            }
            for chk in &ts.check_constraints {
                let cname = chk
                    .name
                    .clone()
                    .unwrap_or_else(|| format!("{}_check", ts.name));
                rows.push(constraint_row(&cname, &ts.name, "CHECK"));
            }
            for col in &ts.columns {
                if col.check_expr.is_some() {
                    let cname = col
                        .check_name
                        .clone()
                        .unwrap_or_else(|| format!("{}_{}_check", ts.name, col.name));
                    rows.push(constraint_row(&cname, &ts.name, "CHECK"));
                }
            }
            for idx in &ts.indices {
                if idx.unique {
                    rows.push(constraint_row(&idx.name, &ts.name, "UNIQUE"));
                }
            }
        }
        Ok(QueryResult { columns, rows })
    }
}

fn constraint_row(name: &str, table: &str, kind: &str) -> Vec<Value> {
    vec![
        Value::Text("citadel".into()),
        Value::Text("public".into()),
        Value::Text(name.to_string().into()),
        Value::Text("citadel".into()),
        Value::Text("public".into()),
        Value::Text(table.to_string().into()),
        Value::Text(kind.to_string().into()),
    ]
}

fn data_type_name(dt: &DataType) -> &'static str {
    match dt {
        DataType::Integer => "INTEGER",
        DataType::Real => "REAL",
        DataType::Text => "TEXT",
        DataType::Blob => "BLOB",
        DataType::Boolean => "BOOLEAN",
        DataType::Date => "DATE",
        DataType::Time => "TIME",
        DataType::Timestamp => "TIMESTAMP",
        DataType::Interval => "INTERVAL",
        DataType::Json => "JSON",
        DataType::Jsonb => "JSONB",
        DataType::Null => "NULL",
        DataType::TsVector => "TSVECTOR",
        DataType::TsQuery => "TSQUERY",
        DataType::Array => "ARRAY",
    }
}

/// One row per event for multi-event triggers (per SQL spec).
pub struct InfoSchemaTriggers;
impl VirtualTable for InfoSchemaTriggers {
    fn name(&self) -> &str {
        "information_schema.triggers"
    }
    fn scan(&self, _db: &Database, schema: &SchemaManager) -> Result<QueryResult> {
        let columns = vec![
            "trigger_catalog".to_string(),
            "trigger_schema".to_string(),
            "trigger_name".to_string(),
            "event_manipulation".to_string(),
            "event_object_catalog".to_string(),
            "event_object_schema".to_string(),
            "event_object_table".to_string(),
            "action_order".to_string(),
            "action_condition".to_string(),
            "action_statement".to_string(),
            "action_orientation".to_string(),
            "action_timing".to_string(),
            "action_reference_old_table".to_string(),
            "action_reference_new_table".to_string(),
            "action_reference_old_row".to_string(),
            "action_reference_new_row".to_string(),
            "created".to_string(),
        ];
        let mut all: Vec<&crate::types::TriggerDef> = schema.all_triggers().collect();
        all.sort_by(|a, b| a.target.cmp(&b.target).then(a.name.cmp(&b.name)));
        let mut order_in_group: rustc_hash::FxHashMap<(String, String, String, String), i64> =
            rustc_hash::FxHashMap::default();
        let mut rows = Vec::new();
        for td in all {
            for ev in &td.events {
                let event_name = match ev {
                    crate::parser::TriggerEvent::Insert => "INSERT".to_string(),
                    crate::parser::TriggerEvent::Update(_) => "UPDATE".to_string(),
                    crate::parser::TriggerEvent::Delete => "DELETE".to_string(),
                };
                let timing_name = match td.timing {
                    crate::parser::TriggerTiming::Before => "BEFORE".to_string(),
                    crate::parser::TriggerTiming::After => "AFTER".to_string(),
                    crate::parser::TriggerTiming::InsteadOf => "INSTEAD OF".to_string(),
                };
                let orientation = match td.granularity {
                    crate::parser::TriggerGranularity::ForEachRow => "ROW".to_string(),
                    crate::parser::TriggerGranularity::ForEachStatement => "STATEMENT".to_string(),
                };
                let key = (
                    td.target.clone(),
                    event_name.clone(),
                    timing_name.clone(),
                    orientation.clone(),
                );
                let order = order_in_group.entry(key).or_insert(0);
                *order += 1;
                let order_val = *order;
                let action_condition = match &td.when_sql {
                    Some(s) => Value::Text(s.clone().into()),
                    None => Value::Null,
                };
                let old_table_alias = td
                    .referencing
                    .as_ref()
                    .and_then(|r| r.old_table_alias.clone());
                let new_table_alias = td
                    .referencing
                    .as_ref()
                    .and_then(|r| r.new_table_alias.clone());
                rows.push(vec![
                    Value::Text("citadel".into()),
                    Value::Text("public".into()),
                    Value::Text(td.name.clone().into()),
                    Value::Text(event_name.into()),
                    Value::Text("citadel".into()),
                    Value::Text("public".into()),
                    Value::Text(td.target.clone().into()),
                    Value::Integer(order_val),
                    action_condition,
                    Value::Text(td.body_sql.clone().into()),
                    Value::Text(orientation.into()),
                    Value::Text(timing_name.into()),
                    old_table_alias
                        .map(|s| Value::Text(s.into()))
                        .unwrap_or(Value::Null),
                    new_table_alias
                        .map(|s| Value::Text(s.into()))
                        .unwrap_or(Value::Null),
                    Value::Null,
                    Value::Null,
                    Value::Timestamp(td.created_at_micros),
                ]);
            }
        }
        Ok(QueryResult { columns, rows })
    }
}

/// Surfaces `enabled` status — PG hides this from `information_schema.triggers`.
pub struct CitadelTriggersStatus;
impl VirtualTable for CitadelTriggersStatus {
    fn name(&self) -> &str {
        "citadel_triggers_status"
    }
    fn scan(&self, _db: &Database, schema: &SchemaManager) -> Result<QueryResult> {
        let columns = vec![
            "trigger_name".to_string(),
            "table_name".to_string(),
            "enabled".to_string(),
        ];
        let mut all: Vec<&crate::types::TriggerDef> = schema.all_triggers().collect();
        all.sort_by(|a, b| a.target.cmp(&b.target).then(a.name.cmp(&b.name)));
        let rows = all
            .into_iter()
            .map(|td| {
                vec![
                    Value::Text(td.name.clone().into()),
                    Value::Text(td.target.clone().into()),
                    Value::Boolean(td.enabled),
                ]
            })
            .collect();
        Ok(QueryResult { columns, rows })
    }
}

/// `matviewowner` and `tablespace` are constants — citadel has no permission/storage concept.
pub struct PgMatviews;
impl VirtualTable for PgMatviews {
    fn name(&self) -> &str {
        "pg_matviews"
    }
    fn scan(&self, _db: &Database, schema: &SchemaManager) -> Result<QueryResult> {
        let columns = vec![
            "schemaname".to_string(),
            "matviewname".to_string(),
            "matviewowner".to_string(),
            "tablespace".to_string(),
            "hasindexes".to_string(),
            "ispopulated".to_string(),
            "definition".to_string(),
        ];
        let mut entries: Vec<&crate::types::MatviewDef> = schema.all_matviews().collect();
        entries.sort_by(|a, b| a.name.cmp(&b.name));
        let rows = entries
            .into_iter()
            .map(|mv| {
                let hasindexes = schema
                    .get(&mv.backing_table)
                    .map(|ts| !ts.indices.is_empty())
                    .unwrap_or(false);
                vec![
                    Value::Text("public".into()),
                    Value::Text(mv.name.clone().into()),
                    Value::Text("citadel".into()),
                    Value::Null,
                    Value::Boolean(hasindexes),
                    Value::Boolean(mv.with_data),
                    Value::Text(mv.select_sql.clone().into()),
                ]
            })
            .collect();
        Ok(QueryResult { columns, rows })
    }
}