vibesql-executor 0.2.0

Query execution engine for vibesql SQL 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
use crate::errors::ExecutorError;

/// Evaluate an INSERT expression to SqlValue
/// Supports literals, DEFAULT keyword, procedural variables, and trigger pseudo-variables (OLD/NEW)
#[allow(dead_code)]
pub fn evaluate_insert_expression(
    expr: &vibesql_ast::Expression,
    column: &vibesql_catalog::ColumnSchema,
    procedural_context: Option<&crate::procedural::ExecutionContext>,
) -> Result<vibesql_types::SqlValue, ExecutorError> {
    evaluate_insert_expression_with_trigger_context(
        expr,
        column,
        procedural_context,
        None,
        None,
        None,
    )
}

/// Evaluate an INSERT expression with trigger context support
/// This is used when executing INSERT statements inside trigger bodies
///
/// `cte_results` carries the enclosing INSERT statement's WITH-clause CTEs
/// (if any) so subqueries in VALUES rows can reference CTE names, matching
/// SQLite (issue #5359).
pub fn evaluate_insert_expression_with_trigger_context(
    expr: &vibesql_ast::Expression,
    column: &vibesql_catalog::ColumnSchema,
    procedural_context: Option<&crate::procedural::ExecutionContext>,
    trigger_context: Option<&crate::trigger_execution::TriggerContext>,
    database: Option<&vibesql_storage::Database>,
    cte_results: Option<&std::collections::HashMap<String, crate::select::cte::CteResult>>,
) -> Result<vibesql_types::SqlValue, ExecutorError> {
    match expr {
        vibesql_ast::Expression::Literal(lit) => Ok(lit.clone()),
        vibesql_ast::Expression::Default => {
            // Use column's default value, or NULL if no default is defined
            if let Some(default_expr) = &column.default_value {
                // Evaluate the default expression
                evaluate_default_expression(default_expr)
            } else {
                // No default value defined, use NULL
                Ok(vibesql_types::SqlValue::Null)
            }
        }
        vibesql_ast::Expression::ColumnRef(col_id)
            if col_id.schema_canonical().is_none() && col_id.table_canonical().is_none() =>
        {
            let col_name = col_id.column_canonical();
            // Check if this is a procedural variable reference
            if let Some(ctx) = procedural_context {
                // Try to resolve as procedural variable
                if let Some(value) = ctx.get_value(col_name) {
                    return Ok(value.clone());
                }
            }
            // Not a procedural variable, or no context provided
            Err(ExecutorError::UnsupportedExpression(format!(
                "Column reference '{}' not supported in INSERT VALUES. Did you mean to use a procedural variable?",
                col_name
            )))
        }
        vibesql_ast::Expression::PseudoVariable { .. } => {
            // Pseudo-variables (OLD.x, NEW.y) require full expression evaluation with trigger
            // context
            if let (Some(ctx), Some(db)) = (trigger_context, database) {
                // Create a dummy row for evaluation (pseudo-variables don't depend on current row)
                let dummy_row = vibesql_storage::Row::new(vec![]);
                let evaluator =
                    crate::ExpressionEvaluator::with_trigger_context(ctx.table_schema, db, ctx);
                evaluator.eval(expr, &dummy_row)
            } else {
                Err(ExecutorError::UnsupportedExpression(
                    "Pseudo-variables (OLD/NEW) are only valid within trigger bodies".to_string(),
                ))
            }
        }
        _ => {
            // For any other expression type, use full expression evaluator
            if let (Some(ctx), Some(db)) = (trigger_context, database) {
                // Create a dummy row for evaluation (trigger context available)
                let dummy_row = vibesql_storage::Row::new(vec![]);
                let mut evaluator =
                    crate::ExpressionEvaluator::with_trigger_context(ctx.table_schema, db, ctx);
                if let Some(ctes) = cte_results {
                    evaluator = evaluator.with_cte_context(ctes);
                }
                evaluator.eval(expr, &dummy_row)
            } else if let Some(db) = database {
                // No trigger context, but we have database access - evaluate the expression
                // Create a minimal dummy schema for expression evaluation
                // INSERT value expressions don't reference columns from current row
                let dummy_schema =
                    vibesql_catalog::TableSchema::new("__insert_expr__".to_string(), vec![]);
                let dummy_row = vibesql_storage::Row::new(vec![]);
                let mut evaluator = crate::ExpressionEvaluator::with_database(&dummy_schema, db);
                if let Some(ctes) = cte_results {
                    evaluator = evaluator.with_cte_context(ctes);
                }
                evaluator.eval(expr, &dummy_row)
            } else {
                Err(ExecutorError::UnsupportedExpression(
                    "Complex expressions in INSERT VALUES require database context".to_string(),
                ))
            }
        }
    }
}

