Skip to content

Commit abe4bce

Browse files
committed
sql_parser: expression parser refactoring (11)
1 parent d4e222a commit abe4bce

15 files changed

+381
-516
lines changed

src/parser/expression/between_expr.rs

+34-30
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,36 @@ impl<'a> BetweenExpressionParser for Parser<'a> {
2626

2727
let upper_bound = self.parse_expression()?;
2828

29-
if is_not {
30-
Ok(Expression::BinaryMatchingExpression(
31-
Box::new(expression),
32-
BinaryMatchingExpression::Not(Box::new(BinaryMatchingExpression::Between(
33-
BetweenExpression {
34-
lower_bound: Box::new(lower_bound),
35-
upper_bound: Box::new(upper_bound),
36-
},
37-
))),
38-
))
39-
} else {
40-
Ok(Expression::BinaryMatchingExpression(
41-
Box::new(expression),
42-
BinaryMatchingExpression::Between(BetweenExpression {
29+
let matching_expression = if is_not {
30+
BinaryMatchingExpression::Not(Box::new(BinaryMatchingExpression::Between(
31+
BetweenExpression {
4332
lower_bound: Box::new(lower_bound),
4433
upper_bound: Box::new(upper_bound),
45-
}),
46-
))
47-
}
34+
},
35+
)))
36+
} else {
37+
BinaryMatchingExpression::Between(BetweenExpression {
38+
lower_bound: Box::new(lower_bound),
39+
upper_bound: Box::new(upper_bound),
40+
})
41+
};
42+
43+
Ok(Expression::BinaryMatchingExpression(
44+
Box::new(expression),
45+
matching_expression,
46+
))
4847
}
4948
}
5049

