Skip to content

Commit 64b6d0c

Browse files
committed
Rename ASTNode to Expr
The struct previously named ASTNode was rather confusingly named, as there were plenty of other structs that were also nodes in the AST. Expr is a better name.
1 parent 1f87083 commit 64b6d0c

File tree

8 files changed

+241
-248
lines changed

8 files changed

+241
-248
lines changed

src/sqlast/ddl.rs

Lines changed: 2 additions & 2 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};
3+
use super::{Expr, SQLIdent, SQLObjectName};
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

src/sqlast/mod.rs

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub type SQLIdent = String;
5454
/// (e.g. boolean vs string), so the caller must handle expressions of
5555
/// inappropriate type, like `WHERE 1` or `SELECT 1=1`, as necessary.
5656
#[derive(Debug, Clone, PartialEq, Hash)]
57-
pub enum ASTNode {
57+
pub enum Expr {
5858
/// Identifier e.g. table name or column name
5959
SQLIdentifier(SQLIdent),
6060
/// Unqualified wildcard (`*`). SQL allows this in limited contexts (such as right
@@ -67,54 +67,51 @@ pub enum ASTNode {
6767
/// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col`
6868
SQLCompoundIdentifier(Vec<SQLIdent>),
6969
/// `IS NULL` expression
70-
SQLIsNull(Box<ASTNode>),
70+
SQLIsNull(Box<Expr>),
7171
/// `IS NOT NULL` expression
72-
SQLIsNotNull(Box<ASTNode>),
72+
SQLIsNotNull(Box<Expr>),
7373
/// `[ NOT ] IN (val1, val2, ...)`
7474
SQLInList {
75-
expr: Box<ASTNode>,
76-
list: Vec<ASTNode>,
75+
expr: Box<Expr>,
76+
list: Vec<Expr>,
7777
negated: bool,
7878
},
7979
/// `[ NOT ] IN (SELECT ...)`
8080
SQLInSubquery {
81-
expr: Box<ASTNode>,
81+
expr: Box<Expr>,
8282
subquery: Box<SQLQuery>,
8383
negated: bool,
8484
},
8585
/// `<expr> [ NOT ] BETWEEN <low> AND <high>`
8686
SQLBetween {
87-
expr: Box<ASTNode>,
87+
expr: Box<Expr>,
8888
negated: bool,
89-
low: Box<ASTNode>,
90-
high: Box<ASTNode>,
89+
low: Box<Expr>,
90+
high: Box<Expr>,
9191
},
9292
/// Binary expression e.g. `1 + 1` or `foo > bar`
9393
SQLBinaryExpr {
94-
left: Box<ASTNode>,
94+
left: Box<Expr>,
9595
op: SQLOperator,
96-
right: Box<ASTNode>,
96+
right: Box<Expr>,
9797
},
9898
/// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
99-
SQLCast {
100-
expr: Box<ASTNode>,
101-
data_type: SQLType,
102-
},
99+
SQLCast { expr: Box<Expr>, data_type: SQLType },
103100
SQLExtract {
104101
field: SQLDateTimeField,
105-
expr: Box<ASTNode>,
102+
expr: Box<Expr>,
106103
},
107104
/// `expr COLLATE collation`
108105
SQLCollate {
109-
expr: Box<ASTNode>,
106+
expr: Box<Expr>,
110107
collation: SQLObjectName,
111108
},
112109
/// Nested expression e.g. `(foo > bar)` or `(1)`
113-
SQLNested(Box<ASTNode>),
110+
SQLNested(Box<Expr>),
114111
/// Unary expression
115112
SQLUnary {
116113
operator: SQLOperator,
117-
expr: Box<ASTNode>,
114+
expr: Box<Expr>,
118115
},
119116
/// SQLValue
120117
SQLValue(Value),
@@ -125,10 +122,10 @@ pub enum ASTNode {
125122
/// `< 0` nor `1, 2, 3` as allowed in a <simple when clause> per
126123
/// https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-when-clause
127124
SQLCase {
128-
operand: Option<Box<ASTNode>>,
129-
conditions: Vec<ASTNode>,
130-
results: Vec<ASTNode>,
131-
else_result: Option<Box<ASTNode>>,
125+
operand: Option<Box<Expr>>,
126+
conditions: Vec<Expr>,
127+
results: Vec<Expr>,
128+
else_result: Option<Box<Expr>>,
132129
},
133130
/// An exists expression `EXISTS(SELECT ...)`, used in expressions like
134131
/// `WHERE EXISTS (SELECT ...)`.
@@ -138,16 +135,16 @@ pub enum ASTNode {
138135
SQLSubquery(Box<SQLQuery>),
139136
}
140137

141-
impl ToString for ASTNode {
138+
impl ToString for Expr {
142139
fn to_string(&self) -> String {
143140
match self {
144-
ASTNode::SQLIdentifier(s) => s.to_string(),
145-
ASTNode::SQLWildcard => "*".to_string(),
146-
ASTNode::SQLQualifiedWildcard(q) => q.join(".") + ".*",
147-
ASTNode::SQLCompoundIdentifier(s) => s.join("."),
148-
ASTNode::SQLIsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()),
149-
ASTNode::SQLIsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()),
150-
ASTNode::SQLInList {
141+
Expr::SQLIdentifier(s) => s.to_string(),
142+
Expr::SQLWildcard => "*".to_string(),
143+
Expr::SQLQualifiedWildcard(q) => q.join(".") + ".*",
144+
Expr::SQLCompoundIdentifier(s) => s.join("."),
145+
Expr::SQLIsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()),
146+
Expr::SQLIsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()),
147+
Expr::SQLInList {
151148
expr,
152149
list,
153150
negated,
@@ -157,7 +154,7 @@ impl ToString for ASTNode {
157154
if *negated { "NOT " } else { "" },
158155
comma_separated_string(list)
159156
),
160-
ASTNode::SQLInSubquery {
157+
Expr::SQLInSubquery {
161158
expr,
162159
subquery,
163160
negated,
@@ -167,7 +164,7 @@ impl ToString for ASTNode {
167164
if *negated { "NOT " } else { "" },
168165
subquery.to_string()
169166
),
170-
ASTNode::SQLBetween {
167+
Expr::SQLBetween {
171168
expr,
172169
negated,
173170
low,
@@ -179,32 +176,32 @@ impl ToString for ASTNode {
179176
low.to_string(),
180177
high.to_string()
181178
),
182-
ASTNode::SQLBinaryExpr { left, op, right } => format!(
179+
Expr::SQLBinaryExpr { left, op, right } => format!(
183180
"{} {} {}",
184181
left.as_ref().to_string(),
185182
op.to_string(),
186183
right.as_ref().to_string()
187184
),
188-
ASTNode::SQLCast { expr, data_type } => format!(
185+
Expr::SQLCast { expr, data_type } => format!(
189186
"CAST({} AS {})",
190187
expr.as_ref().to_string(),
191188
data_type.to_string()
192189
),
193-
ASTNode::SQLExtract { field, expr } => {
190+
Expr::SQLExtract { field, expr } => {
194191
format!("EXTRACT({} FROM {})", field.to_string(), expr.to_string())
195192
}
196-
ASTNode::SQLCollate { expr, collation } => format!(
193+
Expr::SQLCollate { expr, collation } => format!(
197194
"{} COLLATE {}",
198195
expr.as_ref().to_string(),
199196
collation.to_string()
200197
),
201-
ASTNode::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
202-
ASTNode::SQLUnary { operator, expr } => {
198+
Expr::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
199+
Expr::SQLUnary { operator, expr } => {
203200
format!("{} {}", operator.to_string(), expr.as_ref().to_string())
204201
}
205-
ASTNode::SQLValue(v) => v.to_string(),
206-
ASTNode::SQLFunction(f) => f.to_string(),
207-
ASTNode::SQLCase {
202+
Expr::SQLValue(v) => v.to_string(),
203+
Expr::SQLFunction(f) => f.to_string(),
204+
Expr::SQLCase {
208205
operand,
209206
conditions,
210207
results,
@@ -225,16 +222,16 @@ impl ToString for ASTNode {
225222
}
226223
s + " END"
227224
}
228-
ASTNode::SQLExists(s) => format!("EXISTS ({})", s.to_string()),
229-
ASTNode::SQLSubquery(s) => format!("({})", s.to_string()),
225+
Expr::SQLExists(s) => format!("EXISTS ({})", s.to_string()),
226+
Expr::SQLSubquery(s) => format!("({})", s.to_string()),
230227
}
231228
}
232229
}
233230

234231
/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
235232
#[derive(Debug, Clone, PartialEq, Hash)]
236233
pub struct SQLWindowSpec {
237-
pub partition_by: Vec<ASTNode>,
234+
pub partition_by: Vec<Expr>,
238235
pub order_by: Vec<SQLOrderByExpr>,
239236
pub window_frame: Option<SQLWindowFrame>,
240237
}
@@ -371,14 +368,14 @@ pub enum SQLStatement {
371368
/// Column assignments
372369
assignments: Vec<SQLAssignment>,
373370
/// WHERE
374-
selection: Option<ASTNode>,
371+
selection: Option<Expr>,
375372
},
376373
/// DELETE
377374
SQLDelete {
378375
/// FROM
379376
table_name: SQLObjectName,
380377
/// WHERE
381-
selection: Option<ASTNode>,
378+
selection: Option<Expr>,
382379
},
383380
/// CREATE VIEW
384381
SQLCreateView {
@@ -571,7 +568,7 @@ impl ToString for SQLObjectName {
571568
#[derive(Debug, Clone, PartialEq, Hash)]
572569
pub struct SQLAssignment {
573570
pub id: SQLIdent,
574-
pub value: ASTNode,
571+
pub value: Expr,
575572
}
576573

577574
impl ToString for SQLAssignment {
@@ -587,7 +584,7 @@ pub struct SQLColumnDef {
587584
pub data_type: SQLType,
588585
pub is_primary: bool,
589586
pub is_unique: bool,
590-
pub default: Option<ASTNode>,
587+
pub default: Option<Expr>,
591588
pub allow_null: bool,
592589
}
593590

@@ -614,7 +611,7 @@ impl ToString for SQLColumnDef {
614611
#[derive(Debug, Clone, PartialEq, Hash)]
615612
pub struct SQLFunction {
616613
pub name: SQLObjectName,
617-
pub args: Vec<ASTNode>,
614+
pub args: Vec<Expr>,
618615
pub over: Option<SQLWindowSpec>,
619616
// aggregate functions may specify eg `COUNT(DISTINCT x)`
620617
pub distinct: bool,

src/sqlast/query.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ pub struct SQLQuery {
1111
/// ORDER BY
1212
pub order_by: Vec<SQLOrderByExpr>,
1313
/// LIMIT { <N> | ALL }
14-
pub limit: Option<ASTNode>,
14+
pub limit: Option<Expr>,
1515
/// OFFSET <N> { ROW | ROWS }
16-
pub offset: Option<ASTNode>,
16+
pub offset: Option<Expr>,
1717
/// FETCH { FIRST | NEXT } <N> [ PERCENT ] { ROW | ROWS } | { ONLY | WITH TIES }
1818
pub fetch: Option<Fetch>,
1919
}
@@ -117,11 +117,11 @@ pub struct SQLSelect {
117117
/// JOIN
118118
pub joins: Vec<Join>,
119119
/// WHERE
120-
pub selection: Option<ASTNode>,
120+
pub selection: Option<Expr>,
121121
/// GROUP BY
122-
pub group_by: Vec<ASTNode>,
122+
pub group_by: Vec<Expr>,
123123
/// HAVING
124-
pub having: Option<ASTNode>,
124+
pub having: Option<Expr>,
125125
}
126126

127127
impl ToString for SQLSelect {
@@ -175,9 +175,9 @@ impl ToString for Cte {
175175
#[derive(Debug, Clone, PartialEq, Hash)]
176176
pub enum SQLSelectItem {
177177
/// Any expression, not followed by `[ AS ] alias`
178-
UnnamedExpression(ASTNode),
178+
UnnamedExpr(Expr),
179179
/// An expression, followed by `[ AS ] alias`
180-
ExpressionWithAlias { expr: ASTNode, alias: SQLIdent },
180+
NamedExpr { expr: Expr, alias: SQLIdent },
181181
/// `alias.*` or even `schema.table.*`
182182
QualifiedWildcard(SQLObjectName),
183183
/// An unqualified `*`
@@ -187,8 +187,8 @@ pub enum SQLSelectItem {
187187
impl ToString for SQLSelectItem {
188188
fn to_string(&self) -> String {
189189
match &self {
190-
SQLSelectItem::UnnamedExpression(expr) => expr.to_string(),
191-
SQLSelectItem::ExpressionWithAlias { expr, alias } => {
190+
SQLSelectItem::UnnamedExpr(expr) => expr.to_string(),
191+
SQLSelectItem::NamedExpr { expr, alias } => {
192192
format!("{} AS {}", expr.to_string(), alias)
193193
}
194194
SQLSelectItem::QualifiedWildcard(prefix) => format!("{}.*", prefix.to_string()),
@@ -206,9 +206,9 @@ pub enum TableFactor {
206206
/// Arguments of a table-valued function, as supported by Postgres
207207
/// and MSSQL. Note that deprecated MSSQL `FROM foo (NOLOCK)` syntax
208208
/// will also be parsed as `args`.
209-
args: Vec<ASTNode>,
209+
args: Vec<Expr>,
210210
/// MSSQL-specific `WITH (...)` hints such as NOLOCK.
211-
with_hints: Vec<ASTNode>,
211+
with_hints: Vec<Expr>,
212212
},
213213
Derived {
214214
lateral: bool,
@@ -337,15 +337,15 @@ pub enum JoinOperator {
337337

338338
#[derive(Debug, Clone, PartialEq, Hash)]
339339
pub enum JoinConstraint {
340-
On(ASTNode),
340+
On(Expr),
341341
Using(Vec<SQLIdent>),
342342
Natural,
343343
}
344344

345345
/// SQL ORDER BY expression
346346
#[derive(Debug, Clone, PartialEq, Hash)]
347347
pub struct SQLOrderByExpr {
348-
pub expr: ASTNode,
348+
pub expr: Expr,
349349
pub asc: Option<bool>,
350350
}
351351

@@ -363,7 +363,7 @@ impl ToString for SQLOrderByExpr {
363363
pub struct Fetch {
364364
pub with_ties: bool,
365365
pub percent: bool,
366-
pub quantity: Option<ASTNode>,
366+
pub quantity: Option<Expr>,
367367
}
368368

369369
impl ToString for Fetch {
@@ -384,7 +384,7 @@ impl ToString for Fetch {
384384
}
385385

386386
#[derive(Debug, Clone, PartialEq, Hash)]
387-
pub struct SQLValues(pub Vec<Vec<ASTNode>>);
387+
pub struct SQLValues(pub Vec<Vec<Expr>>);
388388

389389
impl ToString for SQLValues {
390390
fn to_string(&self) -> String {

0 commit comments

Comments
 (0)