microsoft / microsoft/vscode-mssql

[Bug]: Incorrect Indentation When Formatting MSSQL Queries Using UNION/UNION ALL Inside a CTE

Open
#20,591 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Formatter Bug User-filed
Dominant language
TypeScript
Stars
1.9k
Forks
610
Avg merge
2d 3h
Merged PRs (30d)
97

Description

### Description

When formatting T-SQL queries that contain a UNION or UNION ALL inside a CTE, the formatter produces unexpected indentation. Specifically, the first SELECT inside the CTE is indented far more than expected.

### Example (UNION):

```
WITH
test
AS
(
SELECT
*
FROM
table1
UNION
SELECT
*
FROM
table2
)
SELECT
*
FROM
test;
```

### Example (UNION ALL):

```
WITH
test
AS
(
SELECT
*
FROM
table1
UNION
ALL
SELECT
*
FROM
table2
)
SELECT
*
FROM
test;
```

In both cases, the formatter incorrectly indents the first SELECT statement within the CTE. The indentation becomes inconsistent compared with the rest of the query and with typical SQL formatting conventions.

### Expected behavior:

The SELECT statements within the CTE should be aligned consistently, without excessive indentation.

### Steps to Reproduce

Copy the queries from above and format with mssql extension

### Affected Area

- [ ] Connection dialog (SQL Server | Azure browse/Fabric browse)
- [ ] Query editor
- [ ] Query results panel
- [ ] Object Explorer
- [ ] GitHub Copilot integration
- [ ] Preview/Edit data
- [ ] Table Designer
- [ ] Schema Designer
- [ ] Schema Compare
- [ ] Local SQL Server Container provisioning
- [ ] SQL database in Fabric provisioning
- [ ] DACPAC/BACPAC export/import
- [ ] SQL Database projects
- [ ] Query Plan Visualizer
- [x] Other (please describe below)

### If you selected "Other", please describe the affected area

Formatter

### Environment Information

Operating System: Windows with wsl2
VSCode version: 1.106.0
mssql Extension version: 1.36.0

### Confirmation

- [x] I have searched existing issues and couldn't find a match
- [ ] I want to work on this issue

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 reproducing the two CTE examples with the MSSQL extension's formatter and compare the indentation of the first SELECT with the later SELECT. Trace the formatter implementation for UNION and UNION ALL inside CTEs, then verify that both examples produce consistently aligned SELECT statements.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.