stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
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
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Nested Loop Join Operator.
//!
//! This operator implements the classic nested loop join with O(N*M) complexity.
//! It's the fallback algorithm used when:
//! - No equality join keys exist (non-equi joins)
//! - Join condition uses complex expressions
//! - CROSS JOIN is requested
//!
//! Despite its higher complexity, it supports all join conditions and types.

use crate::core::value::NULL_VALUE;
use crate::core::{Result, Row, Value};
use crate::executor::expression::JoinFilter;
use crate::executor::operator::{ColumnInfo, Operator, RowRef};
use crate::functions::registry::global_registry;
use crate::parser::ast::Expression;

use super::hash_join::JoinType;

/// Nested Loop Join Operator.
///
/// For each row in the outer (left) input, scans all rows in the inner (right)
/// input and emits matches based on the join condition.
pub struct NestedLoopJoinOperator {
    // Input operators
    left: Box<dyn Operator>,
    right: Box<dyn Operator>,

    // Join configuration
    join_type: JoinType,
    condition: Option<Expression>,

    // Compiled filter (created in open())
    filter: Option<JoinFilter>,

    // Output schema
    schema: Vec<ColumnInfo>,
    left_col_count: usize,
    right_col_count: usize,

    // Materialized right side (inner loop)
    right_rows: Vec<Row>,

    // Current state
    current_left_row: Option<Row>,
    current_right_idx: usize,
    left_had_match: bool,

    // Track matched right rows for RIGHT/FULL OUTER
    right_matched: Vec<bool>,

    // Phase for returning unmatched right rows
    returning_unmatched_right: bool,
    unmatched_right_idx: usize,

    // Cached null rows for OUTER joins (avoid repeated allocation)
    cached_null_right: Vec<Value>,
    cached_null_left: Vec<Value>,

    // State tracking
    opened: bool,
    left_exhausted: bool,
}

impl NestedLoopJoinOperator {
    /// Create a new nested loop join operator.
    ///
    /// # Arguments
    /// * `left` - Left (outer) input operator
    /// * `right` - Right (inner) input operator
    /// * `join_type` - Type of join (INNER, LEFT, RIGHT, FULL, CROSS)
    /// * `condition` - Join condition (None for CROSS JOIN)
    pub fn new(
        left: Box<dyn Operator>,
        right: Box<dyn Operator>,
        join_type: JoinType,
        condition: Option<Expression>,
    ) -> Self {
        // Build combined schema
        let mut schema = Vec::new();
        schema.extend(left.schema().iter().cloned());
        schema.extend(right.schema().iter().cloned());

        let left_col_count = left.schema().len();
        let right_col_count = right.schema().len();

        Self {
            left,
            right,
            join_type,
            condition,
            filter: None,
            schema,
            left_col_count,
            right_col_count,
            right_rows: Vec::new(),
            current_left_row: None,
            current_right_idx: 0,
            left_had_match: false,
            right_matched: Vec::new(),
            returning_unmatched_right: false,
            unmatched_right_idx: 0,
            cached_null_right: Vec::new(), // Initialized in open()
            cached_null_left: Vec::new(),  // Initialized in open()
            opened: false,
            left_exhausted: false,
        }
    }

    /// Create a NULL row for the left side (uses cached values).
    #[inline]
    fn null_left_row(&self) -> Row {
        Row::from_values(self.cached_null_left.clone())
    }

    /// Create a NULL row for the right side (uses cached values).
    #[inline]
    fn null_right_row(&self) -> Row {
        Row::from_values(self.cached_null_right.clone())
    }

    /// Combine left and right rows into output row.
    #[inline]
    fn combine(&self, left: &Row, right: &Row) -> Row {
        Row::from_combined(left, right)
    }

    /// Get the next left row from the outer input.
    #[inline]
    fn advance_left(&mut self) -> Result<bool> {
        match self.left.next()? {
            Some(row_ref) => {
                self.current_left_row = Some(row_ref.into_owned());
                self.current_right_idx = 0;
                self.left_had_match = false;
                Ok(true)
            }
            None => {
                self.left_exhausted = true;
                Ok(false)
            }
        }
    }
}

