jellezijlstra / sqltree Goto Github PK
View Code? Open in Web Editor NEWA SQL parser
License: Apache License 2.0
A SQL parser
License: Apache License 2.0
e.g.
with a as (select 1 from a union all select 2 from a) select 1
print(format("select 1 AS
x y
"))
SELECT 1 AS x y
should be select 1 AS x y
print(format("select x cross join y "))
Traceback (most recent call last):
File "", line 1, in
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/formatter.py", line 821, in format
sqltree(sql, dialect), dialect=dialect, line_length=line_length, indent=indent
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/api.py", line 12, in sqltree
return parse(tokens, dialect)
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/parser.py", line 964, in parse
return _parse_statement(p)
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/parser.py", line 984, in _parse_statement
_assert_done(p)
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/parser.py", line 970, in _assert_done
raise ParseError.from_unexpected_token(remaining, expected)
sqltree.parser.ParseError: Unexpected 'CROSS' (expected EOF)
0: select x cross join y
print(format("select 1E-2 "))
SELECT 1e - 2
select 1 from (select a from b) as a
is working but select 1 from (select a from b)
isn't
There should be a nicely usable API. Some ideas:
sqltree format "select *" # prints formatted SQL
sqltree parse "select *" # prints pretty-printed parse tree
sqltree translate --from=mysql --to=sqlite "select *" # prints SQL changed to work with SQLite
MySQL supports queries like SELECT COUNT(*) c FROM t
. We should support this but first I need to figure out what exactly the allowed syntax is.
print(format("select '''y' "))
print(format("select '''y' "))
Traceback (most recent call last):
File "", line 1, in
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/formatter.py", line 821, in format
sqltree(sql, dialect), dialect=dialect, line_length=line_length, indent=indent
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/api.py", line 12, in sqltree
return parse(tokens, dialect)
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/parser.py", line 964, in parse
return _parse_statement(p)
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/parser.py", line 984, in _parse_statement
_assert_done(p)
File "/Users/toby_mao/dev/sqlglot/env/lib/python3.8/site-packages/sqltree/parser.py", line 970, in _assert_done
raise ParseError.from_unexpected_token(remaining, expected)
sqltree.parser.ParseError: Unexpected "'y'" (expected EOF)
0: select '''y'
in sql, the escape for a quote is another quote
Provide a Transformer class deriving from Visitor that produces a new AST. The default transfoormer should just return a copy of the original tree.
print(format("select x from foo left join bar on foo.a = bar.a right join baz on baz.a = bar.b"))
SELECT x
FROM
foo
LEFTJOIN
bar
ON foo.a = bar.a
RIGHTJOIN
baz
ON baz.a = bar.b
should be left join and right join
sqltree should support multiple SQL dialects, as implemented by major databases. I'm interested initially in Redshift, Presto, and MySQL.
I added a Dialect enum, but that's probably not enough. We should also add version support (--dialect=mysql --version=5.7.0
), because sometimes syntax changes between versions. Instead, there should be a Dialect class that gets passed along to the parser, tokenizer, formatter, etc. It should have methods for every syntactic detail that changes between dialects.
We should provide a Fixit rule that formats SQL embedded in Python strings.
Ideas for how it would work:
presto.query
-> Presto dialect object); use that dialect for parsing and formatting the SQL.sql = "some string"
-> format it with some default dialectA declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.