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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! Expression rewriting and traversal
//!
//! This module provides recursive expression rewriting to apply IN subquery
//! optimizations throughout the entire query AST.

use vibesql_ast::{Expression, SelectItem, SelectStmt};

use super::{
    correlation::is_correlated,
    transformations::{add_distinct_to_in_subquery, rewrite_exists_to_in, rewrite_in_to_exists},
};

/// Check whether an expression contains any unqualified column references.
///
/// Used to decide whether the IN → EXISTS rewrite can safely qualify the
/// left-hand expression: with multiple outer tables, unqualified refs cannot
/// be attributed to the right table without schema information.
fn has_unqualified_column_refs(expr: &Expression) -> bool {
    match expr {
        Expression::ColumnRef(col_id) => col_id.table_canonical().is_none(),
        Expression::BinaryOp { left, right, .. } => {
            has_unqualified_column_refs(left) || has_unqualified_column_refs(right)
        }
        Expression::UnaryOp { expr: inner, .. }
        | Expression::IsNull { expr: inner, .. }
        | Expression::Cast { expr: inner, .. }
        | Expression::Collate { expr: inner, .. } => has_unqualified_column_refs(inner),
        Expression::Between { expr: inner, low, high, .. } => {
            has_unqualified_column_refs(inner)
                || has_unqualified_column_refs(low)
                || has_unqualified_column_refs(high)
        }
        Expression::Function { args, .. } | Expression::AggregateFunction { args, .. } => {
            args.iter().any(has_unqualified_column_refs)
        }
        Expression::RowValueConstructor(children) => {
            children.iter().any(has_unqualified_column_refs)
        }
        Expression::InList { expr: inner, values, .. } => {
            has_unqualified_column_refs(inner) || values.iter().any(has_unqualified_column_refs)
        }
        Expression::Case { operand, when_clauses, else_result } => {
            operand.as_ref().is_some_and(|e| has_unqualified_column_refs(e))
                || when_clauses.iter().any(|clause| {
                    clause.conditions.iter().any(has_unqualified_column_refs)
                        || has_unqualified_column_refs(&clause.result)
                })
                || else_result.as_ref().is_some_and(|e| has_unqualified_column_refs(e))
        }
        // Subqueries resolve their own scopes; literals and other leaf
        // expressions contain no column refs. Conservatively report `true`
        // only for the traversed forms above; qualify_outer_column_refs leaves
        // other forms unchanged anyway.
        _ => false,
    }
}

/// Rewrite an expression to optimize IN subqueries
///
/// This function recursively traverses the expression tree and applies
/// the appropriate optimization for each IN subquery encountered.
pub(super) fn rewrite_expression(
    expr: &Expression,
    rewrite_subquery_fn: &impl Fn(&SelectStmt) -> SelectStmt,
) -> Expression {
    rewrite_expression_with_context(expr, rewrite_subquery_fn, &[])
}

