shssoichiro / sqlformat-rs

SQL Formatter written in Rust
Apache License 2.0
89 stars 26 forks source link

`'1 week'::interval` gets unnecessary whitespace #65

Closed bartlomieju closed 5 days ago

bartlomieju commented 6 days ago
sqlformat::format(
  "SELECT * FROM users WHERE created_at > now() - '1 week'::interval",
  &QueryParams::None,
  &FormatOptions::default(),
);

This results in:

SELECT
  *
FROM
  users
WHERE
  created_at > now() - '1 week' :: interval

but I expected that I would get:

SELECT
  *
FROM
  users
WHERE
  created_at > now() - '1 week'::interval
bartlomieju commented 5 days ago

I'm somewhat confused here, when I tried adding a test in the repo:

    #[test]
    fn it_formats_double_colons_in_where() {
        let input = "select * from users where created_at > now() - '1 week'::interval";
        let options = FormatOptions {
            uppercase: Some(false),
            ..FormatOptions::default()
        };
        let expected = indoc!(
            "
select
  *
from
  users
where
  created_at > now() - '1 week'::interval"
        );

        assert_eq!(format(input, &QueryParams::None, &options), expected);
    }

It passes fine, however when using sqlformat-rs as a library with the repro in the issue I do get whitespace around ::. It appears this has been already fixed on main but not yet released, so 0.3.1 suffers from this problem.

bartlomieju commented 5 days ago

Ah yes, looks like it was already fixed in https://github.com/shssoichiro/sqlformat-rs/pull/63. So just need to wait on a release :)