Skip to content

Commit 6795971

Browse files
committed
sql_parser: is_null and not_null expression refactored
1 parent 4aaabc5 commit 6795971

File tree

2 files changed

+15
-62
lines changed

2 files changed

+15
-62
lines changed

src/ast/expression/mod.rs

+6-22
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ pub enum Expression {
6060
/// Is, e.g. $expression IS $expression
6161
IsExpression(AnIsExpression),
6262

63+
/// Is Null, e.g. $expression IS NULL
64+
IsNull(Box<Expression>),
65+
66+
/// Is Not Null, e.g. $expression IS NOT NULL
67+
IsNotNull(Box<Expression>),
68+
6369
/// In, e.g. $expression IN $expression
6470
InExpression(InExpression),
6571

6672
/// Between
6773
BetweenExpression(BetweenExpression),
6874

69-
/// A binary matching expressions (e.g. $expr1 NOT MATCH $expr2)
70-
BinaryMatchingExpression(Box<Expression>, BinaryMatchingExpression),
71-
72-
/// A unary matching expression (e.g. expression IS NOT NULL)
73-
UnaryMatchingExpression(Box<Expression>, UnaryMatchingExpression),
74-
7575
/// An exists statement
7676
ExistsStatement(ExistsStatement),
7777

@@ -133,22 +133,6 @@ pub enum DataType {
133133
BoundedDataType(DataTypeName, String, String),
134134
}
135135

136-
/// An unary matching expression type
137-
#[derive(Debug, PartialEq, Clone)]
138-
pub enum UnaryMatchingExpression {
139-
/// Is Null
140-
IsNull,
141-
/// Is Not Null
142-
IsNotNull,
143-
}
144-
145-
/// A binary matching expression
146-
#[derive(Debug, PartialEq, Clone)]
147-
pub enum BinaryMatchingExpression {
148-
/// For NOT $BinaryMatchingExpression use cases
149-
Not(Box<BinaryMatchingExpression>),
150-
}
151-
152136
/// A like expression type
153137
#[derive(Debug, PartialEq, Clone)]
154138
pub struct LikeExpressionType {

src/parser/expression/mod.rs

+9-40
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ mod precedence;
1111
mod raise_expr;
1212
mod regexp_match_expr;
1313
use crate::parser::errors::ParsingError;
14-
use crate::{
15-
BinaryOp, Expression, Keyword, LiteralValue, Parser, TokenType, UnaryMatchingExpression,
16-
UnaryOp,
17-
};
14+
use crate::{BinaryOp, Expression, Keyword, LiteralValue, Parser, TokenType, UnaryOp};
1815
use between_expr::BetweenExpressionParser;
1916
use case_expr::CaseExpressionParser;
2017
use cast_expr::CastExpressionParser;
@@ -141,27 +138,18 @@ impl ExpressionParser for Parser<'_> {
141138
return RegexpMatchExpressionParser::parse_regexp_match_expression(self, left, false);
142139
} else if let Ok(Keyword::Isnull) = self.peek_as_keyword() {
143140
self.consume_as_keyword(Keyword::Isnull)?;
144-
Ok(Expression::UnaryMatchingExpression(
145-
Box::new(left),
146-
UnaryMatchingExpression::IsNull,
147-
))
141+
Ok(Expression::IsNull(Box::new(left)))
148142
} else if let Ok(Keyword::Notnull) = self.peek_as_keyword() {
149143
self.consume_as_keyword(Keyword::Notnull)?;
150-
Ok(Expression::UnaryMatchingExpression(
151-
Box::new(left),
152-
UnaryMatchingExpression::IsNotNull,
153-
))
144+
Ok(Expression::IsNotNull(Box::new(left)))
154145
} else if let Ok(Keyword::Not) = self.peek_as_keyword() {
155146
self.consume_as_keyword(Keyword::Not)?;
156147

157148
if let Ok(nested_keyword) = self.peek_as_keyword() {
158149
match nested_keyword {
159150
Keyword::Null => {
160151
self.consume_as_keyword(Keyword::Null)?;
161-
Ok(Expression::UnaryMatchingExpression(
162-
Box::new(left),
163-
UnaryMatchingExpression::IsNotNull,
164-
))
152+
Ok(Expression::IsNotNull(Box::new(left)))
165153
}
166154
Keyword::Between => {
167155
BetweenExpressionParser::parse_between_expression(self, left, true)
@@ -695,50 +683,31 @@ mod binary_op_tests {
695683
}
696684

697685
#[cfg(test)]
698-
mod unary_matching_expression_tests {
686+
mod null_expression_tests {
699687
use crate::parser::test_utils::run_sunny_day_test;
700688
use crate::select::test_utils::select_expr;
701-
use crate::{Expression, UnaryMatchingExpression};
689+
use crate::Expression;
702690

703691
use crate::parser::expression::test_utils::*;
704692

705-
fn unary_matching_expression(
706-
expression: Expression,
707-
unary_matching_expression: UnaryMatchingExpression,
708-
) -> Expression {
709-
Expression::UnaryMatchingExpression(Box::new(expression), unary_matching_expression)
710-
}
711-
712693
#[test]
713694
fn isnull() {
714695
run_sunny_day_test(
715696
"SELECT 1 ISNULL;",
716-
select_expr(unary_matching_expression(
717-
numeric_expr("1"),
718-
UnaryMatchingExpression::IsNull,
719-
))
720-
.into(),
697+
select_expr(Expression::IsNull(Box::new(numeric_expr("1")))).into(),
721698
);
722699
}
723700

724701
#[test]
725702
fn notnull() {
726703
run_sunny_day_test(
727704
"SELECT 1 NOTNULL;",
728-
select_expr(unary_matching_expression(
729-
numeric_expr("1"),
730-
UnaryMatchingExpression::IsNotNull,
731-
))
732-
.into(),
705+
select_expr(Expression::IsNotNull(Box::new(numeric_expr("1")))).into(),
733706
);
734707

735708
run_sunny_day_test(
736709
"SELECT 1 NOT NULL;",
737-
select_expr(unary_matching_expression(
738-
numeric_expr("1"),
739-
UnaryMatchingExpression::IsNotNull,
740-
))
741-
.into(),
710+
select_expr(Expression::IsNotNull(Box::new(numeric_expr("1")))).into(),
742711
);
743712
}
744713
}

0 commit comments

Comments
 (0)