Skip to main content

No project description provided

Project description

sqloxide

GitHub Workflow Status (with event)Downloads



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

Parsing

Parsing a SQL query is relatively straight forward:

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

Note that you get back what looks like a JSON document but in actual python types, this is a typed AST that matches the sqlparser-rs AST schema.

We can convert this AST back into a SQL query by running:

from sqloxide import restore_ast

query = restore_ast(ast=output)
print(query)

This reconstruction is helpful if you want to make manual edits to the AST in python.

AST Rewrites

If you want a more structured approach to AST edits, we also expose APIs that allow you to use the visitor pattern to make query modifications.

Here is an example for mutating a subset of the expressions in the query to be SHOUTING UPPERCASE:

from sqloxide import parse_sql, mutate_expressions

sql = "SELECT something from somewhere where something = 1 and something_else = 2"

def func(x):
    if "CompoundIdentifier" in x.keys():
        for y in x["CompoundIdentifier"]:
            y["value"] = y["value"].upper()
    return x

ast = parse_sql(sql=sql, dialect="ansi")
result = mutate_expressions(parsed_query=ast, func=func)
print(result)
---
>>> ['SELECT something FROM somewhere WHERE something = 1 AND something_else = 2']

What if you needed to make a structured edit to the table name in the above query? There is also an API for that as well:

from sqloxide import parse_sql, mutate_relations

def func(x):
    return x.replace("somewhere", "anywhere")
result = mutate_relations(parsed_query=ast, func=func)
print(result)
---
>>> ['SELECT something FROM anywhere WHERE something = 1 AND something_else = 2']

These features combined allow for powerful semantic rewrites of queries, if you have any examples you'd like to share please contribue back to the examples/ folder!

Benchmarks

We run 4 benchmarks, comparing to some python native sql parsing libraries:

  • test_sqloxide - parse query and get a python object back from rust
  • test_sqlparser - testing sqlparse, query -> AST
  • test_mozsqlparser - testing moz-sql-parser, full roundtrip as in the docs, query -> JSON
  • test_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

  1. Install rustup

  2. poetry install will automatically create the venv, compile the package and install it into the venv via the build script.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqloxide_fork-0.1.51.tar.gz (14.7 kB view details)

Uploaded Source

Built Distributions

sqloxide_fork-0.1.51-cp312-none-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.12 Windows x86-64

sqloxide_fork-0.1.51-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

sqloxide_fork-0.1.51-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

