use crate::db::query::plan::{OrderDirection, PlanError, validate::validate_order};
use crate::db::{
predicate::{Predicate, normalize, normalize_enum_literals},
query::plan::OrderSpec,
schema::{SchemaInfo, ValidateError, reject_unsupported_query_features, validate},
};
use thiserror::Error as ThisError;
#[derive(Clone, Debug)]
pub struct FilterExpr(pub Predicate);
impl FilterExpr {
pub(crate) fn lower_with(&self, schema: &SchemaInfo) -> Result<Predicate, ValidateError> {
let normalized_enum_literals = normalize_enum_literals(schema, &self.0)?;
reject_unsupported_query_features(&normalized_enum_literals)?;
validate(schema, &normalized_enum_literals)?;
Ok(normalize(&normalized_enum_literals))
}
}
#[derive(Clone, Debug)]
pub struct SortExpr {
fields: Vec<(String, OrderDirection)>,
}
impl SortExpr {
#[must_use]
pub const fn new(fields: Vec<(String, OrderDirection)>) -> Self {
Self { fields }
}
#[must_use]
pub fn fields(&self) -> &[(String, OrderDirection)] {
&self.fields
}
pub(crate) fn lower_with(&self, schema: &SchemaInfo) -> Result<OrderSpec, SortLowerError> {
let spec = OrderSpec {
fields: self.fields.clone(),
};
validate_order(schema, &spec)?;
Ok(spec)
}
}
#[derive(Debug, ThisError)]
pub(crate) enum SortLowerError {
#[error("{0}")]
Validate(Box<ValidateError>),
#[error("{0}")]
Plan(Box<PlanError>),
}
impl From<PlanError> for SortLowerError {
fn from(err: PlanError) -> Self {
Self::Plan(Box::new(err))
}
}
impl From<ValidateError> for SortLowerError {
fn from(err: ValidateError) -> Self {
Self::Validate(Box::new(err))
}
}