1use crate::{posql_time::PoSQLTimestamp, Identifier};
8use alloc::{boxed::Box, string::String, vec::Vec};
9use bigdecimal::BigDecimal;
10use core::{
11 fmt,
12 fmt::{Display, Formatter},
13 hash::Hash,
14};
15use serde::{Deserialize, Serialize};
16
17#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
19pub enum SetExpression {
20 Query {
22 result_exprs: Vec<SelectResultExpr>,
24 from: Vec<Box<TableExpression>>,
26 where_expr: Option<Box<Expression>>,
29 group_by: Vec<Identifier>,
31 },
32}
33
34#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
35pub enum SelectResultExpr {
37 ALL,
39 AliasedResultExpr(AliasedResultExpr),
41}
42
43#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
44pub struct AliasedResultExpr {
46 pub expr: Box<Expression>,
48 pub alias: Identifier,
50}
51
52impl AliasedResultExpr {
53 #[must_use]
55 pub fn new(expr: Expression, alias: Identifier) -> Self {
56 Self {
57 expr: Box::new(expr),
58 alias,
59 }
60 }
61
62 #[must_use]
65 pub fn try_as_identifier(&self) -> Option<&Identifier> {
66 match self.expr.as_ref() {
67 Expression::Column(column) => Some(column),
68 _ => None,
69 }
70 }
71}
72
73#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
75pub enum TableExpression {
76 Named {
78 table: Identifier,
80 schema: Option<Identifier>,
82 },
83}
84
85#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone, Copy)]
87pub enum BinaryOperator {
88 Add,
90
91 Subtract,
93
94 Multiply,
96
97 Division,
99
100 And,
102
103 Or,
105
106 Equal,
108
109 LessThan,
111
112 GreaterThan,
114}
115
116#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone, Copy)]
118pub enum UnaryOperator {
119 Not,
121}
122
123#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone, Copy)]
125pub enum AggregationOperator {
127 Max,
129 Min,
131 Sum,
133 Count,
135 First,
137}
138
139impl Display for AggregationOperator {
140 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
141 match self {
142 AggregationOperator::Max => write!(f, "max"),
143 AggregationOperator::Min => write!(f, "min"),
144 AggregationOperator::Sum => write!(f, "sum"),
145 AggregationOperator::Count => write!(f, "count"),
146 AggregationOperator::First => write!(f, "first"),
147 }
148 }
149}
150
151#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Hash)]
153pub enum Expression {
154 Literal(Literal),
156
157 Column(Identifier),
159
160 Unary {
162 op: UnaryOperator,
164 expr: Box<Expression>,
166 },
167
168 Binary {
170 op: BinaryOperator,
172 left: Box<Expression>,
174 right: Box<Expression>,
176 },
177
178 Wildcard,
180
181 Aggregation {
183 op: AggregationOperator,
185 expr: Box<Expression>,
187 },
188}
189
190impl Expression {
191 #[must_use]
193 pub fn sum(self) -> Box<Self> {
194 Box::new(Expression::Aggregation {
195 op: AggregationOperator::Sum,
196 expr: Box::new(self),
197 })
198 }
199
200 #[must_use]
202 pub fn max(self) -> Box<Self> {
203 Box::new(Expression::Aggregation {
204 op: AggregationOperator::Max,
205 expr: Box::new(self),
206 })
207 }
208
209 #[must_use]
211 pub fn min(self) -> Box<Self> {
212 Box::new(Expression::Aggregation {
213 op: AggregationOperator::Min,
214 expr: Box::new(self),
215 })
216 }
217
218 #[must_use]
220 pub fn count(self) -> Box<Self> {
221 Box::new(Expression::Aggregation {
222 op: AggregationOperator::Count,
223 expr: Box::new(self),
224 })
225 }
226
227 #[must_use]
229 pub fn first(self) -> Box<Self> {
230 Box::new(Expression::Aggregation {
231 op: AggregationOperator::First,
232 expr: Box::new(self),
233 })
234 }
235 #[must_use]
241 pub fn alias(self, alias: &str) -> AliasedResultExpr {
242 AliasedResultExpr {
243 expr: Box::new(self),
244 alias: alias.parse().unwrap(),
245 }
246 }
247}
248impl core::ops::Add<Box<Expression>> for Box<Expression> {
249 type Output = Box<Expression>;
250
251 fn add(self, rhs: Box<Expression>) -> Box<Expression> {
252 Box::new(Expression::Binary {
253 op: BinaryOperator::Add,
254 left: self,
255 right: rhs,
256 })
257 }
258}
259impl core::ops::Mul<Box<Expression>> for Box<Expression> {
260 type Output = Box<Expression>;
261
262 fn mul(self, rhs: Box<Expression>) -> Box<Expression> {
263 Box::new(Expression::Binary {
264 op: BinaryOperator::Multiply,
265 left: self,
266 right: rhs,
267 })
268 }
269}
270impl core::ops::Div<Box<Expression>> for Box<Expression> {
271 type Output = Box<Expression>;
272
273 fn div(self, rhs: Box<Expression>) -> Box<Expression> {
274 Box::new(Expression::Binary {
275 op: BinaryOperator::Division,
276 left: self,
277 right: rhs,
278 })
279 }
280}
281impl core::ops::Sub<Box<Expression>> for Box<Expression> {
282 type Output = Box<Expression>;
283
284 fn sub(self, rhs: Box<Expression>) -> Box<Expression> {
285 Box::new(Expression::Binary {
286 op: BinaryOperator::Subtract,
287 left: self,
288 right: rhs,
289 })
290 }
291}
292
293#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
295pub struct OrderBy {
296 pub expr: Identifier,
298 pub direction: OrderByDirection,
300}
301
302#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
304pub enum OrderByDirection {
305 Asc,
307 Desc,
309}
310
311impl Display for OrderByDirection {
312 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
314 match self {
315 OrderByDirection::Asc => write!(f, "asc"),
316 OrderByDirection::Desc => write!(f, "desc"),
317 }
318 }
319}
320
321#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
323pub struct Slice {
324 pub number_rows: u64,
328
329 pub offset_value: i64,
335}
336
337#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Hash)]
339pub enum Literal {
340 Boolean(bool),
342 BigInt(i64),
344 Int128(i128),
346 VarChar(String),
348 Decimal(BigDecimal),
350 Timestamp(PoSQLTimestamp),
352}
353
354impl From<bool> for Literal {
355 fn from(val: bool) -> Self {
356 Literal::Boolean(val)
357 }
358}
359
360macro_rules! impl_int_to_literal {
362 ($tt:ty) => {
363 impl From<$tt> for Literal {
364 fn from(val: $tt) -> Self {
365 Literal::BigInt(i64::from(val))
366 }
367 }
368 };
369}
370
371impl_int_to_literal!(i8);
372impl_int_to_literal!(u8);
373impl_int_to_literal!(i16);
374impl_int_to_literal!(u16);
375impl_int_to_literal!(i32);
376impl_int_to_literal!(u32);
377impl_int_to_literal!(i64);
378
379impl From<i128> for Literal {
380 fn from(val: i128) -> Self {
381 Literal::Int128(val)
382 }
383}
384
385macro_rules! impl_string_to_literal {
387 ($tt:ty) => {
388 impl From<$tt> for Literal {
389 fn from(val: $tt) -> Self {
390 Literal::VarChar(val.into())
391 }
392 }
393 };
394}
395
396impl_string_to_literal!(&str);
397impl_string_to_literal!(String);
398
399impl From<BigDecimal> for Literal {
400 fn from(val: BigDecimal) -> Self {
401 Literal::Decimal(val)
402 }
403}
404
405impl From<PoSQLTimestamp> for Literal {
406 fn from(time: PoSQLTimestamp) -> Self {
407 Literal::Timestamp(time)
408 }
409}
410
411pub(crate) fn append<T>(list: Vec<T>, item: T) -> Vec<T> {
413 let mut result = list;
414 result.push(item);
415 result
416}