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
#[cfg(feature = "parallel")]
use rayon::prelude::*;

use super::{
    build::{build_hash_table_composite_parallel, build_hash_table_parallel, CompositeKey},
    FromResult,
};
#[cfg(feature = "parallel")]
use crate::select::parallel::ParallelConfig;
use crate::{
    errors::ExecutorError, evaluator::CombinedExpressionEvaluator, schema::CombinedSchema,
    select::join::combine_rows,
};

/// Create a row with all NULL values
#[allow(dead_code)]
pub(crate) fn create_null_row(col_count: usize) -> vibesql_storage::Row {
    vibesql_storage::Row::new(vec![vibesql_types::SqlValue::Null; col_count])
}

/// Hash join LEFT OUTER JOIN implementation (optimized for equi-joins)
///
/// This implementation uses a hash join algorithm for better performance
/// on equi-join conditions with LEFT OUTER JOIN semantics.
///
/// Algorithm:
/// 1. Build phase: Hash the right table into a HashMap (O(m))
/// 2. Probe phase: For each left row, lookup matches (O(n))
///    - If matches found: emit left + right rows
///    - If no match: emit left + NULLs (preserves left rows)
///
/// Total: O(n + m) instead of O(n * m) for nested loop join
///
/// Performance: Critical for Q13 where customer LEFT JOIN orders
/// with 150k customers and 1.5M orders.
pub(in crate::select::join) fn hash_join_left_outer(
    left: FromResult,
    right: FromResult,
    left_col_idx: usize,
    right_col_idx: usize,
) -> Result<FromResult, ExecutorError> {
    // Get column counts (handles nested joins with multiple tables)
    let right_col_count = right.schema.total_columns;
    let left_col_count = left.schema.total_columns;

    // Combine schemas using merge to preserve all tables from nested joins
    let combined_schema = CombinedSchema::merge(left.schema.clone(), right.schema.clone());

    // Use as_slice() for zero-cost access without triggering row materialization
    let left_slice = left.as_slice();
    let right_slice = right.as_slice();

    // Build hash table on the RIGHT side (we need to preserve ALL left rows)
    // For LEFT OUTER JOIN, we always probe with left, so build on right
    // Uses parallel hashing when available for large tables
    let hash_table = build_hash_table_parallel(right_slice, right_col_idx);

    // Pre-compute combined row size for efficient allocation
    let combined_size = left_col_count + right_col_count;

    // Parallel probe phase for LEFT OUTER JOIN
    // Every left row must appear in output (with matches or NULLs)
    #[cfg(feature = "parallel")]
    {
        let config = ParallelConfig::global();
        if config.should_parallelize_join(left_slice.len()) {
            // Pre-create null values for unmatched rows (shared across threads via reference)
            let null_values = vec![vibesql_types::SqlValue::Null; right_col_count];

            let result_rows: Vec<vibesql_storage::Row> = left_slice
                .par_iter()
                .flat_map(|left_row| {
                    let key = &left_row.values[left_col_idx];

                    // For NULL keys in left, emit left + NULLs
                    if key == &vibesql_types::SqlValue::Null {
                        let mut combined = Vec::with_capacity(combined_size);
                        combined.extend_from_slice(&left_row.values);
                        combined.extend_from_slice(&null_values);
                        return vec![vibesql_storage::Row::new(combined)];
                    }

                    if let Some(right_indices) = hash_table.get(key) {
                        // Found matches - emit all combinations
                        right_indices
                            .iter()
                            .map(|&right_idx| {
                                let mut combined = Vec::with_capacity(combined_size);
                                combined.extend_from_slice(&left_row.values);
                                combined.extend_from_slice(&right_slice[right_idx].values);
                                vibesql_storage::Row::new(combined)
                            })
                            .collect()
                    } else {
                        // No match - emit left row with NULLs
                        let mut combined = Vec::with_capacity(combined_size);
                        combined.extend_from_slice(&left_row.values);
                        combined.extend_from_slice(&null_values);
                        vec![vibesql_storage::Row::new(combined)]
                    }
                })
                .collect();

            return Ok(FromResult::from_rows(combined_schema, result_rows));
        }
    }

    // Sequential fallback: Two-phase approach for better allocation
    // Phase 1: Count total rows needed (matched + unmatched)
    let mut match_count = 0usize;
    let mut unmatched_count = 0usize;

    for left_row in left_slice {
        let key = &left_row.values[left_col_idx];

        if key == &vibesql_types::SqlValue::Null {
            unmatched_count += 1;
        } else if let Some(right_indices) = hash_table.get(key) {
            match_count += right_indices.len();
        } else {
            unmatched_count += 1;
        }
    }

    // Phase 2: Allocate result with exact capacity and populate
    let mut result_rows = Vec::with_capacity(match_count + unmatched_count);

    // Create a single null row for reuse (reduces allocations for unmatched rows)
    let null_values = vec![vibesql_types::SqlValue::Null; right_col_count];

    for left_row in left_slice {
        let key = &left_row.values[left_col_idx];

        // For NULL keys in left, still emit the row with NULL right side
        if key == &vibesql_types::SqlValue::Null {
            let mut combined = Vec::with_capacity(combined_size);
            combined.extend_from_slice(&left_row.values);
            combined.extend_from_slice(&null_values);
            result_rows.push(vibesql_storage::Row::new(combined));
            continue;
        }

        if let Some(right_indices) = hash_table.get(key) {
            // Found matches - emit all combinations
            for &right_idx in right_indices {
                let mut combined = Vec::with_capacity(combined_size);
                combined.extend_from_slice(&left_row.values);
                combined.extend_from_slice(&right_slice[right_idx].values);
                result_rows.push(vibesql_storage::Row::new(combined));
            }
        } else {
            // No match - emit left row with NULLs for right columns
            let mut combined = Vec::with_capacity(combined_size);
            combined.extend_from_slice(&left_row.values);
            combined.extend_from_slice(&null_values);
            result_rows.push(vibesql_storage::Row::new(combined));
        }
    }

    Ok(FromResult::from_rows(combined_schema, result_rows))
}

