Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named placeholders are broken due to whitespace #57

Open
michaelborn opened this issue Apr 21, 2022 · 3 comments
Open

Named placeholders are broken due to whitespace #57

michaelborn opened this issue Apr 21, 2022 · 3 comments

Comments

@michaelborn
Copy link

Name placeholders like WHERE name = :name are broken and do not replace at all.

Example:

SELECT supplier_name, city FROM suppliers
WHERE supplier_id = :name

this formats as:

SELECT
  supplier_name,
  city
FROM
  suppliers
WHERE
  supplier_id =: name

Notice the =: and the space between the : and name - =: name

You can see this right on the demo page:

https://www.vertical-blank.com/sql-formatter/

@michaelborn
Copy link
Author

Note that positional placeholders are still formatted and replace correctly.

@ItayPolackGadassiAkamai

Seeing a similar issue with parameter injection, depending on the dialect.
When using TSql parameter injection works, when using StandardSql, SparkSql, and maybe others, an extra whitespace is added, and parameter injection does not work:

// Bad example
SqlFormatter.of(Dialect.SparkSql).format("@thing", Map.of("thing", "foo")); // Result: "@ thing"

// Good example
SqlFormatter.of(Dialect.TSql).format("@thing", Map.of("thing", "foo")); // Result: "foo"

@ItayPolackGadassiAkamai

If anyone bumps into this issue, a possible workaround is to add '@' as a named placeholder type:

Formatter sparkSqlFormatter = SqlFormatter
            .of(Dialect.SparkSql)
            // Required for named placeholders to work
            .extend(dialectConfig -> dialectConfig.plusNamedPlaceholderTypes("@"));
            // ... any other needed extensions

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

No branches or pull requests

2 participants