Skip to content

feat: rewrite SQL formatter with token-based architecture (#705) - #717

Merged
datlechin merged 2 commits into
mainfrom
feat/sql-formatter-rewrite
Apr 13, 2026
Merged

datlechin merged 2 commits into
mainfrom
feat/sql-formatter-rewrite

Conversation

@datlechin

Copy link
Copy Markdown
Member

Summary

Full rewrite of the SQL formatter — replaces the 10-step regex pipeline with a token-based architecture for correct handling of nested structures, subqueries, CASE expressions, and window functions.

Closes #705

Architecture

Input SQL → SQLTokenizer → [SQLToken] → SQLTokenFormatter → Formatted Output
  • SQLTokenizer — character-by-character lexer (no regex). Produces typed tokens: keywords, identifiers, strings, numbers, operators, punctuation, comments, placeholders.
  • SQLTokenFormatter — walks the token stream with a clause context stack and indent tracking. Makes formatting decisions per-token with full nesting awareness.
  • SQLFormatterService — unchanged public API. Orchestrates tokenize → format → cursor map.

What's fixed (all 11 original bugs + 2 edge cases)

  • LEFT JOIN no longer splits into LEFT\nJOIN
  • ON properly placed on new line after JOIN
  • Removed dead formatJoins option (was a no-op)
  • SELECT column alignment works for all SELECT clauses including nested subqueries
  • FROM word-boundary matching no longer matches inside words
  • WHERE AND/OR alignment respects nesting depth
  • WITH / CTE support — single and multiple CTEs
  • INSERT INTO VALUES, UPDATE SET WHERE, DELETE FROM WHERE support
  • CREATE TABLE with indented column definitions
  • CASE WHEN THEN ELSE END properly indented relative to column position
  • Inline parens (COUNT(*), VARCHAR(255)) stay inline
  • BETWEEN x AND y — AND stays inline
  • OVER(PARTITION BY ... ORDER BY ...) — window functions stay inline

SQL constructs tested (20 cases)

Construct Status
Simple SELECT * ✅
Multi-column SELECT (7 cols) ✅
WHERE with AND/OR/BETWEEN/LIKE/IN/IS NOT NULL ✅
ORDER BY + LIMIT + OFFSET ✅
GROUP BY + HAVING with aggregates ✅
All JOIN types (INNER/LEFT/RIGHT/CROSS) + ON ✅
Subquery in FROM ✅
Subquery in WHERE (IN) ✅
Subquery in SELECT column ✅
Double CASE expression ✅
Triple CTE (WITH...AS) ✅
UNION ALL / UNION / INTERSECT chain ✅
INSERT INTO VALUES ✅
UPDATE SET WHERE ✅
DELETE FROM WHERE ✅
CREATE TABLE with constraints ✅
Line + block comments ✅
Complex nested (subquery + CASE + IN) ✅
Multiple statements (;) ✅
Window functions OVER(PARTITION BY...ORDER BY) ✅

Files changed

File Change
SQLTokenizer.swift New — character-by-character SQL lexer
SQLFormatterService.swift Full rewrite — token-based formatter
SQLFormatterTypes.swift Removed dead options (formatJoins, alignColumns, alignWhere)
SQLFormatterServiceTests.swift Full rewrite — exact output assertions
SQLTokenizerTests.swift New — 17 tokenizer tests

Test plan

  • Press ⌥⌘F on various SQL queries — verify formatting
  • Test idempotency — format twice gives same result
  • Test empty/whitespace input — shows error
  • Test with MySQL, PostgreSQL, SQLite dialects

@datlechin
datlechin merged commit f12fe51 into main Apr 13, 2026
2 checks passed
@datlechin
datlechin deleted the feat/sql-formatter-rewrite branch April 13, 2026 04:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve SQL prettify/format algorithm

1 participant