/// Multi-column hash join LEFT OUTER JOIN implementation
///
/// This implementation uses composite keys for hash join when there are multiple
/// equi-join conditions between the same table pair (e.g., `a.x = b.x AND a.y = b.y`).
///
/// Using composite keys instead of single-column keys eliminates the need for
/// post-join filtering of additional equi-join conditions. This is critical for
/// LEFT OUTER JOIN correctness because post-join filters incorrectly skip rows
/// where conditions evaluate to NULL (unmatched left rows have NULL right columns).
///
/// Algorithm:
/// 1. Build phase: Create hash table with composite keys from right table (O(m))
/// 2. Probe phase: For each left row, create composite key and lookup (O(n))
///    - If matches found: emit left + right rows
///    - If no match (or NULL key): emit left + NULLs (preserves left rows)
///
/// Total: O(n + m) with correct LEFT JOIN semantics
///
/// This fixes TPC-DS Q75 where compound LEFT JOIN conditions like:
/// `ss_ticket_number = sr_ticket_number AND ss_item_sk = sr_item_sk`
/// must match on BOTH columns, not filter after single-column match.
pub(in crate::select::join) fn hash_join_left_outer_multi(
    left: FromResult,
    right: FromResult,
    left_col_indices: &[usize],
    right_col_indices: &[usize],
) -> Result<FromResult, ExecutorError> {
    // Get column counts (handles nested joins with multiple tables)
    let right_col_count = right.schema.total_columns;
    let left_col_count = left.schema.total_columns;

    // Combine schemas using merge to preserve all tables from nested joins
    let combined_schema = CombinedSchema::merge(left.schema.clone(), right.schema.clone());

    // Use as_slice() for zero-cost access (we can't swap build/probe for LEFT JOIN)
    let left_slice = left.as_slice();
    let right_slice = right.as_slice();

    // Build hash table on the RIGHT side with composite keys
    // For LEFT OUTER JOIN, we always probe with left, so build on right
    let hash_table = build_hash_table_composite_parallel(right_slice, right_col_indices);

    // Pre-compute combined row size for efficient allocation
    let combined_size = left_col_count + right_col_count;

    // Parallel probe phase for multi-column LEFT OUTER JOIN
    // Every left row must appear in output (with matches or NULLs)
    #[cfg(feature = "parallel")]
    {
        let config = ParallelConfig::global();
        if config.should_parallelize_join(left_slice.len()) {
            // Pre-create null values for unmatched rows
            let null_values = vec![vibesql_types::SqlValue::Null; right_col_count];

            let result_rows: Vec<vibesql_storage::Row> = left_slice
                .par_iter()
                .flat_map(|left_row| {
                    let probe_key = CompositeKey::from_row(left_row, left_col_indices);

                    // For NULL keys in left (any column is NULL), emit left + NULLs
                    if probe_key.has_null() {
                        let mut combined = Vec::with_capacity(combined_size);
                        combined.extend_from_slice(&left_row.values);
                        combined.extend_from_slice(&null_values);
                        return vec![vibesql_storage::Row::new(combined)];
                    }

                    if let Some(right_indices) = hash_table.get(&probe_key) {
                        // Found matches - emit all combinations
                        right_indices
                            .iter()
                            .map(|&right_idx| {
                                let mut combined = Vec::with_capacity(combined_size);
                                combined.extend_from_slice(&left_row.values);
                                combined.extend_from_slice(&right_slice[right_idx].values);
                                vibesql_storage::Row::new(combined)
                            })
                            .collect()
                    } else {
                        // No match - emit left row with NULLs
                        let mut combined = Vec::with_capacity(combined_size);
                        combined.extend_from_slice(&left_row.values);
                        combined.extend_from_slice(&null_values);
                        vec![vibesql_storage::Row::new(combined)]
                    }
                })
                .collect();

            return Ok(FromResult::from_rows(combined_schema, result_rows));
        }
    }

    // Sequential fallback
    // Create a single null row for reuse (reduces allocations for unmatched rows)
    let null_values = vec![vibesql_types::SqlValue::Null; right_col_count];

    // Estimate result size - at least left_slice.len() since we preserve all left rows
    let mut result_rows = Vec::with_capacity(left_slice.len());

    for left_row in left_slice {
        let probe_key = CompositeKey::from_row(left_row, left_col_indices);

        // For NULL keys in left (any column is NULL), still emit with NULL right side
        // This preserves LEFT JOIN semantics: all left rows must appear in output
        if probe_key.has_null() {
            let mut combined = Vec::with_capacity(combined_size);
            combined.extend_from_slice(&left_row.values);
            combined.extend_from_slice(&null_values);
            result_rows.push(vibesql_storage::Row::new(combined));
            continue;
        }

        if let Some(right_indices) = hash_table.get(&probe_key) {
            // Found matches - emit all combinations
            for &right_idx in right_indices {
                let mut combined = Vec::with_capacity(combined_size);
                combined.extend_from_slice(&left_row.values);
                combined.extend_from_slice(&right_slice[right_idx].values);
                result_rows.push(vibesql_storage::Row::new(combined));
            }
        } else {
            // No match - emit left row with NULLs for right columns
            let mut combined = Vec::with_capacity(combined_size);
            combined.extend_from_slice(&left_row.values);
            combined.extend_from_slice(&null_values);
            result_rows.push(vibesql_storage::Row::new(combined));
        }
    }

    Ok(FromResult::from_rows(combined_schema, result_rows))
}

