This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-2533-3fd342fe823f8bd787b574adf3074d0b4da59b91
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git

commit 0718147b755953612bf099670e6d6906db92dcdc
Author: Luca Cappelletti <[email protected]>
AuthorDate: Mon Sep 21 16:26:26 2026 +0000

    Render prefix ~ operator with a space (#2533)
---
 src/ast/mod.rs              | 35 +++++++++++++++++------------------
 tests/sqlparser_common.rs   | 22 +++++++++++++++++-----
 tests/sqlparser_postgres.rs |  7 +++++++
 3 files changed, 41 insertions(+), 23 deletions(-)

diff --git a/src/ast/mod.rs b/src/ast/mod.rs
index b6d67df5..4817a3f0 100644
--- a/src/ast/mod.rs
+++ b/src/ast/mod.rs
@@ -1955,24 +1955,23 @@ impl fmt::Display for Expr {
                     if add_parens { ")" } else { "" },
                 )
             }
-            Expr::UnaryOp { op, expr } => {
-                if op == &UnaryOperator::PGPostfixFactorial {
-                    write!(f, "{expr}{op}")
-                } else if matches!(
-                    op,
-                    UnaryOperator::Not
-                        | UnaryOperator::Hash
-                        | UnaryOperator::AtDashAt
-                        | UnaryOperator::DoubleAt
-                        | UnaryOperator::PGAbs
-                        | UnaryOperator::QuestionDash
-                        | UnaryOperator::QuestionPipe
-                ) {
-                    write!(f, "{op} {expr}")
-                } else {
-                    write!(f, "{op}{expr}")
-                }
-            }
+            Expr::UnaryOp { op, expr } => match op {
+                UnaryOperator::PGPostfixFactorial => write!(f, "{expr}{op}"),
+                UnaryOperator::Not
+                | UnaryOperator::BitwiseNot
+                | UnaryOperator::Hash
+                | UnaryOperator::AtDashAt
+                | UnaryOperator::DoubleAt
+                | UnaryOperator::PGAbs
+                | UnaryOperator::QuestionDash
+                | UnaryOperator::QuestionPipe => write!(f, "{op} {expr}"),
+                UnaryOperator::Plus
+                | UnaryOperator::Minus
+                | UnaryOperator::BangNot
+                | UnaryOperator::PGPrefixFactorial
+                | UnaryOperator::PGSquareRoot
+                | UnaryOperator::PGCubeRoot => write!(f, "{op}{expr}"),
+            },
             Expr::Convert {
                 is_try,
                 expr,
diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs
index 2de6062b..4069ff10 100644
--- a/tests/sqlparser_common.rs
+++ b/tests/sqlparser_common.rs
@@ -19469,11 +19469,7 @@ fn test_parse_alter_user() {
 
 #[test]
 fn parse_generic_unary_ops() {
-    let unary_ops = &[
-        ("~", UnaryOperator::BitwiseNot),
-        ("-", UnaryOperator::Minus),
-        ("+", UnaryOperator::Plus),
-    ];
+    let unary_ops = &[("-", UnaryOperator::Minus), ("+", UnaryOperator::Plus)];
     for (str_op, op) in unary_ops {
         let select = verified_only_select(&format!("SELECT {}expr", str_op));
         assert_eq!(
@@ -19484,6 +19480,16 @@ fn parse_generic_unary_ops() {
             select.projection[0]
         );
     }
+
+    let select = verified_only_select("SELECT ~ expr");
+    assert_eq!(
+        UnnamedExpr(UnaryOp {
+            op: UnaryOperator::BitwiseNot,
+            expr: Box::new(Identifier(Ident::new("expr"))),
+        }),
+        select.projection[0]
+    );
+    one_statement_parses_to("SELECT ~expr", "SELECT ~ expr");
 }
 
 #[test]
@@ -20060,3 +20066,9 @@ fn parse_insert_by_name() {
         _ => unreachable!(),
     }
 }
+
+#[test]
+fn parse_bitwise_not_renders_apart_from_operand() {
+    all_dialects().verified_stmt("SELECT ~ -1");
+    all_dialects().verified_stmt("SELECT ~ ~ 1");
+}
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs
index c85cb26f..bf535383 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -9968,3 +9968,10 @@ fn parse_pg_abs_space_before_negative_operand() {
         err
     );
 }
+
+#[test]
+fn parse_bitwise_not_before_pg_prefix_operators() {
+    pg().one_statement_parses_to("SELECT ~ @2", "SELECT ~ @ 2");
+    pg().verified_stmt("SELECT ~ @ 2");
+    pg().one_statement_parses_to("SELECT ~ #x", "SELECT ~ # x");
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to