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::reject_unsupported_query_features(&self.0)?;
20        predicate::validate(schema, &self.0)?;
21
22        Ok(normalize(&self.0))
23    }
24}
25
26///
27/// SortExpr
28/// Schema-agnostic sort expression for dynamic query input.
29/// Lowered into a validated order spec at the intent boundary.
30///
31
32#[derive(Clone, Debug)]
33pub struct SortExpr {
34    pub fields: Vec<(String, OrderDirection)>,
35}
36
37impl SortExpr {
38    /// Lower the sort expression into a validated order spec for the provided schema.
39    pub fn lower_with(&self, schema: &SchemaInfo) -> Result<OrderSpec, SortLowerError> {
40        let spec = OrderSpec {
41            fields: self.fields.clone(),
42        };
43
44        validate_order(schema, &spec)?;
45
46        Ok(spec)
47    }
48}
49
50///
51/// SortLowerError
52/// Errors returned when lowering sort expressions into order specs.
53///
54
55#[derive(Debug, ThisError)]
56pub enum SortLowerError {
57    #[error("{0}")]
58    Validate(#[from] ValidateError),
59
60    #[error("{0}")]
61    Plan(#[from] PlanError),
62}