Skip to content

Commit d6116af

Browse files
committed
Add Expr::value and update clickhouse test
1 parent 4d101b0 commit d6116af

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

src/ast/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,12 @@ pub enum Expr {
10361036
Lambda(LambdaFunction),
10371037
}
10381038

1039+
impl Expr {
1040+
pub fn value(value: impl Into<ValueWithSpan>) -> Self {
1041+
Expr::Value(value.into())
1042+
}
1043+
}
1044+
10391045
/// The contents inside the `[` and `]` in a subscript expression.
10401046
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10411047
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

tests/sqlparser_clickhouse.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn parse_map_access_expr() {
7171
left: Box::new(BinaryOp {
7272
left: Box::new(Identifier(Ident::new("id"))),
7373
op: BinaryOperator::Eq,
74-
right: Box::new(Expr::Value(Value::SingleQuotedString("test".to_string()))),
74+
right: Box::new(Expr::value(Value::SingleQuotedString("test".to_string()))),
7575
}),
7676
op: BinaryOperator::And,
7777
right: Box::new(BinaryOp {
@@ -82,13 +82,13 @@ fn parse_map_access_expr() {
8282
"indexOf",
8383
[
8484
Expr::Identifier(Ident::new("string_name")),
85-
Expr::Value(Value::SingleQuotedString("app".to_string()))
85+
Expr::value(Value::SingleQuotedString("app".to_string()))
8686
]
8787
),
8888
})],
8989
}),
9090
op: BinaryOperator::NotEq,
91-
right: Box::new(Expr::Value(Value::SingleQuotedString("foo".to_string()))),
91+
right: Box::new(Expr::value(Value::SingleQuotedString("foo".to_string()))),
9292
}),
9393
}),
9494
group_by: GroupByExpr::Expressions(vec![], vec![]),
@@ -114,8 +114,8 @@ fn parse_array_expr() {
114114
assert_eq!(
115115
&Expr::Array(Array {
116116
elem: vec![
117-
Expr::Value(Value::SingleQuotedString("1".to_string())),
118-
Expr::Value(Value::SingleQuotedString("2".to_string())),
117+
Expr::value(Value::SingleQuotedString("1".to_string())),
118+
Expr::value(Value::SingleQuotedString("2".to_string())),
119119
],
120120
named: false,
121121
}),
@@ -1014,14 +1014,14 @@ fn parse_select_parametric_function() {
10141014
assert_eq!(parameters.args.len(), 2);
10151015
assert_eq!(
10161016
parameters.args[0],
1017-
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(Value::Number(
1017+
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::value(Value::Number(
10181018
"0.5".parse().unwrap(),
10191019
false
10201020
))))
10211021
);
10221022
assert_eq!(
10231023
parameters.args[1],
1024-
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(Value::Number(
1024+
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::value(Value::Number(
10251025
"0.6".parse().unwrap(),
10261026
false
10271027
))))
@@ -1074,19 +1074,19 @@ fn parse_select_order_by_with_fill_interpolate() {
10741074
asc: Some(true),
10751075
nulls_first: Some(true),
10761076
with_fill: Some(WithFill {
1077-
from: Some(Expr::Value(number("10"))),
1078-
to: Some(Expr::Value(number("20"))),
1079-
step: Some(Expr::Value(number("2"))),
1077+
from: Some(Expr::value(number("10"))),
1078+
to: Some(Expr::value(number("20"))),
1079+
step: Some(Expr::value(number("2"))),
10801080
}),
10811081
},
10821082
OrderByExpr {
10831083
expr: Expr::Identifier(Ident::new("lname")),
10841084
asc: Some(false),
10851085
nulls_first: Some(false),
10861086
with_fill: Some(WithFill {
1087-
from: Some(Expr::Value(number("30"))),
1088-
to: Some(Expr::Value(number("40"))),
1089-
step: Some(Expr::Value(number("3"))),
1087+
from: Some(Expr::value(number("30"))),
1088+
to: Some(Expr::value(number("40"))),
1089+
step: Some(Expr::value(number("3"))),
10901090
}),
10911091
},
10921092
],
@@ -1096,14 +1096,14 @@ fn parse_select_order_by_with_fill_interpolate() {
10961096
expr: Some(Expr::BinaryOp {
10971097
left: Box::new(Expr::Identifier(Ident::new("col1"))),
10981098
op: BinaryOperator::Plus,
1099-
right: Box::new(Expr::Value(number("1"))),
1099+
right: Box::new(Expr::value(number("1"))),
11001100
}),
11011101
}])
11021102
})
11031103
},
11041104
select.order_by.expect("ORDER BY expected")
11051105
);
1106-
assert_eq!(Some(Expr::Value(number("2"))), select.limit);
1106+
assert_eq!(Some(Expr::value(number("2"))), select.limit);
11071107
}
11081108

