Skip to main content

cynos_query/executor/
scan.rs

1//! Table and index scan executors.
2
3use crate::executor::Relation;
4use alloc::rc::Rc;
5use alloc::string::String;
6use alloc::vec::Vec;
7use cynos_core::Row;
8
9/// Table scan executor - scans all rows from a table.
10pub struct TableScanExecutor {
11    table: String,
12    rows: Vec<Rc<Row>>,
13}
14
15impl TableScanExecutor {
16    /// Creates a new table scan executor.
17    pub fn new(table: impl Into<String>, rows: Vec<Rc<Row>>) -> Self {
18        Self {
19            table: table.into(),
20            rows,
21        }
22    }
23
24    /// Executes the scan and returns the relation.
25    pub fn execute(&self) -> Relation {
26        Relation::from_rows(self.rows.clone(), alloc::vec![self.table.clone()])
27    }
28}
29
30/// Index scan executor - scans rows using an index.
31pub struct IndexScanExecutor {
32    table: String,
33    rows: Vec<Rc<Row>>,
34}
35
36impl IndexScanExecutor {
37    /// Creates a new index scan executor with pre-fetched rows.
38    pub fn new(table: impl Into<String>, rows: Vec<Rc<Row>>) -> Self {
39        Self {
40            table: table.into(),
41            rows,
42        }
43    }
44
45    /// Executes the scan and returns the relation.
46    pub fn execute(&self) -> Relation {
47        Relation::from_rows(self.rows.clone(), alloc::vec![self.table.clone()])
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use cynos_core::Value;
55    use alloc::vec;
56
57    #[test]
58    fn test_table_scan() {
59        let rows = vec![
60            Rc::new(Row::new(1, vec![Value::Int64(1), Value::String("Alice".into())])),
61            Rc::new(Row::new(2, vec![Value::Int64(2), Value::String("Bob".into())])),
62        ];
63        let executor = TableScanExecutor::new("users", rows);
64        let result = executor.execute();
65
66        assert_eq!(result.len(), 2);
67        assert_eq!(result.tables(), &["users"]);
68    }
69
70    #[test]
71    fn test_index_scan() {
72        let rows = vec![Rc::new(Row::new(1, vec![Value::Int64(42)]))];
73        let executor = IndexScanExecutor::new("users", rows);
74        let result = executor.execute();
75
76        assert_eq!(result.len(), 1);
77    }
78}