iridium_core 0.1.7

SQL Server-compatible Rust engine core for Iridium SQL
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
use crate::ast::{
    AlterTableAction, AlterTableStmt, CreateIndexStmt, CreateSchemaStmt, CreateTableStmt,
    CreateTypeStmt, DropIndexStmt, DropSchemaStmt, DropTableStmt, DropTypeStmt,
    TableConstraintSpec,
};
use crate::catalog::{
    Catalog, CheckConstraintDef, ColumnDef, ForeignKeyDef, IdentityDef, TableDef, TableTypeDef,
};
use crate::error::DbError;
use crate::storage::Storage;

use super::tooling::format_view_definition;
use super::tooling::SessionOptions;
use super::type_mapping::data_type_spec_to_runtime;

/// S6: Shared constraint application logic extracted from create_table and alter_table.
/// Eliminates ~65 lines of duplicated constraint handling.
fn apply_table_constraint(table: &mut TableDef, tc: TableConstraintSpec) -> Result<(), DbError> {
    match tc {
        TableConstraintSpec::Default { name, column, expr } => {
            let col = table
                .columns
                .iter_mut()
                .find(|c| c.name.eq_ignore_ascii_case(&column))
                .ok_or_else(|| DbError::column_not_found(&column))?;
            col.default = Some(expr);
            col.default_constraint_name = Some(name);
        }
        TableConstraintSpec::Check { name, expr } => {
            table
                .check_constraints
                .push(CheckConstraintDef { name, expr });
        }
        TableConstraintSpec::ForeignKey {
            name,
            columns,
            referenced_table,
            referenced_columns,
            on_delete,
            on_update,
        } => {
            table.foreign_keys.push(ForeignKeyDef {
                name,
                columns,
                referenced_table,
                referenced_columns,
                on_delete: on_delete.unwrap_or(crate::ast::ReferentialAction::NoAction),
                on_update: on_update.unwrap_or(crate::ast::ReferentialAction::NoAction),
            });
        }
        TableConstraintSpec::PrimaryKey { name: _, columns } => {
            for col_name in &columns {
                let col = table
                    .columns
                    .iter_mut()
                    .find(|c| c.name.eq_ignore_ascii_case(col_name))
                    .ok_or_else(|| DbError::column_not_found(col_name))?;
                col.primary_key = true;
                col.nullable = false;
            }
            if columns.len() == 1 {
                if let Some(col) = table
                    .columns
                    .iter_mut()
                    .find(|c| c.name.eq_ignore_ascii_case(&columns[0]))
                {
                    col.unique = true;
                }
            }
        }
        TableConstraintSpec::Unique { name: _, columns } => {
            for col_name in &columns {
                let col = table
                    .columns
                    .iter_mut()
                    .find(|c| c.name.eq_ignore_ascii_case(col_name))
                    .ok_or_else(|| DbError::column_not_found(col_name))?;
                col.unique = true;
            }
        }
    }
    Ok(())
}

pub(crate) struct SchemaExecutor<'a> {
    pub(crate) catalog: &'a mut dyn Catalog,
    pub(crate) storage: &'a mut dyn Storage,
    pub(crate) session_options: &'a SessionOptions,
}

impl<'a> SchemaExecutor<'a> {
    pub(crate) fn create_type(&mut self, stmt: CreateTypeStmt) -> Result<(), DbError> {
        let schema = stmt.name.schema_or_dbo().to_string();
        if self.catalog.get_schema_id(&schema).is_none() {
            return Err(DbError::schema_not_found(schema));
        }
        let object_id = self.catalog.alloc_object_id();
        self.catalog.create_table_type(TableTypeDef {
            object_id,
            schema,
            name: stmt.name.name,
            columns: stmt.columns,
            table_constraints: stmt.table_constraints,
        })
    }

    pub(crate) fn drop_type(&mut self, stmt: DropTypeStmt) -> Result<(), DbError> {
        let schema = stmt.name.schema_or_dbo().to_string();
        self.catalog.drop_table_type(&schema, &stmt.name.name)
    }

    pub(crate) fn create_table(&mut self, stmt: CreateTableStmt) -> Result<(), DbError> {
        let schema_name = stmt.name.schema_or_dbo().to_string();
        let schema_id = self
            .catalog
            .get_schema_id(&schema_name)
            .ok_or_else(|| DbError::schema_not_found(schema_name.clone()))?;

        if self
            .catalog
            .find_table(&schema_name, &stmt.name.name)
            .is_some()
        {
            return Err(DbError::duplicate_table(&schema_name, &stmt.name.name));
        }

        let table_id = self.catalog.alloc_table_id();

        let mut column_fks: Vec<(String, crate::ast::ForeignKeyRef)> = Vec::new();
        for spec in &stmt.columns {
            if let Some(fk) = &spec.foreign_key {
                column_fks.push((spec.name.clone(), fk.clone()));
            }
        }

        let columns = stmt
            .columns
            .into_iter()
            .map(|spec| self.build_column_def(spec))
            .collect::<Result<Vec<_>, _>>()?;

        let mut table = TableDef {
            id: table_id,
            schema_id,
            schema_name: schema_name.clone(),
            name: stmt.name.name,
            columns,
            check_constraints: vec![],
            foreign_keys: vec![],
        };

        for (col_name, fk_ref) in column_fks {
            table.foreign_keys.push(ForeignKeyDef {
                name: format!("FK__{}__{}", table.name, col_name),
                columns: vec![col_name],
                referenced_table: fk_ref.referenced_table,
                referenced_columns: fk_ref.referenced_columns,
                on_delete: fk_ref
                    .on_delete
                    .unwrap_or(crate::ast::ReferentialAction::NoAction),
                on_update: fk_ref
                    .on_update
                    .unwrap_or(crate::ast::ReferentialAction::NoAction),
            });
        }

        for tc in stmt.table_constraints {
            apply_table_constraint(&mut table, tc)?;
        }

        self.catalog.register_table(table.clone());
        self.storage.ensure_table(table_id)?;

        // Create clustered indexes for PRIMARY KEYs
        for col in &table.columns {
            if col.primary_key {
                let index_name = format!("PK__{}__{}", table.name, col.name);
                self.catalog
                    .create_index_with_options(
                        "dbo",
                        &index_name,
                        &table.schema_name,
                        &table.name,
                        std::slice::from_ref(&col.name),
                        true,       // is_clustered
                        col.unique, // is_unique
                    )
                    .map_err(|e| {
                        DbError::Execution(format!("Failed to create primary key index: {}", e))
                    })?;
            }
        }

        Ok(())
    }

    pub(crate) fn drop_table(&mut self, stmt: DropTableStmt) -> Result<(), DbError> {
        let schema_name = stmt.name.schema_or_dbo().to_string();
        let table_id = self.catalog.drop_table(&schema_name, &stmt.name.name)?;
        self.storage.remove_table(table_id)?;
        Ok(())
    }

    pub(crate) fn create_schema(&mut self, stmt: CreateSchemaStmt) -> Result<(), DbError> {
        self.catalog.create_schema(&stmt.name)
    }

    pub(crate) fn drop_schema(&mut self, stmt: DropSchemaStmt) -> Result<(), DbError> {
        self.catalog.drop_schema(&stmt.name)
    }

    pub(crate) fn create_view(&mut self, stmt: crate::ast::CreateViewStmt) -> Result<(), DbError> {
        let schema = stmt.name.schema_or_dbo().to_string();
        let view_name = stmt.name.name;
        let query = stmt.query;
        let schema_id = self.catalog.get_schema_id(&schema).unwrap_or(1);
        let mut view = crate::catalog::ViewDef {
            object_id: self.catalog.alloc_object_id(),
            schema,
            name: view_name,
            schema_id,
            query: crate::ast::Statement::Dml(crate::ast::DmlStatement::Select(query)),
            definition_sql: String::new(),
        };
        view.definition_sql = format_view_definition(&view);
        self.catalog.create_view(view)
    }

    pub(crate) fn drop_view(&mut self, stmt: crate::ast::DropViewStmt) -> Result<(), DbError> {
        let schema = stmt.name.schema_or_dbo();
        self.catalog.drop_view(schema, &stmt.name.name)
    }

    pub(crate) fn create_synonym(
        &mut self,
        stmt: crate::ast::CreateSynonymStmt,
    ) -> Result<(), DbError> {
        let schema = stmt.name.schema_or_dbo().to_string();
        let object_id = self.catalog.alloc_object_id();
        self.catalog.create_synonym(crate::catalog::SynonymDef {
            object_id,
            schema,
            name: stmt.name.name,
            base_object: stmt.base_object,
        })
    }

