1
1
use crate :: { intermediate_ast:: * , Identifier , SelectStatement } ;
2
2
3
- /// A == B
3
+ /// Construct a new boxed `Expression` A == B
4
4
pub fn equal ( left : Box < Expression > , right : Box < Expression > ) -> Box < Expression > {
5
5
Box :: new ( Expression :: Binary {
6
6
op : BinaryOperator :: Equal ,
@@ -9,7 +9,7 @@ pub fn equal(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
9
9
} )
10
10
}
11
11
12
- /// A >= B
12
+ /// Construct a new boxed `Expression` A >= B
13
13
pub fn ge ( left : Box < Expression > , right : Box < Expression > ) -> Box < Expression > {
14
14
Box :: new ( Expression :: Binary {
15
15
op : BinaryOperator :: GreaterThanOrEqual ,
@@ -18,7 +18,7 @@ pub fn ge(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
18
18
} )
19
19
}
20
20
21
- /// A <= B
21
+ /// Construct a new boxed `Expression` A <= B
22
22
pub fn le ( left : Box < Expression > , right : Box < Expression > ) -> Box < Expression > {
23
23
Box :: new ( Expression :: Binary {
24
24
op : BinaryOperator :: LessThanOrEqual ,
@@ -27,15 +27,15 @@ pub fn le(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
27
27
} )
28
28
}
29
29
30
- /// NOT P
30
+ /// Construct a new boxed `Expression` NOT P
31
31
pub fn not ( expr : Box < Expression > ) -> Box < Expression > {
32
32
Box :: new ( Expression :: Unary {
33
33
op : UnaryOperator :: Not ,
34
34
expr,
35
35
} )
36
36
}
37
37
38
- /// P AND Q
38
+ /// Construct a new boxed `Expression` P AND Q
39
39
pub fn and ( left : Box < Expression > , right : Box < Expression > ) -> Box < Expression > {
40
40
Box :: new ( Expression :: Binary {
41
41
op : BinaryOperator :: And ,
@@ -44,7 +44,7 @@ pub fn and(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
44
44
} )
45
45
}
46
46
47
- /// P OR Q
47
+ /// Construct a new boxed `Expression` P OR Q
48
48
pub fn or ( left : Box < Expression > , right : Box < Expression > ) -> Box < Expression > {
49
49
Box :: new ( Expression :: Binary {
50
50
op : BinaryOperator :: Or ,
@@ -53,7 +53,9 @@ pub fn or(left: Box<Expression>, right: Box<Expression>) -> Box<Expression> {
53
53
} )
54
54
}
55
55
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.
57
59
pub fn tab ( schema : Option < & str > , name : & str ) -> Box < TableExpression > {
58
60
Box :: new ( TableExpression :: Named {
59
61
table : name. parse ( ) . unwrap ( ) ,
@@ -71,25 +73,25 @@ pub fn lit<L: Into<Literal>>(literal: L) -> Box<Expression> {
71
73
Box :: new ( Expression :: Literal ( literal. into ( ) ) )
72
74
}
73
75
74
- /// SELECT *
76
+ /// Select all columns from a table i.e. SELECT *
75
77
pub fn col_res_all ( ) -> SelectResultExpr {
76
78
SelectResultExpr :: ALL
77
79
}
78
80
79
- /// SELECT COL AS ALIAS
81
+ /// Select one column from a table and give it an alias i.e. SELECT COL AS ALIAS
80
82
pub fn col_res ( col_val : Box < Expression > , alias : & str ) -> SelectResultExpr {
81
83
SelectResultExpr :: AliasedResultExpr ( AliasedResultExpr {
82
84
expr : col_val,
83
85
alias : alias. parse ( ) . unwrap ( ) ,
84
86
} )
85
87
}
86
88
87
- /// SELECT COL1, COL2, ...
89
+ /// Select multiple columns from a table i.e. SELECT COL1, COL2, ...
88
90
pub fn cols_res ( names : & [ & str ] ) -> Vec < SelectResultExpr > {
89
91
names. iter ( ) . map ( |name| col_res ( col ( name) , name) ) . collect ( )
90
92
}
91
93
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
93
95
pub fn min_res ( expr : Box < Expression > , alias : & str ) -> SelectResultExpr {
94
96
SelectResultExpr :: AliasedResultExpr ( AliasedResultExpr {
95
97
expr : Box :: new ( Expression :: Aggregation {
@@ -100,7 +102,7 @@ pub fn min_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
100
102
} )
101
103
}
102
104
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
104
106
pub fn max_res ( expr : Box < Expression > , alias : & str ) -> SelectResultExpr {
105
107
SelectResultExpr :: AliasedResultExpr ( AliasedResultExpr {
106
108
expr : Expression :: Aggregation {
@@ -112,7 +114,7 @@ pub fn max_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
112
114
} )
113
115
}
114
116
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
116
118
pub fn sum_res ( expr : Box < Expression > , alias : & str ) -> SelectResultExpr {
117
119
SelectResultExpr :: AliasedResultExpr ( AliasedResultExpr {
118
120
expr : Expression :: Aggregation {
@@ -124,7 +126,7 @@ pub fn sum_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
124
126
} )
125
127
}
126
128
127
- /// SELECT COUNT(EXPR) AS ALIAS
129
+ /// Count an expression and give it an alias i.e. SELECT COUNT(EXPR) AS ALIAS
128
130
pub fn count_res ( expr : Box < Expression > , alias : & str ) -> SelectResultExpr {
129
131
SelectResultExpr :: AliasedResultExpr ( AliasedResultExpr {
130
132
expr : Expression :: Aggregation {
@@ -136,7 +138,7 @@ pub fn count_res(expr: Box<Expression>, alias: &str) -> SelectResultExpr {
136
138
} )
137
139
}
138
140
139
- /// SELECT COUNT(*) AS ALIAS
141
+ /// Count rows and give the result an alias i.e. SELECT COUNT(*) AS ALIAS
140
142
pub fn count_all_res ( alias : & str ) -> SelectResultExpr {
141
143
SelectResultExpr :: AliasedResultExpr ( AliasedResultExpr {
142
144
expr : Expression :: Aggregation {
@@ -148,7 +150,7 @@ pub fn count_all_res(alias: &str) -> SelectResultExpr {
148
150
} )
149
151
}
150
152
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 ...
152
154
pub fn query (
153
155
result_exprs : Vec < SelectResultExpr > ,
154
156
tab : Box < TableExpression > ,
@@ -163,7 +165,9 @@ pub fn query(
163
165
} )
164
166
}
165
167
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.
167
171
pub fn query_all (
168
172
result_exprs : Vec < SelectResultExpr > ,
169
173
tab : Box < TableExpression > ,
@@ -177,7 +181,9 @@ pub fn query_all(
177
181
} )
178
182
}
179
183
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`
181
187
pub fn select (
182
188
expr : Box < SetExpression > ,
183
189
order_by : Vec < OrderBy > ,
@@ -190,15 +196,15 @@ pub fn select(
190
196
}
191
197
}
192
198
193
- /// ORDER BY ID [ASC|DESC]
199
+ /// Order by one column i.e. ORDER BY ID [ASC|DESC]
194
200
pub fn order ( id : & str , direction : OrderByDirection ) -> Vec < OrderBy > {
195
201
vec ! [ OrderBy {
196
202
expr: id. parse( ) . unwrap( ) ,
197
203
direction,
198
204
} ]
199
205
}
200
206
201
- /// ORDER BY ID0 [ASC|DESC], ID1 [ASC|DESC], ...
207
+ /// Order by multiple columns i.e. ORDER BY ID0 [ASC|DESC], ID1 [ASC|DESC], ...
202
208
pub fn orders ( ids : & [ & str ] , directions : & [ OrderByDirection ] ) -> Vec < OrderBy > {
203
209
ids. iter ( )
204
210
. zip ( directions. iter ( ) )
@@ -209,15 +215,15 @@ pub fn orders(ids: &[&str], directions: &[OrderByDirection]) -> Vec<OrderBy> {
209
215
. collect :: < Vec < _ > > ( )
210
216
}
211
217
212
- /// LIMIT N OFFSET M
218
+ /// Slice a query result using `LIMIT` and `OFFSET` clauses i.e. LIMIT N OFFSET M
213
219
pub fn slice ( number_rows : u64 , offset_value : i64 ) -> Option < Slice > {
214
220
Some ( Slice {
215
221
number_rows,
216
222
offset_value,
217
223
} )
218
224
}
219
225
220
- /// GROUP BY ID0, ID1, ...
226
+ /// Group by clause with multiple columns i.e. GROUP BY ID0, ID1, ...
221
227
pub fn group_by ( ids : & [ & str ] ) -> Vec < Identifier > {
222
228
ids. iter ( ) . map ( |id| id. parse ( ) . unwrap ( ) ) . collect ( )
223
229
}
0 commit comments