/// Evaluate a DEFAULT expression to get its value
/// Supports literals, special functions (CURRENT_DATE, CURRENT_USER, etc.), sequences,
/// and unary expressions (+val, -val) for numeric defaults
/// Note: For NextValue expressions, this function signature needs database access
/// This will require refactoring to pass db context
pub fn evaluate_default_expression(
    expr: &vibesql_ast::Expression,
) -> Result<vibesql_types::SqlValue, ExecutorError> {
    match expr {
        vibesql_ast::Expression::Literal(lit) => Ok(lit.clone()),
        // Support unary plus/minus for DEFAULT expressions like "default +4.32" or "default -5"
        vibesql_ast::Expression::UnaryOp { op, expr } => {
            let inner = evaluate_default_expression(expr)?;
            match op {
                vibesql_ast::UnaryOperator::Plus => Ok(inner), // +x = x
                vibesql_ast::UnaryOperator::Minus => {
                    // Negate the value
                    match inner {
                        vibesql_types::SqlValue::Integer(i) => {
                            Ok(vibesql_types::SqlValue::Integer(-i))
                        }
                        vibesql_types::SqlValue::Bigint(i) => {
                            Ok(vibesql_types::SqlValue::Bigint(-i))
                        }
                        vibesql_types::SqlValue::Smallint(i) => {
                            Ok(vibesql_types::SqlValue::Smallint(-i))
                        }
                        vibesql_types::SqlValue::Float(f) => Ok(vibesql_types::SqlValue::Float(-f)),
                        vibesql_types::SqlValue::Real(f) => Ok(vibesql_types::SqlValue::Real(-f)),
                        vibesql_types::SqlValue::Double(f) => {
                            Ok(vibesql_types::SqlValue::Double(-f))
                        }
                        vibesql_types::SqlValue::Numeric(f) => {
                            Ok(vibesql_types::SqlValue::Numeric(-f))
                        }
                        _ => Err(ExecutorError::UnsupportedExpression(format!(
                            "Cannot apply unary minus to {:?}",
                            inner
                        ))),
                    }
                }
                _ => Err(ExecutorError::UnsupportedExpression(format!(
                    "Unary operator {:?} not supported in DEFAULT expressions",
                    op
                ))),
            }
        }
        vibesql_ast::Expression::NextValue { sequence_name } => {
            // NEXT VALUE FOR sequence - this should have been handled at a higher level
            // with access to the database. If we get here, it's an error.
            Err(ExecutorError::UnsupportedExpression(format!(
                "Sequence '{}' requires database context - this should have been handled earlier",
                sequence_name
            )))
        }
        // SQLite compatibility: unquoted identifiers in DEFAULT clauses are treated as string literals
        // e.g., CREATE TABLE t(x TEXT DEFAULT hello) treats 'hello' as the string "hello"
        vibesql_ast::Expression::ColumnRef(col_id)
            if col_id.schema_canonical().is_none() && col_id.table_canonical().is_none() =>
        {
            // Convert the column name to a string literal
            let col_name = col_id.column_canonical();
            Ok(vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from(col_name)))
        }
        vibesql_ast::Expression::Function { name, .. } => {
            // Evaluate special SQL functions that can be used in DEFAULT
            match name.to_uppercase().as_str() {
                "CURRENT_DATE" => {
                    use chrono::Datelike;
                    let now = chrono::Local::now();
                    let date =
                        vibesql_types::Date::new(now.year(), now.month() as u8, now.day() as u8)
                            .map_err(|e| {
                                ExecutorError::UnsupportedFeature(format!(
                                    "Failed to create date: {}",
                                    e
                                ))
                            })?;
                    Ok(vibesql_types::SqlValue::Date(date))
                }
                "CURRENT_TIME" => {
                    use chrono::Timelike;
                    let now = chrono::Local::now();
                    let time_naive = now.time();
                    let time = vibesql_types::Time::new(
                        time_naive.hour() as u8,
                        time_naive.minute() as u8,
                        time_naive.second() as u8,
                        time_naive.nanosecond(),
                    )
                    .map_err(|e| {
                        ExecutorError::UnsupportedFeature(format!("Failed to create time: {}", e))
                    })?;
                    Ok(vibesql_types::SqlValue::Time(time))
                }
                "CURRENT_TIMESTAMP" => {
                    use chrono::{Datelike, Timelike};
                    let now = chrono::Local::now();
                    let time_naive = now.time();
                    let date =
                        vibesql_types::Date::new(now.year(), now.month() as u8, now.day() as u8)
                            .map_err(|e| {
                                ExecutorError::UnsupportedFeature(format!(
                                    "Failed to create date: {}",
                                    e
                                ))
                            })?;
                    let time = vibesql_types::Time::new(
                        time_naive.hour() as u8,
                        time_naive.minute() as u8,
                        time_naive.second() as u8,
                        time_naive.nanosecond(),
                    )
                    .map_err(|e| {
                        ExecutorError::UnsupportedFeature(format!("Failed to create time: {}", e))
                    })?;
                    Ok(vibesql_types::SqlValue::Timestamp(vibesql_types::Timestamp::new(
                        date, time,
                    )))
                }
                "CURRENT_USER" | "USER" | "SESSION_USER" => {
                    // Return current user (placeholder - would come from session context)
                    Ok(vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("public")))
                }
                "CURRENT_ROLE" => {
                    // Return current role (placeholder - would come from session context)
                    Ok(vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("public")))
                }
                _ => Err(ExecutorError::UnsupportedExpression(format!(
                    "Function '{}' not supported in DEFAULT expressions",
                    name
                ))),
            }
        }
        // The parser represents bare `DEFAULT CURRENT_DATE` /
        // `CURRENT_TIME` / `CURRENT_TIMESTAMP` as dedicated AST variants
        // (not `Function`), so evaluate them here with the same clock
        // semantics as the `Function`-named forms above. This is the
        // path the Raft freeze pass relies on to materialize a
        // deterministic value at propose time (#5381).
        vibesql_ast::Expression::CurrentDate => {
            use chrono::Datelike;
            let now = chrono::Local::now();
            let date = vibesql_types::Date::new(now.year(), now.month() as u8, now.day() as u8)
                .map_err(|e| {
                    ExecutorError::UnsupportedFeature(format!("Failed to create date: {}", e))
                })?;
            Ok(vibesql_types::SqlValue::Date(date))
        }
        vibesql_ast::Expression::CurrentTime { .. } => {
            use chrono::Timelike;
            let now = chrono::Local::now();
            let time_naive = now.time();
            let time = vibesql_types::Time::new(
                time_naive.hour() as u8,
                time_naive.minute() as u8,
                time_naive.second() as u8,
                time_naive.nanosecond(),
            )
            .map_err(|e| {
                ExecutorError::UnsupportedFeature(format!("Failed to create time: {}", e))
            })?;
            Ok(vibesql_types::SqlValue::Time(time))
        }
        vibesql_ast::Expression::CurrentTimestamp { .. } => {
            use chrono::{Datelike, Timelike};
            let now = chrono::Local::now();
            let time_naive = now.time();
            let date = vibesql_types::Date::new(now.year(), now.month() as u8, now.day() as u8)
                .map_err(|e| {
                    ExecutorError::UnsupportedFeature(format!("Failed to create date: {}", e))
                })?;
            let time = vibesql_types::Time::new(
                time_naive.hour() as u8,
                time_naive.minute() as u8,
                time_naive.second() as u8,
                time_naive.nanosecond(),
            )
            .map_err(|e| {
                ExecutorError::UnsupportedFeature(format!("Failed to create time: {}", e))
            })?;
            Ok(vibesql_types::SqlValue::Timestamp(vibesql_types::Timestamp::new(date, time)))
        }
        _ => Err(ExecutorError::UnsupportedExpression(
            "Only literals and functions are supported in DEFAULT expressions".to_string(),
        )),
    }
}

