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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//! Tests for table elimination optimizer pass

#[cfg(test)]
mod derive_prefix_from_table_name_tests {
    use super::super::prefix::derive_prefix_from_table_name;

    #[test]
    fn short_aliases_two_chars() {
        // TPC-DS style 2-char aliases
        assert_eq!(derive_prefix_from_table_name("ss"), Some("ss_".to_string()));
        assert_eq!(derive_prefix_from_table_name("ws"), Some("ws_".to_string()));
        assert_eq!(derive_prefix_from_table_name("cs"), Some("cs_".to_string()));
    }

    #[test]
    fn short_aliases_three_chars() {
        // 3-char aliases
        assert_eq!(derive_prefix_from_table_name("inv"), Some("inv_".to_string()));
    }

    #[test]
    fn dimension_tables() {
        // Tables ending with _dim use first letter
        assert_eq!(derive_prefix_from_table_name("date_dim"), Some("d_".to_string()));
        assert_eq!(derive_prefix_from_table_name("time_dim"), Some("t_".to_string()));
        assert_eq!(derive_prefix_from_table_name("item_dim"), Some("i_".to_string()));
    }

    #[test]
    fn multi_word_tables() {
        // Multi-word tables use acronym (first letter of each word)
        assert_eq!(derive_prefix_from_table_name("customer_address"), Some("ca_".to_string()));
        assert_eq!(derive_prefix_from_table_name("store_sales"), Some("ss_".to_string()));
        assert_eq!(derive_prefix_from_table_name("web_returns"), Some("wr_".to_string()));
        assert_eq!(derive_prefix_from_table_name("catalog_page"), Some("cp_".to_string()));
    }

    #[test]
    fn single_word_tables() {
        // Single word tables use first letter
        assert_eq!(derive_prefix_from_table_name("customer"), Some("c_".to_string()));
        assert_eq!(derive_prefix_from_table_name("item"), Some("i_".to_string()));
        assert_eq!(derive_prefix_from_table_name("store"), Some("s_".to_string()));
        assert_eq!(derive_prefix_from_table_name("warehouse"), Some("w_".to_string()));
    }

    #[test]
    fn case_insensitive() {
        // Should handle mixed case
        assert_eq!(derive_prefix_from_table_name("DATE_DIM"), Some("d_".to_string()));
        assert_eq!(derive_prefix_from_table_name("Customer"), Some("c_".to_string()));
        assert_eq!(derive_prefix_from_table_name("Store_Sales"), Some("ss_".to_string()));
    }

    #[test]
    fn empty_string() {
        // Empty string falls through to short alias logic (len <= 3)
        // and returns "_" (empty + underscore)
        assert_eq!(derive_prefix_from_table_name(""), Some("_".to_string()));
    }
}

#[cfg(test)]
mod find_common_prefix_tests {
    use super::super::prefix::find_common_prefix;

    #[test]
    fn common_underscore_prefix() {
        let cols = vec!["d_year".to_string(), "d_date_sk".to_string(), "d_month".to_string()];
        assert_eq!(find_common_prefix(&cols), Some("d_".to_string()));
    }

    #[test]
    fn two_char_prefix() {
        let cols = vec!["ca_state".to_string(), "ca_city".to_string(), "ca_zip".to_string()];
        assert_eq!(find_common_prefix(&cols), Some("ca_".to_string()));
    }

    #[test]
    fn no_common_prefix() {
        let cols = vec!["foo".to_string(), "bar".to_string(), "baz".to_string()];
        assert_eq!(find_common_prefix(&cols), None);
    }

    #[test]
    fn empty_columns() {
        let cols: Vec<String> = vec![];
        assert_eq!(find_common_prefix(&cols), None);
    }

    #[test]
    fn single_column() {
        let cols = vec!["d_year".to_string()];
        assert_eq!(find_common_prefix(&cols), Some("d_".to_string()));
    }

    #[test]
    fn partial_match_not_all_columns() {
        // First two columns share prefix, but third doesn't
        let cols = vec!["d_year".to_string(), "d_month".to_string(), "t_hour".to_string()];
        assert_eq!(find_common_prefix(&cols), None);
    }

