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

postgresql: better handle DISTINCT clause

Open holymonson opened this issue 4 years ago • 4 comments

At present sql-formatter produces:

SELECT
  DISTINCT ON (c1, c2) c1,
  c2
FROM
  t1;

But we expect:

SELECT
DISTINCT ON (c1, c2)
  c1,
  c2
FROM
  t1;

-- or 

SELECT DISTINCT ON (c1, c2)
  c1,
  c2
FROM
  t1;

holymonson avatar May 04 '21 05:05 holymonson

The latest version in master produces:

SELECT
  DISTINCT
  ON (c1, c2) c1,
  c2
FROM
  t1;

This isn't really better either :(

nene avatar May 03 '22 16:05 nene

With 9.0.0 there's another slight change. It now gets formatted as:

SELECT DISTINCT
  ON (c1, c2) c1,
  c2
FROM
  t1;

Still not there though :(

nene avatar Aug 10 '22 07:08 nene

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT] [ON]']); should be able to solve the issue?

QoVoQ avatar Aug 23 '23 01:08 QoVoQ

This would result in:

SELECT DISTINCT ON
  (c1, c2) c1,
  c2
FROM
  t1;

which isn't better. What one really wants is:

SELECT DISTINCT ON (c1, c2)
  c1,
  c2
FROM
  t1;

nene avatar Aug 29 '23 07:08 nene