/// Hash join LEFT OUTER JOIN with additional filter conditions
///
/// This implementation applies non-equi-join ON conditions DURING the join,
/// not as a post-filter. This is critical for correct LEFT JOIN semantics.
///
/// For queries like:
/// ```sql
/// SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.x AND t2.z = 'ok'
/// ```
///
/// When t1.a = t2.x matches but t2.z != 'ok':
/// - Incorrect (post-filter): Row is filtered out entirely
/// - Correct (this impl): Emit t1 columns + NULLs for t2 columns
///
/// Algorithm:
/// 1. Build phase: Hash the right table (O(m))
/// 2. Probe phase: For each left row (O(n)):
///    a. If no equi-join match: emit left + NULLs
///    b. If equi-join match: check additional filter for each match
///       - If any match passes filter: emit left + right for each passing match
///       - If ALL matches fail filter: emit left + NULLs (preserves left row!)
///
/// Total: O(n + m) with correct LEFT JOIN semantics
pub(in crate::select::join) fn hash_join_left_outer_with_filter(
    left: FromResult,
    right: FromResult,
    left_col_idx: usize,
    right_col_idx: usize,
    additional_filter: &vibesql_ast::Expression,
    combined_schema: &CombinedSchema,
    database: &vibesql_storage::Database,
) -> Result<FromResult, ExecutorError> {
    // Get column counts
    let right_col_count = right.schema.total_columns;
    let left_col_count = left.schema.total_columns;

    // Use as_slice() for zero-cost access
    let left_slice = left.as_slice();
    let right_slice = right.as_slice();

    // Build hash table on the RIGHT side
    let hash_table = build_hash_table_parallel(right_slice, right_col_idx);

    // Pre-compute combined row size for efficient allocation
    let combined_size = left_col_count + right_col_count;

    // Create evaluator for filter conditions
    let evaluator = CombinedExpressionEvaluator::with_database(combined_schema, database);

    // Pre-create null values for unmatched/filtered rows
    let null_values = vec![vibesql_types::SqlValue::Null; right_col_count];

    // Estimate result size - at least left_slice.len() since we preserve all left rows
    let mut result_rows = Vec::with_capacity(left_slice.len());

    for left_row in left_slice {
        let key = &left_row.values[left_col_idx];

        // For NULL keys in left, emit left + NULLs (NULL never matches in equi-join)
        if key == &vibesql_types::SqlValue::Null {
            let mut combined = Vec::with_capacity(combined_size);
            combined.extend_from_slice(&left_row.values);
            combined.extend_from_slice(&null_values);
            result_rows.push(vibesql_storage::Row::new(combined));
            continue;
        }

        if let Some(right_indices) = hash_table.get(key) {
            // Found equi-join matches - check additional filter for each
            let mut any_match_passed = false;

            for &right_idx in right_indices {
                let right_row = &right_slice[right_idx];

                // Create combined row for filter evaluation
                let combined_row = combine_rows(left_row, right_row);

                // Clear CSE cache before evaluation
                evaluator.clear_cse_cache();

                // Evaluate the additional filter
                match evaluator.eval(additional_filter, &combined_row) {
                    Ok(vibesql_types::SqlValue::Boolean(true)) => {
                        // Filter passed - emit this combination
                        any_match_passed = true;
                        let mut combined = Vec::with_capacity(combined_size);
                        combined.extend_from_slice(&left_row.values);
                        combined.extend_from_slice(&right_row.values);
                        result_rows.push(vibesql_storage::Row::new(combined));
                    }
                    Ok(vibesql_types::SqlValue::Boolean(false))
                    | Ok(vibesql_types::SqlValue::Null) => {
                        // Filter didn't pass - continue checking other matches
                        continue;
                    }
                    Ok(_) | Err(_) => {
                        // Non-boolean result or error - treat as non-match
                        continue;
                    }
                }
            }

            // If NO matches passed the filter, emit left + NULLs
            // This preserves LEFT JOIN semantics: all left rows must appear
            if !any_match_passed {
                let mut combined = Vec::with_capacity(combined_size);
                combined.extend_from_slice(&left_row.values);
                combined.extend_from_slice(&null_values);
                result_rows.push(vibesql_storage::Row::new(combined));
            }
        } else {
            // No equi-join match - emit left + NULLs
            let mut combined = Vec::with_capacity(combined_size);
            combined.extend_from_slice(&left_row.values);
            combined.extend_from_slice(&null_values);
            result_rows.push(vibesql_storage::Row::new(combined));
        }
    }

    // Re-create combined schema (we consumed left/right)
    let result_schema = combined_schema.clone();
    Ok(FromResult::from_rows(result_schema, result_rows))
}