    #[test]
    fn fallback_to_two_char_prefix() {
        // No underscore, but shares first 2 chars
        let cols = vec!["item1".to_string(), "item2".to_string()];
        assert_eq!(find_common_prefix(&cols), Some("it".to_string()));
    }
}

#[cfg(test)]
mod eliminate_unused_tables_tests {
    use vibesql_ast::{BinaryOperator, Expression, FromClause, JoinType, SelectItem, SelectStmt};
    use vibesql_types::SqlValue;

    use super::super::eliminate_unused_tables;

    fn make_column_ref(table: Option<&str>, column: &str) -> Expression {
        match table {
            Some(t) => Expression::ColumnRef(vibesql_ast::ColumnIdentifier::qualified(
                t, false, column, false,
            )),
            None => Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(column, false)),
        }
    }

    fn make_table(name: &str, alias: Option<&str>) -> FromClause {
        FromClause::Table {
            index_hint: None,
            name: name.to_string(),
            alias: alias.map(|a| a.to_string()),
            column_aliases: None,
            quoted: false,
        }
    }

    fn make_cross_join(left: FromClause, right: FromClause) -> FromClause {
        FromClause::Join {
            left: Box::new(left),
            right: Box::new(right),
            join_type: JoinType::Cross,
            condition: None,
            using_columns: None,
            natural: false,
            alias: None,
        }
    }

    #[test]
    fn single_table_unchanged() {
        // Single table should not be eliminated
        let stmt = SelectStmt {
            with_clause: None,
            distinct: false,
            select_list: vec![SelectItem::Expression {
                expr: make_column_ref(Some("t1"), "col1"),
                alias: None,
                source_text: None,
            }],
            into_table: None,
            into_variables: None,
            from: Some(make_table("table1", Some("t1"))),
            where_clause: None,
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        };

        let result = eliminate_unused_tables(&stmt);
        assert!(matches!(result.from, Some(FromClause::Table { .. })));
    }

    #[test]
    fn table_in_select_not_eliminated() {
        // Table referenced in SELECT should not be eliminated
        let stmt = SelectStmt {
            with_clause: None,
            distinct: false,
            select_list: vec![
                SelectItem::Expression {
                    expr: make_column_ref(Some("t1"), "col1"),
                    alias: None,
                    source_text: None,
                },
                SelectItem::Expression {
                    expr: make_column_ref(Some("t2"), "col2"),
                    alias: None,
                    source_text: None,
                },
            ],
            into_table: None,
            into_variables: None,
            from: Some(make_cross_join(
                make_table("table1", Some("t1")),
                make_table("table2", Some("t2")),
            )),
            where_clause: None,
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        };

        let result = eliminate_unused_tables(&stmt);
        // Both tables should be kept since both are in SELECT
        assert!(matches!(result.from, Some(FromClause::Join { .. })));
    }

    #[test]
    fn table_in_equijoin_not_eliminated() {
        // Table in equijoin should not be eliminated
        let stmt = SelectStmt {
            with_clause: None,
            distinct: false,
            select_list: vec![SelectItem::Expression {
                expr: make_column_ref(Some("t1"), "col1"),
                alias: None,
                source_text: None,
            }],
            into_table: None,
            into_variables: None,
            from: Some(make_cross_join(
                make_table("table1", Some("t1")),
                make_table("table2", Some("t2")),
            )),
            where_clause: Some(Expression::BinaryOp {
                op: BinaryOperator::Equal,
                left: Box::new(make_column_ref(Some("t1"), "id")),
                right: Box::new(make_column_ref(Some("t2"), "id")),
            }),
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        };

        let result = eliminate_unused_tables(&stmt);
        // Both tables should be kept since they're in equijoin
        assert!(matches!(result.from, Some(FromClause::Join { .. })));
    }

    #[test]
    fn unused_table_with_filter_eliminated() {
        // Table not in SELECT and not in equijoin should be eliminated
        // with its filter converted to EXISTS
        let stmt = SelectStmt {
            with_clause: None,
            distinct: false,
            select_list: vec![SelectItem::Expression {
                expr: make_column_ref(Some("t1"), "col1"),
                alias: None,
                source_text: None,
            }],
            into_table: None,
            into_variables: None,
            from: Some(make_cross_join(
                make_table("table1", Some("t1")),
                make_table("date_dim", Some("d")),
            )),
            where_clause: Some(Expression::BinaryOp {
                op: BinaryOperator::Equal,
                left: Box::new(make_column_ref(Some("d"), "d_year")),
                right: Box::new(Expression::Literal(SqlValue::Integer(2000))),
            }),
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        };

        let result = eliminate_unused_tables(&stmt);

        // FROM should now only have table1
        match &result.from {
            Some(FromClause::Table { name, .. }) => {
                assert_eq!(name, "table1");
            }
            _ => panic!("Expected single table, got: {:?}", result.from),
        }

        // WHERE should contain EXISTS check
        match &result.where_clause {
            Some(Expression::Exists { negated, .. }) => {
                assert!(!negated);
            }
            _ => panic!("Expected EXISTS clause, got: {:?}", result.where_clause),
        }
    }

    #[test]
    fn select_literal_subquery_unchanged() {
        // SELECT 1 FROM ... subqueries should not be optimized
        let stmt = SelectStmt {
            with_clause: None,
            distinct: false,
            select_list: vec![SelectItem::Expression {
                expr: Expression::Literal(SqlValue::Integer(1)),
                alias: None,
                source_text: None,
            }],
            into_table: None,
            into_variables: None,
            from: Some(make_cross_join(
                make_table("table1", Some("t1")),
                make_table("table2", Some("t2")),
            )),
            where_clause: None,
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        };

        let result = eliminate_unused_tables(&stmt);
        // Should be unchanged (both tables kept)
        assert!(matches!(result.from, Some(FromClause::Join { .. })));
    }

    #[test]
    fn no_from_clause_unchanged() {
        let stmt = SelectStmt {
            with_clause: None,
            distinct: false,
            select_list: vec![SelectItem::Expression {
                expr: Expression::Literal(SqlValue::Integer(42)),
                alias: None,
                source_text: None,
            }],
            into_table: None,
            into_variables: None,
            from: None,
            where_clause: None,
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        };

        let result = eliminate_unused_tables(&stmt);
        assert!(result.from.is_none());
    }

    #[test]
    fn select_star_references_all_tables() {
        // SELECT * should reference all tables, preventing elimination
        let stmt = SelectStmt {
            with_clause: None,
            distinct: false,
            select_list: vec![SelectItem::Wildcard { alias: None }],
            into_table: None,
            into_variables: None,
            from: Some(make_cross_join(
                make_table("table1", Some("t1")),
                make_table("table2", Some("t2")),
            )),
            where_clause: None,
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        };

        let result = eliminate_unused_tables(&stmt);
        // Both tables should be kept due to SELECT *
        assert!(matches!(result.from, Some(FromClause::Join { .. })));
    }

    #[test]
    fn cross_join_without_filter_preserved() {
        // Regression test: tables in cross joins without filters should NOT be eliminated
        // because cross joins multiply rows intentionally.
        // Example: SELECT 86 * - cor0.col2 FROM tab1, tab2 AS cor0
        // This should return 9 rows (3x3), not 3 rows.
        let stmt = SelectStmt {
            with_clause: None,
            distinct: false,
            // SELECT only references cor0.col2
            select_list: vec![SelectItem::Expression {
                expr: Expression::BinaryOp {
                    op: BinaryOperator::Multiply,
                    left: Box::new(Expression::Literal(SqlValue::Integer(86))),
                    right: Box::new(Expression::UnaryOp {
                        op: vibesql_ast::UnaryOperator::Minus,
                        expr: Box::new(make_column_ref(Some("cor0"), "col2")),
                    }),
                },
                alias: Some("col0".to_string()),
                source_text: None,
            }],
            into_table: None,
            into_variables: None,
            // Cross join - tab1 is NOT referenced but has no filter
            from: Some(make_cross_join(make_table("tab1", None), make_table("tab2", Some("cor0")))),
            // No WHERE clause
            where_clause: None,
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        };

        let result = eliminate_unused_tables(&stmt);
        // Both tables should be kept - tab1 has no filter, so cross join
        // must be preserved to maintain correct row count
        assert!(
            matches!(result.from, Some(FromClause::Join { .. })),
            "Expected cross join to be preserved, got {:?}",
            result.from
        );
    }

    #[test]
    fn all_tables_eliminable_keeps_unchanged() {
        // Regression test: when ALL tables could be eliminated,
        // we should keep them all to preserve FROM clause.
        // This ensures WHERE clauses like NULL IS NOT NULL work correctly.
        // Example: SELECT - 0 FROM tab0, tab0 cor0 WHERE NULL IS NOT NULL
        let stmt = SelectStmt {
            with_clause: None,
            distinct: false,
            // SELECT with literal (no column refs)
            select_list: vec![SelectItem::Expression {
                expr: Expression::UnaryOp {
                    op: vibesql_ast::UnaryOperator::Minus,
                    expr: Box::new(Expression::Literal(SqlValue::Integer(0))),
                },
                alias: Some("col3".to_string()),
                source_text: None,
            }],
            into_table: None,
            into_variables: None,
            // Cross join of same table
            from: Some(make_cross_join(make_table("tab0", None), make_table("tab0", Some("cor0")))),
            // WHERE clause with no column refs (NULL IS NOT NULL)
            where_clause: Some(Expression::IsNull {
                expr: Box::new(Expression::Literal(SqlValue::Null)),
                negated: true,
            }),
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        };

        let result = eliminate_unused_tables(&stmt);
        // Both tables should be kept (not eliminated) because eliminating
        // both would leave no FROM clause
        assert!(
            matches!(result.from, Some(FromClause::Join { .. })),
            "Expected FROM clause to be preserved, got {:?}",
            result.from
        );
    }
}

