sqloxide
wraps rust bindings for sqlparser-rs into a python package using pyO3
.
The original goal of this project was to have a very fast, efficient, and accurate SQL parser I could use for building data lineage graphs across large code bases (think hundreds of auto-generated .sql files). Most existing sql parsing approaches for python are either very slow or not accurate (especially in regards to deeply nested queries, sub-selects and/or table aliases). Looking to the rust community for support, I found the excellent sqlparser-rs
crate which is quite easy to wrap in python code.
The project provides manylinux2014
wheels on pypi so it should be compatible with most linux distributions. Native wheels are also now available for OSX and Windows.
To install from pypi:
pip install sqloxide
from sqloxide import parse_sql
sql = """
SELECT employee.first_name, employee.last_name,
call.start_time, call.end_time, call_outcome.outcome_text
FROM employee
INNER JOIN call ON call.employee_id = employee.id
INNER JOIN call_outcome ON call.call_outcome_id = call_outcome.id
ORDER BY call.start_time ASC;
"""
output = parse_sql(sql=sql, dialect='ansi')
print(output)
>>> [
{
"Query": {
"ctes": [],
"body": {
"Select": {
"distinct": false,
"top": null,
"projection": [
{
"UnnamedExpr": {
"CompoundIdentifier": [
{
"value": "employee",
"quote_style": null
},
{
"value": "first_name",
"quote_style": null
}
]
}
},
{
"UnnamedExpr": {
"CompoundIdentifier": [
{
"value": "employee",
"quote_style": null
},
{
"value": "last_name",
"quote_style": null
}
]
}
},
{
"UnnamedExpr": {
"CompoundIdentifier": [
{
"value": "call",
"quote_style": null
},
{
"value": "start_time",
"quote_style": null
}
]
}
},
{ # OUTPUT TRUNCATED
We run 4 benchmarks, comparing to some python native sql parsing libraries:
test_sqloxide
- parse query and get a python object back from rusttest_sqlparser
- testing sqlparse, query -> ASTtest_mozsqlparser
- testing moz-sql-parser, full roundtrip as in the docs, query -> JSONtest_sqlglot
- testing sqlglot, query -> AST
To run them on your machine:
poetry run pytest tests/benchmark.py
------------------------------------------------------------------------------------------- benchmark: 4 tests -------------------------------------------------------------------------------------------
Name (time in us) Min Max Mean StdDev Median IQR Outliers OPS Rounds Iterations
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_sqloxide 24.1100 (1.0) 48.2900 (1.0) 24.6202 (1.0) 0.9245 (1.0) 24.5100 (1.0) 0.1700 (1.0) 297;480 40,616.9973 (1.0) 9485 1
test_sqlglot 638.6120 (26.49) 1,030.3130 (21.34) 658.4604 (26.74) 27.9328 (30.21) 649.3820 (26.49) 6.2600 (36.82) 164;174 1,518.6941 (0.04) 1373 1
test_sqlparser 1,470.6840 (61.00) 7,881.2710 (163.21) 1,533.2183 (62.27) 268.1834 (290.09) 1,505.6490 (61.43) 34.0900 (200.53) 4;39 652.2228 (0.02) 586 1
test_mozsqlparser 2,608.2070 (108.18) 12,572.8330 (260.36) 2,865.6004 (116.39) 958.5833 (>1000.0) 2,733.5470 (111.53) 230.5500 (>1000.0) 4;4 348.9670 (0.01) 316 1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The depgraph
example reads a bunch of .sql
files from disk using glob, and builds a dependency graph of all of the objects using graphviz.
poetry run python ./examples/depgraph.py --path {path/to/folder/with/queries}
-
Install
rustup
-
poetry install
will automatically create the venv, compile the package and install it into the venv via the build script.