Skip to content

Commit 59abff3

Browse files
authored
refactor: make intermediate AST utility available without a feature flag (#61)
# Rationale for this change This PR exists to address non-blocking issues mentioned in #59 without blocking #56. <!-- Why are you proposing this change? If this is already explained clearly in the linked Jira ticket then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> # What changes are included in this PR? - remove `parser-test-utility` feature - rename `proof-of-sql-parser/src/test_utility.rs` to `utility.rs` - improve docs for the file above <!-- There is no need to duplicate the description in the ticket here but it is sometimes worth providing a summary of the individual changes in this PR. --> # Are these changes tested? Existing tests should pass <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? -->
1 parent 73a6a26 commit 59abff3

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

crates/proof-of-sql-parser/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ lalrpop-util = { workspace = true, features = ["lexer", "unicode"] }
2323
serde = { workspace = true, features = ["serde_derive"] }
2424
thiserror = { workspace = true }
2525

26-
[features]
27-
parser-test-utility = []
28-
2926
[build-dependencies]
3027
lalrpop = { version = "0.20.0" }
3128

crates/proof-of-sql-parser/src/intermediate_ast_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
intermediate_ast::OrderByDirection::{Asc, Desc},
33
intermediate_decimal::IntermediateDecimal,
44
sql::*,
5-
test_utility::*,
5+
utility::*,
66
SelectStatement,
77
};
88

crates/proof-of-sql-parser/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ extern crate lalrpop_util;
99

1010
pub mod intermediate_ast;
1111

12-
#[cfg(all(test, feature = "parser-test-utility"))]
12+
#[cfg(test)]
1313
mod intermediate_ast_tests;
1414

15-
#[cfg(feature = "parser-test-utility")]
1615
/// Shortcuts to construct intermediate AST nodes.
17-
pub mod test_utility;
16+
pub mod utility;
1817

1918
pub(crate) mod select_statement;
2019
pub use select_statement::SelectStatement;

crates/proof-of-sql-parser/src/test_utility.rs renamed to crates/proof-of-sql-parser/src/utility.rs

+28-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{intermediate_ast::*, Identifier, SelectStatement};
22

3-
/// A == B
3+
/// Construct a new boxed `Expression` A == B
44
pub fn equal(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
55
Box::new(Expression::Binary {
66
op: BinaryOperator::Equal,
@@ -9,7 +9,7 @@ pub fn equal(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
99
})
1010
}
1111

12-
/// A >= B
12+
/// Construct a new boxed `Expression` A >= B
1313
pub fn ge(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
1414
Box::new(Expression::Binary {
1515
op: BinaryOperator::GreaterThanOrEqual,
@@ -18,7 +18,7 @@ pub fn ge(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
1818
})
1919
}
2020

21-
/// A <= B
21+
/// Construct a new boxed `Expression` A <= B
2222
pub fn le(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
2323
Box::new(Expression::Binary {
2424
op: BinaryOperator::LessThanOrEqual,
@@ -27,15 +27,15 @@ pub fn le(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
2727
})
2828
}
2929

30-
/// NOT P
30+
/// Construct a new boxed `Expression` NOT P
3131
pub fn not(expr: Box<Expression>) -> Box<Expression> {
3232
Box::new(Expression::Unary {
3333
op: UnaryOperator::Not,
3434
expr,
3535
})
3636
}
3737

38-
/// P AND Q
38+
/// Construct a new boxed `Expression` P AND Q
3939
pub fn and(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
4040
Box::new(Expression::Binary {
4141
op: BinaryOperator::And,
@@ -44,7 +44,7 @@ pub fn and(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
4444
})
4545
}
4646

47-
/// P OR Q
47+
/// Construct a new boxed `Expression` P OR Q
4848
pub fn or(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
4949
Box::new(Expression::Binary {
5050
op: BinaryOperator::Or,
@@ -53,7 +53,9 @@ pub fn or(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
5353
})
5454
}
5555

56-
/// Get table from schema and name
56+
/// Get table from schema and name.
57+
///
58+
/// If the schema is `None`, the table is assumed to be in the default schema.
5759
pub fn tab(schema: Option<&str>, name: &str) -> Box<TableExpression> {
5860
Box::new(TableExpression::Named {
5961
table: name.parse().unwrap(),
@@ -71,25 +73,25 @@ pub fn lit<L: Into<Literal>>(literal: L) -> Box<Expression> {
7173
Box::new(Expression::Literal(literal.into()))
7274
}
7375

74-
/// SELECT *
76+
/// Select all columns from a table i.e. SELECT *
7577
pub fn col_res_all() -> SelectResultExpr {
7678
SelectResultExpr::ALL
7779
}
7880

79-
/// SELECT COL AS ALIAS
81+
/// Select one column from a table and give it an alias i.e. SELECT COL AS ALIAS
8082
pub fn col_res(col_val: Box<Expression>, alias: &str) -> SelectResultExpr {
8183
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
8284
expr: col_val,
8385
alias: alias.parse().unwrap(),
8486
})
8587
}
8688

87-
/// SELECT COL1, COL2, ...
89+
/// Select multiple columns from a table i.e. SELECT COL1, COL2, ...
8890
pub fn cols_res(names: &[&str]) -> Vec<SelectResultExpr> {
8991
names.iter().map(|name| col_res(col(name), name)).collect()
9092
}
9193

92-
/// SELECT MIN(EXPR) AS ALIAS
94+
/// Compute the minimum of an expression and give it an alias i.e. SELECT MIN(EXPR) AS ALIAS
9395
pub fn min_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
9496
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
9597
expr: Box::new(Expression::Aggregation {
@@ -100,7 +102,7 @@ pub fn min_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
100102
})
101103
}
102104

103-
/// SELECT MAX(EXPR) AS ALIAS
105+
/// Compute the maximum of an expression and give it an alias i.e. SELECT MAX(EXPR) AS ALIAS
104106
pub fn max_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
105107
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
106108
expr: Expression::Aggregation {
@@ -112,7 +114,7 @@ pub fn max_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
112114
})
113115
}
114116

115-
/// SELECT SUM(EXPR) AS ALIAS
117+
/// Compute the sum of an expression and give it an alias i.e. SELECT SUM(EXPR) AS ALIAS
116118
pub fn sum_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
117119
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
118120
expr: Expression::Aggregation {
@@ -124,7 +126,7 @@ pub fn sum_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
124126
})
125127
}
126128

127-
/// SELECT COUNT(EXPR) AS ALIAS
129+
/// Count an expression and give it an alias i.e. SELECT COUNT(EXPR) AS ALIAS
128130
pub fn count_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
129131
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
130132
expr: Expression::Aggregation {
@@ -136,7 +138,7 @@ pub fn count_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
136138
})
137139
}
138140

