2026-04-13 13:14:01 +02:00
|
|
|
/// A parsed `SELECT-FROM-WHERE-GROUP BY-ORDER BY-LIMIT` statement in the
|
|
|
|
|
/// current SQL subset.
|
2026-04-09 12:38:43 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub struct Select {
|
2026-04-09 12:50:06 +02:00
|
|
|
/// Output expressions requested by the query.
|
2026-04-09 12:38:43 +02:00
|
|
|
pub projection: Vec<SelectItem>,
|
2026-04-10 09:56:18 +02:00
|
|
|
/// Source tables and their optional aliases.
|
|
|
|
|
pub from: Vec<TableRef>,
|
2026-04-09 12:50:06 +02:00
|
|
|
/// Optional filter predicate.
|
2026-04-09 12:38:43 +02:00
|
|
|
pub selection: Option<Expr>,
|
2026-04-13 13:14:01 +02:00
|
|
|
/// Grouping columns. Empty means no `GROUP BY` clause.
|
|
|
|
|
pub group_by: Vec<Expr>,
|
2026-04-10 10:10:46 +02:00
|
|
|
/// Optional output ordering.
|
|
|
|
|
pub order_by: Vec<OrderByItem>,
|
2026-04-10 15:22:30 +02:00
|
|
|
/// Optional row limit.
|
|
|
|
|
pub limit: Option<usize>,
|
2026-04-09 12:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 09:56:18 +02:00
|
|
|
/// One source entry in a `FROM` list.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub struct TableRef {
|
|
|
|
|
/// The predicate-backed table name.
|
|
|
|
|
pub name: String,
|
|
|
|
|
/// Optional table alias used for qualification.
|
|
|
|
|
pub alias: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 10:10:46 +02:00
|
|
|
/// One item in an `ORDER BY` clause.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub struct OrderByItem {
|
|
|
|
|
/// Output column name to sort by.
|
|
|
|
|
pub expr: Expr,
|
|
|
|
|
/// Sort direction.
|
|
|
|
|
pub direction: SortDirection,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 12:50:06 +02:00
|
|
|
/// One item in a `SELECT` projection list.
|
2026-04-09 12:38:43 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub enum SelectItem {
|
2026-04-09 12:50:06 +02:00
|
|
|
/// `*`
|
2026-04-09 12:38:43 +02:00
|
|
|
Wildcard,
|
2026-04-09 12:50:06 +02:00
|
|
|
/// A projected expression, optionally renamed with `AS`.
|
2026-04-09 12:38:43 +02:00
|
|
|
Expr { expr: Expr, alias: Option<String> },
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 12:50:06 +02:00
|
|
|
/// A SQL expression in the current subset.
|
2026-04-09 12:38:43 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub enum Expr {
|
2026-04-09 12:50:06 +02:00
|
|
|
/// A column reference.
|
2026-04-09 12:38:43 +02:00
|
|
|
Identifier(String),
|
2026-04-09 12:50:06 +02:00
|
|
|
/// A literal value.
|
2026-04-09 12:38:43 +02:00
|
|
|
Literal(Literal),
|
2026-04-09 12:50:06 +02:00
|
|
|
/// A binary expression.
|
2026-04-09 12:38:43 +02:00
|
|
|
Binary {
|
|
|
|
|
left: Box<Expr>,
|
|
|
|
|
op: BinaryOp,
|
|
|
|
|
right: Box<Expr>,
|
|
|
|
|
},
|
2026-04-13 13:14:01 +02:00
|
|
|
/// An aggregate function applied to an argument.
|
|
|
|
|
Aggregate {
|
|
|
|
|
func: AggregateFunc,
|
|
|
|
|
arg: AggregateArg,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An aggregate function in the current SQL subset.
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub enum AggregateFunc {
|
|
|
|
|
/// Row count (with `*`) or count of non-null values (with a column).
|
|
|
|
|
Count,
|
|
|
|
|
/// Sum of integer values.
|
|
|
|
|
Sum,
|
|
|
|
|
/// Minimum value.
|
|
|
|
|
Min,
|
|
|
|
|
/// Maximum value.
|
|
|
|
|
Max,
|
|
|
|
|
/// Arithmetic mean of integer values.
|
|
|
|
|
Avg,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The argument to an aggregate function: either `*` (only valid for
|
|
|
|
|
/// `COUNT`) or an expression.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub enum AggregateArg {
|
|
|
|
|
/// `COUNT(*)` style argument.
|
|
|
|
|
Star,
|
|
|
|
|
/// An expression argument such as `SUM(col)`.
|
|
|
|
|
Expr(Box<Expr>),
|
2026-04-09 12:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-09 12:50:06 +02:00
|
|
|
/// A SQL literal in the current subset.
|
2026-04-09 12:38:43 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub enum Literal {
|
2026-04-09 12:50:06 +02:00
|
|
|
/// A string literal.
|
2026-04-09 12:38:43 +02:00
|
|
|
String(String),
|
2026-04-10 15:22:30 +02:00
|
|
|
/// An integer literal.
|
|
|
|
|
Integer(i64),
|
2026-04-09 12:50:06 +02:00
|
|
|
/// The `NULL` literal.
|
2026-04-09 12:38:43 +02:00
|
|
|
Null,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 12:50:06 +02:00
|
|
|
/// A binary operator in the current subset.
|
2026-04-09 12:38:43 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub enum BinaryOp {
|
2026-04-09 12:50:06 +02:00
|
|
|
/// Equality.
|
2026-04-09 12:38:43 +02:00
|
|
|
Eq,
|
2026-04-10 15:22:30 +02:00
|
|
|
/// Inequality.
|
|
|
|
|
Ne,
|
2026-04-10 10:00:55 +02:00
|
|
|
/// Boolean conjunction.
|
|
|
|
|
And,
|
2026-04-10 15:22:30 +02:00
|
|
|
/// Boolean disjunction.
|
|
|
|
|
Or,
|
2026-04-09 12:38:43 +02:00
|
|
|
}
|
2026-04-10 10:10:46 +02:00
|
|
|
|
|
|
|
|
/// Sort direction for `ORDER BY`.
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub enum SortDirection {
|
|
|
|
|
/// Ascending order.
|
|
|
|
|
Asc,
|
|
|
|
|
/// Descending order.
|
|
|
|
|
Desc,
|
|
|
|
|
}
|