impl Operator for NestedLoopJoinOperator {
    fn open(&mut self) -> Result<()> {
        // Open both inputs
        self.left.open()?;
        self.right.open()?;

        // Pre-cache null rows for OUTER joins (avoids repeated allocation)
        // NULL_VALUE is a static constant, cloning Vec is just memcpy
        if matches!(
            self.join_type,
            JoinType::Left | JoinType::Right | JoinType::Full
        ) {
            self.cached_null_right = vec![NULL_VALUE; self.right_col_count];
            self.cached_null_left = vec![NULL_VALUE; self.left_col_count];
        }

        // Build column names for filter compilation
        let left_cols: Vec<String> = self.left.schema().iter().map(|c| c.name.clone()).collect();
        let right_cols: Vec<String> = self.right.schema().iter().map(|c| c.name.clone()).collect();

        // Compile join filter if condition exists
        if let Some(ref cond) = self.condition {
            self.filter = Some(JoinFilter::new(
                cond,
                &left_cols,
                &right_cols,
                global_registry(),
            )?);
        }

        // Materialize right side (inner loop must be restarted for each left row)
        while let Some(row_ref) = self.right.next()? {
            self.right_rows.push(row_ref.into_owned());
        }

        // Initialize matched tracking for RIGHT/FULL OUTER
        if matches!(self.join_type, JoinType::Right | JoinType::Full) {
            self.right_matched = vec![false; self.right_rows.len()];
        }

        // Get first left row
        self.advance_left()?;

        self.opened = true;
        Ok(())
    }

    fn next(&mut self) -> Result<Option<RowRef>> {
        if !self.opened {
            return Err(crate::core::Error::internal(
                "NestedLoopJoinOperator::next called before open",
            ));
        }

        let is_left_outer = matches!(self.join_type, JoinType::Left | JoinType::Full);
        let is_right_outer = matches!(self.join_type, JoinType::Right | JoinType::Full);
        let is_cross = matches!(self.join_type, JoinType::Cross);

        // Phase 2: Return unmatched right rows (for RIGHT/FULL OUTER)
        if self.returning_unmatched_right {
            while self.unmatched_right_idx < self.right_rows.len() {
                let idx = self.unmatched_right_idx;
                self.unmatched_right_idx += 1;

                if !self.right_matched[idx] {
                    let null_left = self.null_left_row();
                    let right_row = &self.right_rows[idx];
                    let combined = self.combine(&null_left, right_row);
                    return Ok(Some(RowRef::Owned(combined)));
                }
            }
            return Ok(None);
        }

        // Phase 1: Nested loop join
        loop {
            // Check if left is exhausted
            if self.left_exhausted {
                // Switch to returning unmatched right rows if needed
                if is_right_outer {
                    self.returning_unmatched_right = true;
                    self.unmatched_right_idx = 0;
                    return self.next();
                }
                return Ok(None);
            }

            let left_row = match &self.current_left_row {
                Some(row) => row,
                None => {
                    // Try to get next left row
                    if !self.advance_left()? {
                        if is_right_outer {
                            self.returning_unmatched_right = true;
                            self.unmatched_right_idx = 0;
                            return self.next();
                        }
                        return Ok(None);
                    }
                    self.current_left_row.as_ref().unwrap()
                }
            };

            // Try to find a match in right rows
            while self.current_right_idx < self.right_rows.len() {
                let right_idx = self.current_right_idx;
                self.current_right_idx += 1;

                let right_row = &self.right_rows[right_idx];

                // Check join condition
                let matches = if let Some(ref filter) = self.filter {
                    filter.matches(left_row, right_row)
                } else {
                    // CROSS JOIN or no condition
                    is_cross || self.condition.is_none()
                };

                if matches {
                    self.left_had_match = true;

                    // Mark right row as matched for OUTER joins
                    if is_right_outer {
                        self.right_matched[right_idx] = true;
                    }

                    let combined = self.combine(left_row, right_row);
                    return Ok(Some(RowRef::Owned(combined)));
                }
            }

            // Exhausted right side for current left row
            // Handle LEFT/FULL OUTER: emit left row with NULLs if no match
            if is_left_outer && !self.left_had_match {
                let left_row = self.current_left_row.take().unwrap();
                self.advance_left()?;
                let null_right = self.null_right_row();
                // Use owned variant - both rows are owned and won't be used again
                let combined = Row::from_combined_owned(left_row, null_right);
                return Ok(Some(RowRef::Owned(combined)));
            }

            // Move to next left row
            if !self.advance_left()? {
                // Left exhausted - handle unmatched right rows
                if is_right_outer {
                    self.returning_unmatched_right = true;
                    self.unmatched_right_idx = 0;
                    return self.next();
                }
                return Ok(None);
            }
        }
    }

    fn close(&mut self) -> Result<()> {
        self.left.close()?;
        self.right.close()?;
        Ok(())
    }

    fn schema(&self) -> &[ColumnInfo] {
        &self.schema
    }

