Skip to content

Commit 308a723

Browse files
Make BitwiseNot ("~") available for all dialects, not just PostgreSQL (#2081)
1 parent bc6e1d6 commit 308a723

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

src/ast/operator.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,35 @@ use super::display_separated;
3333
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3434
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3535
pub enum UnaryOperator {
36+
/// `@-@` Length or circumference (PostgreSQL/Redshift geometric operator)
37+
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
38+
AtDashAt,
39+
/// Unary logical not operator: e.g. `! false` (Hive-specific)
40+
BangNot,
41+
/// Bitwise Not, e.g. `~9`
42+
BitwiseNot,
43+
/// `@@` Center (PostgreSQL/Redshift geometric operator)
44+
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
45+
DoubleAt,
46+
/// `#` Number of points in path or polygon (PostgreSQL/Redshift geometric operator)
47+
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
48+
Hash,
3649
/// Plus, e.g. `+9`
3750
Plus,
3851
/// Minus, e.g. `-9`
3952
Minus,
4053
/// Not, e.g. `NOT(true)`
4154
Not,
42-
/// Bitwise Not, e.g. `~9` (PostgreSQL-specific)
43-
PGBitwiseNot,
44-
/// Square root, e.g. `|/9` (PostgreSQL-specific)
45-
PGSquareRoot,
55+
/// Absolute value, e.g. `@ -9` (PostgreSQL-specific)
56+
PGAbs,
4657
/// Cube root, e.g. `||/27` (PostgreSQL-specific)
4758
PGCubeRoot,
4859
/// Factorial, e.g. `9!` (PostgreSQL-specific)
4960
PGPostfixFactorial,
5061
/// Factorial, e.g. `!!9` (PostgreSQL-specific)
5162
PGPrefixFactorial,
52-
/// Absolute value, e.g. `@ -9` (PostgreSQL-specific)
53-
PGAbs,
54-
/// Unary logical not operator: e.g. `! false` (Hive-specific)
55-
BangNot,
56-
/// `#` Number of points in path or polygon (PostgreSQL/Redshift geometric operator)
57-
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
58-
Hash,
59-
/// `@-@` Length or circumference (PostgreSQL/Redshift geometric operator)
60-
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
61-
AtDashAt,
62-
/// `@@` Center (PostgreSQL/Redshift geometric operator)
63-
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
64-
DoubleAt,
63+
/// Square root, e.g. `|/9` (PostgreSQL-specific)
64+
PGSquareRoot,
6565
/// `?-` Is horizontal? (PostgreSQL/Redshift geometric operator)
6666
/// see <https://www.postgresql.org/docs/9.5/functions-geometry.html>
6767
QuestionDash,
@@ -73,19 +73,19 @@ pub enum UnaryOperator {
7373
impl fmt::Display for UnaryOperator {
7474
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7575
f.write_str(match self {
76-
UnaryOperator::Plus => "+",
76+
UnaryOperator::AtDashAt => "@-@",
77+
UnaryOperator::BangNot => "!",
78+
UnaryOperator::BitwiseNot => "~",
79+
UnaryOperator::DoubleAt => "@@",
80+
UnaryOperator::Hash => "#",
7781
UnaryOperator::Minus => "-",
7882
UnaryOperator::Not => "NOT",
79-
UnaryOperator::PGBitwiseNot => "~",
80-
UnaryOperator::PGSquareRoot => "|/",
83+
UnaryOperator::PGAbs => "@",
8184
UnaryOperator::PGCubeRoot => "||/",
8285
UnaryOperator::PGPostfixFactorial => "!",
8386
UnaryOperator::PGPrefixFactorial => "!!",
84-
UnaryOperator::PGAbs => "@",
85-
UnaryOperator::BangNot => "!",
86-
UnaryOperator::Hash => "#",
87-
UnaryOperator::AtDashAt => "@-@",
88-
UnaryOperator::DoubleAt => "@@",
87+
UnaryOperator::PGSquareRoot => "|/",
88+
UnaryOperator::Plus => "+",
8989
UnaryOperator::QuestionDash => "?-",
9090
UnaryOperator::QuestionPipe => "?|",
9191
})

src/parser/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,15 +1633,13 @@ impl<'a> Parser<'a> {
16331633
| tok @ Token::PGSquareRoot
16341634
| tok @ Token::PGCubeRoot
16351635
| tok @ Token::AtSign
1636-
| tok @ Token::Tilde
16371636
if dialect_is!(dialect is PostgreSqlDialect) =>
16381637
{
16391638
let op = match tok {
16401639
Token::DoubleExclamationMark => UnaryOperator::PGPrefixFactorial,
16411640
Token::PGSquareRoot => UnaryOperator::PGSquareRoot,
16421641
Token::PGCubeRoot => UnaryOperator::PGCubeRoot,
16431642
Token::AtSign => UnaryOperator::PGAbs,
1644-
Token::Tilde => UnaryOperator::PGBitwiseNot,
16451643
_ => unreachable!(),
16461644
};
16471645
Ok(Expr::UnaryOp {
@@ -1651,6 +1649,10 @@ impl<'a> Parser<'a> {
16511649
),
16521650
})
16531651
}
1652+
Token::Tilde => Ok(Expr::UnaryOp {
1653+
op: UnaryOperator::BitwiseNot,
1654+
expr: Box::new(self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?),
1655+
}),
16541656
tok @ Token::Sharp
16551657
| tok @ Token::AtDashAt
16561658
| tok @ Token::AtAt

tests/sqlparser_common.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17613,3 +17613,22 @@ fn test_parse_alter_user() {
1761317613
}
1761417614
verified_stmt("ALTER USER u1 SET DEFAULT_SECONDARY_ROLES=('ALL'), PASSWORD='secret', WORKLOAD_IDENTITY=(TYPE=AWS, ARN='arn:aws:iam::123456789:r1/')");
1761517615
}
17616+
17617+
#[test]
17618+
fn parse_generic_unary_ops() {
17619+
let unary_ops = &[
17620+
("~", UnaryOperator::BitwiseNot),
17621+
("-", UnaryOperator::Minus),
17622+
("+", UnaryOperator::Plus),
17623+
];
17624+
for (str_op, op) in unary_ops {
17625+
let select = verified_only_select(&format!("SELECT {}expr", &str_op));
17626+
assert_eq!(
17627+
UnnamedExpr(UnaryOp {
17628+
op: *op,
17629+
expr: Box::new(Identifier(Ident::new("expr"))),
17630+
}),
17631+
select.projection[0]
17632+
);
17633+
}
17634+
}

tests/sqlparser_postgres.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,13 +2144,11 @@ fn parse_ampersand_arobase() {
21442144
#[test]
21452145
fn parse_pg_unary_ops() {
21462146
let pg_unary_ops = &[
2147-
("~", UnaryOperator::PGBitwiseNot),
21482147
("|/", UnaryOperator::PGSquareRoot),
21492148
("||/", UnaryOperator::PGCubeRoot),
21502149
("!!", UnaryOperator::PGPrefixFactorial),
21512150
("@", UnaryOperator::PGAbs),
21522151
];
2153-
21542152
for (str_op, op) in pg_unary_ops {
21552153
let select = pg().verified_only_select(&format!("SELECT {}a", &str_op));
21562154
assert_eq!(

0 commit comments

Comments
 (0)