Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions sqlparse/filters/aligned_indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ class AlignedIndentFilter:
r'(INNER\s+|OUTER\s+|STRAIGHT\s+)?|'
r'(CROSS\s+|NATURAL\s+)?)?JOIN\b')
by_words = r'(GROUP|ORDER)\s+BY\b'
split_words = ('FROM',
join_words, 'ON', by_words,
'WHERE', 'AND', 'OR',
'HAVING', 'LIMIT',
'UNION', 'VALUES',
'SET', 'BETWEEN', 'EXCEPT')
# Keywords that start a new aligned line. They are matched as regular
# expressions (see ``_next_token``), and ``Token.match`` uses an
# unanchored ``search``, so each bare keyword is wrapped in word
# boundaries. Without them ``ON`` matches inside ``ONLY`` and ``SET``
# inside ``OFFSET``, which broke the ``OFFSET ... FETCH NEXT n ROWS ONLY``
# paging clause across lines (issue #842).
split_words = (r'\bFROM\b',
join_words, r'\bON\b', by_words,
r'\bWHERE\b', r'\bAND\b', r'\bOR\b',
r'\bHAVING\b', r'\bLIMIT\b', r'\bOFFSET\b',
r'\bUNION\b', r'\bVALUES\b',
r'\bSET\b', r'\bBETWEEN\b', r'\bEXCEPT\b')

def __init__(self, char=' ', n='\n'):
self.n = n
Expand Down
13 changes: 13 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,19 @@ def test_window_functions(self):
'(PARTITION BY b, c ORDER BY d DESC) as row_num',
' from table'])

def test_offset_fetch(self):
# the OFFSET ... FETCH NEXT n ROWS ONLY paging clause should stay on
# a single aligned line and not be split by substring matches of the
# clause keywords (ON in ONLY, SET in OFFSET) -- issue #842
sql = ('select id from tbl where id > 0 order by id asc '
'offset 0 rows fetch next 100 rows only')
assert self.formatter(sql) == '\n'.join([
'select id',
' from tbl',
' where id > 0',
' order by id asc',
'offset 0 rows fetch next 100 rows only'])


class TestSpacesAroundOperators:
@staticmethod
Expand Down