diff --git a/crates/proof-of-sql-parser/Cargo.toml b/crates/proof-of-sql-parser/Cargo.toml index c1a77b262..745c2fa02 100644 --- a/crates/proof-of-sql-parser/Cargo.toml +++ b/crates/proof-of-sql-parser/Cargo.toml @@ -23,9 +23,6 @@ lalrpop-util = { workspace = true, features = ["lexer", "unicode"] } serde = { workspace = true, features = ["serde_derive"] } thiserror = { workspace = true } -[features] -parser-test-utility = [] - [build-dependencies] lalrpop = { version = "0.20.0" } diff --git a/crates/proof-of-sql-parser/src/intermediate_ast_tests.rs b/crates/proof-of-sql-parser/src/intermediate_ast_tests.rs index 11b965b04..483056a29 100644 --- a/crates/proof-of-sql-parser/src/intermediate_ast_tests.rs +++ b/crates/proof-of-sql-parser/src/intermediate_ast_tests.rs @@ -2,7 +2,7 @@ use crate::{ intermediate_ast::OrderByDirection::{Asc, Desc}, intermediate_decimal::IntermediateDecimal, sql::*, - test_utility::*, + utility::*, SelectStatement, }; diff --git a/crates/proof-of-sql-parser/src/lib.rs b/crates/proof-of-sql-parser/src/lib.rs index 5ae47faf8..0d0296a13 100644 --- a/crates/proof-of-sql-parser/src/lib.rs +++ b/crates/proof-of-sql-parser/src/lib.rs @@ -9,12 +9,11 @@ extern crate lalrpop_util; pub mod intermediate_ast; -#[cfg(all(test, feature = "parser-test-utility"))] +#[cfg(test)] mod intermediate_ast_tests; -#[cfg(feature = "parser-test-utility")] /// Shortcuts to construct intermediate AST nodes. -pub mod test_utility; +pub mod utility; pub(crate) mod select_statement; pub use select_statement::SelectStatement; diff --git a/crates/proof-of-sql-parser/src/test_utility.rs b/crates/proof-of-sql-parser/src/utility.rs similarity index 75% rename from crates/proof-of-sql-parser/src/test_utility.rs rename to crates/proof-of-sql-parser/src/utility.rs index ad5abfa8f..c42d4c9df 100644 --- a/crates/proof-of-sql-parser/src/test_utility.rs +++ b/crates/proof-of-sql-parser/src/utility.rs @@ -1,6 +1,6 @@ use crate::{intermediate_ast::*, Identifier, SelectStatement}; -/// A == B +/// Construct a new boxed `Expression` A == B pub fn equal(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::Equal, @@ -9,7 +9,7 @@ pub fn equal(left: Box, right: Box) -> Box { }) } -/// A >= B +/// Construct a new boxed `Expression` A >= B pub fn ge(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::GreaterThanOrEqual, @@ -18,7 +18,7 @@ pub fn ge(left: Box, right: Box) -> Box { }) } -/// A <= B +/// Construct a new boxed `Expression` A <= B pub fn le(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::LessThanOrEqual, @@ -27,7 +27,7 @@ pub fn le(left: Box, right: Box) -> Box { }) } -/// NOT P +/// Construct a new boxed `Expression` NOT P pub fn not(expr: Box) -> Box { Box::new(Expression::Unary { op: UnaryOperator::Not, @@ -35,7 +35,7 @@ pub fn not(expr: Box) -> Box { }) } -/// P AND Q +/// Construct a new boxed `Expression` P AND Q pub fn and(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::And, @@ -44,7 +44,7 @@ pub fn and(left: Box, right: Box) -> Box { }) } -/// P OR Q +/// Construct a new boxed `Expression` P OR Q pub fn or(left: Box, right: Box) -> Box { Box::new(Expression::Binary { op: BinaryOperator::Or, @@ -53,7 +53,9 @@ pub fn or(left: Box, right: Box) -> Box { }) } -/// Get table from schema and name +/// Get table from schema and name. +/// +/// If the schema is `None`, the table is assumed to be in the default schema. pub fn tab(schema: Option<&str>, name: &str) -> Box { Box::new(TableExpression::Named { table: name.parse().unwrap(), @@ -71,12 +73,12 @@ pub fn lit>(literal: L) -> Box { Box::new(Expression::Literal(literal.into())) } -/// SELECT * +/// Select all columns from a table i.e. SELECT * pub fn col_res_all() -> SelectResultExpr { SelectResultExpr::ALL } -/// SELECT COL AS ALIAS +/// Select one column from a table and give it an alias i.e. SELECT COL AS ALIAS pub fn col_res(col_val: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: col_val, @@ -84,12 +86,12 @@ pub fn col_res(col_val: Box, alias: &str) -> SelectResultExpr { }) } -/// SELECT COL1, COL2, ... +/// Select multiple columns from a table i.e. SELECT COL1, COL2, ... pub fn cols_res(names: &[&str]) -> Vec { names.iter().map(|name| col_res(col(name), name)).collect() } -/// SELECT MIN(EXPR) AS ALIAS +/// Compute the minimum of an expression and give it an alias i.e. SELECT MIN(EXPR) AS ALIAS pub fn min_res(expr: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: Box::new(Expression::Aggregation { @@ -100,7 +102,7 @@ pub fn min_res(expr: Box, alias: &str) -> SelectResultExpr { }) } -/// SELECT MAX(EXPR) AS ALIAS +/// Compute the maximum of an expression and give it an alias i.e. SELECT MAX(EXPR) AS ALIAS pub fn max_res(expr: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: Expression::Aggregation { @@ -112,7 +114,7 @@ pub fn max_res(expr: Box, alias: &str) -> SelectResultExpr { }) } -/// SELECT SUM(EXPR) AS ALIAS +/// Compute the sum of an expression and give it an alias i.e. SELECT SUM(EXPR) AS ALIAS pub fn sum_res(expr: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: Expression::Aggregation { @@ -124,7 +126,7 @@ pub fn sum_res(expr: Box, alias: &str) -> SelectResultExpr { }) } -/// SELECT COUNT(EXPR) AS ALIAS +/// Count an expression and give it an alias i.e. SELECT COUNT(EXPR) AS ALIAS pub fn count_res(expr: Box, alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: Expression::Aggregation { @@ -136,7 +138,7 @@ pub fn count_res(expr: Box, alias: &str) -> SelectResultExpr { }) } -/// SELECT COUNT(*) AS ALIAS +/// Count rows and give the result an alias i.e. SELECT COUNT(*) AS ALIAS pub fn count_all_res(alias: &str) -> SelectResultExpr { SelectResultExpr::AliasedResultExpr(AliasedResultExpr { expr: Expression::Aggregation { @@ -148,7 +150,7 @@ pub fn count_all_res(alias: &str) -> SelectResultExpr { }) } -/// SELECT COL1, COL2, ... FROM TAB WHERE EXPR GROUP BY ... +/// Generate a `SetExpression` of the kind SELECT COL1, COL2, ... FROM TAB WHERE EXPR GROUP BY ... pub fn query( result_exprs: Vec, tab: Box, @@ -163,7 +165,9 @@ pub fn query( }) } -/// SELECT COL1, COL2, ... FROM TAB GROUP BY ... +/// Generate a `SetExpression` of the kind SELECT COL1, COL2, ... FROM TAB GROUP BY ... +/// +/// Note that there is no WHERE clause. pub fn query_all( result_exprs: Vec, tab: Box, @@ -177,7 +181,9 @@ pub fn query_all( }) } -/// SELECT ... ORDER BY ... [LIMIT ... OFFSET ...] +/// Generate a query of the kind SELECT ... ORDER BY ... [LIMIT ... OFFSET ...] +/// +/// Note that `expr` is a boxed `SetExpression` pub fn select( expr: Box, order_by: Vec, @@ -190,7 +196,7 @@ pub fn select( } } -/// ORDER BY ID [ASC|DESC] +/// Order by one column i.e. ORDER BY ID [ASC|DESC] pub fn order(id: &str, direction: OrderByDirection) -> Vec { vec![OrderBy { expr: id.parse().unwrap(), @@ -198,7 +204,7 @@ pub fn order(id: &str, direction: OrderByDirection) -> Vec { }] } -/// ORDER BY ID0 [ASC|DESC], ID1 [ASC|DESC], ... +/// Order by multiple columns i.e. ORDER BY ID0 [ASC|DESC], ID1 [ASC|DESC], ... pub fn orders(ids: &[&str], directions: &[OrderByDirection]) -> Vec { ids.iter() .zip(directions.iter()) @@ -209,7 +215,7 @@ pub fn orders(ids: &[&str], directions: &[OrderByDirection]) -> Vec { .collect::>() } -/// LIMIT N OFFSET M +/// Slice a query result using `LIMIT` and `OFFSET` clauses i.e. LIMIT N OFFSET M pub fn slice(number_rows: u64, offset_value: i64) -> Option { Some(Slice { number_rows, @@ -217,7 +223,7 @@ pub fn slice(number_rows: u64, offset_value: i64) -> Option { }) } -/// GROUP BY ID0, ID1, ... +/// Group by clause with multiple columns i.e. GROUP BY ID0, ID1, ... pub fn group_by(ids: &[&str]) -> Vec { ids.iter().map(|id| id.parse().unwrap()).collect() }