llkv-plan 0.8.5-alpha

Query planner for the LLKV toolkit.
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
//! Generic iterative traversal utilities for AST-like structures.
//!
//! This module provides postorder traversal patterns that avoid stack overflow
//! on deeply nested expression trees. The approach uses explicit work and result
//! stacks instead of recursion, which is critical for handling complex SQL queries
//! (e.g., 55,000+ line files with deeply nested WHERE clauses).
//!
//! # Two Approaches
//!
//! ## 1. Trait-Based Traversal (for borrowed trees)
//!
//! Use [`Traversable`] and [`traverse_postorder`] when you have a tree of borrowed
//! nodes and want to compute a result without consuming the tree.
//!
//! ## 2. Manual Frame Pattern (for owned/consumed trees)
//!
//! When transforming owned trees (consuming input, producing output), use the
//! manual Frame pattern with these helpers:
//!
//! - Define a `Frame` enum with `Enter(InputNode)` and `Exit*` variants
//! - Use `Vec<Frame>` as work_stack, `Vec<OutputNode>` as result_stack
//! - Process frames in a loop, building results in postorder
//!
//! See existing implementations in:
//! - `llkv-sql/src/sql_engine.rs`: `translate_condition_with_context`
//! - `llkv-executor/src/translation/expression.rs`: `translate_predicate_with`
//!
//! # Example (Trait-Based)
//!
//! ```
//! use llkv_plan::traversal::{Traversable, traverse_postorder};
//! use llkv_result::Result as LlkvResult;
//!
//! enum MyExpr {
//!     Add(Box<MyExpr>, Box<MyExpr>),
//!     Value(i32),
//! }
//!
//! impl Traversable for MyExpr {
//!     type Output = i32;
//!
//!     fn visit_children(&self) -> LlkvResult<Vec<&Self>> {
//!         match self {
//!             MyExpr::Add(left, right) => Ok(vec![left.as_ref(), right.as_ref()]),
//!             MyExpr::Value(_) => Ok(vec![]),
//!         }
//!     }
//!
//!     fn construct(&self, children: Vec<i32>) -> LlkvResult<i32> {
//!         match self {
//!             MyExpr::Add(_, _) => Ok(children[0] + children[1]),
//!             MyExpr::Value(v) => Ok(*v),
//!         }
//!     }
//! }
//!
//! let expr = MyExpr::Add(
//!     Box::new(MyExpr::Value(2)),
//!     Box::new(MyExpr::Value(3)),
//! );
//! assert_eq!(traverse_postorder(&expr).unwrap(), 5);
//! ```
//!
//! # Example (TransformFrame Pattern)
//!
//! ```
//! use llkv_plan::TransformFrame;
//!
//! #[derive(Clone)]
//! enum InputExpr {
//!     Binary { left: Box<InputExpr>, op: BinaryOp, right: Box<InputExpr> },
//!     Unary { op: UnaryOp, expr: Box<InputExpr> },
//!     Value(i32),
//! }
//!
//! #[derive(Clone)]
//! enum BinaryOp { Add, Subtract }
//!
//! #[derive(Clone)]
//! enum UnaryOp { Negate }
//!
//! enum OutputExpr {
//!     Binary { left: Box<OutputExpr>, op: BinaryOp, right: Box<OutputExpr> },
//!     Unary { op: UnaryOp, expr: Box<OutputExpr> },
//!     Value(i32),
//! }
//!
//! enum ExprExitContext {
//!     Binary(BinaryOp),
//!     Unary,
//! }
//!
//! type ExprFrame<'a> = TransformFrame<'a, InputExpr, OutputExpr, ExprExitContext>;
//!
//! fn transform_expr(root_expr: &InputExpr) -> OutputExpr {
//!     let mut work_stack = vec![ExprFrame::Enter(root_expr)];
//!     let mut result_stack = Vec::new();
//!
//!     while let Some(frame) = work_stack.pop() {
//!         match frame {
//!             ExprFrame::Enter(node) => match node {
//!                 InputExpr::Binary { left, op, right } => {
//!                     work_stack.push(ExprFrame::Exit(ExprExitContext::Binary(op.clone())));
//!                     work_stack.push(ExprFrame::Enter(right));
//!                     work_stack.push(ExprFrame::Enter(left));
//!                 }
//!                 InputExpr::Unary { op: _, expr } => {
//!                     work_stack.push(ExprFrame::Exit(ExprExitContext::Unary));
//!                     work_stack.push(ExprFrame::Enter(expr));
//!                 }
//!                 InputExpr::Value(v) => {
//!                     work_stack.push(ExprFrame::Leaf(OutputExpr::Value(*v)));
//!                 }
//!             },
//!             ExprFrame::Exit(exit_context) => match exit_context {
//!                 ExprExitContext::Binary(op) => {
//!                     let right = result_stack.pop().unwrap();
//!                     let left = result_stack.pop().unwrap();
//!                     result_stack.push(OutputExpr::Binary {
//!                         left: Box::new(left),
//!                         op,
//!                         right: Box::new(right),
//!                     });
//!                 }
//!                 ExprExitContext::Unary => {
//!                     let expr = result_stack.pop().unwrap();
//!                     result_stack.push(OutputExpr::Unary {
//!                         op: UnaryOp::Negate,
//!                         expr: Box::new(expr),
//!                     });
//!                 }
//!             },
//!             ExprFrame::Leaf(output) => {
//!                 result_stack.push(output);
//!             }
//!         }
//!     }
//!
//!     result_stack.pop().unwrap()
//! }
//! # transform_expr(&InputExpr::Value(42));
//! ```

