This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 7449e273 Fix unary minus rendering into -- line comments (#2532)
7449e273 is described below
commit 7449e2732dad5934bbce0b607223df406af5cf8c
Author: Luca Cappelletti <[email protected]>
AuthorDate: Mon Sep 21 19:26:53 2026 +0000
Fix unary minus rendering into -- line comments (#2532)
---
src/ast/mod.rs | 28 +++++++++++++++++++++++++++-
tests/sqlparser_common.rs | 8 ++++++++
tests/sqlparser_postgres.rs | 9 +++++++++
3 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/src/ast/mod.rs b/src/ast/mod.rs
index 4817a3f0..dcfd1a96 100644
--- a/src/ast/mod.rs
+++ b/src/ast/mod.rs
@@ -1965,8 +1965,14 @@ impl fmt::Display for Expr {
| UnaryOperator::PGAbs
| UnaryOperator::QuestionDash
| UnaryOperator::QuestionPipe => write!(f, "{op} {expr}"),
+ UnaryOperator::Minus => {
+ if starts_with_operator_char(expr) {
+ write!(f, "{op} {expr}")
+ } else {
+ write!(f, "{op}{expr}")
+ }
+ }
UnaryOperator::Plus
- | UnaryOperator::Minus
| UnaryOperator::BangNot
| UnaryOperator::PGPrefixFactorial
| UnaryOperator::PGSquareRoot
@@ -8083,6 +8089,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 4069ff10..c4aa607d 100644
--- a/tests/sqlparser_common.rs
+++ b/tests/sqlparser_common.rs
@@ -20072,3 +20072,11 @@ fn parse_bitwise_not_renders_apart_from_operand() {
all_dialects().verified_stmt("SELECT ~ -1");
all_dialects().verified_stmt("SELECT ~ ~ 1");
}
+
+#[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 bf535383..ab2d4b8e 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -9975,3 +9975,12 @@ fn parse_bitwise_not_before_pg_prefix_operators() {
pg().verified_stmt("SELECT ~ @ 2");
pg().one_statement_parses_to("SELECT ~ #x", "SELECT ~ # x");
}
+
+#[test]
+fn parse_unary_minus_before_pg_prefix_operators() {
+ pg().one_statement_parses_to("SELECT - ~1", "SELECT - ~ 1");
+ pg().verified_stmt("SELECT - ~ 1");
+ 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]