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
//! Benchmarks for parallel nested loop join execution
//!
//! These benchmarks measure the performance improvements from morsel-driven
//! parallelism in nested loop joins (PR #4280).
//!
//! Nested loop joins are used when:
//! - No equi-join condition exists (e.g., range predicates like a.x < b.y)
//! - Hash join is not applicable
//! - Cross joins are requested
//!
//! Expected performance characteristics:
//! - 2-4x speedup on large joins with 4+ cores
//! - Better utilization on non-equi join predicates
//! - Linear scaling up to available cores for CPU-bound joins
//!
//! Usage:
//!   cargo bench --bench nested_loop_parallel
//!
//! Environment variables:
//!   WARMUP_ITERATIONS - Number of warmup runs (default: 3)
//!   BENCHMARK_ITERATIONS - Number of timed runs (default: 10)
//!   MAX_THREADS - Maximum thread count to test (default: 8)
//!   BENCHMARK_FILTER - Run specific benchmark (data_size, thread_scaling, predicates)

mod harness;

use std::{env, hint::black_box, time::Instant};

use harness::{print_group_header, BenchConfig, BenchResult, BenchStats, Harness};
use vibesql_catalog::{ColumnSchema, TableSchema};
use vibesql_executor::SelectExecutor;
use vibesql_parser::Parser;
use vibesql_storage::{Database, Row};
use vibesql_types::{DataType, SqlValue};

/// Parse a SELECT statement from SQL
fn parse_select(sql: &str) -> vibesql_ast::SelectStmt {
    match Parser::parse_sql(sql) {
        Ok(vibesql_ast::Statement::Select(select_stmt)) => *select_stmt,
        _ => panic!("Failed to parse SELECT statement"),
    }
}

/// Run a query and return execution time
fn run_query(db: &Database, sql: &str) -> BenchResult {
    let stmt = parse_select(sql);
    let executor = SelectExecutor::new(db);
    let start = Instant::now();
    match executor.execute(&stmt) {
        Ok(rows) => {
            black_box(rows);
            BenchResult::Ok(start.elapsed())
        }
        Err(e) => BenchResult::Error(e.to_string()),
    }
}

/// Get max threads to test from environment
fn get_max_threads() -> usize {
    env::var("MAX_THREADS").ok().and_then(|s| s.parse().ok()).unwrap_or(8)
}

/// Get benchmark filter from environment
fn get_benchmark_filter() -> Option<String> {
    env::var("BENCHMARK_FILTER").ok().map(|s| s.to_lowercase())
}

// =============================================================================
// Test Data Setup
// =============================================================================

