Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dbt-labs/dbt-sql-formatter
makes your sql less bad
https://github.com/dbt-labs/dbt-sql-formatter
Last synced: 3 months ago
JSON representation
makes your sql less bad
- Host: GitHub
- URL: https://github.com/dbt-labs/dbt-sql-formatter
- Owner: dbt-labs
- Created: 2017-08-31T18:15:50.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-26T11:54:14.000Z (almost 5 years ago)
- Last Synced: 2024-10-30T05:42:47.907Z (3 months ago)
- Language: Python
- Size: 15.6 KB
- Stars: 61
- Watchers: 13
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-dbt - dbt-sql-formatter - Makes your sql less bad. (Utilities)
README
A validating SQL parser that can lint select statements
Lots left to do here:
1. Finish formatting logic (only partially implemented)
2. Cross-db support (Snowflake, Redshift, BigQuery, Postgres, etc)
3. Handle more jinja blocks (config, ref, source, etc)
4. Add validating to formatting code (make sure we don't drop tokens)
5. Make this distributable (editor plugin at a POC?)
6. Lots and lots of testing!### Requirements
```
pip install pyparsing
```### Example
```
import sql_parser
from format import Formatterparser = 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 pivotedfrom table
)
select
*from my_cte
```### Thanks
Heavily inspired by (and partially copied from) code in:
- https://github.com/mozilla/moz-sql-parser
- https://github.com/pyparsing/pyparsing/blob/master/examples/bigquery_view_parser.py