/// Apply DEFAULT values for unspecified columns
/// Now accepts database parameter to handle sequence NextValue expressions
///
/// Returns the first auto-generated sequence value (for LAST_INSERT_ROWID support),
/// or None if no sequence values were generated.
#[allow(dead_code)]
pub fn apply_default_values(
    schema: &vibesql_catalog::TableSchema,
    row_values: &mut [vibesql_types::SqlValue],
    database: &mut vibesql_storage::Database,
) -> Result<Option<i64>, ExecutorError> {
    apply_default_values_with_table_name(schema, row_values, database, &schema.name)
}

/// Apply DEFAULT values for unspecified columns, with explicit table name for storage lookup
///
/// This variant is used when the storage table name differs from the schema name
/// (e.g., for schema-qualified tables).
///
/// Returns the first auto-generated sequence value (for LAST_INSERT_ROWID support),
/// or None if no sequence values were generated.
pub fn apply_default_values_with_table_name(
    schema: &vibesql_catalog::TableSchema,
    row_values: &mut [vibesql_types::SqlValue],
    database: &mut vibesql_storage::Database,
    storage_table_name: &str,
) -> Result<Option<i64>, ExecutorError> {
    apply_default_values_with_batch_context(schema, row_values, database, storage_table_name, None)
}

/// Apply DEFAULT values for unspecified columns, with batch context for multi-row inserts
///
/// The `batch_max_ipk` parameter is used to track the maximum INTEGER PRIMARY KEY value
/// already assigned within the current batch of inserts. This prevents duplicate values
/// when multiple rows in the same INSERT have NULL for the INTEGER PRIMARY KEY column.
///
/// Returns the first auto-generated sequence value (for LAST_INSERT_ROWID support),
/// or None if no sequence values were generated.
pub fn apply_default_values_with_batch_context(
    schema: &vibesql_catalog::TableSchema,
    row_values: &mut [vibesql_types::SqlValue],
    database: &mut vibesql_storage::Database,
    storage_table_name: &str,
    batch_max_ipk: Option<i64>,
) -> Result<Option<i64>, ExecutorError> {
    let mut first_generated_id: Option<i64> = None;

    // Handle INTEGER PRIMARY KEY NULL auto-generation (SQLite semantics)
    // This must happen before regular default value processing
    if let Some(ipk_idx) = schema.get_integer_primary_key_index() {
        if row_values[ipk_idx] == vibesql_types::SqlValue::Null {
            // Auto-generate: max(existing_pk, batch_max) + 1, or 1 if table is empty
            let table_max = compute_next_integer_pk_value(database, storage_table_name, ipk_idx)?;
            // The next value should be max of (table_max, batch_max_ipk + 1)
            let next_val = match batch_max_ipk {
                Some(batch_max) => table_max.max(batch_max + 1),
                None => table_max,
            };
            row_values[ipk_idx] = vibesql_types::SqlValue::Integer(next_val);

            // Track for LAST_INSERT_ROWID
            if first_generated_id.is_none() {
                first_generated_id = Some(next_val);
            }
        }
    }

    for (col_idx, col) in schema.columns.iter().enumerate() {
        // If column is NULL and has a default value, apply it
        if row_values[col_idx] == vibesql_types::SqlValue::Null {
            if let Some(default_expr) = &col.default_value {
                // Handle NextValue expressions specially
                let default_value = match default_expr {
                    vibesql_ast::Expression::NextValue { sequence_name } => {
                        // Get the next value from the sequence
                        let seq =
                            database.catalog.get_sequence_mut(sequence_name).map_err(|e| {
                                ExecutorError::UnsupportedExpression(format!(
                                    "Sequence error: {:?}",
                                    e
                                ))
                            })?;
                        let next_val = seq.next_value().map_err(|e| {
                            ExecutorError::ConstraintViolation(format!("Sequence error: {}", e))
                        })?;

                        // Track the first generated ID for LAST_INSERT_ROWID
                        if first_generated_id.is_none() {
                            first_generated_id = Some(next_val);
                        }

                        vibesql_types::SqlValue::Integer(next_val)
                    }
                    _ => evaluate_default_expression(default_expr)?,
                };
                let coerced_value = super::validation::coerce_value(default_value, &col.data_type)?;
                row_values[col_idx] = coerced_value;
            }
        }
    }
    Ok(first_generated_id)
}

