You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by lu...@apache.org on 2026/09/19 19:14:50 UTC

(datafusion-sqlparser-rs) branch unary-minus-comment-guard created (now 9d70d510)

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

LucaCappelletti94 pushed a change to branch unary-minus-comment-guard
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git


      at 9d70d510 Fix unary minus rendering into -- line comments

This branch includes the following new commits:

     new 9d70d510 Fix unary minus rendering into -- line comments

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org


(datafusion-sqlparser-rs) 01/01: Fix unary minus rendering into -- line comments

Posted by lu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

LucaCappelletti94 pushed a commit to branch unary-minus-comment-guard
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git

commit 9d70d510aa6c613c6ba59fbb0260814e8cfb4bab
Author: LucaCappelletti94 <ca...@gmail.com>
AuthorDate: Sat Sep 19 21:14:40 2026 +0200

    Fix unary minus rendering into -- line comments
---
 src/ast/mod.rs              | 23 ++++++++++++++++++++++-
 tests/sqlparser_common.rs   | 10 ++++++++++
 tests/sqlparser_postgres.rs |  8 ++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/src/ast/mod.rs b/src/ast/mod.rs
index 20058b83..55cee92d 100644
--- a/src/ast/mod.rs
+++ b/src/ast/mod.rs
@@ -1966,7 +1966,8 @@ impl fmt::Display for Expr {
                         | UnaryOperator::DoubleAt
                         | UnaryOperator::QuestionDash
                         | UnaryOperator::QuestionPipe
-                ) {
+                ) || (op == &UnaryOperator::Minus && starts_with_operator_char(expr))
+                {
                     write!(f, "{op} {expr}")
                 } else {
                     write!(f, "{op}{expr}")
@@ -8083,6 +8084,26 @@ impl fmt::Display for FunctionArg {
     }
 }
 
+/// Whether `expr` renders with an operator character first. A prefix `-` must
+/// not abut one: `--` starts a line comment and operator-run dialects fuse
+/// `-` into a longer custom operator token.
+fn starts_with_operator_char(expr: &Expr) -> bool {
+    use fmt::Write;
+    struct FirstChar(Option<char>);
+    impl fmt::Write for FirstChar {
+        fn write_str(&mut self, s: &str) -> fmt::Result {
+            if self.0.is_none() {
+                self.0 = s.chars().next();
+            }
+            Ok(())
+        }
+    }
+    let mut first = FirstChar(None);
+    let _ = write!(first, "{expr}");
+    const OPERATOR_CHARS: &str = "+-*/<>=~!@%^&|";
+    first.0.is_some_and(|c| OPERATOR_CHARS.contains(c))
+}
+
 /// `FunctionArgOperator::Space` has no token of its own, so the name and the
 /// value are separated by a single space instead.
 fn fmt_named_function_arg(
diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs
index 2de6062b..9afc3384 100644
--- a/tests/sqlparser_common.rs
+++ b/tests/sqlparser_common.rs
@@ -20060,3 +20060,13 @@ fn parse_insert_by_name() {
         _ => unreachable!(),
     }
 }
+
+#[test]
+fn parse_unary_minus_never_renders_line_comment() {
+    // `--` opens a line comment, so a negation of a negation must keep its
+    // two minus signs apart when rendered.
+    all_dialects().verified_stmt("SELECT - -1");
+    all_dialects().verified_stmt("SELECT - - -1");
+    all_dialects().verified_stmt("SELECT -1");
+    all_dialects().verified_stmt("SELECT -x");
+}
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs
index d71e49b2..abc0e99e 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -9953,3 +9953,11 @@ fn parse_insert_by_name_keywords_as_table_and_alias() {
         statement => panic!("Expected INSERT statement, got: {statement:?}"),
     }
 }
+
+#[test]
+fn parse_unary_minus_before_pg_prefix_operators() {
+    // `-` followed by an operator character fuses into a custom operator
+    // token, so the space must survive rendering.
+    pg().verified_stmt("SELECT - ~1");
+    pg().verified_stmt("SELECT - @2");
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org