11091109
#[test]
@@ -1144,9 +1144,9 @@ fn parse_with_fill() {
11441144
let select = clickhouse().verified_query(sql);
11451145
assert_eq!(
11461146
Some(WithFill {
1147-
from: Some(Expr::Value(number("10"))),
1148-
to: Some(Expr::Value(number("20"))),
1149-
step: Some(Expr::Value(number("2"))),
1147+
from: Some(Expr::value(number("10"))),
1148+
to: Some(Expr::value(number("20"))),
1149+
step: Some(Expr::value(number("2"))),
11501150
}),
11511151
select.order_by.expect("ORDER BY expected").exprs[0].with_fill
11521152
);
@@ -1183,7 +1183,7 @@ fn parse_interpolate_body_with_columns() {
11831183
expr: Some(Expr::BinaryOp {
11841184
left: Box::new(Expr::Identifier(Ident::new("col1"))),
11851185
op: BinaryOperator::Plus,
1186-
right: Box::new(Expr::Value(number("1"))),
1186+
right: Box::new(Expr::value(number("1"))),
11871187
}),
11881188
},
11891189
InterpolateExpr {
@@ -1195,7 +1195,7 @@ fn parse_interpolate_body_with_columns() {
11951195
expr: Some(Expr::BinaryOp {
11961196
left: Box::new(Expr::Identifier(Ident::new("col4"))),
11971197
op: BinaryOperator::Plus,
1198-
right: Box::new(Expr::Value(number("4"))),
1198+
right: Box::new(Expr::value(number("4"))),
11991199
}),
12001200
},
12011201
])
@@ -1236,7 +1236,7 @@ fn test_prewhere() {
12361236
Some(&BinaryOp {
12371237
left: Box::new(Identifier(Ident::new("x"))),
12381238
op: BinaryOperator::Eq,
1239-
right: Box::new(Expr::Value(Value::Number("1".parse().unwrap(), false))),
1239+
right: Box::new(Expr::value(Value::Number("1".parse().unwrap(), false))),
12401240
})
12411241
);
12421242
let selection = query.as_ref().body.as_select().unwrap().selection.as_ref();
@@ -1245,7 +1245,7 @@ fn test_prewhere() {
12451245
Some(&BinaryOp {
12461246
left: Box::new(Identifier(Ident::new("y"))),
12471247
op: BinaryOperator::Eq,
1248-
right: Box::new(Expr::Value(Value::Number("2".parse().unwrap(), false))),
1248+
right: Box::new(Expr::value(Value::Number("2".parse().unwrap(), false))),
12491249
})
12501250
);
12511251
}
@@ -1261,13 +1261,13 @@ fn test_prewhere() {
12611261
left: Box::new(BinaryOp {
12621262
left: Box::new(Identifier(Ident::new("x"))),
12631263
op: BinaryOperator::Eq,
1264-
right: Box::new(Expr::Value(Value::Number("1".parse().unwrap(), false))),
1264+
right: Box::new(Expr::value(Value::Number("1".parse().unwrap(), false))),
12651265
}),
12661266
op: BinaryOperator::And,
12671267
right: Box::new(BinaryOp {
12681268
left: Box::new(Identifier(Ident::new("y"))),
12691269
op: BinaryOperator::Eq,
1270-
right: Box::new(Expr::Value(Value::Number("2".parse().unwrap(), false))),
1270+
right: Box::new(Expr::value(Value::Number("2".parse().unwrap(), false))),
12711271
}),
12721272
})
12731273
);
@@ -1375,7 +1375,7 @@ fn parse_create_table_on_commit_and_as_query() {
13751375
assert_eq!(on_commit, Some(OnCommit::PreserveRows));
13761376
assert_eq!(
13771377
query.unwrap().body.as_select().unwrap().projection,
1378-
vec![UnnamedExpr(Expr::Value(Value::Number(
1378+
vec![UnnamedExpr(Expr::value(Value::Number(
13791379
"1".parse().unwrap(),
13801380
false
13811381
)))]
@@ -1391,7 +1391,7 @@ fn parse_freeze_and_unfreeze_partition() {
13911391
for operation_name in &["FREEZE", "UNFREEZE"] {
13921392
let sql = format!("ALTER TABLE t {operation_name} PARTITION '2024-08-14'");
13931393

1394-
let expected_partition = Partition::Expr(Expr::Value(Value::SingleQuotedString(
1394+
let expected_partition = Partition::Expr(Expr::value(Value::SingleQuotedString(
13951395
"2024-08-14".to_string(),
13961396
)));
13971397
match clickhouse_and_generic().verified_stmt(&sql) {
@@ -1421,7 +1421,7 @@ fn parse_freeze_and_unfreeze_partition() {
14211421
match clickhouse_and_generic().verified_stmt(&sql) {
14221422
Statement::AlterTable { operations, .. } => {
14231423
assert_eq!(operations.len(), 1);
1424-
let expected_partition = Partition::Expr(Expr::Value(Value::SingleQuotedString(
1424+
let expected_partition = Partition::Expr(Expr::value(Value::SingleQuotedString(
14251425
"2024-08-14".to_string(),
14261426
)));
14271427
let expected_operation = if operation_name == &"FREEZE" {

0 commit comments

Comments
 (0)