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:54:42 UTC

(datafusion-sqlparser-rs) branch unary-minus-comment-guard updated (654d0723 -> 25e01ac8)

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


 discard 654d0723 Fix unary minus rendering into -- line comments
     new 25e01ac8 Fix unary minus rendering into -- line comments

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (654d0723)
            \
             N -- N -- N   refs/heads/unary-minus-comment-guard (25e01ac8)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

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.


Summary of changes:
 src/ast/mod.rs              | 6 ++----
 tests/sqlparser_common.rs   | 2 --
 tests/sqlparser_postgres.rs | 2 --
 3 files changed, 2 insertions(+), 8 deletions(-)


---------------------------------------------------------------------
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 25e01ac83ca1e7fb72afb7a4250d8f85b6233d0d
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   |  8 ++++++++
 tests/sqlparser_postgres.rs |  7 +++++++
 3 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/src/ast/mod.rs b/src/ast/mod.rs
index 20058b83..6c026b54 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, since `--` starts a line comment and operator-run
+/// dialects fuse `-@`, `-~`, `-#`, `-!!` and `-||/` into single tokens.
+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..f81c302e 100644
--- a/tests/sqlparser_common.rs
+++ b/tests/sqlparser_common.rs
@@ -20060,3 +20060,11 @@ fn parse_insert_by_name() {
         _ => unreachable!(),
     }
 }
+
+#[test]
+fn parse_unary_minus_never_renders_line_comment() {
+    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..28c82b06 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -9953,3 +9953,10 @@ 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() {
+    pg().verified_stmt("SELECT - ~1");
+    pg().verified_stmt("SELECT - @2");
+    pg().one_statement_parses_to("SELECT - #x", "SELECT - # x");
+}


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