Skip to content

Commit 1b8bfc7

Browse files
committed
Rename ASTNode to Expr
The ASTNode enum was confusingly named. In the past, the name made sense, as the enum contained nearly all of the nodes in the AST, but over time, pieces have been split into different structs, like SQLStatement and SQLQuery. The ASTNode enum now contains only contains expression nodes, so Expr is a better name.
1 parent e6b2633 commit 1b8bfc7

File tree

8 files changed

+239
-239
lines changed

8 files changed

+239
-239
lines changed

src/sqlast/ddl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! AST types specific to CREATE/ALTER variants of `SQLStatement`
22
//! (commonly referred to as Data Definition Language, or DDL)
3-
use super::{ASTNode, SQLIdent, SQLObjectName, SQLType};
3+
use super::{Expr, SQLIdent, SQLObjectName, SQLType};
44

55
/// An `ALTER TABLE` (`SQLStatement::SQLAlterTable`) operation
66
#[derive(Debug, Clone, PartialEq, Hash)]
@@ -42,7 +42,7 @@ pub enum TableConstraint {
4242
/// `[ CONSTRAINT <name> ] CHECK (<expr>)`
4343
Check {
4444
name: Option<SQLIdent>,
45-
expr: Box<ASTNode>,
45+
expr: Box<Expr>,
4646
},
4747
}
4848

