summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
authorAdam Greenhall <agreenhall@lyft.com>2015-09-11 22:38:38 -0700
committerVictor Uriarte <victor.m.uriarte@intel.com>2016-06-06 06:31:35 -0700
commit0e5a25f39fcff8cac8c54f0209be39dc86914570 (patch)
treef95bf76cf7ee8200ae0baf95878866351edc39b5 /sqlparse
parent9ad0acafabd8c8216fdacb71310f6ec56ef59ae9 (diff)
downloadsqlparse-0e5a25f39fcff8cac8c54f0209be39dc86914570.tar.gz
Fix/Test `Group-By`
Diffstat (limited to 'sqlparse')
-rw-r--r--sqlparse/filters.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index dad754e..0d0be5c 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -365,6 +365,10 @@ class AlignedIndentFilter:
return self._process(sql.TokenList(tlist.tokens), base_indent=base_indent)
def _process_parenthesis(self, tlist, base_indent=0):
+ if not tlist.token_next_match(0, T.DML, 'SELECT'):
+ # if this isn't a subquery, don't re-indent
+ return tlist
+
sub_indent = base_indent + self._max_kwd_len + 2 # add two for the space and parens
tlist.insert_after(tlist.tokens[0], self.whitespace(sub_indent, newline_before=True))
# de-indent the last parenthesis
@@ -381,7 +385,7 @@ class AlignedIndentFilter:
def _process_identifierlist(self, tlist, base_indent=0):
# columns being selected
new_tokens = []
- identifiers = filter(lambda t: isinstance(t, sql.Identifier), tlist.tokens)
+ identifiers = filter(lambda t: t.ttype not in (T.Punctuation, T.Whitespace, T.Newline), tlist.tokens)
for i, token in enumerate(identifiers):
if i > 0:
new_tokens.append(self.newline())
@@ -441,7 +445,13 @@ class AlignedIndentFilter:
# process any sub-sub statements
for sgroup in tlist.get_sublists():
- self._process(sgroup, base_indent=base_indent)
+ prev_token = tlist.token_prev(tlist.token_index(sgroup))
+ indent_offset = 0
+ if prev_token and prev_token.match(T.Keyword, 'BY'):
+ # HACK: make "group by" and "order by" indents work. these are longer than _max_kwd_len.
+ # TODO: generalize this
+ indent_offset = 3
+ self._process(sgroup, base_indent=base_indent + indent_offset)
return tlist
def _process(self, tlist, base_indent=0, verbose=False):