Menu
Public repository / master
AkurAI-Framework / crates/sql/src/ast.rs
Latest change fa37e2544d31a1199954a395f9e5b3ebb237b17f - feat(sql): aggregates (COUNT/SUM/AVG/MIN/MAX) + GROUP BY (gpt-5.5) by Ólafur Búi Ólafsson
//! The SQL abstract syntax tree — the output of [`crate::parse`].
//!
//! This is a deliberately small subset (single-table `SELECT`, `INSERT`,
//! `UPDATE`, `DELETE`, `CREATE TABLE`) sufficient to drive the storage engine
//! later. It carries no execution semantics; it is pure shape.
/// A single parsed SQL statement.
#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
Select(SelectStmt),
Insert(InsertStmt),
Update(UpdateStmt),
Delete(DeleteStmt),
CreateTable(CreateTableStmt),
}
/// `SELECT <cols|*> FROM <table> [WHERE <expr>] [GROUP BY <col>] [ORDER BY <col> [ASC|DESC]] [LIMIT <n>]`.
#[derive(Debug, Clone, PartialEq)]
pub struct SelectStmt {
pub columns: Vec<SelectColumn>,
pub table: String,
pub where_clause: Option<Expr>,
pub group_by: Option<String>,
pub order_by: Option<OrderBy>,
pub limit: Option<i64>,
}
/// A projected column in a `SELECT` list.
#[derive(Debug, Clone, PartialEq)]
pub enum SelectColumn {
/// `*` — all columns.
All,
/// A named column.
Named(String),
/// An aggregate function call, such as `COUNT(*)` or `SUM(amount)`.
Aggregate(AggregateExpr),
}
/// An aggregate function call in a `SELECT` list.
#[derive(Debug, Clone, PartialEq)]
pub struct AggregateExpr {
pub func: AggregateFunction,
pub arg: AggregateArg,
}
/// Supported aggregate functions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregateFunction {
Count,
Sum,
Avg,
Min,
Max,
}
/// The argument to an aggregate function.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AggregateArg {
/// `*`, currently valid only for `COUNT(*)`.
All,
/// A named column argument.
Column(String),
}
/// An `ORDER BY <column> [ASC|DESC]` clause.
#[derive(Debug, Clone, PartialEq)]
pub struct OrderBy {
pub column: String,
pub direction: Direction,
}
/// Sort direction. `ASC` is the default when none is written.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Asc,
Desc,
}
/// `INSERT INTO <table> (<cols>) VALUES (<values>)`.
#[derive(Debug, Clone, PartialEq)]
pub struct InsertStmt {
pub table: String,
pub columns: Vec<String>,
pub values: Vec<Literal>,
}
/// `UPDATE <table> SET <col> = <value> [, <col> = <value>]* [WHERE <expr>]`.
#[derive(Debug, Clone, PartialEq)]
pub struct UpdateStmt {
pub table: String,
/// Each `SET` target: a column name and the literal value to assign.
pub assignments: Vec<(String, Literal)>,
pub where_clause: Option<Expr>,
}
/// `DELETE FROM <table> [WHERE <expr>]`.
#[derive(Debug, Clone, PartialEq)]
pub struct DeleteStmt {
pub table: String,
pub where_clause: Option<Expr>,
}
/// `CREATE TABLE <table> (<col> <type> [NOT NULL], …)`.
#[derive(Debug, Clone, PartialEq)]
pub struct CreateTableStmt {
pub table: String,
pub columns: Vec<ColumnDef>,
}
/// One column definition in a `CREATE TABLE`.
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnDef {
pub name: String,
pub ty: DataType,
pub not_null: bool,
}
/// A column's declared storage type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataType {
Integer,
Text,
Real,
}
/// A `WHERE` expression tree. `AND` binds tighter than `OR`.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
/// A literal value.
Literal(Literal),
/// A column reference.
Column(String),
/// `left <op> right`.
Comparison {
left: Box<Expr>,
op: CompareOp,
right: Box<Expr>,
},
/// `left AND right`.
And(Box<Expr>, Box<Expr>),
/// `left OR right`.
Or(Box<Expr>, Box<Expr>),
/// `NOT operand`.
Not(Box<Expr>),
}
/// A comparison operator inside a `WHERE` expression.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompareOp {
Eq,
NotEq,
Lt,
LtEq,
Gt,
GtEq,
}
/// A literal value: the leaves of expressions and `INSERT` value lists.
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
Null,
Int(i64),
Float(f64),
Str(String),
}