sql_cli/data/batch_window_evaluator.rs
1//! Batch evaluation system for window functions
2//!
3//! This module provides optimized batch evaluation of window functions
4//! to eliminate per-row overhead and improve performance significantly.
5
6use std::collections::HashMap;
7use std::sync::Arc;
8
9use crate::sql::parser::ast::{SqlExpression, WindowSpec};
10use crate::sql::window_context::WindowContext;
11
12/// Specification for a single window function in a query
13///
14/// This structure captures all metadata needed to evaluate
15/// a window function and place its results in the output table
16#[derive(Debug, Clone)]
17pub struct WindowFunctionSpec {
18 /// The window specification (PARTITION BY, ORDER BY, frame)
19 pub spec: WindowSpec,
20
21 /// Function name (e.g., "LAG", "LEAD", "ROW_NUMBER")
22 pub function_name: String,
23
24 /// Arguments to the window function
25 pub args: Vec<SqlExpression>,
26
27 /// Column index in the output table where results should be placed
28 pub output_column_index: usize,
29}
30
31/// Batch evaluator for window functions
32///
33/// This structure manages batch evaluation of window functions
34/// to avoid repeated context lookups and per-row overhead
35pub struct BatchWindowEvaluator {
36 /// All window function specifications in the query
37 specs: Vec<WindowFunctionSpec>,
38
39 /// Pre-created window contexts, keyed by spec hash
40 contexts: HashMap<u64, Arc<WindowContext>>,
41}
42
43impl BatchWindowEvaluator {
44 /// Create a new batch window evaluator
45 pub fn new() -> Self {
46 Self {
47 specs: Vec::new(),
48 contexts: HashMap::new(),
49 }
50 }
51
52 // Additional methods will be added in subsequent steps:
53 // - add_spec() - Add a window function specification
54 // - create_contexts() - Pre-create all window contexts
55 // - evaluate_batch() - Batch evaluate all window functions
56 // - get_results() - Retrieve results for a specific row
57}
58
59impl Default for BatchWindowEvaluator {
60 fn default() -> Self {
61 Self::new()
62 }
63}