/// Compute the next INTEGER PRIMARY KEY value for auto-generation
///
/// Returns max(existing_pk_values) + 1, or 1 if the table is empty.
/// This implements SQLite's behavior where inserting NULL into an INTEGER PRIMARY KEY
/// column auto-generates the next available value.
fn compute_next_integer_pk_value(
    database: &vibesql_storage::Database,
    table_name: &str,
    pk_col_idx: usize,
) -> Result<i64, ExecutorError> {
    let table = match database.get_table(table_name) {
        Some(t) => t,
        None => {
            // Table doesn't exist in storage yet, start at 1
            return Ok(1);
        }
    };

    // Find the maximum value in the PRIMARY KEY column
    let mut max_val: i64 = 0;

    for row in table.scan() {
        if let Some(value) = row.get(pk_col_idx) {
            let int_val = match value {
                vibesql_types::SqlValue::Integer(i) => *i,
                vibesql_types::SqlValue::Bigint(i) => *i,
                vibesql_types::SqlValue::Null => continue, // Skip NULL values
                _ => continue, // Skip non-integer values (shouldn't happen)
            };
            if int_val > max_val {
                max_val = int_val;
            }
        }
    }

    // Return max + 1 (or 1 if table was empty, since max_val starts at 0)
    Ok(max_val + 1)
}

/// Apply generated/computed column values
/// Generated columns are defined with AS(expression) syntax and computed on INSERT/UPDATE
pub fn apply_generated_columns(
    schema: &vibesql_catalog::TableSchema,
    row_values: &mut [vibesql_types::SqlValue],
    _database: &vibesql_storage::Database,
) -> Result<(), ExecutorError> {
    // Create a temporary row to evaluate generated expressions.
    // GeneratedColumn context: non-deterministic date/time uses (e.g.
    // `z AS (date())`) are rejected at evaluation time (SQLite, date2-140).
    let temp_row = vibesql_storage::Row::new(row_values.to_vec());
    let evaluator = crate::ExpressionEvaluator::new(schema)
        .with_schema_context(crate::evaluator::SchemaExprContext::GeneratedColumn);

    for (col_idx, col) in schema.columns.iter().enumerate() {
        // If column has a generated expression, compute and apply it
        if let Some(generated_expr) = &col.generated_expr {
            let generated_value = evaluator.eval(generated_expr, &temp_row)?;
            let coerced_value = super::validation::coerce_value(generated_value, &col.data_type)?;
            row_values[col_idx] = coerced_value;
        }
    }
    Ok(())
}