sqloxide_fork-0.1.51-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl (2.3 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.5+ i686

sqloxide_fork-0.1.51-cp312-cp312-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

sqloxide_fork-0.1.51-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

sqloxide_fork-0.1.51-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

sqloxide_fork-0.1.51-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl (2.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.5+ i686

sqloxide_fork-0.1.51-cp311-cp311-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

sqloxide_fork-0.1.51-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

sqloxide_fork-0.1.51-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

sqloxide_fork-0.1.51-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl (2.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.5+ i686

sqloxide_fork-0.1.51-cp310-cp310-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

sqloxide_fork-0.1.51-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

sqloxide_fork-0.1.51-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

sqloxide_fork-0.1.51-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl (2.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ i686

sqloxide_fork-0.1.51-cp39-cp39-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

sqloxide_fork-0.1.51-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

sqloxide_fork-0.1.51-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

sqloxide_fork-0.1.51-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl (2.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ i686

sqloxide_fork-0.1.51-cp38-cp38-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

File details

Details for the file sqloxide_fork-0.1.51.tar.gz.

File metadata

  • Download URL: sqloxide_fork-0.1.51.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for sqloxide_fork-0.1.51.tar.gz
Algorithm Hash digest
SHA256 4e5cf78f34e789056b57d46fa98392b57b2e4920c8ecd8336739401a4acc2ee1
MD5 e6d5474b20864a81be9554dfe80bbc56
BLAKE2b-256 56ff1c03748186318b4c858a202ebae2d4a792013d5b4e289c32df80ab17d0e1

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp312-none-win_amd64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 687581bb9be02299a923a4015fd8bda484a105a45c74914e959a9b2706f11ac9
MD5 bacf1ea590ab2296fe5663143c1ea6b8
BLAKE2b-256 d4cc6e8e33600fde407c97d428a1cbacaaf68966f6f4cf91b0186e499b5b25ae

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 08f97e15b64d21b431e615ea609cca972e19e509ba27377152b7435fa35d14d9
MD5 f5a298b609bb1aa76341f8bdf0c3000e
BLAKE2b-256 17f9658c26b359c974e17308bce9e8cee3e1a285f8fb2f75235979083a6761df

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9af3cc2450f531c8dfffe97ce43184ebc2e19773f4ee511064ca4929eda9d593
MD5 cdd5799ec33886b272b9a6b25c8920f2
BLAKE2b-256 8d9d9cae69f2882dc31e3a590ef9ab8c97614be7de1fd880bf0555b97504c01b

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 eab34558d0cfb1220811c594f83515df77e95fe62e15a39675c30917288485f6
MD5 cb70ba665b1a09d03a28e5c2ba2de6fb
BLAKE2b-256 15a6182db7d2fe90ef42564b25647a01a16a4e63da493b55f8b0cd472a38c763

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 35768a77cb953819045f246623eb4b96b7b62fc9b397503a5c07610d6779788c
MD5 3b436c70f1162cc516957e20f665a65c
BLAKE2b-256 dcd7fe7f57b26939b3721b3699957f96262b85d63496c8f1c852dd64716c6ac2

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 750f5d322f8640ced821a0a565dcc591a3400c80360bb8931df7ff4d5acc6def
MD5 e952a2bcab4a8c6891e040da051b5b3e
BLAKE2b-256 2603a2d5806d1af9ab05638a17663ac8eeb5b8a16cb10cb5f0ed2ed97d5f851f

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 69925196cf49b8cccd070c019077b59321a8ff11a7eaed1882f4b6be434e7102
MD5 4afaf97e47db863a7159e070b25f1c5e
BLAKE2b-256 7e464e9238fd50367097a3814b509dccc0354d99105ac7761f9262343992ab5e

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 2f1c221b507493cf75ffc24d15df3c1de91a457d765ee04783ae3164d90f5722
MD5 3fd72668dee0e9beb90f4e9d47b539d6
BLAKE2b-256 9738d818c799b894d2f9c252a204940253338059809628edfc7e77837b84069b

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6da369aa87aa4f67b2ff6b9d2bf623a034ef9d64cf7ae36745eaa6c46096727a
MD5 48ee660b63cd48e57f5841bee3118a7a
BLAKE2b-256 d557e80303bbf58b1fc64bbabc54e8238a7858cf8842cdc93926d89120c2fc9f

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2a39ec1544c19dd8b71ae33c2cb7b35bcb5dde4bd4a9006608b7b6225fa9b5e8
MD5 2250067e0730e251a97fb60574de3327
BLAKE2b-256 38a78d337c6912a78f1527bb6d4005c1b9447711ac8baafa6f07f545df254d35

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1a1e1f9b0bbce23e0de2dcddaea6d149c4db29501b549c62118d09c6752f3e76
MD5 1a60bed162d6f1b81a3263c02d173491
BLAKE2b-256 4f1153ee5cfb385889ce474765326514469c5847872c5e3b35afd6ac0694eac9

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 353a2513c3ba2a9083365ded159a759f0cc7240c1add83f70a9dabb8a908360c
MD5 65aa32de9ded9ca084e12f0537db33be
BLAKE2b-256 48330e03d817f50b01d2d4aa789ff800daaebaab0fddc8e09dca00316f3a358e

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 135424f280735ba7ca83da682eb57c83ba45724793d298f245c4cf0534edc159
MD5 d11bdc2a254769c330f1d52d5c24ff50
BLAKE2b-256 f738c4dac7d6148191a6547829dda4dd04391baaf7c177e4cf1ee79f02986e49

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f5adebf206b152ac9a5b55cdd606ef6f6c775a62a0d22f1a9f71b635d6ea0a55
MD5 91540c1faccc1a8a312fd310d9684e40
BLAKE2b-256 ac22ea5a569ff9e00bee58f7cc2661c8f4a7e405862649871e6faa0d07c240ac

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fa07ae11f17f5dffac55747db12fd3154138a50ee3c0f5bb9dc8def702d9c82e
MD5 37b62b2b32eb1d7217ed681b4dc8280a
BLAKE2b-256 1daea7c21acc866c6035f69fd51c7ea3454034597356ae2da772d2d96dc73ee1

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 e943a38c5f3cd483f90f7f297eddd11777b90ba0b94a90ec40ae18a4a2389294
MD5 9f0cd31f9ffdfe4e349caa299b9c21b4
BLAKE2b-256 19c4a6b62b70db1c08e936bf4aa1e6bdab5d5f27236688d879aabd6cfaca0509

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0d56c4edff6b52b375258534f3fcca59e970b564e21162098d69080e119a29fe
MD5 35e9a7e382f2fbc3618bc299a51de2ce
BLAKE2b-256 a0111d630d419b223773e01c4005c9f656878030d1d2d2a4a93d3b86ba9a91c2

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 98af5dcc8872024eca7c475aed9a7852dee4520adf3dddc2d76096bda960c4ba
MD5 c97847167fc53aafec5e665d3d1bb829
BLAKE2b-256 e02c68dbe5aafba93fecf142dde9de10304562b4f70559b70363f99caed97597

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fa188c67b4760d141484aeab43c2b468f6f5bfe7278c968f842c70f2645ea044
MD5 87266c9064075c02a98376a2afd1c23b
BLAKE2b-256 d15eefe0826f417cc3494b04b9bbbe451c002f4cd62eaf02fc2b8f8c9e48571f

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 8bc81af4c7e2f557f55551e1edfa7de821f789e0eb1557f1684411744cc2b129
MD5 e8e2dce4f4451733e7932692789f3ef7
BLAKE2b-256 145b558611831844c4f9e84e47a154d0a2293230ef320adfc50a773b9d807a3b

See more details on using hashes here.

File details

Details for the file sqloxide_fork-0.1.51-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sqloxide_fork-0.1.51-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a5939cde87ecce277c85f437d4c9966ab082eba490e8d5f7dac4b1b5e6d0bf69
MD5 e0d24bb0bb5e1c29a2f2ceb180f02b39
BLAKE2b-256 9dbed6fde3e646695992a0ec8729434defc372820949d592f3d43986720380ec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page