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/23 01:49:15 UTC

(datafusion-sqlparser-rs) 01/01: Fix exponential Display of nested unary operators

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

LucaCappelletti94 pushed a commit to branch nested-unary-display-blowup
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git

commit 101a7aceaa5322b9c68498fc6e1a8fc53b808148
Author: LucaCappelletti94 <ca...@gmail.com>
AuthorDate: Wed Sep 23 03:49:04 2026 +0200

    Fix exponential Display of nested unary operators
---
 src/ast/mod.rs            | 11 ++++++++---
 tests/sqlparser_common.rs | 12 ++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/ast/mod.rs b/src/ast/mod.rs
index dcde4fc38..41329ea84 100644
--- a/src/ast/mod.rs
+++ b/src/ast/mod.rs
@@ -8116,10 +8116,15 @@ fn starts_with_operator_char(expr: &Expr) -> bool {
     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();
+            match s.chars().next() {
+                Some(c) => {
+                    self.0 = Some(c);
+                    // Failing aborts the render, which would otherwise walk the whole
+                    // subtree at every nesting level of a prefix chain.
+                    Err(fmt::Error)
+                }
+                None => Ok(()),
             }
-            Ok(())
         }
     }
     let mut first = FirstChar(None);
diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs
index 9fb208bd5..41a1d32c1 100644
--- a/tests/sqlparser_common.rs
+++ b/tests/sqlparser_common.rs
@@ -20143,3 +20143,15 @@ fn parse_stage_table_factor() {
         ParserError::ParserError("Expected: identifier, found: @".to_string()),
     );
 }
+
+#[test]
+fn display_nested_unary_minus_chain() {
+    let mut expr = Expr::Identifier(Ident::new("id"));
+    for _ in 0..64 {
+        expr = Expr::UnaryOp {
+            op: UnaryOperator::Minus,
+            expr: Box::new(expr),
+        };
+    }
+    assert_eq!(expr.to_string(), format!("{}-id", "- ".repeat(63)));
+}


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