Skip to content

Commit 79c231b

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 5896f01 commit 79c231b

File tree

3 files changed

+58
-58
lines changed

3 files changed

+58
-58
lines changed

src/sqlast/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,17 @@ pub enum ASTNode {
9191
low: Box<ASTNode>,
9292
high: Box<ASTNode>,
9393
},
94-
/// Binary expression e.g. `1 + 1` or `foo > bar`
95-
SQLBinaryExpr {
94+
/// Binary operation e.g. `1 + 1` or `foo > bar`
95+
SQLBinaryOp {
9696
left: Box<ASTNode>,
9797
op: SQLOperator,
9898
right: Box<ASTNode>,
9999
},
100+
/// Unary operation e.g. `NOT foo`
101+
SQLUnaryOp {
102+
op: SQLOperator,
103+
expr: Box<ASTNode>,
104+
},
100105
/// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
101106
SQLCast {
102107
expr: Box<ASTNode>,
@@ -113,11 +118,6 @@ pub enum ASTNode {
113118
},
114119
/// Nested expression e.g. `(foo > bar)` or `(1)`
115120
SQLNested(Box<ASTNode>),
116-
/// Unary expression
117-
SQLUnary {
118-
operator: SQLOperator,
119-
expr: Box<ASTNode>,
120-
},
121121
/// SQLValue
122122
SQLValue(Value),
123123
/// Scalar function call e.g. `LEFT(foo, 5)`
@@ -181,12 +181,15 @@ impl ToString for ASTNode {
181181
low.to_string(),
182182
high.to_string()
183183
),
184-
ASTNode::SQLBinaryExpr { left, op, right } => format!(
184+
ASTNode::SQLBinaryOp { left, op, right } => format!(
185185
"{} {} {}",
186186
left.as_ref().to_string(),
187187
op.to_string(),
188188
right.as_ref().to_string()
189189
),
190+
ASTNode::SQLUnaryOp { op, expr } => {
191+
format!("{} {}", op.to_string(), expr.as_ref().to_string())
192+
}
190193
ASTNode::SQLCast { expr, data_type } => format!(
191194
"CAST({} AS {})",
192195
expr.as_ref().to_string(),
@@ -201,9 +204,6 @@ impl ToString for ASTNode {
201204
collation.to_string()
202205
),
203206
ASTNode::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
204-
ASTNode::SQLUnary { operator, expr } => {
205-
format!("{} {}", operator.to_string(), expr.as_ref().to_string())
206-
}
207207
ASTNode::SQLValue(v) => v.to_string(),
208208
ASTNode::SQLFunction(f) => f.to_string(),
209209
ASTNode::SQLCase {

src/sqlparser.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ impl Parser {
185185
"EXISTS" => self.parse_exists_expression(),
186186
"EXTRACT" => self.parse_extract_expression(),
187187
"INTERVAL" => self.parse_literal_interval(),
188-
"NOT" => Ok(ASTNode::SQLUnary {
189-
operator: SQLOperator::Not,
188+
"NOT" => Ok(ASTNode::SQLUnaryOp {
189+
op: SQLOperator::Not,
190190
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
191191
}),
192192
"TIME" => Ok(ASTNode::SQLValue(Value::Time(self.parse_literal_string()?))),
@@ -226,13 +226,13 @@ impl Parser {
226226
}, // End of Token::SQLWord
227227
Token::Mult => Ok(ASTNode::SQLWildcard),
228228
tok @ Token::Minus | tok @ Token::Plus => {
229-
let operator = if tok == Token::Plus {
229+
let op = if tok == Token::Plus {
230230
SQLOperator::Plus
231231
} else {
232232
SQLOperator::Minus
233233
};
234-
Ok(ASTNode::SQLUnary {
235-
operator,
234+
Ok(ASTNode::SQLUnaryOp {
235+
op,
236236
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
237237
})
238238
}
@@ -543,7 +543,7 @@ impl Parser {
543543
};
544544

545545
if let Some(op) = regular_binary_operator {
546-
Ok(ASTNode::SQLBinaryExpr {
546+
Ok(ASTNode::SQLBinaryOp {
547547
left: Box::new(expr),
548548
op,
549549
right: Box::new(self.parse_subexpr(precedence)?),

tests/sqlparser_common.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn parse_where_delete_statement() {
169169
assert_eq!(SQLObjectName(vec!["foo".to_string()]), table_name);
170170

171171
assert_eq!(
172-
SQLBinaryExpr {
172+
SQLBinaryOp {
173173
left: Box::new(SQLIdentifier("name".to_string())),
174174
op: Eq,
175175
right: Box::new(SQLValue(Value::Long(5))),
@@ -270,7 +270,7 @@ fn parse_column_aliases() {
270270
let sql = "SELECT a.col + 1 AS newname FROM foo AS a";
271271
let select = verified_only_select(sql);
272272
if let SQLSelectItem::ExpressionWithAlias {
273-
expr: ASTNode::SQLBinaryExpr {
273+
expr: ASTNode::SQLBinaryOp {
274274
ref op, ref right, ..
275275
},
276276
ref alias,
@@ -324,8 +324,8 @@ fn parse_select_count_distinct() {
324324
assert_eq!(
325325
&ASTNode::SQLFunction(SQLFunction {
326326
name: SQLObjectName(vec!["COUNT".to_string()]),
327-
args: vec![ASTNode::SQLUnary {
328-
operator: SQLOperator::Plus,
327+
args: vec![ASTNode::SQLUnaryOp {
328+
op: SQLOperator::Plus,
329329
expr: Box::new(ASTNode::SQLIdentifier("x".to_string()))
330330
}],
331331
over: None,
@@ -397,7 +397,7 @@ fn parse_escaped_single_quote_string_predicate() {
397397
WHERE salary <> 'Jim''s salary'";
398398
let ast = verified_only_select(sql);
399399
assert_eq!(
400-
Some(SQLBinaryExpr {
400+
Some(SQLBinaryOp {
401401
left: Box::new(SQLIdentifier("salary".to_string())),
402402
op: NotEq,
403403
right: Box::new(SQLValue(Value::SingleQuotedString(
@@ -414,10 +414,10 @@ fn parse_compound_expr_1() {
414414
use self::SQLOperator::*;
415415
let sql = "a + b * c";
416416
assert_eq!(
417-
SQLBinaryExpr {
417+
SQLBinaryOp {
418418
left: Box::new(SQLIdentifier("a".to_string())),
419419
op: Plus,
420-
right: Box::new(SQLBinaryExpr {
420+
right: Box::new(SQLBinaryOp {
421421
left: Box::new(SQLIdentifier("b".to_string())),
422422
op: Multiply,
423423
right: Box::new(SQLIdentifier("c".to_string()))
@@ -433,8 +433,8 @@ fn parse_compound_expr_2() {
433433
use self::SQLOperator::*;
434434
let sql = "a * b + c";
435435
assert_eq!(
436-
SQLBinaryExpr {
437-
left: Box::new(SQLBinaryExpr {
436+
SQLBinaryOp {
437+
left: Box::new(SQLBinaryOp {
438438
left: Box::new(SQLIdentifier("a".to_string())),
439439
op: Multiply,
440440
right: Box::new(SQLIdentifier("b".to_string()))
@@ -452,14 +452,14 @@ fn parse_unary_math() {
452452
use self::SQLOperator::*;
453453
let sql = "- a + - b";
454454
assert_eq!(
455-
SQLBinaryExpr {
456-
left: Box::new(SQLUnary {
457-
operator: Minus,
455+
SQLBinaryOp {
456+
left: Box::new(SQLUnaryOp {
457+
op: Minus,
458458
expr: Box::new(SQLIdentifier("a".to_string())),
459459
}),
460460
op: Plus,
461-
right: Box::new(SQLUnary {
462-
operator: Minus,
461+
right: Box::new(SQLUnaryOp {
462+
op: Minus,
463463
expr: Box::new(SQLIdentifier("b".to_string())),
464464
}),
465465
},
@@ -492,24 +492,24 @@ fn parse_not_precedence() {
492492
use self::ASTNode::*;
493493
// NOT has higher precedence than OR/AND, so the following must parse as (NOT true) OR true
494494
let sql = "NOT true OR true";
495-
assert_matches!(verified_expr(sql), SQLBinaryExpr {
495+
assert_matches!(verified_expr(sql), SQLBinaryOp {
496496
op: SQLOperator::Or,
497497
..
498498
});
499499

500500
// But NOT has lower precedence than comparison operators, so the following parses as NOT (a IS NULL)
501501
let sql = "NOT a IS NULL";
502-
assert_matches!(verified_expr(sql), SQLUnary {
503-
operator: SQLOperator::Not,
502+
assert_matches!(verified_expr(sql), SQLUnaryOp {
503+
op: SQLOperator::Not,
504504
..
505505
});
506506

507507
// NOT has lower precedence than BETWEEN, so the following parses as NOT (1 NOT BETWEEN 1 AND 2)
508508
let sql = "NOT 1 NOT BETWEEN 1 AND 2";
509509
assert_eq!(
510510
verified_expr(sql),
511-
SQLUnary {
512-
operator: SQLOperator::Not,
511+
SQLUnaryOp {
512+
op: SQLOperator::Not,
513513
expr: Box::new(SQLBetween {
514514
expr: Box::new(SQLValue(Value::Long(1))),
515515
low: Box::new(SQLValue(Value::Long(1))),
@@ -523,9 +523,9 @@ fn parse_not_precedence() {
523523
let sql = "NOT 'a' NOT LIKE 'b'";
524524
assert_eq!(
525525
verified_expr(sql),
526-
SQLUnary {
527-
operator: SQLOperator::Not,
528-
expr: Box::new(SQLBinaryExpr {
526+
SQLUnaryOp {
527+
op: SQLOperator::Not,
528+
expr: Box::new(SQLBinaryOp {
529529
left: Box::new(SQLValue(Value::SingleQuotedString("a".into()))),
530530
op: SQLOperator::NotLike,
531531
right: Box::new(SQLValue(Value::SingleQuotedString("b".into()))),
@@ -537,8 +537,8 @@ fn parse_not_precedence() {
537537
let sql = "NOT a NOT IN ('a')";
538538
assert_eq!(
539539
verified_expr(sql),
540-
SQLUnary {
541-
operator: SQLOperator::Not,
540+
SQLUnaryOp {
541+
op: SQLOperator::Not,
542542
expr: Box::new(SQLInList {
543543
expr: Box::new(SQLIdentifier("a".into())),
544544
list: vec![SQLValue(Value::SingleQuotedString("a".into()))],
@@ -557,7 +557,7 @@ fn parse_like() {
557557
);
558558
let select = verified_only_select(sql);
559559
assert_eq!(
560-
ASTNode::SQLBinaryExpr {
560+
ASTNode::SQLBinaryOp {
561561
left: Box::new(ASTNode::SQLIdentifier("name".to_string())),
562562
op: if negated {
563563
SQLOperator::NotLike
@@ -579,7 +579,7 @@ fn parse_like() {
579579
);
580580
let select = verified_only_select(sql);
581581
assert_eq!(
582-
ASTNode::SQLIsNull(Box::new(ASTNode::SQLBinaryExpr {
582+
ASTNode::SQLIsNull(Box::new(ASTNode::SQLBinaryOp {
583583
left: Box::new(ASTNode::SQLIdentifier("name".to_string())),
584584
op: if negated {
585585
SQLOperator::NotLike
@@ -666,12 +666,12 @@ fn parse_between_with_expr() {
666666
assert_eq!(
667667
ASTNode::SQLIsNull(Box::new(ASTNode::SQLBetween {
668668
expr: Box::new(ASTNode::SQLValue(Value::Long(1))),
669-
low: Box::new(SQLBinaryExpr {
669+
low: Box::new(SQLBinaryOp {
670670
left: Box::new(ASTNode::SQLValue(Value::Long(1))),
671671
op: Plus,
672672
right: Box::new(ASTNode::SQLValue(Value::Long(2))),
673673
}),
674-
high: Box::new(SQLBinaryExpr {
674+
high: Box::new(SQLBinaryOp {
675675
left: Box::new(ASTNode::SQLValue(Value::Long(3))),
676676
op: Plus,
677677
right: Box::new(ASTNode::SQLValue(Value::Long(4))),
@@ -684,15 +684,15 @@ fn parse_between_with_expr() {
684684
let sql = "SELECT * FROM t WHERE 1 = 1 AND 1 + x BETWEEN 1 AND 2";
685685
let select = verified_only_select(sql);
686686
assert_eq!(
687-
ASTNode::SQLBinaryExpr {
688-
left: Box::new(ASTNode::SQLBinaryExpr {
687+
ASTNode::SQLBinaryOp {
688+
left: Box::new(ASTNode::SQLBinaryOp {
689689
left: Box::new(ASTNode::SQLValue(Value::Long(1))),
690690
op: SQLOperator::Eq,
691691
right: Box::new(ASTNode::SQLValue(Value::Long(1))),
692692
}),
693693
op: SQLOperator::And,
694694
right: Box::new(ASTNode::SQLBetween {
695-
expr: Box::new(ASTNode::SQLBinaryExpr {
695+
expr: Box::new(ASTNode::SQLBinaryOp {
696696
left: Box::new(ASTNode::SQLValue(Value::Long(1))),
697697
op: SQLOperator::Plus,
698698
right: Box::new(ASTNode::SQLIdentifier("x".to_string())),
@@ -1356,14 +1356,14 @@ fn parse_parens() {
13561356
use self::SQLOperator::*;
13571357
let sql = "(a + b) - (c + d)";
13581358
assert_eq!(
1359-
SQLBinaryExpr {
1360-
left: Box::new(SQLNested(Box::new(SQLBinaryExpr {
1359+
SQLBinaryOp {
1360+
left: Box::new(SQLNested(Box::new(SQLBinaryOp {
13611361
left: Box::new(SQLIdentifier("a".to_string())),
13621362
op: Plus,
13631363
right: Box::new(SQLIdentifier("b".to_string()))
13641364
}))),
13651365
op: Minus,
1366-
right: Box::new(SQLNested(Box::new(SQLBinaryExpr {
1366+
right: Box::new(SQLNested(Box::new(SQLBinaryOp {
13671367
left: Box::new(SQLIdentifier("c".to_string())),
13681368
op: Plus,
13691369
right: Box::new(SQLIdentifier("d".to_string()))
@@ -1376,20 +1376,20 @@ fn parse_parens() {
13761376
#[test]
13771377
fn parse_searched_case_expression() {
13781378
let sql = "SELECT CASE WHEN bar IS NULL THEN 'null' WHEN bar = 0 THEN '=0' WHEN bar >= 0 THEN '>=0' ELSE '<0' END FROM foo";
1379-
use self::ASTNode::{SQLBinaryExpr, SQLCase, SQLIdentifier, SQLIsNull, SQLValue};
1379+
use self::ASTNode::{SQLBinaryOp, SQLCase, SQLIdentifier, SQLIsNull, SQLValue};
13801380
use self::SQLOperator::*;
13811381
let select = verified_only_select(sql);
13821382
assert_eq!(
13831383
&SQLCase {
13841384
operand: None,
13851385
conditions: vec![
13861386
SQLIsNull(Box::new(SQLIdentifier("bar".to_string()))),
1387-
SQLBinaryExpr {
1387+
SQLBinaryOp {
13881388
left: Box::new(SQLIdentifier("bar".to_string())),
13891389
op: Eq,
13901390
right: Box::new(SQLValue(Value::Long(0)))
13911391
},
1392-
SQLBinaryExpr {
1392+
SQLBinaryOp {
13931393
left: Box::new(SQLIdentifier("bar".to_string())),
13941394
op: GtEq,
13951395
right: Box::new(SQLValue(Value::Long(0)))
@@ -1543,7 +1543,7 @@ fn parse_joins_on() {
15431543
args: vec![],
15441544
with_hints: vec![],
15451545
},
1546-
join_operator: f(JoinConstraint::On(ASTNode::SQLBinaryExpr {
1546+
join_operator: f(JoinConstraint::On(ASTNode::SQLBinaryOp {
15471547
left: Box::new(ASTNode::SQLIdentifier("c1".into())),
15481548
op: SQLOperator::Eq,
15491549
right: Box::new(ASTNode::SQLIdentifier("c2".into())),
@@ -1908,7 +1908,7 @@ fn parse_multiple_statements() {
19081908
fn parse_scalar_subqueries() {
19091909
use self::ASTNode::*;
19101910
let sql = "(SELECT 1) + (SELECT 2)";
1911-
assert_matches!(verified_expr(sql), SQLBinaryExpr {
1911+
assert_matches!(verified_expr(sql), SQLBinaryOp {
19121912
op: SQLOperator::Plus, ..
19131913
//left: box SQLSubquery { .. },
19141914
//right: box SQLSubquery { .. },
@@ -1928,8 +1928,8 @@ fn parse_exists_subquery() {
19281928
let sql = "SELECT * FROM t WHERE NOT EXISTS (SELECT 1)";
19291929
let select = verified_only_select(sql);
19301930
assert_eq!(
1931-
ASTNode::SQLUnary {
1932-
operator: SQLOperator::Not,
1931+
ASTNode::SQLUnaryOp {
1932+
op: SQLOperator::Not,
19331933
expr: Box::new(ASTNode::SQLExists(Box::new(expected_inner))),
19341934
},
19351935
select.selection.unwrap(),

0 commit comments

Comments
 (0)