/// Rewrite expression with outer table context for EXISTS decorrelation
pub(super) fn rewrite_expression_with_context(
    expr: &Expression,
    rewrite_subquery_fn: &impl Fn(&SelectStmt) -> SelectStmt,
    outer_tables: &[String],
) -> Expression {
    match expr {
        // Optimize IN subquery
        Expression::In { expr: in_expr, subquery, negated } => {
            // Validate that this is a single-column IN subquery
            // Multi-column IN requires tuple comparison which we don't optimize
            if subquery.select_list.len() != 1 {
                // Multi-column IN: skip optimization
                return Expression::In {
                    expr: Box::new(rewrite_expression_with_context(
                        in_expr,
                        rewrite_subquery_fn,
                        outer_tables,
                    )),
                    subquery: Box::new(rewrite_subquery_fn(subquery)),
                    negated: *negated,
                };
            }

            // Check if subquery SELECT expression is a simple column reference
            // Complex expressions (e.g., UPPER(col)) can't be safely used in correlation predicates
            let is_simple_column = matches!(
                subquery.select_list.first(),
                Some(SelectItem::Expression { expr: Expression::ColumnRef(_), .. })
            );

            // The IN → EXISTS rewrite moves the left-hand expression into the
            // EXISTS subquery's WHERE clause, so unqualified column refs in it
            // must be qualified with the outer table they belong to (issue
            // #4880). rewrite_in_to_exists qualifies them with the FIRST outer
            // table, which is only correct when there is exactly one outer
            // table. With multiple outer tables (e.g. `SELECT ... FROM t1, t2
            // WHERE x IN (...)` where x belongs to t2), blind qualification
            // fabricates a non-existent column like t1.x. Without schema
            // information we cannot pick the right table, so skip the rewrite
            // and let row-by-row IN evaluation handle correlation (fix for
            // select1-18.1). Note: an aliased single table contributes two
            // entries (alias + name) and is treated conservatively; the
            // DISTINCT fallback below remains correct, just less optimized.
            let can_qualify_lhs = outer_tables.len() <= 1 || !has_unqualified_column_refs(in_expr);

            // Check if subquery is correlated
            if is_correlated(subquery) && is_simple_column && can_qualify_lhs {
                // Correlated subquery with simple column: Rewrite IN → EXISTS
                // This allows database to stop after first match and better leverage indexes
                rewrite_in_to_exists(in_expr, subquery, *negated, outer_tables)
            } else if is_correlated(subquery) {
                // Correlated subquery with complex expression: skip IN → EXISTS
                // Complex expressions can't be safely used in correlation predicates
                // Fall back to DISTINCT optimization only
                let optimized_subquery = add_distinct_to_in_subquery(subquery);
                let optimized_subquery = rewrite_subquery_fn(&optimized_subquery);
                Expression::In {
                    expr: Box::new(rewrite_expression_with_context(
                        in_expr,
                        rewrite_subquery_fn,
                        outer_tables,
                    )),
                    subquery: Box::new(optimized_subquery),
                    negated: *negated,
                }
            } else {
                // Uncorrelated subquery: Add DISTINCT to reduce duplicate processing
                let optimized_subquery = add_distinct_to_in_subquery(subquery);
                let optimized_subquery = rewrite_subquery_fn(&optimized_subquery);
                Expression::In {
                    expr: Box::new(rewrite_expression_with_context(
                        in_expr,
                        rewrite_subquery_fn,
                        outer_tables,
                    )),
                    subquery: Box::new(optimized_subquery),
                    negated: *negated,
                }
            }
        }

        // Recursively rewrite nested expressions (preserve outer_tables context)
        Expression::BinaryOp { op, left, right } => Expression::BinaryOp {
            op: *op,
            left: Box::new(rewrite_expression_with_context(
                left,
                rewrite_subquery_fn,
                outer_tables,
            )),
            right: Box::new(rewrite_expression_with_context(
                right,
                rewrite_subquery_fn,
                outer_tables,
            )),
        },

        Expression::UnaryOp { op, expr } => Expression::UnaryOp {
            op: *op,
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
        },

        Expression::IsNull { expr, negated } => Expression::IsNull {
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
            negated: *negated,
        },

        Expression::IsDistinctFrom { left, right, negated } => Expression::IsDistinctFrom {
            left: Box::new(rewrite_expression_with_context(
                left,
                rewrite_subquery_fn,
                outer_tables,
            )),
            right: Box::new(rewrite_expression_with_context(
                right,
                rewrite_subquery_fn,
                outer_tables,
            )),
            negated: *negated,
        },

        Expression::IsTruthValue { expr, truth_value, negated } => Expression::IsTruthValue {
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
            truth_value: *truth_value,
            negated: *negated,
        },

        Expression::Case { operand, when_clauses, else_result } => Expression::Case {
            operand: operand.as_ref().map(|e| {
                Box::new(rewrite_expression_with_context(e, rewrite_subquery_fn, outer_tables))
            }),
            when_clauses: when_clauses
                .iter()
                .map(|clause| vibesql_ast::CaseWhen {
                    conditions: clause
                        .conditions
                        .iter()
                        .map(|c| {
                            rewrite_expression_with_context(c, rewrite_subquery_fn, outer_tables)
                        })
                        .collect(),
                    result: rewrite_expression_with_context(
                        &clause.result,
                        rewrite_subquery_fn,
                        outer_tables,
                    ),
                })
                .collect(),
            else_result: else_result.as_ref().map(|e| {
                Box::new(rewrite_expression_with_context(e, rewrite_subquery_fn, outer_tables))
            }),
        },

        Expression::ScalarSubquery(subquery) => {
            Expression::ScalarSubquery(Box::new(rewrite_subquery_fn(subquery)))
        }

        Expression::Exists { subquery, negated } => {
            // Try to decorrelate EXISTS to IN for better performance
            if is_correlated(subquery) && !outer_tables.is_empty() {
                if let Some((outer_expr, decorrelated_subquery, neg)) =
                    rewrite_exists_to_in(subquery, *negated, outer_tables)
                {
                    // Successfully decorrelated! Return as IN expression
                    return Expression::In {
                        expr: Box::new(outer_expr),
                        subquery: Box::new(rewrite_subquery_fn(&decorrelated_subquery)),
                        negated: neg,
                    };
                }
            }
            // Fallback: keep EXISTS but recursively optimize inner subquery
            Expression::Exists {
                subquery: Box::new(rewrite_subquery_fn(subquery)),
                negated: *negated,
            }
        }

        Expression::QuantifiedComparison { expr, op, quantifier, subquery } => {
            Expression::QuantifiedComparison {
                expr: Box::new(rewrite_expression_with_context(
                    expr,
                    rewrite_subquery_fn,
                    outer_tables,
                )),
                op: *op,
                quantifier: quantifier.clone(),
                subquery: Box::new(rewrite_subquery_fn(subquery)),
            }
        }

        Expression::InList { expr, values, negated } => Expression::InList {
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
            values: values
                .iter()
                .map(|v| rewrite_expression_with_context(v, rewrite_subquery_fn, outer_tables))
                .collect(),
            negated: *negated,
        },

        Expression::Between { expr, low, high, negated, symmetric } => Expression::Between {
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
            low: Box::new(rewrite_expression_with_context(low, rewrite_subquery_fn, outer_tables)),
            high: Box::new(rewrite_expression_with_context(
                high,
                rewrite_subquery_fn,
                outer_tables,
            )),
            negated: *negated,
            symmetric: *symmetric,
        },

        Expression::Cast { expr, data_type } => Expression::Cast {
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
            data_type: data_type.clone(),
        },

        Expression::Function { name, args, character_unit } => Expression::Function {
            name: name.clone(),
            args: args
                .iter()
                .map(|a| rewrite_expression_with_context(a, rewrite_subquery_fn, outer_tables))
                .collect(),
            character_unit: character_unit.clone(),
        },

        Expression::AggregateFunction { name, distinct, args, order_by, filter } => {
            Expression::AggregateFunction {
                name: name.clone(),
                distinct: *distinct,
                args: args
                    .iter()
                    .map(|a| rewrite_expression_with_context(a, rewrite_subquery_fn, outer_tables))
                    .collect(),
                order_by: order_by.as_ref().map(|items| {
                    items
                        .iter()
                        .map(|item| vibesql_ast::OrderByItem {
                            expr: rewrite_expression_with_context(
                                &item.expr,
                                rewrite_subquery_fn,
                                outer_tables,
                            ),
                            direction: item.direction.clone(),
                            nulls_order: item.nulls_order,
                        })
                        .collect()
                }),
                filter: filter.as_ref().map(|f| {
                    Box::new(rewrite_expression_with_context(f, rewrite_subquery_fn, outer_tables))
                }),
            }
        }

        Expression::Position { substring, string, character_unit } => Expression::Position {
            substring: Box::new(rewrite_expression_with_context(
                substring,
                rewrite_subquery_fn,
                outer_tables,
            )),
            string: Box::new(rewrite_expression_with_context(
                string,
                rewrite_subquery_fn,
                outer_tables,
            )),
            character_unit: character_unit.clone(),
        },

        Expression::Trim { position, removal_char, string } => Expression::Trim {
            position: position.clone(),
            removal_char: removal_char.as_ref().map(|e| {
                Box::new(rewrite_expression_with_context(e, rewrite_subquery_fn, outer_tables))
            }),
            string: Box::new(rewrite_expression_with_context(
                string,
                rewrite_subquery_fn,
                outer_tables,
            )),
        },

        Expression::Extract { field, expr } => Expression::Extract {
            field: field.clone(),
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
        },

        Expression::Like { expr, pattern, negated, escape } => Expression::Like {
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
            pattern: Box::new(rewrite_expression_with_context(
                pattern,
                rewrite_subquery_fn,
                outer_tables,
            )),
            negated: *negated,
            escape: escape.as_ref().map(|e| {
                Box::new(rewrite_expression_with_context(e, rewrite_subquery_fn, outer_tables))
            }),
        },

        Expression::Glob { expr, pattern, negated, escape } => Expression::Glob {
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
            pattern: Box::new(rewrite_expression_with_context(
                pattern,
                rewrite_subquery_fn,
                outer_tables,
            )),
            negated: *negated,
            escape: escape.as_ref().map(|e| {
                Box::new(rewrite_expression_with_context(e, rewrite_subquery_fn, outer_tables))
            }),
        },

        Expression::Interval { value, unit, leading_precision, fractional_precision } => {
            Expression::Interval {
                value: Box::new(rewrite_expression_with_context(
                    value,
                    rewrite_subquery_fn,
                    outer_tables,
                )),
                unit: unit.clone(),
                leading_precision: *leading_precision,
                fractional_precision: *fractional_precision,
            }
        }

        Expression::Conjunction(children) => Expression::Conjunction(
            children
                .iter()
                .map(|child| {
                    rewrite_expression_with_context(child, rewrite_subquery_fn, outer_tables)
                })
                .collect(),
        ),

        Expression::Disjunction(children) => Expression::Disjunction(
            children
                .iter()
                .map(|child| {
                    rewrite_expression_with_context(child, rewrite_subquery_fn, outer_tables)
                })
                .collect(),
        ),

        Expression::RowValueConstructor(children) => Expression::RowValueConstructor(
            children
                .iter()
                .map(|child| {
                    rewrite_expression_with_context(child, rewrite_subquery_fn, outer_tables)
                })
                .collect(),
        ),

        Expression::Collate { expr, collation } => Expression::Collate {
            expr: Box::new(rewrite_expression_with_context(
                expr,
                rewrite_subquery_fn,
                outer_tables,
            )),
            collation: collation.clone(),
        },

        Expression::Raise { action, error_message } => Expression::Raise {
            action: *action,
            error_message: error_message.as_ref().map(|msg| {
                Box::new(rewrite_expression_with_context(msg, rewrite_subquery_fn, outer_tables))
            }),
        },

        // Literals, column refs, and special expressions don't need rewriting
        Expression::Literal(_)
        | Expression::ColumnRef(_)
        | Expression::Wildcard
        | Expression::CurrentDate
        | Expression::CurrentTime { .. }
        | Expression::CurrentTimestamp { .. }
        | Expression::Default
        | Expression::DuplicateKeyValue { .. }
        | Expression::WindowFunction { .. }
        | Expression::NextValue { .. }
        | Expression::MatchAgainst { .. }
        | Expression::PseudoVariable { .. }
        | Expression::SessionVariable { .. }
        | Expression::Placeholder(_)
        | Expression::NumberedPlaceholder(_)
        | Expression::NamedPlaceholder(_) => expr.clone(),
    }
}

