diff --git a/CHANGELOG b/CHANGELOG index 44d5938e..69c402e5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,12 @@ Development Version ------------------- -Nothing yet. +Bug Fixes + +* `format(sql, reindent_aligned=True)` no longer runs consecutive statements + together on one line. `reindent_aligned` implies `strip_whitespace`, which + removes the whitespace separating the statements, and unlike `reindent` the + aligned filter never put a separator back (issue673). Release 0.6.0 (Aug 13, 2026) diff --git a/sqlparse/filters/aligned_indent.py b/sqlparse/filters/aligned_indent.py index 6ac99d62..a3d0f825 100644 --- a/sqlparse/filters/aligned_indent.py +++ b/sqlparse/filters/aligned_indent.py @@ -28,6 +28,7 @@ def __init__(self, char=' ', n='\n'): self.indent = 0 self.char = char self._max_kwd_len = len('select') + self._last_stmt = None def nl(self, offset=1): # offset = 1 represent a single space after SELECT @@ -133,4 +134,14 @@ def _process(self, tlist): def process(self, stmt): self._process(stmt) + + # reindent_aligned implies strip_whitespace, so the whitespace that + # separated this statement from the previous one is gone by now. Put a + # separator back, the way ReindentFilter does, or the statements are + # emitted glued together. + if self._last_stmt is not None: + nl = '\n' if str(self._last_stmt).endswith('\n') else '\n\n' + stmt.tokens.insert(0, sql.Token(T.Whitespace, nl)) + + self._last_stmt = stmt return stmt diff --git a/tests/test_format.py b/tests/test_format.py index 93495067..f04d5038 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -345,6 +345,16 @@ def test_window_functions(self): '(PARTITION BY b, c ORDER BY d DESC) as row_num', ' from table']) + def test_multiple_statements_are_separated(self): + # issue673 + sql = 'select a from t1;\nselect b from t2;\n' + assert self.formatter(sql) == '\n'.join([ + 'select a', + ' from t1;', + '', + 'select b', + ' from t2;']) + class TestSpacesAroundOperators: @staticmethod