Skip to content

Commit cc0f643

Browse files
committed
Standardize BinaryOp and UnaryOp nodes
These were previously called "BinaryExpr" and "Unary"; besides being inconsistent, it's also not correct to say "binary expression" or "unary expression", as it's the operators that have arities, not the expression. Adjust the naming of the variants accordingly.
1 parent c0410cc commit cc0f643

File tree

3 files changed

+53
-53
lines changed

3 files changed

+53
-53
lines changed

src/ast/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ pub enum Expr {
8989
low: Box<Expr>,
9090
high: Box<Expr>,
9191
},
92-
/// Binary expression e.g. `1 + 1` or `foo > bar`
93-
BinaryExpr {
92+
/// Binary operation e.g. `1 + 1` or `foo > bar`
93+
BinaryOp {
9494
left: Box<Expr>,
9595
op: Operator,
9696
right: Box<Expr>,
@@ -108,9 +108,9 @@ pub enum Expr {
108108
},
109109
/// Nested expression e.g. `(foo > bar)` or `(1)`
110110
Nested(Box<Expr>),
111-
/// Unary expression
112-
Unary {
113-
operator: Operator,
111+
/// Unary operation, like `NOT false`
112+
UnaryOp {
113+
op: Operator,
114114
expr: Box<Expr>,
115115
},
116116
/// SQLValue
@@ -176,7 +176,7 @@ impl ToString for Expr {
176176
low.to_string(),
177177
high.to_string()
178178
),
179-
Expr::BinaryExpr { left, op, right } => format!(
179+
Expr::BinaryOp { left, op, right } => format!(
180180
"{} {} {}",
181181
left.as_ref().to_string(),
182182
op.to_string(),
@@ -196,8 +196,8 @@ impl ToString for Expr {
196196
collation.to_string()
197197
),
198198
Expr::Nested(ast) => format!("({})", ast.as_ref().to_string()),
199-
Expr::Unary { operator, expr } => {
200-
format!("{} {}", operator.to_string(), expr.as_ref().to_string())
199+
Expr::UnaryOp { op, expr } => {
200+
format!("{} {}", op.to_string(), expr.as_ref().to_string())
201201
}
202202
Expr::Value(v) => v.to_string(),
203203
Expr::Function(f) => f.to_string(),

src/parser.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ impl Parser {
195195
"DATE" => Ok(Expr::Value(Value::Date(self.parse_literal_string()?))),
196196
"EXISTS" => self.parse_exists_expression(),
197197
"EXTRACT" => self.parse_extract_expression(),
198-
"NOT" => Ok(Expr::Unary {
199-
operator: Operator::Not,
198+
"NOT" => Ok(Expr::UnaryOp {
199+
op: Operator::Not,
200200
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
201201
}),
202202
"TIME" => Ok(Expr::Value(Value::Time(self.parse_literal_string()?))),
@@ -241,8 +241,8 @@ impl Parser {
241241
} else {
242242
Operator::Minus
243243
};
244-
Ok(Expr::Unary {
245-
operator,
244+
Ok(Expr::UnaryOp {
245+
op: operator,
246246
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
247247
})
248248
}
@@ -482,7 +482,7 @@ impl Parser {
482482
};
483483

484484
if let Some(op) = regular_binary_operator {
485-
Ok(Expr::BinaryExpr {
485+
Ok(Expr::BinaryOp {
486486
left: Box::new(expr),
487487
op,
488488
right: Box::new(self.parse_subexpr(precedence)?),

tests/sqlparser_common.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn parse_where_delete_statement() {
166166
assert_eq!(ObjectName(vec!["foo".to_string()]), table_name);
167167

168168
assert_eq!(
169-
Expr::BinaryExpr {
169+
Expr::BinaryOp {
170170
left: Box::new(Expr::Ident("name".to_string())),
171171
op: Operator::Eq,
172172
right: Box::new(Expr::Value(Value::Long(5))),
@@ -259,7 +259,7 @@ fn parse_column_aliases() {
259259
let sql = "SELECT a.col + 1 AS newname FROM foo AS a";
260260
let select = verified_only_select(sql);
261261
if let SelectItem::NamedExpr {
262-
expr: Expr::BinaryExpr {
262+
expr: Expr::BinaryOp {
263263
ref op, ref right, ..
264264
},
265265
ref alias,
@@ -313,8 +313,8 @@ fn parse_select_count_distinct() {
313313
assert_eq!(
314314
&Expr::Function(Function {
315315
name: ObjectName(vec!["COUNT".to_string()]),
316-
args: vec![Expr::Unary {
317-
operator: Operator::Plus,
316+
args: vec![Expr::UnaryOp {
317+
op: Operator::Plus,
318318
expr: Box::new(Expr::Ident("x".to_string()))
319319
}],
320320
over: None,
@@ -384,7 +384,7 @@ fn parse_escaped_single_quote_string_predicate() {
384384
WHERE salary <> 'Jim''s salary'";
385385
let ast = verified_only_select(sql);
386386
assert_eq!(
387-
Some(Expr::BinaryExpr {
387+
Some(Expr::BinaryOp {
388388
left: Box::new(Expr::Ident("salary".to_string())),
389389
op: Operator::NotEq,
390390
right: Box::new(Expr::Value(Value::SingleQuotedString(
@@ -399,10 +399,10 @@ fn parse_escaped_single_quote_string_predicate() {
399399
fn parse_compound_expr_1() {
400400
let sql = "a + b * c";
401401
assert_eq!(
402-
Expr::BinaryExpr {
402+
Expr::BinaryOp {
403403
left: Box::new(Expr::Ident("a".to_string())),
404404
op: Operator::Plus,
405-
right: Box::new(Expr::BinaryExpr {
405+
right: Box::new(Expr::BinaryOp {
406406
left: Box::new(Expr::Ident("b".to_string())),
407407
op: Operator::Multiply,
408408
right: Box::new(Expr::Ident("c".to_string()))
@@ -416,8 +416,8 @@ fn parse_compound_expr_1() {
416416
fn parse_compound_expr_2() {
417417
let sql = "a * b + c";
418418
assert_eq!(
419-
Expr::BinaryExpr {
420-
left: Box::new(Expr::BinaryExpr {
419+
Expr::BinaryOp {
420+
left: Box::new(Expr::BinaryOp {
421421
left: Box::new(Expr::Ident("a".to_string())),
422422
op: Operator::Multiply,
423423
right: Box::new(Expr::Ident("b".to_string()))
@@ -433,14 +433,14 @@ fn parse_compound_expr_2() {
433433
fn parse_unary_math() {
434434
let sql = "- a + - b";
435435
assert_eq!(
436-
Expr::BinaryExpr {
437-
left: Box::new(Expr::Unary {
438-
operator: Operator::Minus,
436+
Expr::BinaryOp {
437+
left: Box::new(Expr::UnaryOp {
438+
op: Operator::Minus,
439439
expr: Box::new(Expr::Ident("a".to_string())),
440440
}),
441441
op: Operator::Plus,
442-
right: Box::new(Expr::Unary {
443-
operator: Operator::Minus,
442+
right: Box::new(Expr::UnaryOp {
443+
op: Operator::Minus,
444444
expr: Box::new(Expr::Ident("b".to_string())),
445445
}),
446446
},
@@ -470,24 +470,24 @@ fn parse_is_not_null() {
470470
fn parse_not_precedence() {
471471
// NOT has higher precedence than OR/AND, so the following must parse as (NOT true) OR true
472472
let sql = "NOT true OR true";
473-
assert_matches!(verified_expr(sql), Expr::BinaryExpr {
473+
assert_matches!(verified_expr(sql), Expr::BinaryOp {
474474
op: Operator::Or,
475475
..
476476
});
477477

478478
// But NOT has lower precedence than comparison operators, so the following parses as NOT (a IS NULL)
479479
let sql = "NOT a IS NULL";
480-
assert_matches!(verified_expr(sql), Expr::Unary {
481-
operator: Operator::Not,
480+
assert_matches!(verified_expr(sql), Expr::UnaryOp {
481+
op: Operator::Not,
482482
..
483483
});
484484

485485
// NOT has lower precedence than BETWEEN, so the following parses as NOT (1 NOT BETWEEN 1 AND 2)
486486
let sql = "NOT 1 NOT BETWEEN 1 AND 2";
487487
assert_eq!(
488488
verified_expr(sql),
489-
Expr::Unary {
490-
operator: Operator::Not,
489+
Expr::UnaryOp {
490+
op: Operator::Not,
491491
expr: Box::new(Expr::Between {
492492
expr: Box::new(Expr::Value(Value::Long(1))),
493493
low: Box::new(Expr::Value(Value::Long(1))),
@@ -501,9 +501,9 @@ fn parse_not_precedence() {
501501
let sql = "NOT 'a' NOT LIKE 'b'";
502502
assert_eq!(
503503
verified_expr(sql),
504-
Expr::Unary {
505-
operator: Operator::Not,
506-
expr: Box::new(Expr::BinaryExpr {
504+
Expr::UnaryOp {
505+
op: Operator::Not,
506+
expr: Box::new(Expr::BinaryOp {
507507
left: Box::new(Expr::Value(Value::SingleQuotedString("a".into()))),
508508
op: Operator::NotLike,
509509
right: Box::new(Expr::Value(Value::SingleQuotedString("b".into()))),
@@ -515,8 +515,8 @@ fn parse_not_precedence() {
515515
let sql = "NOT a NOT IN ('a')";
516516
assert_eq!(
517517
verified_expr(sql),
518-
Expr::Unary {
519-
operator: Operator::Not,
518+
Expr::UnaryOp {
519+
op: Operator::Not,
520520
expr: Box::new(Expr::InList {
521521
expr: Box::new(Expr::Ident("a".into())),
522522
list: vec![Expr::Value(Value::SingleQuotedString("a".into()))],
@@ -535,7 +535,7 @@ fn parse_like() {
535535
);
536536
let select = verified_only_select(sql);
537537
assert_eq!(
538-
Expr::BinaryExpr {
538+
Expr::BinaryOp {
539539
left: Box::new(Expr::Ident("name".to_string())),
540540
op: if negated {
541541
Operator::NotLike
@@ -555,7 +555,7 @@ fn parse_like() {
555555
);
556556
let select = verified_only_select(sql);
557557
assert_eq!(
558-
Expr::IsNull(Box::new(Expr::BinaryExpr {
558+
Expr::IsNull(Box::new(Expr::BinaryOp {
559559
left: Box::new(Expr::Ident("name".to_string())),
560560
op: if negated {
561561
Operator::NotLike
@@ -638,12 +638,12 @@ fn parse_between_with_expr() {
638638
assert_eq!(
639639
Expr::IsNull(Box::new(Expr::Between {
640640
expr: Box::new(Expr::Value(Value::Long(1))),
641-
low: Box::new(Expr::BinaryExpr {
641+
low: Box::new(Expr::BinaryOp {
642642
left: Box::new(Expr::Value(Value::Long(1))),
643643
op: Operator::Plus,
644644
right: Box::new(Expr::Value(Value::Long(2))),
645645
}),
646-
high: Box::new(Expr::BinaryExpr {
646+
high: Box::new(Expr::BinaryOp {
647647
left: Box::new(Expr::Value(Value::Long(3))),
648648
op: Operator::Plus,
649649
right: Box::new(Expr::Value(Value::Long(4))),
@@ -656,15 +656,15 @@ fn parse_between_with_expr() {
656656
let sql = "SELECT * FROM t WHERE 1 = 1 AND 1 + x BETWEEN 1 AND 2";
657657
let select = verified_only_select(sql);
658658
assert_eq!(
659-
Expr::BinaryExpr {
660-
left: Box::new(Expr::BinaryExpr {
659+
Expr::BinaryOp {
660+
left: Box::new(Expr::BinaryOp {
661661
left: Box::new(Expr::Value(Value::Long(1))),
662662
op: Operator::Eq,
663663
right: Box::new(Expr::Value(Value::Long(1))),
664664
}),
665665
op: Operator::And,
666666
right: Box::new(Expr::Between {
667-
expr: Box::new(Expr::BinaryExpr {
667+
expr: Box::new(Expr::BinaryOp {
668668
left: Box::new(Expr::Value(Value::Long(1))),
669669
op: Operator::Plus,
670670
right: Box::new(Expr::Ident("x".to_string())),
@@ -1160,14 +1160,14 @@ fn parse_delimited_identifiers() {
11601160
fn parse_parens() {
11611161
let sql = "(a + b) - (c + d)";
11621162
assert_eq!(
1163-
Expr::BinaryExpr {
1164-
left: Box::new(Expr::Nested(Box::new(Expr::BinaryExpr {
1163+
Expr::BinaryOp {
1164+
left: Box::new(Expr::Nested(Box::new(Expr::BinaryOp {
11651165
left: Box::new(Expr::Ident("a".to_string())),
11661166
op: Operator::Plus,
11671167
right: Box::new(Expr::Ident("b".to_string()))
11681168
}))),
11691169
op: Operator::Minus,
1170-
right: Box::new(Expr::Nested(Box::new(Expr::BinaryExpr {
1170+
right: Box::new(Expr::Nested(Box::new(Expr::BinaryOp {
11711171
left: Box::new(Expr::Ident("c".to_string())),
11721172
op: Operator::Plus,
11731173
right: Box::new(Expr::Ident("d".to_string()))
@@ -1186,12 +1186,12 @@ fn parse_searched_case_expression() {
11861186
operand: None,
11871187
conditions: vec![
11881188
Expr::IsNull(Box::new(Expr::Ident("bar".to_string()))),
1189-
Expr::BinaryExpr {
1189+
Expr::BinaryOp {
11901190
left: Box::new(Expr::Ident("bar".to_string())),
11911191
op: Operator::Eq,
11921192
right: Box::new(Expr::Value(Value::Long(0)))
11931193
},
1194-
Expr::BinaryExpr {
1194+
Expr::BinaryOp {
11951195
left: Box::new(Expr::Ident("bar".to_string())),
11961196
op: Operator::GtEq,
11971197
right: Box::new(Expr::Value(Value::Long(0)))
@@ -1291,7 +1291,7 @@ fn parse_joins_on() {
12911291
args: vec![],
12921292
with_hints: vec![],
12931293
},
1294-
join_operator: f(JoinConstraint::On(Expr::BinaryExpr {
1294+
join_operator: f(JoinConstraint::On(Expr::BinaryOp {
12951295
left: Box::new(Expr::Ident("c1".into())),
12961296
op: Operator::Eq,
12971297
right: Box::new(Expr::Ident("c2".into())),
@@ -1588,7 +1588,7 @@ fn parse_multiple_statements() {
15881588
#[test]
15891589
fn parse_scalar_subqueries() {
15901590
let sql = "(SELECT 1) + (SELECT 2)";
1591-
assert_matches!(verified_expr(sql), Expr::BinaryExpr {
1591+
assert_matches!(verified_expr(sql), Expr::BinaryOp {
15921592
op: Operator::Plus, ..
15931593
//left: box SQLSubquery { .. },
15941594
//right: box SQLSubquery { .. },
@@ -1608,8 +1608,8 @@ fn parse_exists_subquery() {
16081608
let sql = "SELECT * FROM t WHERE NOT EXISTS (SELECT 1)";
16091609
let select = verified_only_select(sql);
16101610
assert_eq!(
1611-
Expr::Unary {
1612-
operator: Operator::Not,
1611+
Expr::UnaryOp {
1612+
op: Operator::Not,
16131613
expr: Box::new(Expr::Exists(Box::new(expected_inner))),
16141614
},
16151615
select.selection.unwrap(),

0 commit comments

Comments
 (0)