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

LucaCappelletti94 pushed a commit to branch 
postgres-root-operators-display-space
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git

commit 2ee10ac093bc21aa7cb875f4a8fc64241afd303d
Author: LucaCappelletti94 <[email protected]>
AuthorDate: Sat Sep 19 22:45:54 2026 +0200

    PostgreSQL: Render prefix |/ and ||/ operators with a space
---
 src/ast/mod.rs              | 12 ++++++------
 tests/sqlparser_postgres.rs | 24 ++++++++++++++++++++++--
 2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/src/ast/mod.rs b/src/ast/mod.rs
index dcfd1a96e..92a0f92a5 100644
--- a/src/ast/mod.rs
+++ b/src/ast/mod.rs
@@ -1964,7 +1964,9 @@ impl fmt::Display for Expr {
                 | UnaryOperator::DoubleAt
                 | UnaryOperator::PGAbs
                 | UnaryOperator::QuestionDash
-                | UnaryOperator::QuestionPipe => write!(f, "{op} {expr}"),
+                | UnaryOperator::QuestionPipe
+                | UnaryOperator::PGSquareRoot
+                | UnaryOperator::PGCubeRoot => write!(f, "{op} {expr}"),
                 UnaryOperator::Minus => {
                     if starts_with_operator_char(expr) {
                         write!(f, "{op} {expr}")
@@ -1972,11 +1974,9 @@ impl fmt::Display for Expr {
                         write!(f, "{op}{expr}")
                     }
                 }
-                UnaryOperator::Plus
-                | UnaryOperator::BangNot
-                | UnaryOperator::PGPrefixFactorial
-                | UnaryOperator::PGSquareRoot
-                | UnaryOperator::PGCubeRoot => write!(f, "{op}{expr}"),
+                UnaryOperator::Plus | UnaryOperator::BangNot | 
UnaryOperator::PGPrefixFactorial => {
+                    write!(f, "{op}{expr}")
+                }
             },
             Expr::Convert {
                 is_try,
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs
index ab2d4b8ee..1985c7fcd 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -2623,8 +2623,6 @@ fn parse_ampersand_arobase() {
 #[test]
 fn parse_pg_unary_ops() {
     let pg_unary_ops = &[
-        ("SELECT |/a", UnaryOperator::PGSquareRoot),
-        ("SELECT ||/a", UnaryOperator::PGCubeRoot),
         ("SELECT !!a", UnaryOperator::PGPrefixFactorial),
         ("SELECT @ a", UnaryOperator::PGAbs),
     ];
@@ -2638,6 +2636,21 @@ fn parse_pg_unary_ops() {
             select.projection[0]
         );
     }
+
+    for (str_op, op) in [
+        ("|/", UnaryOperator::PGSquareRoot),
+        ("||/", UnaryOperator::PGCubeRoot),
+    ] {
+        let select = pg().verified_only_select(&format!("SELECT {str_op} a"));
+        assert_eq!(
+            SelectItem::UnnamedExpr(Expr::UnaryOp {
+                op,
+                expr: Box::new(Expr::Identifier(Ident::new("a"))),
+            }),
+            select.projection[0]
+        );
+        pg().one_statement_parses_to(&format!("SELECT {str_op}a"), 
&format!("SELECT {str_op} a"));
+    }
 }
 
 #[test]
@@ -9984,3 +9997,10 @@ 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_pg_roots_render_apart_from_operand() {
+    pg().verified_stmt("SELECT |/ -2");
+    pg().verified_stmt("SELECT ||/ -2");
+    pg().verified_stmt("SELECT |/ ||/ 2");
+}


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

Reply via email to