use crate::types::Value;
#[derive(Debug, Clone)]
pub enum Statement {
Select(SelectStmt),
Insert(InsertStmt),
Update(UpdateStmt),
Delete(DeleteStmt),
CreateTable(CreateTableStmt),
CreateIndex(CreateIndexStmt),
DropTable(DropTableStmt),
DropIndex(DropIndexStmt),
ShowTables,
DescribeTable(String), }
#[derive(Debug, Clone)]
pub struct SelectStmt {
pub distinct: bool, pub columns: Vec<SelectColumn>,
pub from: TableRef, pub where_clause: Option<Expr>,
pub group_by: Option<Vec<String>>, pub having: Option<Expr>, pub order_by: Option<Vec<OrderByExpr>>,
pub limit: Option<usize>,
pub offset: Option<usize>,
pub latest_by: Option<Vec<String>>, }
#[derive(Debug, Clone)]
pub enum TableRef {
Table { name: String, alias: Option<String> },
Join {
left: Box<TableRef>,
right: Box<TableRef>,
join_type: JoinType,
on_condition: Expr,
},
Subquery {
query: Box<SelectStmt>,
alias: String, },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinType {
Inner,
Left,
Right,
Full,
}
#[derive(Debug, Clone)]
pub enum SelectColumn {
Star, Column(String), ColumnWithAlias(String, String), Expr(Expr, Option<String>), }
#[derive(Debug, Clone)]
pub struct OrderByExpr {
pub expr: Expr,
pub asc: bool, }
#[derive(Debug, Clone)]
pub struct InsertStmt {
pub table: String,
pub columns: Option<Vec<String>>, pub values: Vec<Vec<Expr>>, }
#[derive(Debug, Clone)]
pub struct UpdateStmt {
pub table: String,
pub assignments: Vec<(String, Expr)>, pub where_clause: Option<Expr>,
}
#[derive(Debug, Clone)]
pub struct DeleteStmt {
pub table: String,
pub where_clause: Option<Expr>,
}
#[derive(Debug, Clone)]
pub struct CreateTableStmt {
pub table: String,
pub columns: Vec<ColumnDef>,
}
#[derive(Debug, Clone)]
pub struct ColumnDef {
pub name: String,
pub data_type: DataType,
pub nullable: bool,
pub primary_key: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DataType {
Integer,
Float,
Text,
Boolean,
Timestamp,
Vector(Option<usize>), Geometry,
}
#[derive(Debug, Clone)]
pub struct CreateIndexStmt {
pub index_name: String,
pub table: String,
pub column: String,
pub index_type: IndexType,
}
#[derive(Debug, Clone)]
pub enum IndexType {
BTree,
Column, Text,
Vector,
Spatial,
Timestamp,
}
#[derive(Debug, Clone)]
pub struct DropTableStmt {
pub table: String,
}
#[derive(Debug, Clone)]
pub struct DropIndexStmt {
pub index_name: String,
}
#[derive(Debug, Clone)]
pub enum Expr {
Column(String),
Literal(Value),
BinaryOp {
left: Box<Expr>,
op: BinaryOperator,
right: Box<Expr>,
},
UnaryOp {
op: UnaryOperator,
expr: Box<Expr>,
},
FunctionCall {
name: String,
args: Vec<Expr>,
distinct: bool, },
WindowFunction {
func: WindowFunc,
partition_by: Option<Vec<String>>, order_by: Option<Vec<OrderByExpr>>, },
In {
expr: Box<Expr>,
list: Vec<Expr>, negated: bool,
},
Between {
expr: Box<Expr>,
low: Box<Expr>,
high: Box<Expr>,
negated: bool,
},
Like {
expr: Box<Expr>,
pattern: Box<Expr>,
negated: bool,
},
IsNull {
expr: Box<Expr>,
negated: bool,
},
Subquery(Box<SelectStmt>),
Match {
column: String,
query: String,
},
KnnSearch {
column: String,
query_vector: Vec<f32>,
k: usize,
},
KnnDistance {
column: String,
query_vector: Vec<f32>,
},
StWithin {
column: String,
min_x: f64,
min_y: f64,
max_x: f64,
max_y: f64,
},
StDistance {
column: String,
x: f64,
y: f64,
},
StKnn {
column: String,
x: f64,
y: f64,
k: usize,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum BinaryOperator {
Eq, Ne, Lt, Gt, Le, Ge,
And,
Or,
Add, Sub, Mul, Div, Mod,
L2Distance, CosineDistance, DotProduct, }
#[derive(Debug, Clone, PartialEq)]
pub enum UnaryOperator {
Not,
Minus,
Plus,
}
impl BinaryOperator {
pub fn precedence(&self) -> u8 {
match self {
BinaryOperator::Or => 1,
BinaryOperator::And => 2,
BinaryOperator::Eq | BinaryOperator::Ne |
BinaryOperator::Lt | BinaryOperator::Gt |
BinaryOperator::Le | BinaryOperator::Ge => 3,
BinaryOperator::L2Distance | BinaryOperator::CosineDistance | BinaryOperator::DotProduct => 3,
BinaryOperator::Add | BinaryOperator::Sub => 4,
BinaryOperator::Mul | BinaryOperator::Div | BinaryOperator::Mod => 5,
}
}
}
#[derive(Debug, Clone)]
pub enum WindowFunc {
RowNumber,
Rank,
DenseRank,
Lag {
expr: Box<Expr>,
offset: Option<usize>, default: Option<Box<Expr>>, },
Lead {
expr: Box<Expr>,
offset: Option<usize>, default: Option<Box<Expr>>, },
}