diff --git a/crates/proof-of-sql-parser/src/intermediate_ast.rs b/crates/proof-of-sql-parser/src/intermediate_ast.rs index 8e5c796e1..70bec1122 100644 --- a/crates/proof-of-sql-parser/src/intermediate_ast.rs +++ b/crates/proof-of-sql-parser/src/intermediate_ast.rs @@ -50,7 +50,8 @@ pub struct AliasedResultExpr { impl AliasedResultExpr { /// Create a new `AliasedResultExpr` - #[must_use] pub fn new(expr: Expression, alias: Identifier) -> Self { + #[must_use] + pub fn new(expr: Expression, alias: Identifier) -> Self { Self { expr: Box::new(expr), alias, @@ -59,7 +60,8 @@ impl AliasedResultExpr { /// Try to get the identifier of the expression if it is a column /// Otherwise return None - #[must_use] pub fn try_as_identifier(&self) -> Option<&Identifier> { + #[must_use] + pub fn try_as_identifier(&self) -> Option<&Identifier> { match self.expr.as_ref() { Expression::Column(column) => Some(column), _ => None, @@ -186,7 +188,8 @@ pub enum Expression { impl Expression { /// Create a new `SUM()` - #[must_use] pub fn sum(self) -> Box { + #[must_use] + pub fn sum(self) -> Box { Box::new(Expression::Aggregation { op: AggregationOperator::Sum, expr: Box::new(self), @@ -194,7 +197,8 @@ impl Expression { } /// Create a new `MAX()` - #[must_use] pub fn max(self) -> Box { + #[must_use] + pub fn max(self) -> Box { Box::new(Expression::Aggregation { op: AggregationOperator::Max, expr: Box::new(self), @@ -202,7 +206,8 @@ impl Expression { } /// Create a new `MIN()` - #[must_use] pub fn min(self) -> Box { + #[must_use] + pub fn min(self) -> Box { Box::new(Expression::Aggregation { op: AggregationOperator::Min, expr: Box::new(self), @@ -210,7 +215,8 @@ impl Expression { } /// Create a new `COUNT()` - #[must_use] pub fn count(self) -> Box { + #[must_use] + pub fn count(self) -> Box { Box::new(Expression::Aggregation { op: AggregationOperator::Count, expr: Box::new(self), @@ -218,14 +224,16 @@ impl Expression { } /// Create a new `FIRST()` - #[must_use] pub fn first(self) -> Box { + #[must_use] + pub fn first(self) -> Box { Box::new(Expression::Aggregation { op: AggregationOperator::First, expr: Box::new(self), }) } /// Create an `AliasedResultExpr` from an `Expression` using the provided alias. - #[must_use] pub fn alias(self, alias: &str) -> AliasedResultExpr { + #[must_use] + pub fn alias(self, alias: &str) -> AliasedResultExpr { AliasedResultExpr { expr: Box::new(self), alias: alias.parse().unwrap(), diff --git a/crates/proof-of-sql-parser/src/intermediate_decimal.rs b/crates/proof-of-sql-parser/src/intermediate_decimal.rs index f80a84ba3..1cfa7e1c9 100644 --- a/crates/proof-of-sql-parser/src/intermediate_decimal.rs +++ b/crates/proof-of-sql-parser/src/intermediate_decimal.rs @@ -59,7 +59,7 @@ impl IntermediateDecimal { match u8::try_from(self.value.digits()) { Ok(v) => v, Err(_) => u8::MAX, // Returning u8::MAX on truncation - } + } } /// Get the scale of the fixed-point representation of this intermediate decimal. @@ -68,7 +68,7 @@ impl IntermediateDecimal { match i8::try_from(self.value.fractional_digit_count()) { Ok(v) => v, Err(_) => i8::MAX, // Returning i8::MAX on truncation - } + } } /// Attempts to convert the decimal to `BigInt` while adjusting it to the specified precision and scale. diff --git a/crates/proof-of-sql-parser/src/posql_time/timezone.rs b/crates/proof-of-sql-parser/src/posql_time/timezone.rs index 597ee6b6c..c4f775afd 100644 --- a/crates/proof-of-sql-parser/src/posql_time/timezone.rs +++ b/crates/proof-of-sql-parser/src/posql_time/timezone.rs @@ -14,7 +14,8 @@ pub enum PoSQLTimeZone { impl PoSQLTimeZone { /// Parse a timezone from a count of seconds - #[must_use] pub fn from_offset(offset: i32) -> Self { + #[must_use] + pub fn from_offset(offset: i32) -> Self { if offset == 0 { PoSQLTimeZone::Utc } else { diff --git a/crates/proof-of-sql-parser/src/resource_id.rs b/crates/proof-of-sql-parser/src/resource_id.rs index 8f568eb59..fa1e453ca 100644 --- a/crates/proof-of-sql-parser/src/resource_id.rs +++ b/crates/proof-of-sql-parser/src/resource_id.rs @@ -18,7 +18,8 @@ pub struct ResourceId { impl ResourceId { /// Constructor for [`ResourceId`]s. - #[must_use] pub fn new(schema: Identifier, object_name: Identifier) -> Self { + #[must_use] + pub fn new(schema: Identifier, object_name: Identifier) -> Self { Self { schema, object_name, @@ -43,12 +44,14 @@ impl ResourceId { } /// The schema identifier of this [`ResourceId`]. - #[must_use] pub fn schema(&self) -> Identifier { + #[must_use] + pub fn schema(&self) -> Identifier { self.schema } /// The `object_name` identifier of this [`ResourceId`]. - #[must_use] pub fn object_name(&self) -> Identifier { + #[must_use] + pub fn object_name(&self) -> Identifier { self.object_name } @@ -63,7 +66,8 @@ impl ResourceId { /// This method performs that transformation as well. /// For more information, see /// . - #[must_use] pub fn storage_format(&self) -> String { + #[must_use] + pub fn storage_format(&self) -> String { let ResourceId { schema, object_name, diff --git a/crates/proof-of-sql-parser/src/select_statement.rs b/crates/proof-of-sql-parser/src/select_statement.rs index 246d6d77c..cd97c4ce8 100644 --- a/crates/proof-of-sql-parser/src/select_statement.rs +++ b/crates/proof-of-sql-parser/src/select_statement.rs @@ -42,7 +42,8 @@ impl SelectStatement { /// /// Return: /// - The vector with all tables referenced by the intermediate ast, encoded as resource ids. - #[must_use] pub fn get_table_references(&self, default_schema: Identifier) -> Vec { + #[must_use] + pub fn get_table_references(&self, default_schema: Identifier) -> Vec { let set_expression: &SetExpression = &(self.expr); match set_expression { @@ -79,8 +80,10 @@ fn convert_table_expr_to_resource_id_vector( match table_ref { TableExpression::Named { table, schema } => { - let schema = schema - .as_ref().map_or_else(|| default_schema.name(), super::identifier::Identifier::as_str); + let schema = schema.as_ref().map_or_else( + || default_schema.name(), + super::identifier::Identifier::as_str, + ); tables.push(ResourceId::try_new(schema, table.as_str()).unwrap()); } diff --git a/crates/proof-of-sql-parser/src/utility.rs b/crates/proof-of-sql-parser/src/utility.rs index fb7ba2251..d6b0d8957 100644 --- a/crates/proof-of-sql-parser/src/utility.rs +++ b/crates/proof-of-sql-parser/src/utility.rs @@ -1,13 +1,21 @@ -use crate::{intermediate_ast::{AggregationOperator, AliasedResultExpr, BinaryOperator, Expression, Literal, OrderBy, OrderByDirection, SelectResultExpr, SetExpression, Slice, TableExpression, UnaryOperator}, Identifier, SelectStatement}; +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}; /// Construct an identifier from a str -#[must_use] pub fn ident(name: &str) -> Identifier { +#[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, right: Box) -> Box { +#[must_use] +pub fn equal(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::Equal, left, @@ -16,7 +24,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Construct a new boxed `Expression` A >= B -#[must_use] pub fn ge(left: Box, right: Box) -> Box { +#[must_use] +pub fn ge(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::GreaterThanOrEqual, left, @@ -25,7 +34,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Construct a new boxed `Expression` A <= B -#[must_use] pub fn le(left: Box, right: Box) -> Box { +#[must_use] +pub fn le(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::LessThanOrEqual, left, @@ -34,7 +44,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Construct a new boxed `Expression` NOT P -#[must_use] pub fn not(expr: Box) -> Box { +#[must_use] +pub fn not(expr: Box) -> Box { Box::new(Expression::Unary { op: UnaryOperator::Not, expr, @@ -42,7 +53,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Construct a new boxed `Expression` P AND Q -#[must_use] pub fn and(left: Box, right: Box) -> Box { +#[must_use] +pub fn and(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::And, left, @@ -51,7 +63,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Construct a new boxed `Expression` P OR Q -#[must_use] pub fn or(left: Box, right: Box) -> Box { +#[must_use] +pub fn or(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::Or, left, @@ -60,7 +73,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Construct a new boxed `Expression` A + B -#[must_use] pub fn add(left: Box, right: Box) -> Box { +#[must_use] +pub fn add(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::Add, left, @@ -69,7 +83,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Construct a new boxed `Expression` A - B -#[must_use] pub fn sub(left: Box, right: Box) -> Box { +#[must_use] +pub fn sub(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::Subtract, left, @@ -78,7 +93,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Construct a new boxed `Expression` A * B -#[must_use] pub fn mul(left: Box, right: Box) -> Box { +#[must_use] +pub fn mul(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::Multiply, left, @@ -87,7 +103,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Construct a new boxed `Expression` A / B -#[must_use] pub fn div(left: Box, right: Box) -> Box { +#[must_use] +pub fn div(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::Division, left, @@ -98,7 +115,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; /// Get table from schema and name. /// /// If the schema is `None`, the table is assumed to be in the default schema. -#[must_use] pub fn tab(schema: Option<&str>, name: &str) -> Box { +#[must_use] +pub fn tab(schema: Option<&str>, name: &str) -> Box { Box::new(TableExpression::Named { table: name.parse().unwrap(), schema: schema.map(|schema| schema.parse().unwrap()), @@ -106,7 +124,8 @@ use alloc::{boxed::Box, vec, vec::Vec}; } /// Get column from name -#[must_use] pub fn col(name: &str) -> Box { +#[must_use] +pub fn col(name: &str) -> Box { Box::new(Expression::Column(name.parse().unwrap())) } @@ -116,7 +135,8 @@ pub fn lit>(literal: L) -> Box { } /// Compute the sum of an expression -#[must_use] pub fn sum(expr: Box) -> Box { +#[must_use] +pub fn sum(expr: Box) -> Box { Box::new(Expression::Aggregation { op: AggregationOperator::Sum, expr, @@ -124,7 +144,8 @@ pub fn lit>(literal: L) -> Box { } /// Compute the minimum of an expression -#[must_use] pub fn min(expr: Box) -> Box { +#[must_use] +pub fn min(expr: Box) -> Box { Box::new(Expression::Aggregation { op: AggregationOperator::Min, expr, @@ -132,7 +153,8 @@ pub fn lit>(literal: L) -> Box { } /// Compute the maximum of an expression -#[must_use] pub fn max(expr: Box) -> Box { +#[must_use] +pub fn max(expr: Box) -> Box { Box::new(Expression::Aggregation { op: AggregationOperator::Max, expr, @@ -140,7 +162,8 @@ pub fn lit>(literal: L) -> Box { } /// Count the amount of non-null entries of expression -#[must_use] pub fn count(expr: Box) -> Box { +#[must_use] +pub fn count(expr: Box) -> Box { Box::new(Expression::Aggregation { op: AggregationOperator::Count, expr, @@ -148,12 +171,14 @@ pub fn lit>(literal: L) -> Box { } /// Count the rows -#[must_use] pub fn count_all() -> Box { +#[must_use] +pub fn count_all() -> Box { count(Box::new(Expression::Wildcard)) } /// An expression with an alias i.e. EXPR AS ALIAS -#[must_use] pub fn aliased_expr(expr: Box, alias: &str) -> AliasedResultExpr { +#[must_use] +pub fn aliased_expr(expr: Box, alias: &str) -> AliasedResultExpr { AliasedResultExpr { expr, alias: alias.parse().unwrap(), @@ -161,12 +186,14 @@ pub fn lit>(literal: L) -> Box { } /// Select all columns from a table i.e. SELECT * -#[must_use] pub fn col_res_all() -> SelectResultExpr { +#[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 -#[must_use] pub fn col_res(col_val: Box, alias: &str) -> SelectResultExpr { +#[must_use] +pub fn col_res(col_val: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: col_val, alias: alias.parse().unwrap(), @@ -174,12 +201,14 @@ pub fn lit>(literal: L) -> Box { } /// Select multiple columns from a table i.e. SELECT COL1, COL2, ... -#[must_use] pub fn cols_res(names: &[&str]) -> Vec { +#[must_use] +pub fn cols_res(names: &[&str]) -> Vec { 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 -#[must_use] pub fn min_res(expr: Box, alias: &str) -> SelectResultExpr { +#[must_use] +pub fn min_res(expr: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: min(expr), alias: alias.parse().unwrap(), @@ -187,7 +216,8 @@ pub fn lit>(literal: L) -> Box { } /// Compute the maximum of an expression and give it an alias i.e. SELECT MAX(EXPR) AS ALIAS -#[must_use] pub fn max_res(expr: Box, alias: &str) -> SelectResultExpr { +#[must_use] +pub fn max_res(expr: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: max(expr), alias: alias.parse().unwrap(), @@ -195,7 +225,8 @@ pub fn lit>(literal: L) -> Box { } /// Compute the sum of an expression and give it an alias i.e. SELECT SUM(EXPR) AS ALIAS -#[must_use] pub fn sum_res(expr: Box, alias: &str) -> SelectResultExpr { +#[must_use] +pub fn sum_res(expr: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: sum(expr), alias: alias.parse().unwrap(), @@ -203,7 +234,8 @@ pub fn lit>(literal: L) -> Box { } /// Count the amount of non-null entries of expression and give it an alias i.e. SELECT COUNT(EXPR) AS ALIAS -#[must_use] pub fn count_res(expr: Box, alias: &str) -> SelectResultExpr { +#[must_use] +pub fn count_res(expr: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: count(expr), alias: alias.parse().unwrap(), @@ -211,7 +243,8 @@ pub fn lit>(literal: L) -> Box { } /// Count rows and give the result an alias i.e. SELECT COUNT(*) AS ALIAS -#[must_use] pub fn count_all_res(alias: &str) -> SelectResultExpr { +#[must_use] +pub fn count_all_res(alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: Expression::Aggregation { op: AggregationOperator::Count, @@ -223,7 +256,8 @@ pub fn lit>(literal: L) -> Box { } /// Generate a `SetExpression` of the kind SELECT COL1, COL2, ... FROM TAB WHERE EXPR GROUP BY ... -#[must_use] pub fn query( +#[must_use] +pub fn query( result_exprs: Vec, tab: Box, where_expr: Box, @@ -240,7 +274,8 @@ pub fn lit>(literal: L) -> Box { /// 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( +#[must_use] +pub fn query_all( result_exprs: Vec, tab: Box, group_by: Vec, @@ -256,7 +291,8 @@ pub fn lit>(literal: L) -> Box { /// Generate a query of the kind SELECT ... ORDER BY ... [LIMIT ... OFFSET ...] /// /// Note that `expr` is a boxed `SetExpression` -#[must_use] pub fn select( +#[must_use] +pub fn select( expr: Box, order_by: Vec, slice: Option, @@ -269,7 +305,8 @@ pub fn lit>(literal: L) -> Box { } /// Order by one column i.e. ORDER BY ID [ASC|DESC] -#[must_use] pub fn order(id: &str, direction: OrderByDirection) -> Vec { +#[must_use] +pub fn order(id: &str, direction: OrderByDirection) -> Vec { vec![OrderBy { expr: id.parse().unwrap(), direction, @@ -277,7 +314,8 @@ pub fn lit>(literal: L) -> Box { } /// Order by multiple columns i.e. ORDER BY ID0 [ASC|DESC], ID1 [ASC|DESC], ... -#[must_use] pub fn orders(ids: &[&str], directions: &[OrderByDirection]) -> Vec { +#[must_use] +pub fn orders(ids: &[&str], directions: &[OrderByDirection]) -> Vec { ids.iter() .zip(directions.iter()) .map(|(id, dir)| OrderBy { @@ -288,7 +326,8 @@ pub fn lit>(literal: L) -> Box { } /// 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 { +#[must_use] +pub fn slice(number_rows: u64, offset_value: i64) -> Option { Some(Slice { number_rows, offset_value, @@ -296,6 +335,7 @@ pub fn lit>(literal: L) -> Box { } /// Group by clause with multiple columns i.e. GROUP BY ID0, ID1, ... -#[must_use] pub fn group_by(ids: &[&str]) -> Vec { +#[must_use] +pub fn group_by(ids: &[&str]) -> Vec { ids.iter().map(|id| id.parse().unwrap()).collect() }