Skip to main content

sochdb_query/executor/
filter.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3//! Filter operator (WHERE clause).
4
5use crate::sql::ast::Expr;
6use super::eval::eval_predicate;
7use super::node::PlanNode;
8use super::types::{Row, Schema};
9use sochdb_core::Result;
10
11/// Filter operator: passes through only rows satisfying the predicate.
12///
13/// ```text
14/// Filter(predicate)
15///   └── input
16/// ```
17pub struct FilterNode {
18    input: Box<dyn PlanNode>,
19    predicate: Expr,
20}
21
22impl FilterNode {
23    pub fn new(input: Box<dyn PlanNode>, predicate: Expr) -> Self {
24        Self { input, predicate }
25    }
26}
27
28impl PlanNode for FilterNode {
29    fn schema(&self) -> &Schema {
30        self.input.schema()
31    }
32
33    fn next(&mut self) -> Result<Option<Row>> {
34        loop {
35            match self.input.next()? {
36                Some(row) => {
37                    if eval_predicate(&self.predicate, &row, self.input.schema())? {
38                        return Ok(Some(row));
39                    }
40                    // Row didn't match predicate, continue to next
41                }
42                None => return Ok(None),
43            }
44        }
45    }
46
47    fn reset(&mut self) -> Result<()> {
48        self.input.reset()
49    }
50}