Skip to content

Commit f2fda57

Browse files
authored
Merge pull request #112 from benesch/expr-consistency
Improve consistency of binary/unary op nodes
2 parents b379480 + ae25dce commit f2fda57

File tree

4 files changed

+133
-119
lines changed

4 files changed

+133
-119
lines changed

src/sqlast/mod.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ pub use self::query::{
2727
Cte, Fetch, Join, JoinConstraint, JoinOperator, SQLOrderByExpr, SQLQuery, SQLSelect,
2828
SQLSelectItem, SQLSetExpr, SQLSetOperator, SQLValues, TableAlias, TableFactor, TableWithJoins,
2929
};
30+
pub use self::sql_operator::{SQLBinaryOperator, SQLUnaryOperator};
3031
pub use self::sqltype::SQLType;
3132
pub use self::value::{SQLDateTimeField, Value};
3233

33-
pub use self::sql_operator::SQLOperator;
34-
3534
/// Like `vec.join(", ")`, but for any types implementing ToString.
3635
fn comma_separated_string<I>(iter: I) -> String
3736
where
@@ -89,12 +88,17 @@ pub enum ASTNode {
8988
low: Box<ASTNode>,
9089
high: Box<ASTNode>,
9190
},
92-
/// Binary expression e.g. `1 + 1` or `foo > bar`
93-
SQLBinaryExpr {
91+
/// Binary operation e.g. `1 + 1` or `foo > bar`
92+
SQLBinaryOp {
9493
left: Box<ASTNode>,
95-
op: SQLOperator,
94+
op: SQLBinaryOperator,
9695
right: Box<ASTNode>,
9796
},
97+
/// Unary operation e.g. `NOT foo`
98+
SQLUnaryOp {
99+
op: SQLUnaryOperator,
100+
expr: Box<ASTNode>,
101+
},
98102
/// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
99103
SQLCast {
100104
expr: Box<ASTNode>,
@@ -111,11 +115,6 @@ pub enum ASTNode {
111115
},
112116
/// Nested expression e.g. `(foo > bar)` or `(1)`
113117
SQLNested(Box<ASTNode>),
114-
/// Unary expression
115-
SQLUnary {
116-
operator: SQLOperator,
117-
expr: Box<ASTNode>,
118-
},
119118
/// SQLValue
120119
SQLValue(Value),
121120
/// Scalar function call e.g. `LEFT(foo, 5)`
@@ -179,12 +178,15 @@ impl ToString for ASTNode {
179178
low.to_string(),
180179
high.to_string()
181180
),
182-
ASTNode::SQLBinaryExpr { left, op, right } => format!(
181+
ASTNode::SQLBinaryOp { left, op, right } => format!(
183182
"{} {} {}",
184183
left.as_ref().to_string(),
185184
op.to_string(),
186185
right.as_ref().to_string()
187186
),
187+
ASTNode::SQLUnaryOp { op, expr } => {
188+
format!("{} {}", op.to_string(), expr.as_ref().to_string())
189+
}
188190
ASTNode::SQLCast { expr, data_type } => format!(
189191
"CAST({} AS {})",
190192
expr.as_ref().to_string(),
@@ -199,9 +201,6 @@ impl ToString for ASTNode {
199201
collation.to_string()
200202
),
201203
ASTNode::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
202-
ASTNode::SQLUnary { operator, expr } => {
203-
format!("{} {}", operator.to_string(), expr.as_ref().to_string())
204-
}
205204
ASTNode::SQLValue(v) => v.to_string(),
206205
ASTNode::SQLFunction(f) => f.to_string(),
207206
ASTNode::SQLCase {

src/sqlast/sql_operator.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,27 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13-
/// SQL Operator
13+
/// Unary operators
1414
#[derive(Debug, Clone, PartialEq, Hash)]
15-
pub enum SQLOperator {
15+
pub enum SQLUnaryOperator {
16+
Plus,
17+
Minus,
18+
Not,
19+
}
20+
21+
impl ToString for SQLUnaryOperator {
22+
fn to_string(&self) -> String {
23+
match self {
24+
SQLUnaryOperator::Plus => "+".to_string(),
25+
SQLUnaryOperator::Minus => "-".to_string(),
26+
SQLUnaryOperator::Not => "NOT".to_string(),
27+
}
28+
}
29+
}
30+
31+
/// Binary operators
32+
#[derive(Debug, Clone, PartialEq, Hash)]
33+
pub enum SQLBinaryOperator {
1634
Plus,
1735
Minus,
1836
Multiply,
@@ -26,30 +44,28 @@ pub enum SQLOperator {
2644
NotEq,
2745
And,
2846
Or,
29-
Not,
3047
Like,
3148
NotLike,
3249
}
3350

34-
impl ToString for SQLOperator {
51+
impl ToString for SQLBinaryOperator {
3552
fn to_string(&self) -> String {
3653
match self {
37-
SQLOperator::Plus => "+".to_string(),
38-
SQLOperator::Minus => "-".to_string(),
39-
SQLOperator::Multiply => "*".to_string(),
40-
SQLOperator::Divide => "/".to_string(),
41-
SQLOperator::Modulus => "%".to_string(),
42-
SQLOperator::Gt => ">".to_string(),
43-
SQLOperator::Lt => "<".to_string(),
44-
SQLOperator::GtEq => ">=".to_string(),
45-
SQLOperator::LtEq => "<=".to_string(),
46-
SQLOperator::Eq => "=".to_string(),
47-
SQLOperator::NotEq => "<>".to_string(),
48-
SQLOperator::And => "AND".to_string(),
49-
SQLOperator::Or => "OR".to_string(),
50-
SQLOperator::Not => "NOT".to_string(),
51-
SQLOperator::Like => "LIKE".to_string(),
52-
SQLOperator::NotLike => "NOT LIKE".to_string(),
54+
SQLBinaryOperator::Plus => "+".to_string(),
55+
SQLBinaryOperator::Minus => "-".to_string(),
56+
SQLBinaryOperator::Multiply => "*".to_string(),
57+
SQLBinaryOperator::Divide => "/".to_string(),
58+
SQLBinaryOperator::Modulus => "%".to_string(),
59+
SQLBinaryOperator::Gt => ">".to_string(),
60+
SQLBinaryOperator::Lt => "<".to_string(),
61+
SQLBinaryOperator::GtEq => ">=".to_string(),
62+
SQLBinaryOperator::LtEq => "<=".to_string(),
63+
SQLBinaryOperator::Eq => "=".to_string(),
64+
SQLBinaryOperator::NotEq => "<>".to_string(),
65+
SQLBinaryOperator::And => "AND".to_string(),
66+
SQLBinaryOperator::Or => "OR".to_string(),
67+
SQLBinaryOperator::Like => "LIKE".to_string(),
68+
SQLBinaryOperator::NotLike => "NOT LIKE".to_string(),
5369
}
5470
}
5571
}

src/sqlparser.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ impl Parser {
183183
"EXISTS" => self.parse_exists_expression(),
184184
"EXTRACT" => self.parse_extract_expression(),
185185
"INTERVAL" => self.parse_literal_interval(),
186-
"NOT" => Ok(ASTNode::SQLUnary {
187-
operator: SQLOperator::Not,
186+
"NOT" => Ok(ASTNode::SQLUnaryOp {
187+
op: SQLUnaryOperator::Not,
188188
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
189189
}),
190190
"TIME" => Ok(ASTNode::SQLValue(Value::Time(self.parse_literal_string()?))),
@@ -224,13 +224,13 @@ impl Parser {
224224
}, // End of Token::SQLWord
225225
Token::Mult => Ok(ASTNode::SQLWildcard),
226226
tok @ Token::Minus | tok @ Token::Plus => {
227-
let operator = if tok == Token::Plus {
228-
SQLOperator::Plus
227+
let op = if tok == Token::Plus {
228+
SQLUnaryOperator::Plus
229229
} else {
230-
SQLOperator::Minus
230+
SQLUnaryOperator::Minus
231231
};
232-
Ok(ASTNode::SQLUnary {
233-
operator,
232+
Ok(ASTNode::SQLUnaryOp {
233+
op,
234234
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
235235
})
236236
}
@@ -513,24 +513,24 @@ impl Parser {
513513
let tok = self.next_token().unwrap(); // safe as EOF's precedence is the lowest
514514

515515
let regular_binary_operator = match tok {
516-
Token::Eq => Some(SQLOperator::Eq),
517-
Token::Neq => Some(SQLOperator::NotEq),
518-
Token::Gt => Some(SQLOperator::Gt),
519-
Token::GtEq => Some(SQLOperator::GtEq),
520-
Token::Lt => Some(SQLOperator::Lt),
521-
Token::LtEq => Some(SQLOperator::LtEq),
522-
Token::Plus => Some(SQLOperator::Plus),
523-
Token::Minus => Some(SQLOperator::Minus),
524-
Token::Mult => Some(SQLOperator::Multiply),
525-
Token::Mod => Some(SQLOperator::Modulus),
526-
Token::Div => Some(SQLOperator::Divide),
516+
Token::Eq => Some(SQLBinaryOperator::Eq),
517+
Token::Neq => Some(SQLBinaryOperator::NotEq),
518+
Token::Gt => Some(SQLBinaryOperator::Gt),
519+
Token::GtEq => Some(SQLBinaryOperator::GtEq),
520+
Token::Lt => Some(SQLBinaryOperator::Lt),
521+
Token::LtEq => Some(SQLBinaryOperator::LtEq),
522+
Token::Plus => Some(SQLBinaryOperator::Plus),
523+
Token::Minus => Some(SQLBinaryOperator::Minus),
524+
Token::Mult => Some(SQLBinaryOperator::Multiply),
525+
Token::Mod => Some(SQLBinaryOperator::Modulus),
526+
Token::Div => Some(SQLBinaryOperator::Divide),
527527
Token::SQLWord(ref k) => match k.keyword.as_ref() {
528-
"AND" => Some(SQLOperator::And),
529-
"OR" => Some(SQLOperator::Or),
530-
"LIKE" => Some(SQLOperator::Like),
528+
"AND" => Some(SQLBinaryOperator::And),
529+
"OR" => Some(SQLBinaryOperator::Or),
530+
"LIKE" => Some(SQLBinaryOperator::Like),
531531
"NOT" => {
532532
if self.parse_keyword("LIKE") {
533-
Some(SQLOperator::NotLike)
533+
Some(SQLBinaryOperator::NotLike)
534534
} else {
535535
None
536536
}
@@ -541,7 +541,7 @@ impl Parser {
541541
};
542542

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

0 commit comments

Comments
 (0)