Skip to content

Commit ccbe431

Browse files
alexander-beediezyuiop
authored andcommitted
Make BitwiseNot ("~") available for all dialects, not just PostgreSQL (apache#2081)
(cherry picked from commit 308a723)
1 parent cb25e96 commit ccbe431

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
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
@@ -1630,15 +1630,13 @@ impl<'a> Parser<'a> {
16301630
| tok @ Token::PGSquareRoot
16311631
| tok @ Token::PGCubeRoot
16321632
| tok @ Token::AtSign
1633-
| tok @ Token::Tilde
16341633
if dialect_is!(dialect is PostgreSqlDialect) =>
16351634
{
16361635
let op = match tok {
16371636
Token::DoubleExclamationMark => UnaryOperator::PGPrefixFactorial,
16381637
Token::PGSquareRoot => UnaryOperator::PGSquareRoot,
16391638
Token::PGCubeRoot => UnaryOperator::PGCubeRoot,
16401639
Token::AtSign => UnaryOperator::PGAbs,
1641-
Token::Tilde => UnaryOperator::PGBitwiseNot,
16421640
_ => unreachable!(),
16431641
};
16441642
Ok(Expr::UnaryOp {
@@ -1648,6 +1646,10 @@ impl<'a> Parser<'a> {
16481646
),
16491647
})
16501648
}
1649+
Token::Tilde => Ok(Expr::UnaryOp {
1650+
op: UnaryOperator::BitwiseNot,
1651+
expr: Box::new(self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?),
1652+
}),
16511653
tok @ Token::Sharp
16521654
| tok @ Token::AtDashAt
16531655
| tok @ Token::AtAt

tests/sqlparser_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17138,7 +17138,7 @@ fn test_parse_semantic_view_table_factor() {
1713817138
}
1713917139

1714017140
let ast_sql = r#"SELECT * FROM SEMANTIC_VIEW(
17141-
my_model
17141+
my_model
1714217142
DIMENSIONS DATE_PART('year', date_col), region_name
1714317143
METRICS orders.revenue, orders.count
1714417144
WHERE active = true

tests/sqlparser_postgres.rs

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

0 commit comments

Comments
 (0)