Skip to content

Commit 646d1e1

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. Also rename the UnnamedExpression and ExpressionWithAlias variants of SQLSelectItem to UnnamedExpr and ExprWithAlias, respectively, to match the new shorthand for the word "expression".
1 parent e6b2633 commit 646d1e1

File tree

8 files changed

+255
-264
lines changed

8 files changed

+255
-264
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 & 49 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,52 @@ 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))`
106-
SQLCast {
107-
expr: Box<ASTNode>,
108-
data_type: SQLType,
109-
},
106+
SQLCast { expr: Box<Expr>, data_type: SQLType },
110107
SQLExtract {
111108
field: SQLDateTimeField,
112-
expr: Box<ASTNode>,
109+
expr: Box<Expr>,
113110
},
114111
/// `expr COLLATE collation`
115112
SQLCollate {
116-
expr: Box<ASTNode>,
113+
expr: Box<Expr>,
117114
collation: SQLObjectName,
118115
},
119116
/// Nested expression e.g. `(foo > bar)` or `(1)`
120-
SQLNested(Box<ASTNode>),
117+
SQLNested(Box<Expr>),
121118
/// SQLValue
122119
SQLValue(Value),
123120
/// Scalar function call e.g. `LEFT(foo, 5)`
@@ -128,10 +125,10 @@ pub enum ASTNode {
128125
/// not `< 0` nor `1, 2, 3` as allowed in a `<simple when clause>` per
129126
/// <https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-when-clause>
130127
SQLCase {
131-
operand: Option<Box<ASTNode>>,
132-
conditions: Vec<ASTNode>,
133-
results: Vec<ASTNode>,
134-
else_result: Option<Box<ASTNode>>,
128+
operand: Option<Box<Expr>>,
129+
conditions: Vec<Expr>,
130+
results: Vec<Expr>,
131+
else_result: Option<Box<Expr>>,
135132
},
136133
/// An exists expression `EXISTS(SELECT ...)`, used in expressions like
137134
/// `WHERE EXISTS (SELECT ...)`.
@@ -141,16 +138,16 @@ pub enum ASTNode {
141138
SQLSubquery(Box<SQLQuery>),
142139
}
143140

144-
impl ToString for ASTNode {
141+
impl ToString for Expr {
145142
fn to_string(&self) -> String {
146143
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 {
144+
Expr::SQLIdentifier(s) => s.to_string(),
145+
Expr::SQLWildcard => "*".to_string(),
146+
Expr::SQLQualifiedWildcard(q) => q.join(".") + ".*",
147+
Expr::SQLCompoundIdentifier(s) => s.join("."),
148+
Expr::SQLIsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()),
149+
Expr::SQLIsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()),
150+
Expr::SQLInList {
154151
expr,
155152
list,
156153
negated,
@@ -160,7 +157,7 @@ impl ToString for ASTNode {
160157
if *negated { "NOT " } else { "" },
161158
comma_separated_string(list)
162159
),
163-
ASTNode::SQLInSubquery {
160+
Expr::SQLInSubquery {
164161
expr,
165162
subquery,
166163
negated,
@@ -170,7 +167,7 @@ impl ToString for ASTNode {
170167
if *negated { "NOT " } else { "" },
171168
subquery.to_string()
172169
),
173-
ASTNode::SQLBetween {
170+
Expr::SQLBetween {
174171
expr,
175172
negated,
176173
low,
@@ -182,32 +179,32 @@ impl ToString for ASTNode {
182179
low.to_string(),
183180
high.to_string()
184181
),
185-
ASTNode::SQLBinaryOp { left, op, right } => format!(
182+
Expr::SQLBinaryOp { left, op, right } => format!(
186183
"{} {} {}",
187184
left.as_ref().to_string(),
188185
op.to_string(),
189186
right.as_ref().to_string()
190187
),
191-
ASTNode::SQLUnaryOp { op, expr } => {
188+
Expr::SQLUnaryOp { op, expr } => {
192189
format!("{} {}", op.to_string(), expr.as_ref().to_string())
193190
}
194-
ASTNode::SQLCast { expr, data_type } => format!(
191+
Expr::SQLCast { expr, data_type } => format!(
195192
"CAST({} AS {})",
196193
expr.as_ref().to_string(),
197194
data_type.to_string()
198195
),
199-
ASTNode::SQLExtract { field, expr } => {
196+
Expr::SQLExtract { field, expr } => {
200197
format!("EXTRACT({} FROM {})", field.to_string(), expr.to_string())
201198
}
202-
ASTNode::SQLCollate { expr, collation } => format!(
199+
Expr::SQLCollate { expr, collation } => format!(
203200
"{} COLLATE {}",
204201
expr.as_ref().to_string(),
205202
collation.to_string()
206203
),
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 {
204+
Expr::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
205+
Expr::SQLValue(v) => v.to_string(),
206+
Expr::SQLFunction(f) => f.to_string(),
207+
Expr::SQLCase {
211208
operand,
212209
conditions,
213210
results,
@@ -228,16 +225,16 @@ impl ToString for ASTNode {
228225
}
229226
s + " END"
230227
}
231-
ASTNode::SQLExists(s) => format!("EXISTS ({})", s.to_string()),
232-
ASTNode::SQLSubquery(s) => format!("({})", s.to_string()),
228+
Expr::SQLExists(s) => format!("EXISTS ({})", s.to_string()),
229+
Expr::SQLSubquery(s) => format!("({})", s.to_string()),
233230
}
234231
}
235232
}
236233

237234
/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
238235
#[derive(Debug, Clone, PartialEq, Hash)]
239236
pub struct SQLWindowSpec {
240-
pub partition_by: Vec<ASTNode>,
237+
pub partition_by: Vec<Expr>,
241238
pub order_by: Vec<SQLOrderByExpr>,
242239
pub window_frame: Option<SQLWindowFrame>,
243240
}
@@ -374,14 +371,14 @@ pub enum SQLStatement {
374371
/// Column assignments
375372
assignments: Vec<SQLAssignment>,
376373
/// WHERE
377-
selection: Option<ASTNode>,
374+
selection: Option<Expr>,
378375
},
379376
/// DELETE
380377
SQLDelete {
381378
/// FROM
382379
table_name: SQLObjectName,
383380
/// WHERE
384-
selection: Option<ASTNode>,
381+
selection: Option<Expr>,
385382
},
386383
/// CREATE VIEW
387384
SQLCreateView {
@@ -604,7 +601,7 @@ impl ToString for SQLObjectName {
604601
#[derive(Debug, Clone, PartialEq, Hash)]
605602
pub struct SQLAssignment {
606603
pub id: SQLIdent,
607-
pub value: ASTNode,
604+
pub value: Expr,
608605
}
609606

610607
impl ToString for SQLAssignment {
@@ -617,7 +614,7 @@ impl ToString for SQLAssignment {
617614
#[derive(Debug, Clone, PartialEq, Hash)]
618615
pub struct SQLFunction {
619616
pub name: SQLObjectName,
620-
pub args: Vec<ASTNode>,
617+
pub args: Vec<Expr>,
621618
pub over: Option<SQLWindowSpec>,
622619
// aggregate functions may specify eg `COUNT(DISTINCT x)`
623620
pub distinct: bool,

src/sqlast/query.rs

Lines changed: 15 additions & 15 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+
UnnamedExpr(Expr),
181181
/// An expression, followed by `[ AS ] alias`
182-
ExpressionWithAlias { expr: ASTNode, alias: SQLIdent },
182+
ExprWithAlias { expr: Expr, alias: SQLIdent },
183183
/// `alias.*` or even `schema.table.*`
184184
QualifiedWildcard(SQLObjectName),
185185
/// An unqualified `*`
@@ -189,8 +189,8 @@ pub enum SQLSelectItem {
189189
impl ToString for SQLSelectItem {
190190
fn to_string(&self) -> String {
191191
match &self {
192-
SQLSelectItem::UnnamedExpression(expr) => expr.to_string(),
193-
SQLSelectItem::ExpressionWithAlias { expr, alias } => {
192+
SQLSelectItem::UnnamedExpr(expr) => expr.to_string(),
193+
SQLSelectItem::ExprWithAlias { expr, alias } => {
194194
format!("{} AS {}", expr.to_string(), alias)
195195
}
196196
SQLSelectItem::QualifiedWildcard(prefix) => format!("{}.*", prefix.to_string()),
@@ -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)