use llkv_result::Result as LlkvResult;

/// Internal frame type used by [`traverse_postorder`] for borrowed tree traversal.
///
/// This is distinct from [`TransformFrame`] because it's used internally by the
/// trait-based traversal function and tracks both the node reference and expected
/// child count for the Exit variant.
///
/// Not exported - users should use [`TransformFrame`] for custom traversals.
enum TraversalFrame<'a, T> {
    Enter(&'a T),
    Exit(&'a T, usize), // node and expected child count
}

/// A frame in the traversal work stack for tree transformations.
///
/// Used in manual iterative traversals that consume input nodes and produce
/// transformed output nodes. This enum provides the common structure while
/// allowing implementations to define custom exit variants for operation-specific
/// context (e.g., which binary operator triggered the exit).
///
/// The pattern avoids stack overflow on deeply nested trees (50k+ nodes) by using
/// explicit work and result stacks instead of recursion.
///
/// # Type Parameters
/// * `Input` - The input node type being consumed during traversal
/// * `Output` - The result type being built on the result stack
/// * `ExitContext` - Custom enum carrying operation-specific exit state
///
/// # Traversal Pattern
///
/// ```rust
/// use llkv_plan::TransformFrame;
/// # use llkv_result::Result;
/// # type SqlExpr = String;
/// # type OutputExpr = i32;
/// # #[derive(Clone)]
/// # enum BinaryOp { Add, Subtract }
///
/// // Define operation-specific exit variants
/// enum ScalarExitContext {
///     BinaryOp { op: BinaryOp },
///     UnaryMinus,
/// }
///
/// type ScalarTransformFrame<'a> = TransformFrame<'a, SqlExpr, OutputExpr, ScalarExitContext>;
///
/// fn transform_scalar(expr: &SqlExpr) -> Result<OutputExpr> {
///     let mut work_stack: Vec<ScalarTransformFrame> = vec![ScalarTransformFrame::Enter(expr)];
///     let mut result_stack: Vec<OutputExpr> = Vec::new();
///
///     while let Some(frame) = work_stack.pop() {
///         match frame {
///             ScalarTransformFrame::Enter(node) => {
///                 // Decompose node, push exit frame, then push children
/// #               work_stack.push(ScalarTransformFrame::Leaf(42));
///             }
///             ScalarTransformFrame::Exit(ScalarExitContext::BinaryOp { op }) => {
///                 // Pop children from result_stack, combine, push result
/// #               let right = result_stack.pop().unwrap();
/// #               let left = result_stack.pop().unwrap();
/// #               result_stack.push(left + right);
///             }
///             ScalarTransformFrame::Exit(ScalarExitContext::UnaryMinus) => {
///                 // Pop single child, transform, push result
/// #               let val = result_stack.pop().unwrap();
/// #               result_stack.push(-val);
///             }
///             ScalarTransformFrame::Leaf(output) => {
///                 result_stack.push(output);
///             }
///         }
///     }
///
///     Ok(result_stack.pop().unwrap())
/// }
/// # transform_scalar(&"test".to_string()).unwrap();
/// ```
///
/// # See Also
///
/// Concrete implementations using this pattern:
/// - `llkv-sql/src/sql_engine.rs`: `translate_condition_with_context`, `translate_scalar`
/// - `llkv-executor/src/translation/expression.rs`: `translate_predicate_with`
pub enum TransformFrame<'a, Input, Output, ExitContext> {
    /// Begin visiting an input node (descend to children).
    Enter(&'a Input),

    /// Complete processing of a node (combine child results).
    ///
    /// The `ExitContext` carries operation-specific state needed to combine
    /// child results into the parent node's result. For example, in arithmetic
    /// expression evaluation, this might carry which operator to apply.
    Exit(ExitContext),

    /// A leaf node that has been fully transformed to output.
    ///
    /// Used when a node can be immediately converted without further traversal
    /// (e.g., literals, pre-resolved identifiers).
    Leaf(Output),
}

/// Trait for types that support iterative postorder traversal.
///
/// Implementors define:
/// 1. How to decompose a node into children (in `visit_children`)
/// 2. How to reconstruct a result from child results (in `construct`)
///
/// The traversal guarantees postorder: children are fully processed before
/// their parent's `construct` is called.
pub trait Traversable: Sized {
    /// The result type produced by traversing this tree.
    type Output;

    /// Visit each child of this node.
    ///
    /// Called when first encountering a node. Implementations should:
    /// - Return an iterator over child references
    /// - Return an empty iterator for leaf nodes
    ///
    /// # Returns
    /// An iterator over references to child nodes, or an error if the node is malformed.
    fn visit_children(&self) -> LlkvResult<Vec<&Self>>;

    /// Reconstruct this node's result from processed children.
    ///
    /// Called after all children have been processed. The `children` vec
    /// contains child results in the same order they were visited.
    ///
    /// # Arguments
    /// * `children` - Results from child nodes, in visit order
    ///
    /// # Returns
    /// The reconstructed result for this node.
    fn construct(&self, children: Vec<Self::Output>) -> LlkvResult<Self::Output>;
}

/// Performs iterative postorder traversal of a tree structure.
///
/// This function avoids stack overflow by using explicit work and result stacks
/// instead of recursion. It's designed for deeply nested ASTs common in complex
/// SQL queries.
///
/// # Type Parameters
/// * `T` - The node type (must implement [`Traversable`])
///
/// # Arguments
/// * `root` - The root node to traverse
///
/// # Returns
/// The result produced by traversing the entire tree.
///
/// # Errors
/// Returns errors from `visit_children` or `construct` implementations, or if the
/// traversal encounters an inconsistent state (e.g., result stack underflow).
///
/// # Performance
/// Stack usage is O(tree depth), memory usage is O(tree depth + node count).
/// For very deep trees (50k+ nesting), thread stack size should be >= 16MB.
pub fn traverse_postorder<T>(root: &T) -> LlkvResult<T::Output>
where
    T: Traversable,
{
    let mut work_stack: Vec<TraversalFrame<T>> = vec![TraversalFrame::Enter(root)];
    let mut result_stack: Vec<T::Output> = Vec::new();

    while let Some(frame) = work_stack.pop() {
        match frame {
            TraversalFrame::Enter(node) => {
                let children = node.visit_children()?;
                let child_count = children.len();

                // Schedule exit frame first
                work_stack.push(TraversalFrame::Exit(node, child_count));

                // Then push children in reverse order for correct postorder
                for child in children.into_iter().rev() {
                    work_stack.push(TraversalFrame::Enter(child));
                }
            }
            TraversalFrame::Exit(node, child_count) => {
                // Collect child results from result stack
                if result_stack.len() < child_count {
                    return Err(llkv_result::Error::Internal(
                        "traverse_postorder: result stack underflow".into(),
                    ));
                }

                let start = result_stack.len() - child_count;
                let child_results: Vec<T::Output> = result_stack.drain(start..).collect();

                // Process this node with its children's results
                let output = node.construct(child_results)?;
                result_stack.push(output);
            }
        }
    }

    // Should have exactly one result
    if result_stack.len() != 1 {
        return Err(llkv_result::Error::Internal(format!(
            "traverse_postorder: expected 1 result, got {}",
            result_stack.len()
        )));
    }

    result_stack.pop().ok_or_else(|| {
        llkv_result::Error::Internal("traverse_postorder: empty result stack".into())
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    // Simple expression tree for testing
    #[derive(Debug, Clone, PartialEq)]
    enum TestExpr {
        Add(Box<TestExpr>, Box<TestExpr>),
        Mul(Box<TestExpr>, Box<TestExpr>),
        Value(i32),
    }

    impl Traversable for TestExpr {
        type Output = i32;

        fn visit_children(&self) -> LlkvResult<Vec<&Self>> {
            match self {
                TestExpr::Add(left, right) | TestExpr::Mul(left, right) => {
                    Ok(vec![left.as_ref(), right.as_ref()])
                }
                TestExpr::Value(_) => Ok(vec![]),
            }
        }

        fn construct(&self, children: Vec<i32>) -> LlkvResult<i32> {
            match self {
                TestExpr::Add(_, _) => Ok(children[0] + children[1]),
                TestExpr::Mul(_, _) => Ok(children[0] * children[1]),
                TestExpr::Value(v) => Ok(*v),
            }
        }
    }

    #[test]
    fn test_simple_evaluation() {
        // 2 + 3 = 5
        let expr = TestExpr::Add(Box::new(TestExpr::Value(2)), Box::new(TestExpr::Value(3)));
        assert_eq!(traverse_postorder(&expr).unwrap(), 5);
    }

    #[test]
    fn test_nested_evaluation() {
        // (2 + 3) * 4 = 20
        let expr = TestExpr::Mul(
            Box::new(TestExpr::Add(
                Box::new(TestExpr::Value(2)),
                Box::new(TestExpr::Value(3)),
            )),
            Box::new(TestExpr::Value(4)),
        );
        assert_eq!(traverse_postorder(&expr).unwrap(), 20);
    }

    #[test]
    fn test_deeply_nested() {
        // Build a deeply nested expression: 1 + 1 + 1 + ... (1000 times)
        let mut expr = TestExpr::Value(1);
        for _ in 0..1000 {
            expr = TestExpr::Add(Box::new(expr), Box::new(TestExpr::Value(1)));
        }
        assert_eq!(traverse_postorder(&expr).unwrap(), 1001);
    }
}