Skip to content

fix: preserve indentation of lines inside quoted identifiers with pretty=True#7595

Closed
saluc28 wants to merge 2 commits into
tobymao:mainfrom
saluc28:fix/pretty-indent-quoted-identifiers
Closed

fix: preserve indentation of lines inside quoted identifiers with pretty=True#7595
saluc28 wants to merge 2 commits into
tobymao:mainfrom
saluc28:fix/pretty-indent-quoted-identifiers

Conversation

@saluc28

@saluc28 saluc28 commented May 2, 2026

Copy link
Copy Markdown

Fixes #7593

Root cause

Generator.indent() splits sql on "\n" and prepends spaces to every
line unconditionally. If a quoted string or identifier contains a literal
newline, the continuation line is a content byte — not a structural line
break — so adding indentation there corrupts the value.

Fix

Track quote state while iterating the characters of each line. When the
current position is inside a quoted string ('...') or a quoted identifier
("..."), the line is appended as-is. Outside quotes, normal indentation
applies. Doubled quote characters (the SQL escape for a literal quote) are
handled with a one-step lookahead so they don't prematurely close the state.

Tests

Two assertions added to the existing test_pretty_line_breaks test:

self.assertEqual(transpile('SELECT "1\n2"', pretty=True)[0], 'SELECT\n  "1\n2"')
self.assertEqual(
    transpile('SELECT "Product\n(Foo, Bar)" AS x FROM t', pretty=True)[0],
    'SELECT\n  "Product\n(Foo, Bar)" AS x\nFROM t',
)

@saluc28

saluc28 commented May 3, 2026

Copy link
Copy Markdown
Author

addressed in this PR. duckdb allows any whitespace (including newlines) inside double-quoted identifiers: https://duckdb.org/docs/current/sql/dialect/keywords_and_identifiers

@georgesittas

Copy link
Copy Markdown
Collaborator

Hey @saluc28, thanks for the PR, but this approach is too complicated. We already have logic in the generator to avoid this issue for string literals; it was simply missing from Identifier's generator method.

Take a look at my fix, instead: 17432c6.

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.

pretty=True reindents newlines inside quoted identifiers

2 participants