Skip to content

Conversation

lovasoa
Copy link
Contributor

@lovasoa lovasoa commented May 9, 2025

Now,

println!("{:#}", my_statement);

can display

WITH cte AS (
  SELECT
    my_first_column as a,
    col_num_2 as b
  FROM
    my_table
)
SELECT
  a,
  COUNT(*)
FROM
  cte
GROUP BY
  a
HAVING
  COUNT(*) > 1

I am pretty happy about the composability of the resulting code.
Pretty printing is implemented only for select statements, but generalizing this to other statements is almost mechanical and can be done in a subsequent pr.

This will be a real game changer for SQLPage, where error messages are currently often hard to read because of long sql strings.

@alamb @iffyio

@lovasoa lovasoa changed the title implement pretty-printing with {:#} implement pretty-printing with {:#} May 9, 2025
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TLDR is I think this is a great feature @lovasoa -- thank you so much. I expect it to evolve over time, but this PR has a great foundation

One thought I had was if you would like help adding support for pretty printing other SQL it might be useful to file some tickets explaining / enumerating the other structures. I suspect other people might like to join in and help you

cc @iffyio

f.write_str("WHEN ")?;
self.condition.fmt(f)?;
f.write_str(" THEN")?;
SpaceOrNewline.fmt(f)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🤔

/// let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
///
/// // Regular formatting
/// assert_eq!(format!("{}", ast[0]), "SELECT a, b FROM table_1");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is awesome

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we cannot really configure much if we are using rust's existing Display trait...

//!
//! # Pretty Printing
//!
//! SQL statements can be pretty-printed with proper indentation and line breaks using the alternate flag (`{:#}`):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️


#[test]
fn test_pretty_print_select() {
assert_eq!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great

@lovasoa
Copy link
Contributor Author

lovasoa commented May 13, 2025

I'd love if we could merge this one and then I'll follow up with improvements

@alamb
Copy link
Contributor

alamb commented May 13, 2025

I think this is pretty non controversial so let's merge it. @iffyio let us know if you would like any changes

@alamb alamb merged commit 6120bb5 into apache:main May 13, 2025
9 checks passed
@lovasoa
Copy link
Contributor Author

lovasoa commented May 13, 2025

Great, thank you @alamb !

@lovasoa lovasoa deleted the pretty-print branch May 13, 2025 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature] allow pretty-printing

2 participants