    fn estimated_rows(&self) -> Option<usize> {
        let left_est = self.left.estimated_rows()?;
        let right_est = self.right.estimated_rows()?;

        Some(match self.join_type {
            JoinType::Inner => (left_est * right_est) / 10, // Assume 10% selectivity
            JoinType::Left => left_est,
            JoinType::Right => right_est,
            JoinType::Full => left_est + right_est,
            JoinType::Cross => left_est * right_est,
            JoinType::Semi => left_est.min(right_est),
            JoinType::Anti => left_est,
        })
    }

    fn name(&self) -> &str {
        match self.join_type {
            JoinType::Inner => "NestedLoop (INNER)",
            JoinType::Left => "NestedLoop (LEFT)",
            JoinType::Right => "NestedLoop (RIGHT)",
            JoinType::Full => "NestedLoop (FULL)",
            JoinType::Cross => "NestedLoop (CROSS)",
            JoinType::Semi => "NestedLoop (SEMI)",
            JoinType::Anti => "NestedLoop (ANTI)",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::executor::operator::MaterializedOperator;
    use crate::parser::ast::{Identifier, InfixExpression};
    use crate::parser::token::{Position, Token, TokenType};

    fn make_rows(data: Vec<Vec<i64>>) -> Vec<Row> {
        data.into_iter()
            .map(|vals| Row::from_values(vals.into_iter().map(Value::integer).collect()))
            .collect()
    }

    fn make_operator(data: Vec<Vec<i64>>, cols: Vec<&str>) -> Box<dyn Operator> {
        let rows = make_rows(data);
        let schema = cols.into_iter().map(ColumnInfo::new).collect();
        Box::new(MaterializedOperator::new(rows, schema))
    }

    fn collect_results(op: &mut dyn Operator) -> Result<Vec<Row>> {
        let mut results = Vec::new();
        op.open()?;
        while let Some(row_ref) = op.next()? {
            results.push(row_ref.into_owned());
        }
        op.close()?;
        Ok(results)
    }

    fn make_eq_condition(left_col: &str, right_col: &str) -> Expression {
        Expression::Infix(InfixExpression::new(
            Token::new(TokenType::Operator, "=", Position::default()),
            Box::new(Expression::Identifier(Identifier::new(
                Token::new(TokenType::Identifier, left_col, Position::default()),
                left_col.to_string(),
            ))),
            "=".to_string(),
            Box::new(Expression::Identifier(Identifier::new(
                Token::new(TokenType::Identifier, right_col, Position::default()),
                right_col.to_string(),
            ))),
        ))
    }

    #[test]
    fn test_inner_nested_loop() {
        let left = make_operator(
            vec![vec![1, 10], vec![2, 20], vec![3, 30]],
            vec!["left_id", "value"],
        );
        let right = make_operator(vec![vec![1, 100], vec![3, 300]], vec!["right_id", "data"]);

        let condition = make_eq_condition("left_id", "right_id");

        let mut join = NestedLoopJoinOperator::new(left, right, JoinType::Inner, Some(condition));

        let results = collect_results(&mut join).unwrap();

        // Should have 2 matches: id=1 and id=3
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_cross_join() {
        let left = make_operator(vec![vec![1], vec![2]], vec!["a"]);
        let right = make_operator(vec![vec![10], vec![20]], vec!["b"]);

        let mut join = NestedLoopJoinOperator::new(left, right, JoinType::Cross, None);

        let results = collect_results(&mut join).unwrap();

        // 2 x 2 = 4 rows
        assert_eq!(results.len(), 4);
    }

    #[test]
    fn test_left_nested_loop() {
        let left = make_operator(
            vec![vec![1, 10], vec![2, 20], vec![3, 30]],
            vec!["left_id", "value"],
        );
        let right = make_operator(vec![vec![1, 100]], vec!["right_id", "data"]);

        let condition = make_eq_condition("left_id", "right_id");

        let mut join = NestedLoopJoinOperator::new(left, right, JoinType::Left, Some(condition));

        let results = collect_results(&mut join).unwrap();

        // All 3 left rows preserved
        assert_eq!(results.len(), 3);

        // id=2 and id=3 should have NULLs
        let row2 = results
            .iter()
            .find(|r| r.get(0) == Some(&Value::integer(2)))
            .unwrap();
        assert!(row2.get(2).unwrap().is_null());
    }

    #[test]
    fn test_right_nested_loop() {
        let left = make_operator(vec![vec![1, 10]], vec!["left_id", "value"]);
        let right = make_operator(
            vec![vec![1, 100], vec![2, 200], vec![3, 300]],
            vec!["right_id", "data"],
        );

        let condition = make_eq_condition("left_id", "right_id");

        let mut join = NestedLoopJoinOperator::new(left, right, JoinType::Right, Some(condition));

        let results = collect_results(&mut join).unwrap();

        // All 3 right rows preserved
        assert_eq!(results.len(), 3);
    }
}