sql-formatter icon indicating copy to clipboard operation
sql-formatter copied to clipboard

Allow uppercasing of function names separately from keywords

Open nene opened this issue 3 years ago • 2 comments

Currently we have a single option keywordCase which specifies the case for both keywords and function names.

It would be nice to allow more fine-grained control by having a separate option functionCase to control whether function names should be uppercased or lowercased. It's quite common practice when writing SQL to have different case-convention for the keywords and function names. Most commonly uppercasing keywords and lowercasing function names, like:

SELECT
  round(price * abs(tax)) AS final_price
FROM
  inventory;

This feature is also widespread in many other SQL formatting tools:

nene avatar Jun 13 '22 15:06 nene

The initial most obvious solution I had in my head was to distinguish them solely by name, but this won't work as some function name are also keywords used elsewhere in SQL. For example:

  • SET() function and UPDATE table SET age = 10 WHERE name = 'John';
  • CAST(1 AS INT) casting function and RETURNS INT CAST FROM FLOAT
  • SELECT ANY(age>10) aggregate function and SELECT * FROM foo WHERE ProductID = ANY (SELECT id FROM tbl)

Luckily these are fairly rare cases. But still.

Another option would be to look whether function name is followed by open-parenthesis (. This seems more promising, though one has to keep in mind that not every name followed by parenthesis is a function.

nene avatar Jun 13 '22 15:06 nene

There is also the reverse case: some functions do not require parentheses: ie. CURRENT_DATE, TIMESTAMP, etc.

inferrinizzard avatar Jun 13 '22 21:06 inferrinizzard

functionCase would be a great addition to this already awesome project! It just looks strange to me too see PostgreSQL functions in uppercase.

jacobsvante avatar Oct 02 '22 17:10 jacobsvante

Ah this is also closed by my PR for dataTypeCase (uppercasing + lowercasing for data types) + functionCase:

  • #673

karlhorky avatar Dec 05 '23 11:12 karlhorky