/// Recursively rewrite FROM clause subqueries
pub(super) fn rewrite_from_clause(
    from: &vibesql_ast::FromClause,
    rewrite_subquery_fn: &impl Fn(&SelectStmt) -> SelectStmt,
) -> vibesql_ast::FromClause {
    match from {
        vibesql_ast::FromClause::Table { name, alias, quoted, .. } => {
            vibesql_ast::FromClause::Table {
                index_hint: None,
                name: name.clone(),
                alias: alias.clone(),
                column_aliases: None,
                quoted: *quoted,
            }
        }
        vibesql_ast::FromClause::Join {
            left,
            right,
            join_type,
            condition,
            using_columns,
            natural,
            alias,
        } => vibesql_ast::FromClause::Join {
            left: Box::new(rewrite_from_clause(left, rewrite_subquery_fn)),
            right: Box::new(rewrite_from_clause(right, rewrite_subquery_fn)),
            join_type: join_type.clone(),
            condition: condition.as_ref().map(|c| rewrite_expression(c, rewrite_subquery_fn)),
            using_columns: using_columns.clone(),
            natural: *natural,
            alias: alias.clone(),
        },
        vibesql_ast::FromClause::Subquery { query, alias, .. } => {
            vibesql_ast::FromClause::Subquery {
                query: Box::new(rewrite_subquery_fn(query)),
                alias: alias.clone(),
                column_aliases: None,
            }
        }
        vibesql_ast::FromClause::Values { rows, alias, column_aliases } => {
            // Rewrite expressions in VALUES rows
            vibesql_ast::FromClause::Values {
                rows: rows
                    .iter()
                    .map(|row| {
                        row.iter()
                            .map(|expr| rewrite_expression(expr, rewrite_subquery_fn))
                            .collect()
                    })
                    .collect(),
                alias: alias.clone(),
                column_aliases: column_aliases.clone(),
            }
        }
    }
}