sqloxide
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.
Installation
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
Usage
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
Benchmarks
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 29.6800 (1.0) 50.4300 (1.0) 30.6219 (1.0) 0.7367 (1.0) 30.4900 (1.0) 0.2390 (1.0) 527;716 32,656.3811 (1.0) 9099 1
test_sqlglot 365.8420 (12.33) 692.8950 (13.74) 377.2422 (12.32) 11.7692 (15.98) 375.7825 (12.32) 4.3145 (18.05) 62;97 2,650.8168 (0.08) 2260 1
test_sqlparser 1,577.7720 (53.16) 9,751.9699 (193.38) 1,651.5547 (53.93) 355.5511 (482.64) 1,620.7315 (53.16) 30.9200 (129.37) 3;60 605.4901 (0.02) 538 1
test_mozsqlparser 2,793.8400 (94.13) 12,358.7790 (245.07) 3,091.8519 (100.97) 960.4173 (>1000.0) 2,937.6310 (96.35) 243.3220 (>1000.0) 4;4 323.4308 (0.01) 316 1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Example
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}
Develop
-
Install
rustup -
poetry installwill automatically create the venv, compile the package and install it into the venv via the build script.
Release files for sqlo2 0.25.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| sqlo2-0.25.0-cp310-cp310-win_amd64.whl | CPython 3.10 | CPython 3.10 | Windows x86-64 | Details |
| sqlo2-0.25.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.10 | CPython 3.10 | Linux glibc 2.17+ x86-64 | Details |
| sqlo2-0.25.0-cp310-cp310-macosx_10_9_universal2.whl | CPython 3.10 | CPython 3.10 | macOS 10.9+ universal2 (ARM64, x86-64) | Details |
| sqlo2-0.25.0-cp39-cp39-win_amd64.whl | CPython 3.9 | CPython 3.9 | Windows x86-64 | Details |
| sqlo2-0.25.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.9 | CPython 3.9 | Linux glibc 2.17+ x86-64 | Details |
| sqlo2-0.25.0-cp39-cp39-macosx_10_9_universal2.whl | CPython 3.9 | CPython 3.9 | macOS 10.9+ universal2 (ARM64, x86-64) | Details |
| sqlo2-0.25.0-cp38-cp38-win_amd64.whl | CPython 3.8 | CPython 3.8 | Windows x86-64 | Details |
| sqlo2-0.25.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.8 | CPython 3.8 | Linux glibc 2.17+ x86-64 | Details |
| sqlo2-0.25.0-cp38-cp38-macosx_10_9_universal2.whl | CPython 3.8 | CPython 3.8 | macOS 10.9+ universal2 (ARM64, x86-64) | Details |
| sqlo2-0.25.0-cp37-cp37m-win_amd64.whl | CPython 3.7 | CPython 3.7 pymalloc | Windows x86-64 | Details |
| sqlo2-0.25.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.7 | CPython 3.7 pymalloc | Linux glibc 2.17+ x86-64 | Details |
| sqlo2-0.25.0-cp37-cp37m-macosx_10_9_universal2.whl | CPython 3.7 | CPython 3.7 pymalloc | macOS 10.9+ universal2 (ARM64, x86-64) | Details |
Total release size: 10.9 MB
Release files / sqlo2-0.25.0-cp310-cp310-win_amd64.whl
| Download URL | sqlo2-0.25.0-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 400.8 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
7c043c1e3387110137d7a48530e2098f719311164d479fe4ea9ba161450eda96
|
|
BLAKE2b-256 checksum How to use checksums |
1b31b98779d2ed80658b42b8cb84fe17487dcb5d019487f669c1f0965a8e3787
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | sqlo2-0.25.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
fe4dd2a0f8915c01efc2084df55a7e3549d6dc45968c37a563150a92f984f0ce
|
|
BLAKE2b-256 checksum How to use checksums |
972a1820d76fcb79b14752966a3d2a4fe2b2f483a6336eac34efcd66b8a6ce1b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp310-cp310-macosx_10_9_universal2.whl
| Download URL | sqlo2-0.25.0-cp310-cp310-macosx_10_9_universal2.whl |
|---|---|
| Size | 1.0 MB |
| Tags | CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64) |
|
SHA-256 checksum How to use checksums |
f5f80d49135d9058c37128c887d6f45eecb4ed67f31c9f21c90421a96c59b838
|
|
BLAKE2b-256 checksum How to use checksums |
7ae15050162ed42e1d6ef0d72c7326043e18621cbea6d7254a13e1f23c8281d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp39-cp39-win_amd64.whl
| Download URL | sqlo2-0.25.0-cp39-cp39-win_amd64.whl |
|---|---|
| Size | 400.8 kB |
| Tags | CPython 3.9 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
1169e734220654303884e2d9bbdbd879e7bb3bc7f8662247939e450c7028999e
|
|
BLAKE2b-256 checksum How to use checksums |
7f3dee7094c8ae274565f552a5a8c82a227848c958995650a425cf76989c4b27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | sqlo2-0.25.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
323b3ceef7b1f7fdaf0e9dd713b616b8927643ad3b30bbd09e4f2be47354c685
|
|
BLAKE2b-256 checksum How to use checksums |
a15add51434735646805ded6c15a9fcb34357ed0b096e0d953d6cf7cdc86e1ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp39-cp39-macosx_10_9_universal2.whl
| Download URL | sqlo2-0.25.0-cp39-cp39-macosx_10_9_universal2.whl |
|---|---|
| Size | 1.0 MB |
| Tags | CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64) |
|
SHA-256 checksum How to use checksums |
d466a368a34ff279bb038f4cb6769b75d9030f31d3f86f7337550f5dc27a2df1
|
|
BLAKE2b-256 checksum How to use checksums |
b4f577f75fbb67d2935009701210247ae57da8acf1a86c096662864d45f829d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp38-cp38-win_amd64.whl
| Download URL | sqlo2-0.25.0-cp38-cp38-win_amd64.whl |
|---|---|
| Size | 400.8 kB |
| Tags | CPython 3.8 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
be53e80fd2192dd29a51fab86ba0bc76e4a60c3d6bf41c21fd0540158687d063
|
|
BLAKE2b-256 checksum How to use checksums |
9eb6f6710711974d3e76b8b04d5b6eafd6e6e95c6621e27d9b478ab9980c9ada
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | sqlo2-0.25.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.8 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
699a41bed87ac2ec106cf46c1c88091bb04e23d1ea6f41f3419e636bafdb7e31
|
|
BLAKE2b-256 checksum How to use checksums |
f4d11215b399c44842692dc1cce734dc0713e3797a163e4187aa60534d678477
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp38-cp38-macosx_10_9_universal2.whl
| Download URL | sqlo2-0.25.0-cp38-cp38-macosx_10_9_universal2.whl |
|---|---|
| Size | 1.0 MB |
| Tags | CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64) |
|
SHA-256 checksum How to use checksums |
4bdb9bcb5aab9b503eaeedeca9d67db70992bfa8ec11702e06be46e2f50fd8af
|
|
BLAKE2b-256 checksum How to use checksums |
d815cccfe232e4cee5f84ebff87f9f8c08c4eba9662b2539729120fb38bed965
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp37-cp37m-win_amd64.whl
| Download URL | sqlo2-0.25.0-cp37-cp37m-win_amd64.whl |
|---|---|
| Size | 400.8 kB |
| Tags | CPython 3.7 CPython 3.7 pymalloc Windows x86-64 |
|
SHA-256 checksum How to use checksums |
e1ea813455afb18d6d6bccb32e76125723d6326a91780cb873b0b8259678b181
|
|
BLAKE2b-256 checksum How to use checksums |
c720efed68c5273c0993af0252dfd33bfe061d714759ecf86429aacbc65638bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | sqlo2-0.25.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
8b3bc3581ecce8d5cb8b81b854940898a45d5c3dd556f1dea8548e3834ce494a
|
|
BLAKE2b-256 checksum How to use checksums |
934d02c4f14907676f92df1c33cee7df6c7fb001093562b44a8e66384f2effeb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|
Release files / sqlo2-0.25.0-cp37-cp37m-macosx_10_9_universal2.whl
| Download URL | sqlo2-0.25.0-cp37-cp37m-macosx_10_9_universal2.whl |
|---|---|
| Size | 1.0 MB |
| Tags | CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ universal2 (ARM64, x86-64) |
|
SHA-256 checksum How to use checksums |
1db8699eb9069dc6d48be780decfb1b6d0da12e247a9927773e36142043303fe
|
|
BLAKE2b-256 checksum How to use checksums |
7d3791dbcc24aad9ef243924e55b251f2fbc0d39690ffcd34f2dbac7c2b4a923
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.14
|