139-
/// SELECT COUNT(*) AS ALIAS
141+
/// Count rows and give the result an alias i.e. SELECT COUNT(*) AS ALIAS
140142
pub fn count_all_res(alias: &str) -> SelectResultExpr {
141143
SelectResultExpr::AliasedResultExpr(AliasedResultExpr {
142144
expr: Expression::Aggregation {
@@ -148,7 +150,7 @@ pub fn count_all_res(alias: &str) -> SelectResultExpr {
148150
})
149151
}
150152

151-
/// SELECT COL1, COL2, ... FROM TAB WHERE EXPR GROUP BY ...
153+
/// Generate a `SetExpression` of the kind SELECT COL1, COL2, ... FROM TAB WHERE EXPR GROUP BY ...
152154
pub fn query(
153155
result_exprs: Vec<SelectResultExpr>,
154156
tab: Box<TableExpression>,
@@ -163,7 +165,9 @@ pub fn query(
163165
})
164166
}
165167

166-
/// SELECT COL1, COL2, ... FROM TAB GROUP BY ...
168+
/// Generate a `SetExpression` of the kind SELECT COL1, COL2, ... FROM TAB GROUP BY ...
169+
///
170+
/// Note that there is no WHERE clause.
167171
pub fn query_all(
168172
result_exprs: Vec<SelectResultExpr>,
169173
tab: Box<TableExpression>,
@@ -177,7 +181,9 @@ pub fn query_all(
177181
})
178182
}
179183

180-
/// SELECT ... ORDER BY ... [LIMIT ... OFFSET ...]
184+
/// Generate a query of the kind SELECT ... ORDER BY ... [LIMIT ... OFFSET ...]
185+
///
186+
/// Note that `expr` is a boxed `SetExpression`
181187
pub fn select(
182188
expr: Box<SetExpression>,
183189
order_by: Vec<OrderBy>,
@@ -190,15 +196,15 @@ pub fn select(
190196
}
191197
}
192198

193-
/// ORDER BY ID [ASC|DESC]
199+
/// Order by one column i.e. ORDER BY ID [ASC|DESC]
194200
pub fn order(id: &str, direction: OrderByDirection) -> Vec<OrderBy> {
195201
vec![OrderBy {
196202
expr: id.parse().unwrap(),
197203
direction,
198204
}]
199205
}
200206

201-
/// ORDER BY ID0 [ASC|DESC], ID1 [ASC|DESC], ...
207+
/// Order by multiple columns i.e. ORDER BY ID0 [ASC|DESC], ID1 [ASC|DESC], ...
202208
pub fn orders(ids: &[&str], directions: &[OrderByDirection]) -> Vec<OrderBy> {
203209
ids.iter()
204210
.zip(directions.iter())
@@ -209,15 +215,15 @@ pub fn orders(ids: &[&str], directions: &[OrderByDirection]) -> Vec<OrderBy> {
209215
.collect::<Vec<_>>()
210216
}
211217

212-
/// LIMIT N OFFSET M
218+
/// Slice a query result using `LIMIT` and `OFFSET` clauses i.e. LIMIT N OFFSET M
213219
pub fn slice(number_rows: u64, offset_value: i64) -> Option<Slice> {
214220
Some(Slice {
215221
number_rows,
216222
offset_value,
217223
})
218224
}
219225

220-
/// GROUP BY ID0, ID1, ...
226+
/// Group by clause with multiple columns i.e. GROUP BY ID0, ID1, ...
221227
pub fn group_by(ids: &[&str]) -> Vec<Identifier> {
222228
ids.iter().map(|id| id.parse().unwrap()).collect()
223229
}

0 commit comments

Comments
 (0)