osirisdb 0.7.0

A SQL database engine built from scratch in Rust featuring a custom parser, binder, query planner, optimizer, catalog, and storage engine.
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
use crate::lexer::spanned_token::Span;

/// Modifiers that can precede DDL statements such as `CREATE`.
///
/// These appear as part of `TokenKind::Modifier(Modifier)` and influence
/// the semantics of the DDL command that follows (e.g. `CREATE TEMPORARY TABLE`).
#[derive(Debug, Clone, PartialEq)]
pub enum Modifier {
    /// SQL `TEMPORARY` — the object exists only for the session.
    Temporary,
    /// Short form of `TEMPORARY`.
    Temp,
    /// PostgreSQL `UNLOGGED` — the table is not written to WAL.
    Unlogged,
    /// `GLOBAL` scope modifier (e.g. `GLOBAL TEMPORARY TABLE`).
    Global,
    /// `LOCAL` scope modifier (e.g. `LOCAL TEMPORARY TABLE`).
    Local,
    /// `MATERIALIZED` — used for materialized views.
    Materialized,
    /// `OR REPLACE` — replace an existing object if it exists.
    Replace,
}

/// The kind of a lexical token produced by the SQL [`Lexer`](crate::lexer::Lexer).
///
/// Variants are organised into logical groups: DML keywords, DDL keywords,
/// constraint keywords, logical operators, literals, punctuation, operators,
/// and error sentinels. Keyword variants are produced by
/// [`lookup_keyword`](crate::lexer::lookup_keyword()) when an identifier matches
/// a reserved word; otherwise `Ident` is returned.
#[derive(Debug, Clone, PartialEq)]
pub enum TokenKind {
    // ── DML (Data Manipulation Language) ──
    Select,
    From,
    Where,
    Insert,
    Into,
    Values,
    Update,
    Set,
    Delete,
    Returning,
    Merge,
    // ── DDL (Data Definition Language) ──
    Create,
    Database,
    Connection,
    Encoding,
    Locale,
    Table,
    Tables,
    Schema,
    Authorization,
    Drop,
    Use,
    Truncate,
    Alter,
    Reset,
    Restart,
    Identity,
    Continue,
    Index,
    View,
    Add,
    Attach,
    Column,
    Rename,
    To,
    Owner,
    Type,
    Statistics,
    Storage,
    Options,
    Data,
    Role,
    User,
    Login,
    NoLogin,
    Password,
    Superuser,
    NoSuperuser,
    Replication,
    NoReplication,
    Valid,
    Until,
    CreateDb,
    NoCreateDb,
    CreateRole,
    NoCreateRole,
    NoInherit,
    Extension,
    Version,
    // ── Constraints & Referential Integrity ──
    Constraint,
    Primary,
    Key,
    Foreign,
    References,
    Unique,
    Check,
    Not,
    Null,
    Default,
    Cascade,
    Restrict,
    Action,
    // ── Logical & Pattern-matching Operators ──
    And,
    Or,
    In,
    Out,
    Like,
    Ilike,
    Similar,
    Between,
    Is,
    Exists,
    Any,
    Some,
    Escape,
    // ── Boolean Literals ──
    True,
    False,
    // ── Grouping, Ordering & Pagination ──
    Group,
    Order,
    By,
    Having,
    Limit,
    Offset,
    Asc,
    Desc,
    Distinct,
    All,
    Nulls,
    First,
    Last,
    Fetch,
    Next,
    PercentKw,
    Tie,
    Ties,
    // ── Set Operations ──
    Union,
    Intersect,
    Except,
    // ── Transaction Control ──
    Begin,
    Commit,
    Rollback,
    Transaction,
    Savepoint,
    Release,
    // ── CTEs & Conditional Expressions ──
    With,
    Recursive,
    Case,
    When,
    Then,
    Else,
    End,
    Cast,
    If,
    // ── JOIN Clauses ──
    Join,
    On,
    As,
    Inner,
    Left,
    Right,
    Full,
    Cross,
    Outer,
    Natural,
    Using,
    Lateral,
    // ── Table Options & Advanced Clauses ──
    Inherits,
    Inherit,
    Partition,
    Range,
    List,
    Hash,
    Tablespace,
    Location,
    Collate,
    Generated,
    Always,
    Stored,
    AutoIncrement,
    Detach,
    Minvalue,
    Maxvalue,
    // ── ON COMMIT Options ──
    Preserve,
    Rows,
    // ── Utility Statements ──
    Explain,
    Analyze,
    Describe,
    /// `SHOW`
    Show,
    /// `COPY`
    Copy,
    /// `VACUUM`
    Vacuum,
    // ── Row-level Locking ──
    /// `FOR`
    For,
    /// `SHARE`
    Share,
    /// SQL `UPDATE` keyword in locking context (`FOR UPDATE`) — avoids collision with the DML `Update` variant.
    UpdateKw,
    /// `NO`
    No,
    /// `WAIT`
    Wait,
    /// `SKIP`
    Skip,
    /// `LOCKED`
    Locked,
    /// `ONLY`
    Only,
    // ── Window Functions ──
    /// `OVER`
    Over,
    Filter,
    Window,
    /// SQL `RANGE` keyword in window-frame context — avoids collision with the `Range` table-option variant.
    RangeKw,
    Preceding,
    Following,
    Current,
    Row,
    Unbounded,
    // ── UPSERT / ON CONFLICT ──
    Conflict,
    Do,
    Nothing,
    Excluded,
    // ── Sequence Management ──
    Sequence,
    Start,
    Increment,
    Cache,
    Cycle,
    Owned,
    // ── Data-type Contextual Keywords ──
    Varying,
    Precision,
    Zone,
    Time,
    Enum,
    Domain,
    Base,
    Subtype,
    Canonical,
    Preferred,
    // ── Triggers ──
    Trigger,
    Before,
    After,
    Instead,
    Of,
    InsteadOf, // two words — handle as Ident "INSTEAD" + consume "OF"
    Each,
    Execute,
    Procedure,
    Timeout,
    Idempotent,
    Retries,
    Control,
    Deferrable,
    Initially,
    Deferred,
    Immediate,
    Referencing,
    Old,
    New,
    Statement,
    Function,
    Returns,
    Language,
    Volatile,
    Stable,
    Immutable,
    Strict,
    Parallel,
    Safe,
    Unsafe,
    Restricted,
    Variadic,
    Inout,
    Setof,
    Raises,
    Access,
    Definer,
    Invoker,
    Cost,
    Called,
    Input,
    Security,
    Public,
    Private,
    Void,
    Int,
    Integer,
    /// PostgreSQL `INT2` — alias for `SMALLINT`.
    Int2,
    /// PostgreSQL `INT4` — alias for `INTEGER`.
    Int4,
    /// PostgreSQL `INT8` — alias for `BIGINT`.
    Int8,
    Bigint,
    Smallint,
    Boolean,
    /// Alias for `BOOLEAN`.
    Bool,
    Text,
    Varchar,
    Char,
    /// SQL `CHARACTER` — resolves to `CHAR` or `CHARACTER VARYING` (VARCHAR).
    Character,
    Real,
    Double,
    /// SQL `FLOAT` — single-precision floating point.
    Float,
    /// PostgreSQL `FLOAT4` — alias for `REAL`.
    Float4,
    /// PostgreSQL `FLOAT8` — alias for `DOUBLE PRECISION`.
    Float8,
    Numeric,
    Decimal,
    /// SQL `BINARY` — fixed-length binary string.
    Binary,
    /// SQL `VARBINARY` — variable-length binary string.
    VarBinary,
    Date,
    Timestamp,
    /// PostgreSQL `TIMESTAMPTZ` — timestamp with time zone.
    Timestamptz,
    Interval,
    Json,
    Jsonb,
    Uuid,
    Bytea,
    // ── Trigger Extensions ──
    Priority, // PRIORITY n
    Tags,     // TAGS ('tag1', ...)
    Enabled,  // ENABLED
    Disabled, // DISABLED
    // ── DDL Modifiers ──
    /// A DDL modifier keyword (e.g. `TEMPORARY`, `UNLOGGED`). See [`Modifier`].
    Modifier(Modifier),

