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

LucaCappelletti94 pushed a commit to branch pg-postfix-factorial-spacing
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git

commit fb8e6d8a72752e5b505ac32a3266b9d6377c770b
Author: LucaCappelletti94 <[email protected]>
AuthorDate: Mon Sep 21 23:12:33 2026 +0200

    PostgreSQL: Fix spacing between adjacent postfix factorial operators
---
 src/ast/mod.rs              | 14 +++++++++++++-
 tests/sqlparser_postgres.rs | 18 ++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/ast/mod.rs b/src/ast/mod.rs
index dcfd1a96..ad69975c 100644
--- a/src/ast/mod.rs
+++ b/src/ast/mod.rs
@@ -1956,7 +1956,19 @@ impl fmt::Display for Expr {
                 )
             }
             Expr::UnaryOp { op, expr } => match op {
-                UnaryOperator::PGPostfixFactorial => write!(f, "{expr}{op}"),
+                UnaryOperator::PGPostfixFactorial => {
+                    if matches!(
+                        expr.as_ref(),
+                        Expr::UnaryOp {
+                            op: UnaryOperator::PGPostfixFactorial,
+                            ..
+                        }
+                    ) {
+                        write!(f, "{expr} {op}")
+                    } else {
+                        write!(f, "{expr}{op}")
+                    }
+                }
                 UnaryOperator::Not
                 | UnaryOperator::BitwiseNot
                 | UnaryOperator::Hash
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs
index ab2d4b8e..543ff6aa 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -9984,3 +9984,21 @@ fn parse_unary_minus_before_pg_prefix_operators() {
     pg().verified_stmt("SELECT - @ 2");
     pg().one_statement_parses_to("SELECT - #x", "SELECT - # x");
 }
+
+#[test]
+fn parse_postfix_factorial_spacing() {
+    pg().verified_stmt("SELECT a!");
+    pg().verified_stmt("SELECT 5!");
+    pg().verified_stmt("SELECT (a!)!");
+    pg().verified_stmt("SELECT a! !");
+    pg().verified_stmt("SELECT a! ! !");
+    pg().verified_stmt("SELECT a! ! % 2");
+    pg().one_statement_parses_to("SELECT a! !%2", "SELECT a! ! % 2");
+    pg().one_statement_parses_to("SELECT -a, +b, a! !%2, a", "SELECT -a, +b, 
a! ! % 2, a");
+
+    let err = pg().parse_sql_statements("SELECT a!!").unwrap_err();
+    assert_eq!(
+        ParserError::ParserError("Expected: end of statement, found: 
!!".to_string()),
+        err
+    );
+}


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

Reply via email to