Formatting the output of parse with a CASE is broken
Closed this issue · 1 comments
Alberto15Romero commented
We have parsed a SQL with a CASE statement and when formatting the output of the parse function, the library throws an Exception.
We have opened a pull request with a test to prove it and a solution that might need some hinters to improve it.
How to replicate the issue:
$ python
>>> from moz_sql_parser import parse, format
>>> format(parse("SELECT age, CASE WHEN age < 13 THEN 'child' WHEN age < 20 THEN 'teenager' ELSE 'adult' END AS age_range FROM people"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/__init__.py", line 59, in format
return Formatter(**kwargs).format(json)
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 153, in format
return self.query(json)
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 293, in query
for clause in self.clauses
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 294, in <genexpr>
for part in [getattr(self, clause)(json)]
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 300, in select
return 'SELECT {0}'.format(self.dispatch(json['select']))
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 157, in dispatch
return self.delimited_list(json)
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 177, in delimited_list
return ', '.join(self.dispatch(element) for element in json)
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 177, in <genexpr>
return ', '.join(self.dispatch(element) for element in json)
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 162, in dispatch
return self.value(json)
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 180, in value
parts = [self.dispatch(json['value'])]
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 170, in dispatch
return self.op(json)
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 199, in op
return method(value)
File "/home/alberto/.local/lib/python3.6/site-packages/moz_sql_parser/formatting.py", line 247, in _case
parts.extend(['WHEN', self.dispatch(check['when'])])
KeyError: 'when'
Pull Request
klahnakoski commented
This has been fixed since the new version