@@ -15,11 +15,12 @@ class AlignedIndentFilter(object):
1515 join_words = (r'((LEFT\s+|RIGHT\s+|FULL\s+)?'
1616 r'(INNER\s+|OUTER\s+|STRAIGHT\s+)?|'
1717 r'(CROSS\s+|NATURAL\s+)?)?JOIN\b' )
18+ by_words = ('GROUP BY' , 'ORDER BY' )
1819 split_words = ('FROM' ,
1920 join_words , 'ON' ,
2021 'WHERE' , 'AND' , 'OR' ,
21- 'GROUP' , 'HAVING' , 'LIMIT' ,
22- 'ORDER' , 'UNION' , 'VALUES' ,
22+ 'GROUP BY ' , 'HAVING' , 'LIMIT' ,
23+ 'ORDER BY ' , 'UNION' , 'VALUES' ,
2324 'SET' , 'BETWEEN' , 'EXCEPT' )
2425
2526 def __init__ (self , char = ' ' , n = '\n ' ):
@@ -101,8 +102,12 @@ def _next_token(self, tlist, idx=-1):
101102 def _split_kwds (self , tlist ):
102103 tidx , token = self ._next_token (tlist )
103104 while token :
104- # joins are special case. only consider the first word as aligner
105- if token .match (T .Keyword , self .join_words , regex = True ):
105+ # joins, group/order by are special case. only consider the first
106+ # word as aligner
107+ if (
108+ token .match (T .Keyword , self .join_words , regex = True ) or
109+ token .match (T .Keyword , ('GROUP BY' , 'ORDER BY' ))
110+ ):
106111 token_indent = token .value .split ()[0 ]
107112 else :
108113 token_indent = text_type (token )
@@ -117,7 +122,9 @@ def _process_default(self, tlist):
117122 idx = tlist .token_index (sgroup )
118123 pidx , prev_ = tlist .token_prev (idx )
119124 # HACK: make "group/order by" work. Longer than max_len.
120- offset_ = 3 if (prev_ and prev_ .match (T .Keyword , 'BY' )) else 0
125+ offset_ = 3 if (
126+ prev_ and prev_ .match (T .Keyword , ('GROUP BY' , 'ORDER BY' ))
127+ ) else 0
121128 with offset (self , offset_ ):
122129 self ._process (sgroup )
123130
0 commit comments