    // ── Identifiers ──
    /// An unquoted identifier — the actual text is resolved via the token's [`Span`].
    Ident,
    /// A double-quoted or backtick-quoted identifier (e.g. `"my column"`).
    QuotedIdent,

    // ── Literals ──
    /// An integer literal, e.g. `42`.
    IntLit(i64),
    /// A floating-point literal, e.g. `3.14` or `1e10`.
    FloatLit(f64),
    /// A single-quoted string literal. Content is resolved via the token's [`Span`].
    StringLit,
    /// A bit-string literal, e.g. `B'1010'`.
    BitStringLit,
    /// A hex-string literal, e.g. `X'DEADBEEF'`.
    HexStringLit,
    /// A PostgreSQL `bytea` hex literal (e.g. `\x...`).
    ByteaLit,
    /// A PostgreSQL dollar-quoted string (e.g. `$$body$$`).
    DollarStringLit,
    /// A positional parameter placeholder, e.g. `$1`.
    Parameter(u32),

    // ── Comparison Operators ──
    /// `=`
    Eq,
    /// `!=` or `<>`
    Ne,
    /// `<`
    Lt,
    /// `<=`
    Le,
    /// `>`
    Gt,
    /// `>=`
    Ge,
    // ── Arithmetic Operators ──
    /// `+`
    Plus,
    /// `-`
    Minus,
    /// `*` — multiplication or wildcard select.
    Star,
    /// `/`
    Slash,
    /// `%` — modulo operator.
    Percent,
    // ── String / JSON / Cast Operators ──
    /// `||` — string concatenation.
    Concat,
    /// `->` — JSON field access (returns JSON).
    Arrow,
    /// `->>` — JSON field access (returns text).
    DoubleArrow,
    /// `::` — PostgreSQL type-cast operator.
    DoubleColon,
    /// `->` alias used in some contexts.
    MinusGt,
    // ── Bitwise Operators ──
    /// `|`
    Pipe,
    /// `^`
    Caret,
    /// `&`
    Ampersand,
    /// `~` — bitwise NOT or regex match.
    Tilde,
    /// `<<`
    ShiftLeft,
    /// `>>`
    ShiftRight,
    // ── Regex Operators (PostgreSQL) ──
    /// `~` — case-sensitive regex match.
    RegexMatch,
    /// `~*` — case-insensitive regex match.
    RegexIMatch,
    /// `!~` — negated case-sensitive regex match.
    RegexNotMatch,
    /// `!~*` — negated case-insensitive regex match.
    RegexNotIMatch,
    // ── JSONB Operators (PostgreSQL) ──
    /// `@`
    At,
    /// `@>` — JSONB contains.
    AtGt,
    /// `<@` — JSONB contained-by.
    LtAt,
    /// `@@` — full-text-search match.
    AtAt,
    /// `#` — JSONB path/delete.
    HashToken,
    /// `#>` — JSONB path access (returns JSON).
    HashArrow,
    /// `#>>` — JSONB path access (returns text).
    HashDoubleArrow,
    /// `#-` — JSONB key deletion.
    HashMinus,
    /// `?` — JSONB key existence.
    Question,
    /// `?|` — JSONB any-key existence.
    QuestionPipe,
    /// `?&` — JSONB all-key existence.
    QuestionAmp,
    // ── Punctuation ──
    /// `(`
    LParen,
    /// `)`
    RParen,
    /// `[`
    LBracket,
    /// `]`
    RBracket,
    /// `{`
    LBrace,
    /// `}`
    RBrace,
    /// `,`
    Comma,
    /// `;` — statement terminator.
    Semicolon,
    /// `.` — schema/table/column separator.
    Dot,

    // ── End of Input ──
    /// Signals that the entire input has been consumed.
    Eof,

    // ── Error Sentinels ──
    /// An illegal byte that cannot begin any valid token. Payload is the offending byte.
    Illegal(u8),
    /// A character that is valid ASCII but not expected in the current context.
    UnexpectedChar(u8),
    /// A string literal that was opened but never closed.
    UnterminatedString,
    /// A block comment (`/* ... */`) that was opened but never closed.
    UnterminatedComment,
}

/// A single lexical token produced by the [`Lexer`](crate::lexer::Lexer).
///
/// Pairs a [`TokenKind`] discriminant with a [`Span`] so downstream
/// consumers (parser, error reporter) can map every token back to its
/// exact source location.
#[derive(Debug, Clone, PartialEq)]
pub struct Token {
    /// The semantic category of this token.
    pub kind: TokenKind,
    /// Source location metadata (byte offsets, line, column).
    pub span: Span,
}