#[cfg(test)]
mod helper_function_tests {
    use vibesql_ast::{BinaryOperator, Expression, SelectItem};
    use vibesql_types::SqlValue;

    use super::super::predicate::{combine_predicates, flatten_and_chain};
    use super::super::select_analysis::{collect_unqualified_columns, has_unqualified_column_ref};

    #[test]
    fn flatten_and_chain_single() {
        let expr = Expression::Literal(SqlValue::Integer(1));
        let result = flatten_and_chain(&expr);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn flatten_and_chain_multiple() {
        let expr = Expression::BinaryOp {
            op: BinaryOperator::And,
            left: Box::new(Expression::Literal(SqlValue::Integer(1))),
            right: Box::new(Expression::BinaryOp {
                op: BinaryOperator::And,
                left: Box::new(Expression::Literal(SqlValue::Integer(2))),
                right: Box::new(Expression::Literal(SqlValue::Integer(3))),
            }),
        };
        let result = flatten_and_chain(&expr);
        assert_eq!(result.len(), 3);
    }

    #[test]
    fn combine_predicates_empty() {
        let preds: Vec<Expression> = vec![];
        let result = combine_predicates(preds);
        assert!(matches!(result, Expression::Literal(SqlValue::Boolean(true))));
    }

    #[test]
    fn combine_predicates_single() {
        let preds = vec![Expression::Literal(SqlValue::Integer(42))];
        let result = combine_predicates(preds);
        assert!(matches!(result, Expression::Literal(SqlValue::Integer(42))));
    }

    #[test]
    fn combine_predicates_multiple() {
        let preds = vec![
            Expression::Literal(SqlValue::Integer(1)),
            Expression::Literal(SqlValue::Integer(2)),
        ];
        let result = combine_predicates(preds);
        assert!(matches!(result, Expression::BinaryOp { op: BinaryOperator::And, .. }));
    }

    #[test]
    fn collect_unqualified_columns_finds_refs() {
        let select_list = vec![
            SelectItem::Expression {
                expr: Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple("col1", false)),
                alias: None,
                source_text: None,
            },
            SelectItem::Expression {
                expr: Expression::ColumnRef(vibesql_ast::ColumnIdentifier::qualified(
                    "t1", false, "col2", false,
                )),
                alias: None,
                source_text: None,
            },
        ];
        let result = collect_unqualified_columns(&select_list);
        assert!(result.contains("col1"));
        assert!(!result.contains("col2")); // Qualified column should not be included
    }

    #[test]
    fn has_unqualified_column_ref_detects_unqualified() {
        let qualified = Expression::ColumnRef(vibesql_ast::ColumnIdentifier::qualified(
            "t1", false, "col1", false,
        ));
        assert!(!has_unqualified_column_ref(&qualified));

        let unqualified =
            Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple("col1", false));
        assert!(has_unqualified_column_ref(&unqualified));
    }
}