alamb commented on code in PR #1847:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/1847#discussion_r2085270580


##########
src/ast/mod.rs:
##########
@@ -628,7 +634,12 @@ pub struct CaseWhen {
 
 impl fmt::Display for CaseWhen {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "WHEN {} THEN {}", self.condition, self.result)
+        f.write_str("WHEN ")?;
+        self.condition.fmt(f)?;
+        f.write_str(" THEN")?;
+        SpaceOrNewline.fmt(f)?;

Review Comment:
   that is pretty cool
   
   I wonder about how you will connect this back to your original goal of 
preserving the original whitespace.
   
   Perhaps eventually this could be passed the original span 🤔 



##########
src/lib.rs:
##########
@@ -64,6 +64,27 @@
 //! // The original SQL text can be generated from the AST
 //! assert_eq!(ast[0].to_string(), sql);
 //! ```
+//!
+//! # Pretty Printing
+//!
+//! SQL statements can be pretty-printed with proper indentation and line 
breaks using the alternate flag (`{:#}`):

Review Comment:
   ❤️ 



##########
tests/pretty_print.rs:
##########
@@ -0,0 +1,157 @@
+use sqlparser::dialect::GenericDialect;
+use sqlparser::parser::Parser;
+
+fn prettify(sql: &str) -> String {
+    let ast = Parser::parse_sql(&GenericDialect {}, sql).unwrap();
+    format!("{:#}", ast[0])
+}
+
+#[test]
+fn test_pretty_print_select() {
+    assert_eq!(

Review Comment:
   this is great



##########
src/display_utils.rs:
##########
@@ -0,0 +1,133 @@
+//! Utilities for formatting SQL AST nodes with pretty printing support.
+//!
+//! The module provides formatters that implement the `Display` trait with 
support
+//! for both regular (`{}`) and pretty (`{:#}`) formatting modes. Pretty 
printing
+//! adds proper indentation and line breaks to make SQL statements more 
readable.
+
+use core::fmt::{self, Display, Write};
+
+/// A wrapper around a value that adds an indent to the value when displayed 
with {:#}.
+pub(crate) struct Indent<T>(pub T);

Review Comment:
   since it is crate private we can change it later, but I think it would 
likely be helpful eventually to make the indent configurable (for embedded 
subqueries, for example)



##########
src/ast/mod.rs:
##########
@@ -4204,6 +4236,28 @@ impl fmt::Display for RaisErrorOption {
 }
 
 impl fmt::Display for Statement {
+    /// Formats a SQL statement with support for pretty printing.
+    ///
+    /// When using the alternate flag (`{:#}`), the statement will be 
formatted with proper
+    /// indentation and line breaks. For example:
+    ///
+    /// ```
+    /// # use sqlparser::dialect::GenericDialect;
+    /// # use sqlparser::parser::Parser;
+    /// let sql = "SELECT a, b FROM table_1";
+    /// let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
+    ///
+    /// // Regular formatting
+    /// assert_eq!(format!("{}", ast[0]), "SELECT a, b FROM table_1");

Review Comment:
   this is awesome



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to