Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ae706e1

Browse files
committedJun 19, 2019
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 ae706e1

File tree

8 files changed

+249
-258
lines changed

8 files changed

+249
-258
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 {

‎src/sqlparser.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ impl Parser {
151151
}
152152

153153
/// Parse a new expression
154-
pub fn parse_expr(&mut self) -> Result<ASTNode, ParserError> {
154+
pub fn parse_expr(&mut self) -> Result<Expr, ParserError> {
155155
self.parse_subexpr(0)
156156
}
157157

158158
/// Parse tokens until the precedence changes
159-
pub fn parse_subexpr(&mut self, precedence: u8) -> Result<ASTNode, ParserError> {
159+
pub fn parse_subexpr(&mut self, precedence: u8) -> Result<Expr, ParserError> {
160160
debug!("parsing expr");
161161
let mut expr = self.parse_prefix()?;
162162
debug!("prefix: {:?}", expr);
@@ -173,7 +173,7 @@ impl Parser {
173173
}
174174

175175
/// Parse an expression prefix
176-
pub fn parse_prefix(&mut self) -> Result<ASTNode, ParserError> {
176+
pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
177177
let tok = self
178178
.next_token()
179179
.ok_or_else(|| ParserError::ParserError("Unexpected EOF".to_string()))?;
@@ -185,16 +185,16 @@ impl Parser {
185185
}
186186
"CASE" => self.parse_case_expression(),
187187
"CAST" => self.parse_cast_expression(),
188-
"DATE" => Ok(ASTNode::SQLValue(Value::Date(self.parse_literal_string()?))),
188+
"DATE" => Ok(Expr::SQLValue(Value::Date(self.parse_literal_string()?))),
189189
"EXISTS" => self.parse_exists_expression(),
190190
"EXTRACT" => self.parse_extract_expression(),
191191
"INTERVAL" => self.parse_literal_interval(),
192-
"NOT" => Ok(ASTNode::SQLUnaryOp {
192+
"NOT" => Ok(Expr::SQLUnaryOp {
193193
op: SQLUnaryOperator::Not,
194194
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
195195
}),
196-
"TIME" => Ok(ASTNode::SQLValue(Value::Time(self.parse_literal_string()?))),
197-
"TIMESTAMP" => Ok(ASTNode::SQLValue(Value::Timestamp(
196+
"TIME" => Ok(Expr::SQLValue(Value::Time(self.parse_literal_string()?))),
197+
"TIMESTAMP" => Ok(Expr::SQLValue(Value::Timestamp(
198198
self.parse_literal_string()?,
199199
))),
200200
// Here `w` is a word, check if it's a part of a multi-part
@@ -217,25 +217,25 @@ impl Parser {
217217
}
218218
}
219219
if ends_with_wildcard {
220-
Ok(ASTNode::SQLQualifiedWildcard(id_parts))
220+
Ok(Expr::SQLQualifiedWildcard(id_parts))
221221
} else if self.consume_token(&Token::LParen) {
222222
self.prev_token();
223223
self.parse_function(SQLObjectName(id_parts))
224224
} else {
225-
Ok(ASTNode::SQLCompoundIdentifier(id_parts))
225+
Ok(Expr::SQLCompoundIdentifier(id_parts))
226226
}
227227
}
228-
_ => Ok(ASTNode::SQLIdentifier(w.as_sql_ident())),
228+
_ => Ok(Expr::SQLIdentifier(w.as_sql_ident())),
229229
},
230230
}, // End of Token::SQLWord
231-
Token::Mult => Ok(ASTNode::SQLWildcard),
231+
Token::Mult => Ok(Expr::SQLWildcard),
232232
tok @ Token::Minus | tok @ Token::Plus => {
233233
let op = if tok == Token::Plus {
234234
SQLUnaryOperator::Plus
235235
} else {
236236
SQLUnaryOperator::Minus
237237
};
238-
Ok(ASTNode::SQLUnaryOp {
238+
Ok(Expr::SQLUnaryOp {
239239
op,
240240
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
241241
})
@@ -250,9 +250,9 @@ impl Parser {
250250
Token::LParen => {
251251
let expr = if self.parse_keyword("SELECT") || self.parse_keyword("WITH") {
252252
self.prev_token();
253-
ASTNode::SQLSubquery(Box::new(self.parse_query()?))
253+
Expr::SQLSubquery(Box::new(self.parse_query()?))
254254
} else {
255-
ASTNode::SQLNested(Box::new(self.parse_expr()?))
255+
Expr::SQLNested(Box::new(self.parse_expr()?))
256256
};
257257
self.expect_token(&Token::RParen)?;
258258
Ok(expr)
@@ -261,7 +261,7 @@ impl Parser {
261261
}?;
262262

263263
if self.parse_keyword("COLLATE") {
264-
Ok(ASTNode::SQLCollate {
264+
Ok(Expr::SQLCollate {
265265
expr: Box::new(expr),
266266
collation: self.parse_object_name()?,
267267
})
@@ -270,7 +270,7 @@ impl Parser {
270270
}
271271
}
272272

273-
pub fn parse_function(&mut self, name: SQLObjectName) -> Result<ASTNode, ParserError> {
273+
pub fn parse_function(&mut self, name: SQLObjectName) -> Result<Expr, ParserError> {
274274
self.expect_token(&Token::LParen)?;
275275
let all = self.parse_keyword("ALL");
276276
let distinct = self.parse_keyword("DISTINCT");
@@ -306,7 +306,7 @@ impl Parser {
306306
None
307307
};
308308

309-
Ok(ASTNode::SQLFunction(SQLFunction {
309+
Ok(Expr::SQLFunction(SQLFunction {
310310
name,
311311
args,
312312
over,
@@ -366,7 +366,7 @@ impl Parser {
366366
}
367367
}
368368

369-
pub fn parse_case_expression(&mut self) -> Result<ASTNode, ParserError> {
369+
pub fn parse_case_expression(&mut self) -> Result<Expr, ParserError> {
370370
let mut operand = None;
371371
if !self.parse_keyword("WHEN") {
372372
operand = Some(Box::new(self.parse_expr()?));
@@ -388,7 +388,7 @@ impl Parser {
388388
None
389389
};
390390
self.expect_keyword("END")?;
391-
Ok(ASTNode::SQLCase {
391+
Ok(Expr::SQLCase {
392392
operand,
393393
conditions,
394394
results,
@@ -397,33 +397,33 @@ impl Parser {
397397
}
398398

399399
/// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
400-
pub fn parse_cast_expression(&mut self) -> Result<ASTNode, ParserError> {
400+
pub fn parse_cast_expression(&mut self) -> Result<Expr, ParserError> {
401401
self.expect_token(&Token::LParen)?;
402402
let expr = self.parse_expr()?;
403403
self.expect_keyword("AS")?;
404404
let data_type = self.parse_data_type()?;
405405
self.expect_token(&Token::RParen)?;
406-
Ok(ASTNode::SQLCast {
406+
Ok(Expr::SQLCast {
407407
expr: Box::new(expr),
408408
data_type,
409409
})
410410
}
411411

412412
/// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
413-
pub fn parse_exists_expression(&mut self) -> Result<ASTNode, ParserError> {
413+
pub fn parse_exists_expression(&mut self) -> Result<Expr, ParserError> {
414414
self.expect_token(&Token::LParen)?;
415-
let exists_node = ASTNode::SQLExists(Box::new(self.parse_query()?));
415+
let exists_node = Expr::SQLExists(Box::new(self.parse_query()?));
416416
self.expect_token(&Token::RParen)?;
417417
Ok(exists_node)
418418
}
419419

420-
pub fn parse_extract_expression(&mut self) -> Result<ASTNode, ParserError> {
420+
pub fn parse_extract_expression(&mut self) -> Result<Expr, ParserError> {
421421
self.expect_token(&Token::LParen)?;
422422
let field = self.parse_date_time_field()?;
423423
self.expect_keyword("FROM")?;
424424
let expr = self.parse_expr()?;
425425
self.expect_token(&Token::RParen)?;
426-
Ok(ASTNode::SQLExtract {
426+
Ok(Expr::SQLExtract {
427427
field,
428428
expr: Box::new(expr),
429429
})
@@ -462,7 +462,7 @@ impl Parser {
462462
/// 6. `INTERVAL '1:1' HOUR (5) TO MINUTE (5)`
463463
///
464464
/// Note that we do not currently attempt to parse the quoted value.
465-
pub fn parse_literal_interval(&mut self) -> Result<ASTNode, ParserError> {
465+
pub fn parse_literal_interval(&mut self) -> Result<Expr, ParserError> {
466466
// The SQL standard allows an optional sign before the value string, but
467467
// it is not clear if any implementations support that syntax, so we
468468
// don't currently try to parse it. (The sign can instead be included
@@ -504,7 +504,7 @@ impl Parser {
504504
}
505505
};
506506

507-
Ok(ASTNode::SQLValue(Value::Interval {
507+
Ok(Expr::SQLValue(Value::Interval {
508508
value,
509509
leading_field,
510510
leading_precision,
@@ -514,7 +514,7 @@ impl Parser {
514514
}
515515

516516
/// Parse an operator following an expression
517-
pub fn parse_infix(&mut self, expr: ASTNode, precedence: u8) -> Result<ASTNode, ParserError> {
517+
pub fn parse_infix(&mut self, expr: Expr, precedence: u8) -> Result<Expr, ParserError> {
518518
debug!("parsing infix");
519519
let tok = self.next_token().unwrap(); // safe as EOF's precedence is the lowest
520520

@@ -547,7 +547,7 @@ impl Parser {
547547
};
548548

549549
if let Some(op) = regular_binary_operator {
550-
Ok(ASTNode::SQLBinaryOp {
550+
Ok(Expr::SQLBinaryOp {
551551
left: Box::new(expr),
552552
op,
553553
right: Box::new(self.parse_subexpr(precedence)?),
@@ -556,9 +556,9 @@ impl Parser {
556556
match k.keyword.as_ref() {
557557
"IS" => {
558558
if self.parse_keyword("NULL") {
559-
Ok(ASTNode::SQLIsNull(Box::new(expr)))
559+
Ok(Expr::SQLIsNull(Box::new(expr)))
560560
} else if self.parse_keywords(vec!["NOT", "NULL"]) {
561-
Ok(ASTNode::SQLIsNotNull(Box::new(expr)))
561+
Ok(Expr::SQLIsNotNull(Box::new(expr)))
562562
} else {
563563
self.expected("NULL or NOT NULL after IS", self.peek_token())
564564
}
@@ -586,17 +586,17 @@ impl Parser {
586586
}
587587

588588
/// Parses the parens following the `[ NOT ] IN` operator
589-
pub fn parse_in(&mut self, expr: ASTNode, negated: bool) -> Result<ASTNode, ParserError> {
589+
pub fn parse_in(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
590590
self.expect_token(&Token::LParen)?;
591591
let in_op = if self.parse_keyword("SELECT") || self.parse_keyword("WITH") {
592592
self.prev_token();
593-
ASTNode::SQLInSubquery {
593+
Expr::SQLInSubquery {
594594
expr: Box::new(expr),
595595
subquery: Box::new(self.parse_query()?),
596596
negated,
597597
}
598598
} else {
599-
ASTNode::SQLInList {
599+
Expr::SQLInList {
600600
expr: Box::new(expr),
601601
list: self.parse_expr_list()?,
602602
negated,
@@ -607,13 +607,13 @@ impl Parser {
607607
}
608608

609609
/// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed
610-
pub fn parse_between(&mut self, expr: ASTNode, negated: bool) -> Result<ASTNode, ParserError> {
610+
pub fn parse_between(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
611611
// Stop parsing subexpressions for <low> and <high> on tokens with
612612
// precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
613613
let low = self.parse_subexpr(Self::BETWEEN_PREC)?;
614614
self.expect_keyword("AND")?;
615615
let high = self.parse_subexpr(Self::BETWEEN_PREC)?;
616-
Ok(ASTNode::SQLBetween {
616+
Ok(Expr::SQLBetween {
617617
expr: Box::new(expr),
618618
negated,
619619
low: Box::new(low),
@@ -622,8 +622,8 @@ impl Parser {
622622
}
623623

624624
/// Parse a postgresql casting style which is in the form of `expr::datatype`
625-
pub fn parse_pg_cast(&mut self, expr: ASTNode) -> Result<ASTNode, ParserError> {
626-
Ok(ASTNode::SQLCast {
625+
pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError> {
626+
Ok(Expr::SQLCast {
627627
expr: Box::new(expr),
628628
data_type: self.parse_data_type()?,
629629
})
@@ -1132,8 +1132,8 @@ impl Parser {
11321132
Ok(values)
11331133
}
11341134

1135-
fn parse_sql_value(&mut self) -> Result<ASTNode, ParserError> {
1136-
Ok(ASTNode::SQLValue(self.parse_value()?))
1135+
fn parse_sql_value(&mut self) -> Result<Expr, ParserError> {
1136+
Ok(Expr::SQLValue(self.parse_value()?))
11371137
}
11381138

11391139
fn parse_tab_value(&mut self) -> Result<Vec<Option<String>>, ParserError> {
@@ -1826,8 +1826,8 @@ impl Parser {
18261826
}
18271827

18281828
/// Parse a comma-delimited list of SQL expressions
1829-
pub fn parse_expr_list(&mut self) -> Result<Vec<ASTNode>, ParserError> {
1830-
let mut expr_list: Vec<ASTNode> = vec![];
1829+
pub fn parse_expr_list(&mut self) -> Result<Vec<Expr>, ParserError> {
1830+
let mut expr_list: Vec<Expr> = vec![];
18311831
loop {
18321832
expr_list.push(self.parse_expr()?);
18331833
if !self.consume_token(&Token::Comma) {
@@ -1837,7 +1837,7 @@ impl Parser {
18371837
Ok(expr_list)
18381838
}
18391839

1840-
pub fn parse_optional_args(&mut self) -> Result<Vec<ASTNode>, ParserError> {
1840+
pub fn parse_optional_args(&mut self) -> Result<Vec<Expr>, ParserError> {
18411841
if self.consume_token(&Token::RParen) {
18421842
Ok(vec![])
18431843
} else {
@@ -1852,18 +1852,18 @@ impl Parser {
18521852
let mut projections: Vec<SQLSelectItem> = vec![];
18531853
loop {
18541854
let expr = self.parse_expr()?;
1855-
if let ASTNode::SQLWildcard = expr {
1855+
if let Expr::SQLWildcard = expr {
18561856
projections.push(SQLSelectItem::Wildcard);
1857-
} else if let ASTNode::SQLQualifiedWildcard(prefix) = expr {
1857+
} else if let Expr::SQLQualifiedWildcard(prefix) = expr {
18581858
projections.push(SQLSelectItem::QualifiedWildcard(SQLObjectName(prefix)));
18591859
} else {
18601860
// `expr` is a regular SQL expression and can be followed by an alias
18611861
if let Some(alias) =
18621862
self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)?
18631863
{
1864-
projections.push(SQLSelectItem::ExpressionWithAlias { expr, alias });
1864+
projections.push(SQLSelectItem::ExprWithAlias { expr, alias });
18651865
} else {
1866-
projections.push(SQLSelectItem::UnnamedExpression(expr));
1866+
projections.push(SQLSelectItem::UnnamedExpr(expr));
18671867
}
18681868
}
18691869

@@ -1897,20 +1897,20 @@ impl Parser {
18971897
}
18981898

18991899
/// Parse a LIMIT clause
1900-
pub fn parse_limit(&mut self) -> Result<Option<ASTNode>, ParserError> {
1900+
pub fn parse_limit(&mut self) -> Result<Option<Expr>, ParserError> {
19011901
if self.parse_keyword("ALL") {
19021902
Ok(None)
19031903
} else {
19041904
self.parse_literal_uint()
1905-
.map(|n| Some(ASTNode::SQLValue(Value::Long(n))))
1905+
.map(|n| Some(Expr::SQLValue(Value::Long(n))))
19061906
}
19071907
}
19081908

19091909
/// Parse an OFFSET clause
1910-
pub fn parse_offset(&mut self) -> Result<ASTNode, ParserError> {
1910+
pub fn parse_offset(&mut self) -> Result<Expr, ParserError> {
19111911
let value = self
19121912
.parse_literal_uint()
1913-
.map(|n| ASTNode::SQLValue(Value::Long(n)))?;
1913+
.map(|n| Expr::SQLValue(Value::Long(n)))?;
19141914
self.expect_one_of_keywords(&["ROW", "ROWS"])?;
19151915
Ok(value)
19161916
}

‎src/test_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl TestedDialects {
103103

104104
/// Ensures that `sql` parses as an expression, and is not modified
105105
/// after a serialization round-trip.
106-
pub fn verified_expr(&self, sql: &str) -> ASTNode {
106+
pub fn verified_expr(&self, sql: &str) -> Expr {
107107
let ast = self.run_parser_method(sql, Parser::parse_expr).unwrap();
108108
assert_eq!(sql, &ast.to_string(), "round-tripping without changes");
109109
ast
@@ -130,9 +130,9 @@ pub fn only<T>(v: impl IntoIterator<Item = T>) -> T {
130130
}
131131
}
132132

133-
pub fn expr_from_projection(item: &SQLSelectItem) -> &ASTNode {
133+
pub fn expr_from_projection(item: &SQLSelectItem) -> &Expr {
134134
match item {
135-
SQLSelectItem::UnnamedExpression(expr) => expr,
136-
_ => panic!("Expected UnnamedExpression"),
135+
SQLSelectItem::UnnamedExpr(expr) => expr,
136+
_ => panic!("Expected UnnamedExpr"),
137137
}
138138
}

‎tests/sqlparser_common.rs

Lines changed: 126 additions & 130 deletions
Large diffs are not rendered by default.

‎tests/sqlparser_mssql.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ fn parse_mssql_identifiers() {
2323
let sql = "SELECT @@version, _foo$123 FROM ##temp";
2424
let select = ms_and_generic().verified_only_select(sql);
2525
assert_eq!(
26-
&ASTNode::SQLIdentifier("@@version".to_string()),
26+
&Expr::SQLIdentifier("@@version".to_string()),
2727
expr_from_projection(&select.projection[0]),
2828
);
2929
assert_eq!(
30-
&ASTNode::SQLIdentifier("_foo$123".to_string()),
30+
&Expr::SQLIdentifier("_foo$123".to_string()),
3131
expr_from_projection(&select.projection[1]),
3232
);
3333
assert_eq!(2, select.projection.len());

‎tests/sqlparser_postgres.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ fn parse_create_table_with_defaults() {
106106
options: vec![
107107
ColumnOptionDef {
108108
name: None,
109-
option: ColumnOption::Default(ASTNode::SQLValue(Value::Boolean(
110-
true
111-
))),
109+
option: ColumnOption::Default(Expr::SQLValue(Value::Boolean(true))),
112110
},
113111
ColumnOptionDef {
114112
name: None,

0 commit comments

Comments
 (0)
Please sign in to comment.