    pub(crate) fn drop_synonym(
        &mut self,
        stmt: crate::ast::DropSynonymStmt,
    ) -> Result<(), DbError> {
        let schema = stmt.name.schema_or_dbo();
        self.catalog.drop_synonym(schema, &stmt.name.name)
    }

    pub(crate) fn create_sequence(
        &mut self,
        stmt: crate::ast::CreateSequenceStmt,
    ) -> Result<(), DbError> {
        let schema = stmt.name.schema_or_dbo().to_string();
        let data_type = data_type_spec_to_runtime(&stmt.data_type);
        let object_id = self.catalog.alloc_object_id();
        self.catalog.create_sequence(crate::catalog::SequenceDef {
            object_id,
            schema,
            name: stmt.name.name,
            data_type,
            start_value: stmt.start_value,
            increment: stmt.increment,
            current_value: stmt.start_value,
            minimum_value: stmt.minimum_value,
            maximum_value: stmt.maximum_value,
            is_cycling: stmt.is_cycling,
        })
    }

    pub(crate) fn drop_sequence(
        &mut self,
        stmt: crate::ast::DropSequenceStmt,
    ) -> Result<(), DbError> {
        let schema = stmt.name.schema_or_dbo();
        self.catalog.drop_sequence(schema, &stmt.name.name)
    }

    fn build_column_def(&mut self, spec: crate::ast::ColumnSpec) -> Result<ColumnDef, DbError> {
        let data_type = data_type_spec_to_runtime(&spec.data_type);
        let nullable = if spec.nullable_explicit {
            spec.nullable
        } else {
            self.session_options.ansi_null_dflt_on
        };
        Ok(ColumnDef {
            id: self.catalog.alloc_column_id(),
            name: spec.name,
            data_type,
            nullable,
            primary_key: spec.primary_key,
            unique: spec.unique || spec.primary_key,
            identity: spec.identity.map(|(seed, inc)| IdentityDef::new(seed, inc)),
            default: spec.default,
            default_constraint_name: spec.default_constraint_name,
            check: spec.check,
            check_constraint_name: spec.check_constraint_name,
            computed_expr: spec.computed_expr,
            ansi_padding_on: self.session_options.ansi_padding,
        })
    }

    pub(crate) fn create_index(&mut self, stmt: CreateIndexStmt) -> Result<(), DbError> {
        let index_schema = stmt.name.schema_or_dbo().to_string();
        let table_schema = stmt.table.schema_or_dbo().to_string();

        self.catalog.create_index(
            &index_schema,
            &stmt.name.name,
            &table_schema,
            &stmt.table.name,
            &stmt.columns,
        )?;

        let index_id = self
            .catalog
            .get_indexes()
            .iter()
            .find(|idx| idx.name.eq_ignore_ascii_case(&stmt.name.name))
            .map(|idx| idx.id);

        if let (Some(index_def_id), Some(table)) = (
            index_id,
            self.catalog.find_table(&table_schema, &stmt.table.name),
        ) {
            let rows = self.storage.get_rows(table.id)?;
            let row_refs: Vec<(usize, &[crate::types::Value])> = rows
                .iter()
                .enumerate()
                .map(|(i, r)| (i, r.values.as_slice()))
                .collect();

            if let Some(index_storage) = self.storage.as_index_storage_mut() {
                index_storage.register_index(
                    index_def_id,
                    self.catalog
                        .get_indexes()
                        .iter()
                        .find(|idx| idx.id == index_def_id)
                        .map(|idx| idx.column_ids.clone())
                        .unwrap_or_default(),
                    self.catalog
                        .get_indexes()
                        .iter()
                        .find(|idx| idx.id == index_def_id)
                        .map(|idx| idx.is_unique)
                        .unwrap_or(false),
                    self.catalog
                        .get_indexes()
                        .iter()
                        .find(|idx| idx.id == index_def_id)
                        .map(|idx| idx.is_clustered)
                        .unwrap_or(false),
                );

                if let Some(idx) = index_storage.get_index_mut(index_def_id) {
                    let _ = idx.rebuild_from_rows(row_refs.as_slice());
                }
            }
        }

        Ok(())
    }

    pub(crate) fn drop_index(&mut self, stmt: DropIndexStmt) -> Result<(), DbError> {
        let index_schema = stmt.name.schema_or_dbo().to_string();
        let table_schema = stmt.table.schema_or_dbo().to_string();
        self.catalog.drop_index(
            &index_schema,
            &stmt.name.name,
            &table_schema,
            &stmt.table.name,
        )
    }

