Skip to main content

icydb_core/db/query/
expr.rs

1use crate::db::query::{
2    plan::{OrderDirection, OrderSpec, PlanError, validate::validate_order},
3    predicate::{self, Predicate, SchemaInfo, ValidateError, normalize},
4};
5use thiserror::Error as ThisError;
6
7///
8/// FilterExpr
9/// Schema-agnostic filter expression for dynamic query input.
10/// Lowered into a validated predicate at the intent boundary.
11///
12
13#[derive(Clone, Debug)]
14pub struct FilterExpr(pub Predicate);
15
16impl FilterExpr {
17    /// Lower the filter expression into a validated predicate for the provided schema.
18    pub fn lower_with(&self, schema: &SchemaInfo) -> Result<Predicate, ValidateError> {
19        predicate::validate(schema, &self.0)?;
20
21        Ok(normalize(&self.0))
22    }
23}
24
25///
26/// SortExpr
27/// Schema-agnostic sort expression for dynamic query input.
28/// Lowered into a validated order spec at the intent boundary.
29///
30
31#[derive(Clone, Debug)]
32pub struct SortExpr {
33    pub fields: Vec<(String, OrderDirection)>,
34}
35
36impl SortExpr {
37    /// Lower the sort expression into a validated order spec for the provided schema.
38    pub fn lower_with(&self, schema: &SchemaInfo) -> Result<OrderSpec, SortLowerError> {
39        let spec = OrderSpec {
40            fields: self.fields.clone(),
41        };
42
43        validate_order(schema, &spec)?;
44
45        Ok(spec)
46    }
47}
48
49///
50/// SortLowerError
51/// Errors returned when lowering sort expressions into order specs.
52///
53
54#[derive(Debug, ThisError)]
55pub enum SortLowerError {
56    #[error("{0}")]
57    Validate(#[from] ValidateError),
58
59    #[error("{0}")]
60    Plan(#[from] PlanError),
61}