/// Create tables for nested loop join benchmarks
///
/// Creates two tables:
/// - PRODUCTS: id, price, category
/// - PROMOTIONS: id, min_price, max_price, discount
///
/// The join predicate `price BETWEEN min_price AND max_price` forces
/// a nested loop join (cannot use hash join for range predicates).
fn setup_range_join_tables(db: &mut Database, products: usize, promotions: usize) {
    // Create PRODUCTS table
    let products_schema = TableSchema::new(
        "PRODUCTS".to_string(),
        vec![
            ColumnSchema {
                name: "ID".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
            ColumnSchema {
                name: "PRICE".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
            ColumnSchema {
                name: "CATEGORY".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
        ],
    );
    db.create_table(products_schema).unwrap();

    // Create PROMOTIONS table
    let promotions_schema = TableSchema::new(
        "PROMOTIONS".to_string(),
        vec![
            ColumnSchema {
                name: "ID".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
            ColumnSchema {
                name: "MIN_PRICE".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
            ColumnSchema {
                name: "MAX_PRICE".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
            ColumnSchema {
                name: "DISCOUNT".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
        ],
    );
    db.create_table(promotions_schema).unwrap();

    // Insert products with prices 0-999
    for i in 0..products {
        let row = Row::new(vec![
            SqlValue::Integer(i as i64),
            SqlValue::Integer((i % 1000) as i64), // price 0-999
            SqlValue::Integer((i % 10) as i64),   // category 0-9
        ]);
        db.insert_row("PRODUCTS", row).unwrap();
    }

    // Insert promotions with overlapping price ranges
    for i in 0..promotions {
        let min_price = (i * 100 % 800) as i64; // Range starts: 0, 100, 200, ...
        let max_price = min_price + 200; // 200-wide bands
        let row = Row::new(vec![
            SqlValue::Integer(i as i64),
            SqlValue::Integer(min_price),
            SqlValue::Integer(max_price),
            SqlValue::Integer((5 + i % 20) as i64), // discount 5-24%
        ]);
        db.insert_row("PROMOTIONS", row).unwrap();
    }
}

/// Create tables for cross join benchmarks
fn setup_cross_join_tables(db: &mut Database, left_rows: usize, right_rows: usize) {
    // Create LEFT_TABLE
    let left_schema = TableSchema::new(
        "LEFT_TABLE".to_string(),
        vec![
            ColumnSchema {
                name: "ID".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
            ColumnSchema {
                name: "VALUE".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
        ],
    );
    db.create_table(left_schema).unwrap();

    // Create RIGHT_TABLE
    let right_schema = TableSchema::new(
        "RIGHT_TABLE".to_string(),
        vec![
            ColumnSchema {
                name: "ID".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
            ColumnSchema {
                name: "FACTOR".to_string(),
                data_type: DataType::Integer,
                nullable: false,
                default_value: None,
                generated_expr: None,
                is_exact_integer_type: false,
                collation: None,
            },
        ],
    );
    db.create_table(right_schema).unwrap();

    // Insert data
    for i in 0..left_rows {
        let row =
            Row::new(vec![SqlValue::Integer(i as i64), SqlValue::Integer((i * 7 + 13) as i64)]);
        db.insert_row("LEFT_TABLE", row).unwrap();
    }

    for i in 0..right_rows {
        let row =
            Row::new(vec![SqlValue::Integer(i as i64), SqlValue::Integer((i * 3 + 5) as i64)]);
        db.insert_row("RIGHT_TABLE", row).unwrap();
    }
}

// =============================================================================
// Data Size Scaling Benchmark
// =============================================================================

/// Range join query: price BETWEEN min_price AND max_price
const RANGE_JOIN_SQL: &str = "SELECT p.id, p.price, pr.discount
     FROM products p
     JOIN promotions pr ON p.price BETWEEN pr.min_price AND pr.max_price";

/// Benchmark: Nested loop join scaling with different data sizes
///
/// This measures performance as the outer relation size increases.
fn bench_data_size_scaling(harness: &Harness) {
    print_group_header("Nested Loop Join - Data Size Scaling");

    // Test different outer relation sizes with fixed inner relation
    // Inner (promotions) is small to focus on outer scanning overhead
    let inner_size = 50;

    for outer_size in [1_000, 5_000, 10_000, 50_000, 100_000] {
        let mut db = Database::new();
        setup_range_join_tables(&mut db, outer_size, inner_size);

        let name = format!("range_join/{}x{}", outer_size, inner_size);
        let stats = harness.run(&name, || run_query(&db, RANGE_JOIN_SQL));
        stats.print();
    }
}

// =============================================================================
// Thread Scaling Benchmark
// =============================================================================

/// Benchmark thread scaling for nested loop joins
///
/// Measures:
/// - Execution time at each thread count
/// - Speedup relative to single-threaded (T1 / Tn)
/// - Efficiency (speedup / thread_count)
fn bench_thread_scaling(harness: &Harness) {
    print_group_header("Nested Loop Join - Thread Scaling");

    let max_threads = get_max_threads();
    let thread_counts: Vec<usize> =
        [1, 2, 4, 8, 16].iter().copied().filter(|&t| t <= max_threads).collect();

    eprintln!("Testing thread counts: {:?}", thread_counts);
    eprintln!("(Set MAX_THREADS env var to adjust, default: 8)\n");

    // Use a moderately large dataset to see parallel benefits
    let outer_size = 50_000;
    let inner_size = 100;

    let mut db = Database::new();
    setup_range_join_tables(&mut db, outer_size, inner_size);

    eprintln!("Dataset: {} products x {} promotions\n", outer_size, inner_size);

    let mut baseline_time = None;
    let mut results: Vec<(usize, BenchStats)> = Vec::new();

    for &thread_count in &thread_counts {
        // Configure rayon thread pool for this run
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(thread_count)
            .build()
            .expect("Failed to create thread pool");

        let stats = pool.install(|| {
            let name = format!("range_join_{}t", thread_count);
            harness.run(&name, || run_query(&db, RANGE_JOIN_SQL))
        });

        if thread_count == 1 {
            baseline_time = Some(stats.mean);
        }

        results.push((thread_count, stats));
    }

    // Print results table
    eprintln!("\n{:<8} {:>12} {:>10} {:>12}", "Threads", "Mean Time", "Speedup", "Efficiency");
    eprintln!("{:-<8} {:->12} {:->10} {:->12}", "", "", "", "");

    if let Some(t1) = baseline_time {
        for (thread_count, stats) in &results {
            let speedup = t1.as_secs_f64() / stats.mean.as_secs_f64();
            let efficiency = speedup / *thread_count as f64 * 100.0;

            eprintln!(
                "{:<8} {:>12.2?} {:>10.2}x {:>11.1}%",
                thread_count, stats.mean, speedup, efficiency
            );
        }
    }
}

// =============================================================================
// Predicate Complexity Benchmark
// =============================================================================

/// Benchmark different join predicate types
///
/// Tests various non-equi predicates that force nested loop joins:
/// - Range predicate (BETWEEN)
/// - Inequality (< or >)
/// - Complex compound predicates
fn bench_predicate_types(harness: &Harness) {
    print_group_header("Nested Loop Join - Predicate Types");

    let outer_size = 10_000;
    let inner_size = 100;

    let mut db = Database::new();
    setup_range_join_tables(&mut db, outer_size, inner_size);

    // Range predicate (BETWEEN)
    let range_sql = "SELECT COUNT(*) FROM products p
        JOIN promotions pr ON p.price BETWEEN pr.min_price AND pr.max_price";
    let stats = harness.run("range_between", || run_query(&db, range_sql));
    stats.print();

    // Less-than predicate
    let lt_sql = "SELECT COUNT(*) FROM products p
        JOIN promotions pr ON p.price < pr.max_price";
    let stats = harness.run("less_than", || run_query(&db, lt_sql));
    stats.print();

    // Greater-than predicate
    let gt_sql = "SELECT COUNT(*) FROM products p
        JOIN promotions pr ON p.price > pr.min_price";
    let stats = harness.run("greater_than", || run_query(&db, gt_sql));
    stats.print();

    // Compound predicate (AND)
    let compound_sql = "SELECT COUNT(*) FROM products p
        JOIN promotions pr ON p.price >= pr.min_price AND p.price <= pr.max_price AND p.category < 5";
    let stats = harness.run("compound_and", || run_query(&db, compound_sql));
    stats.print();
}

// =============================================================================
// Cross Join Benchmark
// =============================================================================

/// Benchmark cross join (Cartesian product) performance
///
/// Cross joins are a special case of nested loop joins with no predicate.
/// They produce M x N rows and are compute-intensive.
fn bench_cross_join(harness: &Harness) {
    print_group_header("Cross Join Benchmark");

    // Small tables to avoid excessive output (1000 x 100 = 100K rows)
    for (left, right) in [(100, 100), (500, 100), (1000, 100), (1000, 500)] {
        let mut db = Database::new();
        setup_cross_join_tables(&mut db, left, right);

        let sql = "SELECT COUNT(*) FROM left_table CROSS JOIN right_table";
        let name = format!("cross_join/{}x{}", left, right);
        let stats = harness.run(&name, || run_query(&db, sql));
        stats.print();
    }
}

// =============================================================================
// Main
// =============================================================================

fn main() {
    eprintln!("\n=== Nested Loop Join Parallel Benchmarks ===\n");

    let warmup_iterations =
        env::var("WARMUP_ITERATIONS").ok().and_then(|s| s.parse().ok()).unwrap_or(3);
    let benchmark_iterations =
        env::var("BENCHMARK_ITERATIONS").ok().and_then(|s| s.parse().ok()).unwrap_or(10);
    let config = BenchConfig::new(warmup_iterations, benchmark_iterations, 60);
    let harness = Harness::with_config(config);

    let filter = get_benchmark_filter();

    eprintln!("Configuration:");
    eprintln!("  Max Threads: {}", get_max_threads());
    eprintln!("  Warmup: {} iterations", warmup_iterations);
    eprintln!("  Benchmark: {} iterations", benchmark_iterations);
    eprintln!("  Filter: {:?}", filter);
    eprintln!();

    // Run benchmarks based on filter
    match filter.as_deref() {
        Some("data_size") | Some("size") | Some("scaling") => {
            bench_data_size_scaling(&harness);
        }
        Some("thread_scaling") | Some("thread") | Some("threads") => {
            bench_thread_scaling(&harness);
        }
        Some("predicates") | Some("predicate") => {
            bench_predicate_types(&harness);
        }
        Some("cross_join") | Some("cross") => {
            bench_cross_join(&harness);
        }
        None => {
            // Run all benchmarks
            bench_data_size_scaling(&harness);
            bench_thread_scaling(&harness);
            bench_predicate_types(&harness);
            bench_cross_join(&harness);
        }
        Some(unknown) => {
            eprintln!("Unknown benchmark filter: {}", unknown);
            eprintln!("Valid filters: data_size, thread_scaling, predicates, cross_join");
            std::process::exit(1);
        }
    }

    eprintln!("\n=== Benchmark Complete ===\n");
}