    pub(crate) fn alter_table(&mut self, stmt: AlterTableStmt) -> Result<(), DbError> {
        let schema_name = stmt.table.schema_or_dbo().to_string();
        let table_id = self
            .catalog
            .find_table(&schema_name, &stmt.table.name)
            .map(|t| t.id)
            .ok_or_else(|| DbError::table_not_found(&schema_name, &stmt.table.name))?;

        match stmt.action {
            AlterTableAction::AddColumn(col_spec) => {
                let col = self.build_column_def(col_spec)?;
                let table_mut = self
                    .catalog
                    .find_table_mut(&schema_name, &stmt.table.name)
                    .ok_or_else(|| DbError::table_not_found(&schema_name, &stmt.table.name))?;
                table_mut.columns.push(col);

                // Add NULL values for the new column in existing rows
                let mut rows_vec = {
                    let rows = match self.storage.scan_rows(table_id) {
                        Ok(rows) => rows,
                        Err(_) => return Ok(()),
                    };
                    match rows.collect::<Result<Vec<_>, DbError>>() {
                        Ok(rows_vec) => rows_vec,
                        Err(_) => return Ok(()),
                    }
                };
                for row in rows_vec.iter_mut() {
                    row.values.push(crate::types::Value::Null);
                }
                self.storage.replace_table(table_id, rows_vec)?;
            }
            AlterTableAction::DropColumn(col_name) => {
                let table_mut = self
                    .catalog
                    .find_table_mut(&schema_name, &stmt.table.name)
                    .ok_or_else(|| DbError::table_not_found(&schema_name, &stmt.table.name))?;
                let col_idx = table_mut
                    .columns
                    .iter()
                    .position(|c| c.name.eq_ignore_ascii_case(&col_name))
                    .ok_or_else(|| DbError::column_not_found(&col_name))?;
                table_mut.columns.remove(col_idx);

                // Remove the column values from existing rows
                let mut rows_vec = {
                    let rows = match self.storage.scan_rows(table_id) {
                        Ok(rows) => rows,
                        Err(_) => return Ok(()),
                    };
                    match rows.collect::<Result<Vec<_>, DbError>>() {
                        Ok(rows_vec) => rows_vec,
                        Err(_) => return Ok(()),
                    }
                };
                for row in rows_vec.iter_mut() {
                    if col_idx < row.values.len() {
                        row.values.remove(col_idx);
                    }
                }
                self.storage.replace_table(table_id, rows_vec)?;
            }
            AlterTableAction::AlterColumn {
                name,
                data_type,
                nullable,
            } => {
                let table_mut = self
                    .catalog
                    .find_table_mut(&schema_name, &stmt.table.name)
                    .ok_or_else(|| DbError::table_not_found(&schema_name, &stmt.table.name))?;
                let col = table_mut
                    .columns
                    .iter_mut()
                    .find(|c| c.name.eq_ignore_ascii_case(&name))
                    .ok_or_else(|| DbError::column_not_found(&name))?;
                col.data_type = data_type_spec_to_runtime(&data_type);
                if let Some(n) = nullable {
                    col.nullable = n;
                }
            }
            AlterTableAction::AddConstraint(constraint) => {
                let table_mut = self
                    .catalog
                    .find_table_mut(&schema_name, &stmt.table.name)
                    .ok_or_else(|| DbError::table_not_found(&schema_name, &stmt.table.name))?;
                apply_table_constraint(table_mut, constraint)?;
            }
            AlterTableAction::DropConstraint(constraint_name) => {
                let table_mut = self
                    .catalog
                    .find_table_mut(&schema_name, &stmt.table.name)
                    .ok_or_else(|| DbError::table_not_found(&schema_name, &stmt.table.name))?;

                // Try to remove from check constraints
                let removed = table_mut
                    .check_constraints
                    .iter()
                    .position(|c| c.name.eq_ignore_ascii_case(&constraint_name));
                if let Some(pos) = removed {
                    table_mut.check_constraints.remove(pos);
                } else {
                    // Try to remove from foreign keys
                    let removed = table_mut
                        .foreign_keys
                        .iter()
                        .position(|fk| fk.name.eq_ignore_ascii_case(&constraint_name));
                    if let Some(pos) = removed {
                        table_mut.foreign_keys.remove(pos);
                    } else {
                        return Err(DbError::constraint_not_found(
                            &stmt.table.name,
                            constraint_name,
                        ));
                    }
                }
            }
        }

        Ok(())
    }
}