use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CmpOp {
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
Int(i64),
Float(f32),
Text(String),
Vector(Vec<f32>),
Null,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DateTimeField {
Year,
Month,
Day,
Hour,
Minute,
Second,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Cmp {
column: String,
op: CmpOp,
value: Literal,
},
CmpColumn {
left: String,
op: CmpOp,
right: String,
},
IsNull {
column: String,
negated: bool,
},
Like {
column: String,
pattern: String,
},
InInt {
column: String,
values: Vec<i64>,
},
BetweenInt {
column: String,
low: i64,
high: i64,
},
And(Box<Expr>, Box<Expr>),
Or(Box<Expr>, Box<Expr>),
Not(Box<Expr>),
Exists {
query: Box<SelectStatement>,
},
InSubquery {
column: String,
query: Box<SelectStatement>,
},
ScalarCmp {
column: String,
op: CmpOp,
query: Box<SelectStatement>,
},
Agg {
func: AggregateFunc,
column: String,
},
AggCmp {
func: AggregateFunc,
column: String,
op: CmpOp,
value: Literal,
},
ExtractCmp {
field: DateTimeField,
source: String,
op: CmpOp,
value: Literal,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum WindowFunc {
RowNumber,
Rank,
DenseRank,
Lag {
column: String,
offset: i64,
default: Option<Literal>,
},
Lead {
column: String,
offset: i64,
default: Option<Literal>,
},
Agg {
func: AggregateFunc,
column: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameBound {
UnboundedPreceding,
Preceding(u64),
CurrentRow,
Following(u64),
UnboundedFollowing,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WindowFrame {
pub start: FrameBound,
pub end: FrameBound,
}
#[derive(Debug, Clone, PartialEq)]
pub struct WindowSpec {
pub partition_by: Vec<String>,
pub order_by: Vec<OrderBy>,
pub frame: Option<WindowFrame>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct WindowCall {
pub func: WindowFunc,
pub spec: WindowSpec,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OrderBy {
pub column: String,
pub descending: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregateFunc {
Count,
Sum,
Avg,
Min,
Max,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnDef {
pub name: String,
pub ctype: ColumnType,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColumnType {
Int,
Boolean,
Float,
Double,
Numeric,
Text,
Varchar(usize),
Date,
Timestamp,
Vector,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinType {
Inner,
Left,
Right,
Full,
Cross,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TableRef {
Named {
name: String,
alias: Option<String>,
},
Subquery {
query: Box<SelectStatement>,
alias: String,
},
}
impl TableRef {
pub fn name(&self) -> &str {
match self {
TableRef::Named { alias, name, .. } => alias.as_deref().unwrap_or(name),
TableRef::Subquery { alias, .. } => alias,
}
}
pub fn table_name(&self) -> &str {
match self {
TableRef::Named { name, .. } => name,
TableRef::Subquery { alias, .. } => alias,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Join {
pub jtype: JoinType,
pub table: TableRef,
pub on_expr: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SelectStatement {
pub columns: Vec<String>,
pub star: bool,
pub table: TableRef,
pub filter: Option<Expr>,
pub joins: Vec<Join>,
pub group_by: Vec<String>,
pub aggregates: Vec<(String, AggregateFunc, String)>,
pub window_funcs: Vec<(String, WindowCall)>,
pub order_by: Vec<OrderBy>,
pub order_by_cosine: Option<OrderByCosine>,
pub limit: Option<u64>,
pub having: Option<Expr>,
pub with_ctes: Vec<CTE>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CTE {
pub name: String,
pub query: SelectStatement,
pub recursive_term: Option<(SetOperation, Box<SelectStatement>)>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OrderByCosine {
pub column: String,
pub param: usize,
pub k: u64,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Assignment {
pub column: String,
pub value: Literal,
}
#[derive(Debug, Clone, PartialEq)]
pub struct InsertStatement {
pub table: String,
pub columns: Vec<String>,
pub values: Vec<Literal>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UpdateStatement {
pub table: String,
pub assignments: Vec<Assignment>,
pub filter: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DeleteStatement {
pub table: String,
pub filter: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateTableStatement {
pub table: String,
pub columns: Vec<ColumnDef>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CreateViewStatement {
pub name: String,
pub query: SelectStatement,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AlterTableOp {
AddColumn(ColumnDef),
DropColumn(String),
RenameColumn { old_name: String, new_name: String },
}
#[derive(Debug, Clone, PartialEq)]
pub struct AlterTableStatement {
pub table: String,
pub op: AlterTableOp,
}
#[derive(Debug, Clone, PartialEq)]
pub enum SetOperation {
Union(bool),
Intersect,
Except,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CompoundStatement {
pub first: Box<SelectStatement>,
pub operations: Vec<(SetOperation, SelectStatement)>,
pub order_by: Vec<OrderBy>,
pub limit: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetParameterStatement {
pub name: String,
pub value: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShowParameterStatement {
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResetParameterStatement {
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResetAllStatement;
#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
Select(SelectStatement),
Compound(CompoundStatement),
Insert(InsertStatement),
Update(UpdateStatement),
Delete(DeleteStatement),
CreateTable(CreateTableStatement),
CreateView(CreateViewStatement),
DropView(String),
AlterTable(AlterTableStatement),
Begin,
Commit,
Rollback,
SetParameter(SetParameterStatement),
ShowParameter(ShowParameterStatement),
ResetParameter(ResetParameterStatement),
ResetAll(ResetAllStatement),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError(pub String);