@@ -145,7 +145,7 @@ pub enum ColumnOption {
145145
/// `NOT NULL`
146146
NotNull,
147147
/// `DEFAULT <restricted-expr>`
148-
Default(ASTNode),
148+
Default(Expr),
149149
/// `{ PRIMARY KEY | UNIQUE }`
150150
Unique {
151151
is_primary: bool,
@@ -157,7 +157,7 @@ pub enum ColumnOption {
157157
referred_columns: Vec<SQLIdent>,
158158
},
159159
// `CHECK (<expr>)`
160-
Check(ASTNode),
160+
Check(Expr),
161161
}
162162

163163
impl ToString for ColumnOption {

src/sqlast/mod.rs

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub type SQLIdent = String;
5353
/// (e.g. boolean vs string), so the caller must handle expressions of
5454
/// inappropriate type, like `WHERE 1` or `SELECT 1=1`, as necessary.
5555
#[derive(Debug, Clone, PartialEq, Hash)]
56-
pub enum ASTNode {
56+
pub enum Expr {
5757
/// Identifier e.g. table name or column name
5858
SQLIdentifier(SQLIdent),
5959
/// Unqualified wildcard (`*`). SQL allows this in limited contexts, such as:
@@ -69,55 +69,55 @@ pub enum ASTNode {
6969
/// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col`
7070
SQLCompoundIdentifier(Vec<SQLIdent>),
7171
/// `IS NULL` expression
72-
SQLIsNull(Box<ASTNode>),
72+
SQLIsNull(Box<Expr>),
7373
/// `IS NOT NULL` expression
74-
SQLIsNotNull(Box<ASTNode>),
74+
SQLIsNotNull(Box<Expr>),
7575
/// `[ NOT ] IN (val1, val2, ...)`
7676
SQLInList {
77-
expr: Box<ASTNode>,
78-
list: Vec<ASTNode>,
77+
expr: Box<Expr>,
78+
list: Vec<Expr>,
7979
negated: bool,
8080
},
8181
/// `[ NOT ] IN (SELECT ...)`
8282
SQLInSubquery {
83-
expr: Box<ASTNode>,
83+
expr: Box<Expr>,
8484
subquery: Box<SQLQuery>,
8585
negated: bool,
8686
},
8787
/// `<expr> [ NOT ] BETWEEN <low> AND <high>`
8888
SQLBetween {
89-
expr: Box<ASTNode>,
89+
expr: Box<Expr>,
9090
negated: bool,
91-
low: Box<ASTNode>,
92-
high: Box<ASTNode>,
91+
low: Box<Expr>,
92+
high: Box<Expr>,
9393
},
9494
/// Binary operation e.g. `1 + 1` or `foo > bar`
9595
SQLBinaryOp {
96-
left: Box<ASTNode>,
96+
left: Box<Expr>,
9797
op: SQLBinaryOperator,
98-
right: Box<ASTNode>,
98+
right: Box<Expr>,
9999
},
100100
/// Unary operation e.g. `NOT foo`
101101
SQLUnaryOp {
102102
op: SQLUnaryOperator,
103-
expr: Box<ASTNode>,
103+
expr: Box<Expr>,
104104
},
105105
/// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
106106
SQLCast {
107-
expr: Box<ASTNode>,
107+
expr: Box<Expr>,
108108
data_type: SQLType,
109109
},
110110
SQLExtract {
111111
field: SQLDateTimeField,
112-
expr: Box<ASTNode>,
112+
expr: Box<Expr>,
113113
},
114114
/// `expr COLLATE collation`
115115
SQLCollate {
116-
expr: Box<ASTNode>,
116+
expr: Box<Expr>,
117117
collation: SQLObjectName,
118118
},
119119
/// Nested expression e.g. `(foo > bar)` or `(1)`
120-
SQLNested(Box<ASTNode>),
120+
SQLNested(Box<Expr>),
121121
/// SQLValue
122122
SQLValue(Value),
123123
/// Scalar function call e.g. `LEFT(foo, 5)`
@@ -128,10 +128,10 @@ pub enum ASTNode {
128128
/// not `< 0` nor `1, 2, 3` as allowed in a `<simple when clause>` per
129129
/// <https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-when-clause>
130130
SQLCase {
131-
operand: Option<Box<ASTNode>>,
132-
conditions: Vec<ASTNode>,
133-
results: Vec<ASTNode>,
134-
else_result: Option<Box<ASTNode>>,
131+
operand: Option<Box<Expr>>,
132+
conditions: Vec<Expr>,
133+
results: Vec<Expr>,
134+
else_result: Option<Box<Expr>>,
135135
},
136136
/// An exists expression `EXISTS(SELECT ...)`, used in expressions like
137137
/// `WHERE EXISTS (SELECT ...)`.
@@ -141,16 +141,16 @@ pub enum ASTNode {
141141
SQLSubquery(Box<SQLQuery>),
142142
}
143143

144-
impl ToString for ASTNode {
144+
impl ToString for Expr {
145145
fn to_string(&self) -> String {
146146
match self {
147-
ASTNode::SQLIdentifier(s) => s.to_string(),
148-
ASTNode::SQLWildcard => "*".to_string(),
149-
ASTNode::SQLQualifiedWildcard(q) => q.join(".") + ".*",
150-
ASTNode::SQLCompoundIdentifier(s) => s.join("."),
151-
ASTNode::SQLIsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()),
152-
ASTNode::SQLIsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()),
153-
ASTNode::SQLInList {
147+
Expr::SQLIdentifier(s) => s.to_string(),
148+
Expr::SQLWildcard => "*".to_string(),
149+
Expr::SQLQualifiedWildcard(q) => q.join(".") + ".*",
150+
Expr::SQLCompoundIdentifier(s) => s.join("."),
151+
Expr::SQLIsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()),
152+
Expr::SQLIsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()),
153+
Expr::SQLInList {
154154
expr,
155155
list,
156156
negated,
@@ -160,7 +160,7 @@ impl ToString for ASTNode {
160160
if *negated { "NOT " } else { "" },
161161
comma_separated_string(list)
162162
),
163-
ASTNode::SQLInSubquery {
163+
Expr::SQLInSubquery {
164164
expr,
165165
subquery,
166166
negated,
@@ -170,7 +170,7 @@ impl ToString for ASTNode {
170170
if *negated { "NOT " } else { "" },
171171
subquery.to_string()
172172
),
173-
ASTNode::SQLBetween {
173+
Expr::SQLBetween {
174174
expr,
175175
negated,
176176
low,
@@ -182,32 +182,32 @@ impl ToString for ASTNode {
182182
low.to_string(),
183183
high.to_string()
184184
),
185-
ASTNode::SQLBinaryOp { left, op, right } => format!(
185+
Expr::SQLBinaryOp { left, op, right } => format!(
186186
"{} {} {}",
187187
left.as_ref().to_string(),
188188
op.to_string(),
189189
right.as_ref().to_string()
190190
),
191-
ASTNode::SQLUnaryOp { op, expr } => {
191+
Expr::SQLUnaryOp { op, expr } => {
192192
format!("{} {}", op.to_string(), expr.as_ref().to_string())
193193
}
194-
ASTNode::SQLCast { expr, data_type } => format!(
194+
Expr::SQLCast { expr, data_type } => format!(
195195
"CAST({} AS {})",
196196
expr.as_ref().to_string(),
197197
data_type.to_string()
198198
),
199-
ASTNode::SQLExtract { field, expr } => {
199+
Expr::SQLExtract { field, expr } => {
200200
format!("EXTRACT({} FROM {})", field.to_string(), expr.to_string())
201201
}
202-
ASTNode::SQLCollate { expr, collation } => format!(
202+
Expr::SQLCollate { expr, collation } => format!(
203203
"{} COLLATE {}",
204204
expr.as_ref().to_string(),
205205
collation.to_string()
206206
),
207-
ASTNode::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
208-
ASTNode::SQLValue(v) => v.to_string(),
209-
ASTNode::SQLFunction(f) => f.to_string(),
210-
ASTNode::SQLCase {
207+
Expr::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
208+
Expr::SQLValue(v) => v.to_string(),
209+
Expr::SQLFunction(f) => f.to_string(),
210+
Expr::SQLCase {
211211
operand,
212212
conditions,
213213
results,
@@ -228,16 +228,16 @@ impl ToString for ASTNode {
228228
}
229229
s + " END"
230230
}
231-
ASTNode::SQLExists(s) => format!("EXISTS ({})", s.to_string()),
232-
ASTNode::SQLSubquery(s) => format!("({})", s.to_string()),
231+
Expr::SQLExists(s) => format!("EXISTS ({})", s.to_string()),
232+
Expr::SQLSubquery(s) => format!("({})", s.to_string()),
233233
}
234234
}
235235
}
236236

237237
/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
238238
#[derive(Debug, Clone, PartialEq, Hash)]
239239
pub struct SQLWindowSpec {
240-
pub partition_by: Vec<ASTNode>,
240+
pub partition_by: Vec<Expr>,
241241
pub order_by: Vec<SQLOrderByExpr>,
242242
pub window_frame: Option<SQLWindowFrame>,
243243
}
@@ -374,14 +374,14 @@ pub enum SQLStatement {
374374
/// Column assignments
375375
assignments: Vec<SQLAssignment>,
376376
/// WHERE
377-
selection: Option<ASTNode>,
377+
selection: Option<Expr>,
378378
},
379379
/// DELETE
380380
SQLDelete {
381381
/// FROM
382382
table_name: SQLObjectName,
383383
/// WHERE
384-
selection: Option<ASTNode>,
384+
selection: Option<Expr>,
385385
},
386386
/// CREATE VIEW
387387
SQLCreateView {
@@ -604,7 +604,7 @@ impl ToString for SQLObjectName {
604604
#[derive(Debug, Clone, PartialEq, Hash)]
605605
pub struct SQLAssignment {
606606
pub id: SQLIdent,
607-
pub value: ASTNode,
607+
pub value: Expr,
608608
}
609609

610610
impl ToString for SQLAssignment {
@@ -617,7 +617,7 @@ impl ToString for SQLAssignment {
617617
#[derive(Debug, Clone, PartialEq, Hash)]
618618
pub struct SQLFunction {
619619
pub name: SQLObjectName,
620-
pub args: Vec<ASTNode>,
620+
pub args: Vec<Expr>,
621621
pub over: Option<SQLWindowSpec>,
622622
// aggregate functions may specify eg `COUNT(DISTINCT x)`
623623
pub distinct: bool,

src/sqlast/query.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ pub struct SQLQuery {
2323
/// ORDER BY
2424
pub order_by: Vec<SQLOrderByExpr>,
2525
/// `LIMIT { <N> | ALL }`
26-
pub limit: Option<ASTNode>,
26+
pub limit: Option<Expr>,
2727
/// `OFFSET <N> { ROW | ROWS }`
28-
pub offset: Option<ASTNode>,
28+
pub offset: Option<Expr>,
2929
/// `FETCH { FIRST | NEXT } <N> [ PERCENT ] { ROW | ROWS } | { ONLY | WITH TIES }`
3030
pub fetch: Option<Fetch>,
3131
}
@@ -127,11 +127,11 @@ pub struct SQLSelect {
127127
/// FROM
128128
pub from: Vec<TableWithJoins>,
129129
/// WHERE
130-
pub selection: Option<ASTNode>,
130+
pub selection: Option<Expr>,
131131
/// GROUP BY
132-
pub group_by: Vec<ASTNode>,
132+
pub group_by: Vec<Expr>,
133133
/// HAVING
134-
pub having: Option<ASTNode>,
134+
pub having: Option<Expr>,
135135
}
136136

137137
impl ToString for SQLSelect {
@@ -177,9 +177,9 @@ impl ToString for Cte {
177177
#[derive(Debug, Clone, PartialEq, Hash)]
178178
pub enum SQLSelectItem {
179179
/// Any expression, not followed by `[ AS ] alias`
180-
UnnamedExpression(ASTNode),
180+
UnnamedExpression(Expr),
181181
/// An expression, followed by `[ AS ] alias`
182-
ExpressionWithAlias { expr: ASTNode, alias: SQLIdent },
182+
ExpressionWithAlias { expr: Expr, alias: SQLIdent },
183183
/// `alias.*` or even `schema.table.*`
184184
QualifiedWildcard(SQLObjectName),
185185
/// An unqualified `*`
@@ -224,9 +224,9 @@ pub enum TableFactor {
224224
/// Arguments of a table-valued function, as supported by Postgres
225225
/// and MSSQL. Note that deprecated MSSQL `FROM foo (NOLOCK)` syntax
226226
/// will also be parsed as `args`.
227-
args: Vec<ASTNode>,
227+
args: Vec<Expr>,
228228
/// MSSQL-specific `WITH (...)` hints such as NOLOCK.
229-
with_hints: Vec<ASTNode>,
229+
with_hints: Vec<Expr>,
230230
},
231231
Derived {
232232
lateral: bool,
@@ -361,15 +361,15 @@ pub enum JoinOperator {
361361

362362
#[derive(Debug, Clone, PartialEq, Hash)]
363363
pub enum JoinConstraint {
364-
On(ASTNode),
364+
On(Expr),
365365
Using(Vec<SQLIdent>),
366366
Natural,
367367
}
368368

369369
/// SQL ORDER BY expression
370370
#[derive(Debug, Clone, PartialEq, Hash)]
371371
pub struct SQLOrderByExpr {
372-
pub expr: ASTNode,
372+
pub expr: Expr,
373373
pub asc: Option<bool>,
374374
}
375375

@@ -387,7 +387,7 @@ impl ToString for SQLOrderByExpr {
387387
pub struct Fetch {
388388
pub with_ties: bool,
389389
pub percent: bool,
390-
pub quantity: Option<ASTNode>,
390+
pub quantity: Option<Expr>,
391391
}
392392

393393
impl ToString for Fetch {
@@ -408,7 +408,7 @@ impl ToString for Fetch {
408408
}
409409

410410
#[derive(Debug, Clone, PartialEq, Hash)]
411-
pub struct SQLValues(pub Vec<Vec<ASTNode>>);
411+
pub struct SQLValues(pub Vec<Vec<Expr>>);
412412

413413
impl ToString for SQLValues {
414414
fn to_string(&self) -> String {

0 commit comments

Comments
 (0)