Skip to content

Commit e0970cc

Browse files
committed
Fix an edge-case with subselects in CASE clauses.
1 parent b68e4b7 commit e0970cc

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

sqlparse/filters/reindent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ def _process_case(self, tlist):
162162
with offset(self, len("WHEN ")):
163163
self._process_default(tlist)
164164
end_idx, end = tlist.token_next_by(m=sql.Case.M_CLOSE)
165-
tlist.insert_before(end_idx, self.nl())
165+
if end_idx is not None:
166+
tlist.insert_before(end_idx, self.nl())
166167

167168
def _process_default(self, tlist, stmts=True):
168169
self._split_statements(tlist) if stmts else None

tests/test_grouping.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ def test_grouping_alias_case():
250250
assert p.tokens[0].get_alias() == 'foo'
251251

252252

253+
def test_grouping_subquery_no_parens():
254+
# Not totally sure if this is the right approach...
255+
# When a THEN clause contains a subquery w/o parens around it *and*
256+
# a WHERE condition, the WHERE grouper consumes END too.
257+
# This takes makes sure that it doesn't fail.
258+
p = sqlparse.parse('CASE WHEN 1 THEN select 2 where foo = 1 end')[0]
259+
assert len(p.tokens) == 1
260+
assert isinstance(p.tokens[0], sql.Case)
261+
262+
253263
def test_grouping_alias_returns_none():
254264
# see issue185
255265
p = sqlparse.parse('foo.bar')[0]

0 commit comments

Comments
 (0)