proof_of_sql_parser/utility.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
use crate::{
intermediate_ast::{
AggregationOperator, AliasedResultExpr, BinaryOperator, Expression, Literal, OrderBy,
OrderByDirection, SelectResultExpr, SetExpression, Slice, TableExpression, UnaryOperator,
},
Identifier, SelectStatement,
};
use alloc::{boxed::Box, vec, vec::Vec};
///
/// # Panics
///
/// This function will panic if`name`(if provided) cannot be parsed.
/// Construct an identifier from a str
#[must_use]
pub fn ident(name: &str) -> Identifier {
name.parse().unwrap()
}
/// Construct a new boxed `Expression` A == B
#[must_use]
pub fn equal(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Binary {
op: BinaryOperator::Equal,
left,
right,
})
}
/// Construct a new boxed `Expression` A >= B
#[must_use]
pub fn ge(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Binary {
op: BinaryOperator::GreaterThanOrEqual,
left,
right,
})
}
/// Construct a new boxed `Expression` A <= B
#[must_use]
pub fn le(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Binary {
op: BinaryOperator::LessThanOrEqual,
left,
right,
})
}
/// Construct a new boxed `Expression` NOT P
#[must_use]
pub fn not(expr: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Unary {
op: UnaryOperator::Not,
expr,
})
}
/// Construct a new boxed `Expression` P AND Q
#[must_use]
pub fn and(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Binary {
op: BinaryOperator::And,
left,
right,
})
}
/// Construct a new boxed `Expression` P OR Q
#[must_use]
pub fn or(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Binary {
op: BinaryOperator::Or,
left,
right,
})
}
/// Construct a new boxed `Expression` A + B
#[must_use]
pub fn add(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Binary {
op: BinaryOperator::Add,
left,
right,
})
}
/// Construct a new boxed `Expression` A - B
#[must_use]
pub fn sub(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Binary {
op: BinaryOperator::Subtract,
left,
right,
})
}
/// Construct a new boxed `Expression` A * B
#[must_use]
pub fn mul(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Binary {
op: BinaryOperator::Multiply,
left,
right,
})
}
/// Construct a new boxed `Expression` A / B
#[must_use]
pub fn div(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Binary {
op: BinaryOperator::Division,
left,
right,
})
}
/// Get table from schema and name.
///
/// If the schema is `None`, the table is assumed to be in the default schema.
/// # Panics
///
/// This function will panic if either the `name` or the `schema` (if provided) cannot be parsed as valid [Identifier]s.
#[must_use]
pub fn tab(schema: Option<&str>, name: &str) -> Box<TableExpression> {
Box::new(TableExpression::Named {
table: name.parse().unwrap(),
schema: schema.map(|schema| schema.parse().unwrap()),
})
}
/// Get column from name
///
/// # Panics
///
/// This function will panic if the `name` cannot be parsed into a valid column expression as valid [Identifier]s.
#[must_use]
pub fn col(name: &str) -> Box<Expression> {
Box::new(Expression::Column(name.parse().unwrap()))
}
/// Get literal from value
pub fn lit<L: Into<Literal>>(literal: L) -> Box<Expression> {
Box::new(Expression::Literal(literal.into()))
}
/// Compute the sum of an expression
#[must_use]
pub fn sum(expr: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Aggregation {
op: AggregationOperator::Sum,
expr,
})
}
/// Compute the minimum of an expression
#[must_use]
pub fn min(expr: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Aggregation {
op: AggregationOperator::Min,
expr,
})
}
/// Compute the maximum of an expression
#[must_use]
pub fn max(expr: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Aggregation {
op: AggregationOperator::Max,
expr,
})
}
/// Count the amount of non-null entries of expression
#[must_use]
pub fn count(expr: Box<Expression>) -> Box<Expression> {
Box::new(Expression::Aggregation {
op: AggregationOperator::Count,
expr,
})
}
/// Count the rows
#[must_use]
pub fn count_all() -> Box<Expression> {
count(Box::new(Expression::Wildcard))
}
/// An expression with an alias i.e. EXPR AS ALIAS
///
/// # Panics
///
/// This function will panic if the `alias` cannot be parsed as valid [Identifier]s.
#[must_use]
pub fn aliased_expr(expr: Box<Expression>, alias: &str) -> AliasedResultExpr {
AliasedResultExpr {
expr,
alias: alias.parse().unwrap(),
}
}
/// Select all columns from a table i.e. SELECT *
#[must_use]
pub fn col_res_all() -> SelectResultExpr {
SelectResultExpr::ALL
}
/// Select one column from a table and give it an alias i.e. SELECT COL AS ALIAS
///
/// # Panics
///
/// This function will panic if the `alias` cannot be parsed as valid [Identifier]s.
#[must_use]
pub fn col_res(col_val: Box<Expression>, alias: &str) -> SelectResultExpr {
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
expr: col_val,
alias: alias.parse().unwrap(),
})
}
/// Select multiple columns from a table i.e. SELECT COL1, COL2, ...
#[must_use]
pub fn cols_res(names: &[&str]) -> Vec<SelectResultExpr> {
names.iter().map(|name| col_res(col(name), name)).collect()
}
/// Compute the minimum of an expression and give it an alias i.e. SELECT MIN(EXPR) AS ALIAS
///
/// # Panics
///
/// This function will panic if the `alias` cannot be parsed.
#[must_use]
pub fn min_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
expr: min(expr),
alias: alias.parse().unwrap(),
})
}
/// Compute the maximum of an expression and give it an alias i.e. SELECT MAX(EXPR) AS ALIAS
///
/// # Panics
///
/// This function will panic if the `alias` cannot be parsed.
#[must_use]
pub fn max_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
expr: max(expr),
alias: alias.parse().unwrap(),
})
}
/// Compute the sum of an expression and give it an alias i.e. SELECT SUM(EXPR) AS ALIAS
///
/// # Panics
///
/// This function will panic if the `alias` cannot be parsed.
#[must_use]
pub fn sum_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
expr: sum(expr),
alias: alias.parse().unwrap(),
})
}
/// Count the amount of non-null entries of expression and give it an alias i.e. SELECT COUNT(EXPR) AS ALIAS
///
/// # Panics
///
/// This function will panic if the `alias` cannot be parsed.
#[must_use]
pub fn count_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
expr: count(expr),
alias: alias.parse().unwrap(),
})
}
/// Count rows and give the result an alias i.e. SELECT COUNT(*) AS ALIAS
///
/// # Panics
///
/// This function will panic if the `alias` cannot be parsed.
#[must_use]
pub fn count_all_res(alias: &str) -> SelectResultExpr {
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
expr: Expression::Aggregation {
op: AggregationOperator::Count,
expr: Box::new(Expression::Wildcard),
}
.into(),
alias: alias.parse().unwrap(),
})
}
/// Generate a `SetExpression` of the kind SELECT COL1, COL2, ... FROM TAB WHERE EXPR GROUP BY ...
#[must_use]
pub fn query(
result_exprs: Vec<SelectResultExpr>,
tab: Box<TableExpression>,
where_expr: Box<Expression>,
group_by: Vec<Identifier>,
) -> Box<SetExpression> {
Box::new(SetExpression::Query {
result_exprs,
from: vec![tab],
where_expr: Some(where_expr),
group_by,
})
}
/// Generate a `SetExpression` of the kind SELECT COL1, COL2, ... FROM TAB GROUP BY ...
///
/// Note that there is no WHERE clause.
#[must_use]
pub fn query_all(
result_exprs: Vec<SelectResultExpr>,
tab: Box<TableExpression>,
group_by: Vec<Identifier>,
) -> Box<SetExpression> {
Box::new(SetExpression::Query {
result_exprs,
from: vec![tab],
where_expr: None,
group_by,
})
}
/// Generate a query of the kind SELECT ... ORDER BY ... [LIMIT ... OFFSET ...]
///
/// Note that `expr` is a boxed `SetExpression`
#[must_use]
pub fn select(
expr: Box<SetExpression>,
order_by: Vec<OrderBy>,
slice: Option<Slice>,
) -> SelectStatement {
SelectStatement {
expr,
order_by,
slice,
}
}
/// Order by one column i.e. ORDER BY ID [ASC|DESC]
///
/// # Panics
///
/// This function will panic if the `id` cannot be parsed into an identifier.
#[must_use]
pub fn order(id: &str, direction: OrderByDirection) -> Vec<OrderBy> {
vec![OrderBy {
expr: id.parse().unwrap(),
direction,
}]
}
/// Order by multiple columns i.e. ORDER BY ID0 [ASC|DESC], ID1 [ASC|DESC], ...
///
/// # Panics
///
/// This function will panic if any of the `ids` cannot be parsed
/// into an identifier.
#[must_use]
pub fn orders(ids: &[&str], directions: &[OrderByDirection]) -> Vec<OrderBy> {
ids.iter()
.zip(directions.iter())
.map(|(id, dir)| OrderBy {
expr: id.parse().unwrap(),
direction: *dir,
})
.collect::<Vec<_>>()
}
/// Slice a query result using `LIMIT` and `OFFSET` clauses i.e. LIMIT N OFFSET M
#[must_use]
pub fn slice(number_rows: u64, offset_value: i64) -> Option<Slice> {
Some(Slice {
number_rows,
offset_value,
})
}
/// Group by clause with multiple columns i.e. GROUP BY ID0, ID1, ...
///
/// # Panics
///
/// This function will panic if any of the `ids` cannot be parsed
/// into an identifier.
#[must_use]
pub fn group_by(ids: &[&str]) -> Vec<Identifier> {
ids.iter().map(|id| id.parse().unwrap()).collect()
}