SQL Code Style - Whitespace after curly brackets

Answered

Hi,

we are using the default PostgreSQL dialect and the default Code Style for it.

When formatting the following SQL everything is okay:

SELECT colA FROM tab_a

However, when formatting the following SQL we get an unwanted whitespace introduced. We use the curly brackets to insert and replace strings, but woudl like to use the Code Style formatter by DataGrip.

SELECT colA FROM {tab}_a

SELECT colA FROM {tab} _a -- becomes this after formatting.

Is there an option we can change here?

Thanks so much everyone in advance

0
9 comments
Hi Toby Ruby,

If assumed that `{tab}_a` is a full table name with an underscore in it, then this formatting behavior is unusual.

I tried to reformat this piece of code in Build #DB-223.8617.3, but haven't been bale to reproduce it. Therefore, can you please update to 223.8617.3 version and observe this behavior again.

If the issue remains, please let us and we'll have a look.
0

HI Aleksandr,

we are using Build #PY-223.8617.48, PyCharm 2022.3.2 (Professional Edition), which comes with DataGrip as a bundled plugin.

I can see the change being applied in the Settings preview for Editor > Code Style > SQL > General, as well as when formatting a .sql file.

Thanks

Toby

0

Hi Toby Ruby,

I was finally able to reproduce this scenario. The problem is that the {} brackets are not conventional to postgres SQL syntax and, thus, are not handled as you would expect them to. We've made a workaround for your specific case. Here's what needs to be done:

1. in File | Settings | Database | Query Execution | User Parameters, add either \{\w\w*} or \{\w+} - both will do

2. Select SQL checkbox only in Languages, hit Apply

Reformatting should eliminate white space and keep the object name consistent.

0

Hi Aleksandr,

This solution hasn't worked for us. 

A screenshot of the applied settings on our end provided below for your reference. Could you please review?

0

Hi Suraj Gulabani,

Please try to use \{\w\w*} instead. Should be working fine.

0

I think I found the issue - \{\w\w*} works for the example in the original post, but we were having trouble with other queries which included more than just letters/numbers/underscores inside the curly brackets, e.g. SELECT * FROM {abcd:table}_a. Since the regex token \w represents only letters, numbers, and underscores, this would not prevent the formatting issue for this query.

This pattern seems to work as long as there is no whitespace inside the curly brackets: \{\S+}

This pattern works regardless of what the curly brackets contain, but I guess this could be considered too broad: \{[^}]+}

Regardless, thanks for your help Aleksandr Molchanov!

0

@Alexsandr Molchanov, it turns out I was wrong about the “fix” for the issue. However, it is related to user parameters. The truth is, configuring a correct user parameter appears to be what is causing the issue.

You should be able to reproduce this using Generic SQL and the default User Parameter settings with this example:

SELECT *
FROM abc_${efg}_hij

After auto-formatting, I get this:

SELECT *
FROM abc_${efg} _hij

This is because of the default User Parameter ${name} being configured:

If I remove that User Parameter, or if I un-check the “In scripts” option beneath it, the formatting issue no longer occurs.

The thing is, we want to have this {name} pattern configure as a User Parameter (including to replace “In scripts”),  so that when we run the query in the console, it prompts us to replace these parameters.

0
Thanks for your feedback. We'll need more time to analyze this behavior. We'll get back to you on this.
0

Hello Toby,

I've reproduced this bug and filed a bug report: DBE-19599. You're welcome to watch/vote for it.

 

1

Please sign in to leave a comment.