Skip to content

Commit 1a7eb70

Browse files
committed
sql_parser: expr is expression refactored
1 parent 7816f98 commit 1a7eb70

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

src/ast/expression/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,6 @@ pub enum BinaryMatchingExpression {
150150
/// For NOT $BinaryMatchingExpression use cases
151151
Not(Box<BinaryMatchingExpression>),
152152

153-
/// Is
154-
Is(AnIsExpression),
155-
156153
/// In
157154
In(InExpression),
158155

@@ -232,8 +229,15 @@ impl From<MatchExpression> for Expression {
232229
pub struct AnIsExpression {
233230
/// The expression
234231
pub expression: Box<Expression>,
232+
233+
/// Whether the expression is not
234+
pub not: bool,
235+
235236
/// Whether the expression is distinct
236237
pub distinct: bool,
238+
239+
/// The matching expression
240+
pub matching_expression: Box<Expression>,
237241
}
238242

239243
/// A between expression

src/parser/expression/is_expr.rs

+18-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::parser::errors::ParsingError;
2-
use crate::{AnIsExpression, BinaryMatchingExpression, Expression, Keyword, Parser};
2+
use crate::{AnIsExpression, Expression, Keyword, Parser};
33

44
use super::ExpressionParser;
55

@@ -22,58 +22,44 @@ impl IsExpressionParser for Parser<'_> {
2222
self.consume_as_keyword(Keyword::From)?;
2323
}
2424

25-
let is_expression = BinaryMatchingExpression::Is(AnIsExpression {
26-
expression: Box::new(self.parse_expression()?),
25+
let is_expression = AnIsExpression {
26+
expression: Box::new(expression),
27+
not: is_not,
2728
distinct,
28-
});
29-
30-
let result = if is_not {
31-
BinaryMatchingExpression::Not(Box::new(is_expression))
32-
} else {
33-
is_expression
29+
matching_expression: Box::new(self.parse_expression()?),
3430
};
3531

36-
Ok(Expression::BinaryMatchingExpression(
37-
Box::new(expression),
38-
result,
39-
))
32+
Ok(Expression::IsExpression(is_expression))
4033
}
4134
}
4235

4336
#[cfg(test)]
4437
mod is_expression_tests {
4538
use crate::parser::test_utils::run_sunny_day_test;
4639
use crate::select::test_utils::select_expr;
47-
use crate::{AnIsExpression, BinaryMatchingExpression, Expression};
40+
use crate::{AnIsExpression, Expression};
4841

4942
use crate::parser::expression::test_utils::*;
5043

51-
fn expr_from_expr(
44+
fn is_expression(
5245
expression: Expression,
53-
is_expression: Expression,
46+
matching_expression: Expression,
5447
distinct: bool,
5548
is_not: bool,
5649
) -> Expression {
57-
let binary_matching_expression = if is_not {
58-
BinaryMatchingExpression::Not(Box::new(BinaryMatchingExpression::Is(AnIsExpression {
59-
expression: Box::new(is_expression),
60-
distinct,
61-
})))
62-
} else {
63-
BinaryMatchingExpression::Is(AnIsExpression {
64-
expression: Box::new(is_expression),
65-
distinct,
66-
})
67-
};
68-
69-
Expression::BinaryMatchingExpression(Box::new(expression), binary_matching_expression)
50+
Expression::IsExpression(AnIsExpression {
51+
expression: Box::new(expression),
52+
distinct,
53+
not: is_not,
54+
matching_expression: Box::new(matching_expression),
55+
})
7056
}
7157

7258
#[test]
7359
fn is_expr_test() {
7460
run_sunny_day_test(
7561
"SELECT 1 IS 1;",
76-
select_expr(expr_from_expr(
62+
select_expr(is_expression(
7763
numeric_expr("1"),
7864
numeric_expr("1"),
7965
false,
@@ -87,7 +73,7 @@ mod is_expression_tests {
8773
fn is_not_expr_test() {
8874
run_sunny_day_test(
8975
"SELECT 1 IS NOT 1;",
90-
select_expr(expr_from_expr(
76+
select_expr(is_expression(
9177
numeric_expr("1"),
9278
numeric_expr("1"),
9379
false,
@@ -101,7 +87,7 @@ mod is_expression_tests {
10187
fn is_distinct_expr_test() {
10288
run_sunny_day_test(
10389
"SELECT 1 IS DISTINCT FROM 1;",
104-
select_expr(expr_from_expr(
90+
select_expr(is_expression(
10591
numeric_expr("1"),
10692
numeric_expr("1"),
10793
true,

0 commit comments

Comments
 (0)