Skip to main content

grafeo_core/execution/operators/
single_row.rs

1//! Single row operator for producing a single empty row.
2//!
3//! This is used for queries like `UNWIND [1,2,3] AS x RETURN x` that don't have
4//! a MATCH clause but need an initial row to start with.
5
6use super::{Operator, OperatorResult};
7use crate::execution::DataChunk;
8
9/// An operator that produces exactly one empty row.
10///
11/// This is useful for UNWIND clauses that operate on literal lists
12/// without a prior MATCH clause.
13pub struct SingleRowOperator {
14    /// Whether the single row has been produced.
15    produced: bool,
16}
17
18impl SingleRowOperator {
19    /// Creates a new single row operator.
20    #[must_use]
21    pub fn new() -> Self {
22        Self { produced: false }
23    }
24}
25
26impl Default for SingleRowOperator {
27    fn default() -> Self {
28        Self::new()
29    }
30}
31
32impl Operator for SingleRowOperator {
33    fn next(&mut self) -> OperatorResult {
34        if self.produced {
35            return Ok(None);
36        }
37
38        self.produced = true;
39
40        // Create a single row with no columns
41        let mut chunk = DataChunk::with_capacity(&[], 1);
42        chunk.set_count(1);
43
44        Ok(Some(chunk))
45    }
46
47    fn reset(&mut self) {
48        self.produced = false;
49    }
50
51    fn name(&self) -> &'static str {
52        "SingleRow"
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_single_row_operator() {
62        let mut op = SingleRowOperator::new();
63
64        // First call produces one row
65        let chunk = op.next().unwrap();
66        assert!(chunk.is_some());
67        let chunk = chunk.unwrap();
68        assert_eq!(chunk.row_count(), 1);
69
70        // Second call produces None
71        let chunk = op.next().unwrap();
72        assert!(chunk.is_none());
73
74        // After reset, produces one row again
75        op.reset();
76        let chunk = op.next().unwrap();
77        assert!(chunk.is_some());
78    }
79}