Metadata-Version: 2.1
Name: sqloxide
Version: 0.1.10
Summary: Python bindings for sqlparser-rs
Home-page: https://github.com/wseaton/sqloxide
Author: Will Eaton
Author-email: me@wseaton.com
License: UNKNOWN
Platform: UNKNOWN
Requires-Python: >=3.6.0,<4.0
Description-Content-Type: text/markdown

# sqloxide

![GitHub Workflow Status](https://img.shields.io/github/workflow/status/wseaton/sqloxide/CI)

`sqloxide` wraps rust bindings for [sqlparser-rs](https://github.com/ballista-compute/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).

## 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:
```sh
pip install sqloxide
```

## Usage

```python 
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 3 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](https://pypi.org/project/sqlparse/), query -> AST
* `test_mozsqlparser` - testing [moz-sql-parser](https://pypi.org/project/moz-sql-parser/), full roundtrip as in the docs, query -> JSON


To run them on your machine:

```
poetry run pytest tests/benchmark.py
```

```
----------------------------------------------------------------------------------------------- benchmark: 3 tests -----------------------------------------------------------------------------------------------
Name (time in us)             Min                    Max                   Mean                 StdDev                 Median                   IQR            Outliers          OPS            Rounds  Iterations
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_sqloxide             42.3900 (1.0)         181.7010 (1.0)          54.7211 (1.0)          16.4072 (1.0)          46.6140 (1.0)         10.5403 (1.0)     1203;1194  18,274.4720 (1.0)        6823           1
test_sqlparser         2,339.0280 (55.18)     9,652.6110 (53.12)     2,634.7362 (48.15)       462.2765 (28.18)     2,562.0310 (54.96)      196.6425 (18.66)        8;13     379.5446 (0.02)        281           1
test_mozsqlparser     12,703.8400 (299.69)   44,050.6470 (242.43)   19,561.9232 (357.48)   10,018.5869 (610.62)   15,160.2170 (325.23)   3,216.1253 (305.13)      12;13      51.1197 (0.00)         63           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.


