darold / darold/pgFormatter

Indentation breaks when `HAVING` clause with subquery is used

Open
#195 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PLpgSQL
Stars
2k
Forks
121
Avg merge
4d 21h
Merged PRs (30d)
7

Description

Indentation breaks when HAVING clause with subquery is used:

-- Example 1: indentation breaks when `HAVING` clause is used - too indented on comment
WITH
--
-- CTE comment 1
--
cte_1 AS (
	SELECT max(1) AS col_b
	FROM dual
	GROUP BY dummy
	HAVING max(dummy) > (
			SELECT 'x'
			FROM dual)
			OR max(dummy) = 'y')
	--
	-- CTE comment 2
	--
	, cte_2 AS (
		SELECT dummy
			, max(1) AS col_c
		FROM dual
		GROUP BY dummy
		HAVING max(dummy) = '2'
)
	SELECT *
	FROM cte_2
-- Example 2: indentation breaks when `HAVING` clause is used - not indented enough without comment
WITH
--
-- CTE comment 1
--
cte_1 AS (
	SELECT max(1) AS col_b
	FROM dual
	GROUP BY dummy
	HAVING max(dummy) > (
			SELECT 'x'
			FROM dual)
			OR max(dummy) = 'y'
)
, cte_2 AS (
	SELECT dummy
		, max(1) AS col_c
FROM dual
GROUP BY dummy
HAVING max(dummy) = '2'
)
SELECT *
FROM cte_2
pg_format.pl --keyword-case 2 --function-case 1 --comma-start --wrap-after 1 --wrap-limit 120 --tabs --spaces 4
# input: stdin, output: stdout

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided pg_format.pl command with each SQL example as stdin and compare the formatter output with the reported indentation problems. Trace the handling of HAVING clauses, subqueries, and following CTE comments, then verify that both examples produce correctly aligned output.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, sql
Domain
databases, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.