Skip to content

Commit d203d0b

Browse files
committed
Modify the code according to the recommendations and add more test cases
1 parent 503d08b commit d203d0b

File tree

3 files changed

+43
-52
lines changed

3 files changed

+43
-52
lines changed

src/dialect/hive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl Dialect for HiveDialect {
5252
true
5353
}
5454

55+
/// See Hive <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362061#Tutorial-BuiltInOperators>
5556
fn supports_bang_not_operator(&self) -> bool {
5657
true
5758
}

src/parser/mod.rs

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,13 +2035,6 @@ impl<'a> Parser<'a> {
20352035
}
20362036
}
20372037

2038-
pub fn parse_bang_not(&mut self) -> Result<Expr, ParserError> {
2039-
Ok(Expr::UnaryOp {
2040-
op: UnaryOperator::BangNot,
2041-
expr: Box::new(self.parse_subexpr(self.dialect.prec_value(Precedence::UnaryNot))?),
2042-
})
2043-
}
2044-
20452038
/// Parses fulltext expressions [`sqlparser::ast::Expr::MatchAgainst`]
20462039
///
20472040
/// # Errors
@@ -2813,40 +2806,32 @@ impl<'a> Parser<'a> {
28132806
data_type: self.parse_data_type()?,
28142807
format: None,
28152808
})
2816-
} else if Token::ExclamationMark == tok {
2817-
if self.dialect.supports_factorial_operator() {
2818-
match expr {
2819-
Expr::Value(_) | Expr::Identifier(_) | Expr::Nested(_) | Expr::BinaryOp{..} => Ok(Expr::UnaryOp {
2820-
op: UnaryOperator::PGPostfixFactorial,
2821-
expr: Box::new(expr),
2822-
}),
2823-
_ => {
2824-
self.expected(
2825-
"Value or Identifier or Nested or BinaryOp struct before factorial operator(!)", self.peek_token())
2826-
},
2827-
}
2828-
} else if self.dialect.supports_bang_not_operator() {
2829-
let token = self.next_token();
2830-
match token.token {
2831-
Token::Word(_) | Token::Number(..) => Ok(Expr::UnaryOp {
2809+
} else if Token::ExclamationMark == tok && self.dialect.supports_factorial_operator() {
2810+
Ok(Expr::UnaryOp {
2811+
op: UnaryOperator::PGPostfixFactorial,
2812+
expr: Box::new(expr),
2813+
})
2814+
} else if Token::ExclamationMark == tok && self.dialect.supports_bang_not_operator() {
2815+
let token = self.next_token();
2816+
match token.token {
2817+
Token::Word(_) | Token::Number(..)
2818+
if !matches!(
2819+
expr,
2820+
Expr::Value(_)
2821+
| Expr::Identifier(_)
2822+
| Expr::Nested(_)
2823+
| Expr::BinaryOp { .. }
2824+
) =>
2825+
{
2826+
Ok(Expr::UnaryOp {
28322827
op: UnaryOperator::BangNot,
28332828
expr: Box::new(expr),
2834-
}),
2835-
_ => {
2836-
parser_err!(
2837-
"current dialect support bang not operator, but with wrong synx",
2838-
tok.location
2839-
)
2840-
}
2829+
})
28412830
}
2842-
} else {
2843-
parser_err!(
2844-
format!(
2845-
"current dialect: {:?} does not support factorial operator or bang not operator",
2846-
self.dialect
2847-
),
2848-
self.peek_token().location
2849-
)
2831+
_ => parser_err!(
2832+
"current dialect support bang not operator, but with wrong syntax",
2833+
tok.location
2834+
),
28502835
}
28512836
} else if Token::LBracket == tok {
28522837
if dialect_of!(self is PostgreSqlDialect | DuckDbDialect | GenericDialect) {

tests/sqlparser_common.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11490,19 +11490,24 @@ fn parse_bang_not() {
1149011490
)
1149111491
}
1149211492

11493-
let sql = "SELECT a!";
11494-
assert_eq!(
11495-
dialects.parse_sql_statements(sql).unwrap_err(),
11496-
ParserError::ParserError(
11497-
"current dialect support bang not operator, but with wrong synx".to_string()
11498-
)
11499-
);
11493+
let sql_statements = ["SELECT a!", "SELECT a ! b", "SELECT a ! as b"];
1150011494

11501-
let sql = "SELECT !a";
11502-
assert_eq!(
11503-
all_dialects_where(|d| !d.supports_bang_not_operator())
11504-
.parse_sql_statements(sql)
11505-
.unwrap_err(),
11506-
ParserError::ParserError("Expected: an expression, found: !".to_string())
11507-
);
11495+
for &sql in &sql_statements {
11496+
assert_eq!(
11497+
dialects.parse_sql_statements(sql).unwrap_err(),
11498+
ParserError::ParserError(
11499+
"current dialect support bang not operator, but with wrong syntax".to_string()
11500+
)
11501+
);
11502+
}
11503+
11504+
let sql_statements = ["SELECT !a", "SELECT !a b", "SELECT !a as b"];
11505+
let dialects = all_dialects_where(|d| !d.supports_bang_not_operator());
11506+
11507+
for &sql in &sql_statements {
11508+
assert_eq!(
11509+
dialects.parse_sql_statements(sql).unwrap_err(),
11510+
ParserError::ParserError("Expected: an expression, found: !".to_string())
11511+
);
11512+
}
1150811513
}

0 commit comments

Comments
 (0)