5150
#[cfg(test)]
5251
mod between_expression_tests {
52+
use crate::parser::test_utils::run_sunny_day_test;
53+
use crate::select::test_utils::select_expr;
5354
use crate::{BetweenExpression, BinaryMatchingExpression, BinaryOp, Expression};
5455

5556
use crate::parser::expression::test_utils::*;
5657

57-
fn between_expression(
58+
fn between_expr(
5859
expression: Expression,
5960
lower_bound: Expression,
6061
upper_bound: Expression,
@@ -75,41 +76,44 @@ mod between_expression_tests {
7576
}
7677

7778
#[test]
78-
fn test_expression_between_basic() {
79-
run_sunny_day_expression_test(
79+
fn between_expression() {
80+
run_sunny_day_test(
8081
"SELECT 1 BETWEEN 2 AND 3;",
81-
&between_expression(
82+
select_expr(between_expr(
8283
numeric_expr("1"),
8384
numeric_expr("2"),
8485
numeric_expr("3"),
8586
false,
86-
),
87+
))
88+
.into(),
8789
);
8890
}
8991

9092
#[test]
91-
fn test_expression_between_with_expression() {
92-
run_sunny_day_expression_test(
93+
fn between_with_expression() {
94+
run_sunny_day_test(
9395
"SELECT 1 + 2 BETWEEN 3 AND 4;",
94-
&between_expression(
96+
select_expr(between_expr(
9597
binary_op(BinaryOp::Plus, numeric_expr("1"), numeric_expr("2")),
9698
numeric_expr("3"),
9799
numeric_expr("4"),
98100
false,
99-
),
101+
))
102+
.into(),
100103
);
101104
}
102105

103106
#[test]
104-
fn test_expression_not_between_with_expression() {
105-
run_sunny_day_expression_test(
107+
fn not_between_with_expression() {
108+
run_sunny_day_test(
106109
"SELECT 1 + 2 NOT BETWEEN 3 AND 4;",
107-
&between_expression(
110+
select_expr(between_expr(
108111
binary_op(BinaryOp::Plus, numeric_expr("1"), numeric_expr("2")),
109112
numeric_expr("3"),
110113
numeric_expr("4"),
111114
true,
112-
),
115+
))
116+
.into(),
113117
);
114118
}
115119
}

src/parser/expression/case_expr.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ impl<'a> CaseExpressionParser for Parser<'a> {
5353

5454
#[cfg(test)]
5555
mod case_expression_tests {
56+
use crate::parser::test_utils::run_sunny_day_test;
57+
use crate::select::test_utils::select_expr;
5658
use crate::{BinaryOp, CaseExpression, Expression, WhenExpression};
5759

5860
use crate::expression::test_utils::*;
@@ -70,22 +72,27 @@ mod case_expression_tests {
7072
}
7173

7274
#[test]
73-
fn test_expression_case_basic() {
75+
fn case_expression_test() {
7476
let expression = None;
7577
let when_expressions = vec![WhenExpression {
7678
condition: Box::new(numeric_expr("1")),
7779
then_expression: Box::new(numeric_expr("2")),
7880
}];
7981
let else_expression = Some(Box::new(numeric_expr("3")));
8082

81-
run_sunny_day_expression_test(
83+
run_sunny_day_test(
8284
"SELECT CASE WHEN 1 THEN 2 ELSE 3 END;",
83-
&case_expression(expression, when_expressions, else_expression),
85+
select_expr(case_expression(
86+
expression,
87+
when_expressions,
88+
else_expression,
89+
))
90+
.into(),
8491
);
8592
}
8693

8794
#[test]
88-
fn test_expression_case_with_multiple_when_expressions() {
95+
fn case_with_multiple_when_expressions() {
8996
let expression = None;
9097
let when_expressions = vec![
9198
WhenExpression {
@@ -99,14 +106,19 @@ mod case_expression_tests {
99106
];
100107
let else_expression = Some(Box::new(numeric_expr("5")));
101108

102-
run_sunny_day_expression_test(
109+
run_sunny_day_test(
103110
"SELECT CASE WHEN 1 THEN 2 WHEN 3 THEN 4 ELSE 5 END;",
104-
&case_expression(expression, when_expressions, else_expression),
111+
select_expr(case_expression(
112+
expression,
113+
when_expressions,
114+
else_expression,
115+
))
116+
.into(),
105117
);
106118
}
107119

108120
#[test]
109-
fn test_expression_case_with_main_expression() {
121+
fn case_with_main_expression() {
110122
let expression = Some(Box::new(binary_op(
111123
BinaryOp::EqualsEquals,
112124
numeric_expr("1"),
@@ -118,9 +130,14 @@ mod case_expression_tests {
118130
}];
119131
let else_expression = Some(Box::new(numeric_expr("2")));
120132

121-
run_sunny_day_expression_test(
133+
run_sunny_day_test(
122134
"SELECT CASE 1 == 1 WHEN TRUE THEN 1 ELSE 2 END;",
123-
&case_expression(expression, when_expressions, else_expression),
135+
select_expr(case_expression(
136+
expression,
137+
when_expressions,
138+
else_expression,
139+
))
140+
.into(),
124141
);
125142
}
126143
}

src/parser/expression/cast_expr.rs

+35-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub trait CastExpressionParser {
99

1010
impl<'a> CastExpressionParser for Parser<'a> {
1111
fn parse_cast_expression(&mut self) -> Result<Expression, ParsingError> {
12-
dbg!(&self.peek_token()?);
1312
self.consume_as_keyword(Keyword::Cast)?;
1413

1514
self.consume_as(TokenType::LeftParen)?;
@@ -19,7 +18,6 @@ impl<'a> CastExpressionParser for Parser<'a> {
1918

2019
let data_type = self.parse_data_type()?;
2120

22-
dbg!(&self.peek_token()?);
2321
self.consume_as(TokenType::RightParen)?;
2422

2523
Ok(Expression::Cast(Box::new(expression), data_type))
@@ -28,67 +26,81 @@ impl<'a> CastExpressionParser for Parser<'a> {
2826

2927
#[cfg(test)]
3028
mod cast_expression_tests {
29+
use crate::parser::test_utils::run_sunny_day_test;
30+
use crate::select::test_utils::select_expr;
3131
use crate::{BinaryOp, DataType};
3232

3333
use crate::expression::test_utils::*;
3434

3535
#[test]
36-
fn test_expression_cast_basic() {
37-
run_sunny_day_expression_test(
36+
fn cast_test() {
37+
run_sunny_day_test(
3838
"SELECT CAST(1 AS INTEGER);",
39-
&cast_expr(numeric_expr("1"), DataType::PlainDataType("INTEGER".into())),
39+
select_expr(cast_expr(
40+
numeric_expr("1"),
41+
DataType::PlainDataType("INTEGER".into()),
42+
))
43+
.into(),
4044
);
4145
}
4246

4347
#[test]
44-
fn test_expression_cast_expression() {
45-
run_sunny_day_expression_test(
48+
fn cast_expression() {
49+
run_sunny_day_test(
4650
"SELECT CAST(1 + 2 AS INTEGER);",
47-
&cast_expr(
51+
select_expr(cast_expr(
4852
binary_op(BinaryOp::Plus, numeric_expr("1"), numeric_expr("2")),
4953
DataType::PlainDataType("INTEGER".into()),
50-
),
54+
))
55+
.into(),
5156
);
5257
}
5358

5459
#[test]
55-
fn test_expression_cast_with_null() {
56-
run_sunny_day_expression_test(
60+
fn cast_with_null() {
61+
run_sunny_day_test(
5762
"SELECT CAST(NULL AS INTEGER);",
58-
&cast_expr(null_expr(), DataType::PlainDataType("INTEGER".into())),
63+
select_expr(cast_expr(
64+
null_expr(),
65+
DataType::PlainDataType("INTEGER".into()),
66+
))
67+
.into(),
5968
);
6069
}
6170

6271
#[test]
63-
fn test_expression_cast_with_complex_type() {
64-
run_sunny_day_expression_test(
72+
fn cast_with_sized_type() {
73+
run_sunny_day_test(
6574
"SELECT CAST(1 AS VARCHAR(10));",
66-
&cast_expr(
75+
select_expr(cast_expr(
6776
numeric_expr("1"),
6877
DataType::SizedDataType("VARCHAR".into(), "10".into()),
69-
),
78+
))
79+
.into(),
7080
);
7181
}
7282

7383
#[test]
74-
fn test_expression_cast_with_complex_type2() {
75-
run_sunny_day_expression_test(
84+
fn cast_with_bounded_type() {
85+
run_sunny_day_test(
7686
"SELECT CAST(1 AS VARCHAR(1, 10));",
77-
&cast_expr(
87+
select_expr(cast_expr(
7888
numeric_expr("1"),
7989
DataType::BoundedDataType("VARCHAR".into(), "1".into(), "10".into()),
80-
),
90+
))
91+
.into(),
8192
);
8293
}
8394

8495
#[test]
8596
fn cast_with_multi_name() {
86-
run_sunny_day_expression_test(
97+
run_sunny_day_test(
8798
"SELECT CAST(1 AS DOUBLE TRIPPLE PRECISION) as 1;",
88-
&cast_expr(
99+
select_expr(cast_expr(
89100
numeric_expr("1"),
90101
DataType::PlainDataType("DOUBLE TRIPPLE PRECISION".into()),
91-
),
102+
))
103+
.into(),
92104
);
93105
}
94106
}

src/parser/expression/collate_expr.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,27 @@ impl<'a> CollateExpressionParser for Parser<'a> {
2929
#[cfg(test)]
3030
mod collate_expression_tests {
3131
use crate::parser::expression::test_utils::*;
32+
use crate::parser::test_utils::run_sunny_day_test;
33+
use crate::select::test_utils::select_expr;
3234
use crate::BinaryOp;
3335

3436
#[test]
35-
fn test_expression_collate_basic() {
36-
run_sunny_day_expression_test(
37+
fn collate_test() {
38+
run_sunny_day_test(
3739
"SELECT 1 COLLATE 'utf8';",
38-
&collate_expr(numeric_expr("1"), "'utf8'".to_string()),
40+
select_expr(collate_expr(numeric_expr("1"), "'utf8'".to_string())).into(),
3941
);
4042
}
4143

4244
#[test]
43-
fn test_expression_collate_with_expression() {
44-
run_sunny_day_expression_test(
45+
fn collate_with_expression() {
46+
run_sunny_day_test(
4547
"SELECT 1 + 2 COLLATE 'utf8';",
46-
&collate_expr(
48+
select_expr(collate_expr(
4749
binary_op(BinaryOp::Plus, numeric_expr("1"), numeric_expr("2")),
4850
"'utf8'".to_string(),
49-
),
51+
))
52+
.into(),
5053
);
5154
}
5255
}

0 commit comments

Comments
 (0)