mod parse;
mod render;
mod validate;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Ast {
pub match_aliases: Vec<String>,
pub match_alias: String,
pub where_clause: Option<WhereClause>,
pub where_predicate: Option<VectorPredicate>,
pub where_bitmap_predicate: Option<BitmapPredicate>,
pub with_items: Vec<ProjectionItem>,
pub return_items: Vec<ProjectionItem>,
pub return_alias: String,
pub limit: Option<u64>,
}
impl Ast {
pub fn to_json_pretty(&self) -> String {
render::ast_to_json_pretty(self)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProjectionItem {
Identifier(String),
Function {
name: String,
argument: String,
alias: Option<String>,
},
}
impl ProjectionItem {
fn output_name(&self) -> Option<String> {
match self {
Self::Identifier(value) => Some(value.clone()),
Self::Function { alias, .. } => alias.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WhereClause {
Function {
function: String,
args: Vec<String>,
operator: String,
threshold: String,
},
Comparison {
left: String,
operator: String,
right: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VectorPredicate {
pub function: String,
pub target: String,
pub param: String,
pub operator: String,
pub threshold: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BitmapPredicate {
pub function: String,
pub index_name: String,
pub value_key: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueryError {
InvalidSyntax(String),
InvalidSemantic(String),
}
pub type Result<T> = std::result::Result<T, QueryError>;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Catalog;
#[derive(Debug, Clone, PartialEq)]
pub struct ScalarComparison {
pub field: String,
pub operator: String,
pub value: f64,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TypedAst {
pub ast: Ast,
pub scalar_predicate: Option<ScalarComparison>,
}
pub fn parse(query: &str) -> Result<Ast> {
parse::parse(query)
}
pub fn validate(ast: &Ast, catalog: &Catalog) -> Result<TypedAst> {
validate::validate(ast, catalog)
}
#[cfg(test)]
mod tests;