Skip to content

Commit e64a717

Browse files
committed
add parse_factorial_operator test and optimize redundant code
1 parent d203d0b commit e64a717

File tree

1 file changed

+77
-14
lines changed

1 file changed

+77
-14
lines changed

tests/sqlparser_common.rs

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11467,25 +11467,22 @@ fn parse_bang_not() {
1146711467
let sql = "SELECT !a, !(b > 3)";
1146811468
let Select { projection, .. } = dialects.verified_only_select(sql);
1146911469

11470-
for (i, (op, expr)) in [
11471-
(
11472-
UnaryOperator::BangNot,
11473-
Box::new(Expr::Identifier(Ident::new("a"))),
11474-
),
11475-
(
11476-
UnaryOperator::BangNot,
11477-
Box::new(Expr::Nested(Box::new(Expr::BinaryOp {
11478-
left: Box::new(Expr::Identifier(Ident::new("b"))),
11479-
op: BinaryOperator::Gt,
11480-
right: Box::new(Expr::Value(Value::Number("3".parse().unwrap(), false))),
11481-
}))),
11482-
),
11470+
for (i, expr) in [
11471+
Box::new(Expr::Identifier(Ident::new("a"))),
11472+
Box::new(Expr::Nested(Box::new(Expr::BinaryOp {
11473+
left: Box::new(Expr::Identifier(Ident::new("b"))),
11474+
op: BinaryOperator::Gt,
11475+
right: Box::new(Expr::Value(Value::Number("3".parse().unwrap(), false))),
11476+
}))),
1148311477
]
1148411478
.into_iter()
1148511479
.enumerate()
1148611480
{
1148711481
assert_eq!(
11488-
SelectItem::UnnamedExpr(Expr::UnaryOp { op: op, expr }),
11482+
SelectItem::UnnamedExpr(Expr::UnaryOp {
11483+
op: UnaryOperator::BangNot,
11484+
expr
11485+
}),
1148911486
projection[i]
1149011487
)
1149111488
}
@@ -11511,3 +11508,69 @@ fn parse_bang_not() {
1151111508
);
1151211509
}
1151311510
}
11511+
11512+
#[test]
11513+
fn parse_factorial_operator() {
11514+
let dialects = all_dialects_where(|d| d.supports_factorial_operator());
11515+
let sql = "SELECT a!, (b + c)!";
11516+
let Select { projection, .. } = dialects.verified_only_select(sql);
11517+
11518+
for (i, expr) in [
11519+
Box::new(Expr::Identifier(Ident::new("a"))),
11520+
Box::new(Expr::Nested(Box::new(Expr::BinaryOp {
11521+
left: Box::new(Expr::Identifier(Ident::new("b"))),
11522+
op: BinaryOperator::Plus,
11523+
right: Box::new(Expr::Identifier(Ident::new("c"))),
11524+
}))),
11525+
]
11526+
.into_iter()
11527+
.enumerate()
11528+
{
11529+
assert_eq!(
11530+
SelectItem::UnnamedExpr(Expr::UnaryOp {
11531+
op: UnaryOperator::PGPostfixFactorial,
11532+
expr
11533+
}),
11534+
projection[i]
11535+
)
11536+
}
11537+
11538+
let sql_statements = ["SELECT !a", "SELECT !a b", "SELECT !a as b"];
11539+
11540+
for &sql in &sql_statements {
11541+
assert_eq!(
11542+
dialects.parse_sql_statements(sql).unwrap_err(),
11543+
ParserError::ParserError("Expected: an expression, found: !".to_string())
11544+
);
11545+
}
11546+
11547+
let sql_statements = ["SELECT a!", "SELECT a ! b", "SELECT a ! as b"];
11548+
11549+
// Due to the exclamation mark, which is both part of the `bang not` operator
11550+
// and the `factorial` operator, additional filtering not supports
11551+
// `bang not` operator is required here.
11552+
let dialects =
11553+
all_dialects_where(|d| !d.supports_factorial_operator() && !d.supports_bang_not_operator());
11554+
11555+
for &sql in &sql_statements {
11556+
assert_eq!(
11557+
dialects.parse_sql_statements(sql).unwrap_err(),
11558+
ParserError::ParserError("No infix parser for token ExclamationMark".to_string())
11559+
);
11560+
}
11561+
11562+
// Due to the exclamation mark, which is both part of the `bang not` operator
11563+
// and the `factorial` operator, additional filtering supports
11564+
// `bang not` operator is required here.
11565+
let dialects =
11566+
all_dialects_where(|d| !d.supports_factorial_operator() && d.supports_bang_not_operator());
11567+
11568+
for &sql in &sql_statements {
11569+
assert_eq!(
11570+
dialects.parse_sql_statements(sql).unwrap_err(),
11571+
ParserError::ParserError(
11572+
"current dialect support bang not operator, but with wrong syntax".to_string()
11573+
)
11574+
);
11575+
}
11576+
}

0 commit comments

Comments
 (0)