Awesome
A validating SQL parser that can lint select statements
Lots left to do here:
- Finish formatting logic (only partially implemented)
- Cross-db support (Snowflake, Redshift, BigQuery, Postgres, etc)
- Handle more jinja blocks (config, ref, source, etc)
- Add validating to formatting code (make sure we don't drop tokens)
- Make this distributable (editor plugin at a POC?)
- Lots and lots of testing!
Requirements
pip install pyparsing
Example
import sql_parser
from format import Formatter
parser = sql_parser.BigQueryViewParser()
sql = """
with my_cte as (select sum(case when a=1 then 1 else 0 end) as pivoted from table) select * from my_cte
"""
ast = parser._parse(sql)
f = Formatter()
f.format(ast)
f.document.pprint()
Output:
with my_cte as (
select
sum(case
when a = 1 then 1
else 0
end) as pivoted
from table
)
select
*
from my_cte
Thanks
Heavily inspired by (and partially copied from) code in: