Skip to main content

Fast, multi-dialect SQL parser — a Rust core with Python bindings

Project description

squonk-python

Python bindings for squonk: a maturin-built Rust extension plus typed Python views over the serialized AST.

API

parse() returns a Document, which is both a mapping-compatible view of the raw JSON and a typed helper object for common operations:

import squonk

doc = squonk.parse("select salary from employees", dialect="ansi")

assert doc.source == "select salary from employees"
assert doc.to_sql() == "SELECT salary FROM employees"
assert doc.statements[0].to_sql() == "SELECT salary FROM employees"

# Documents are live views: editing the raw tree changes subsequent rendering.
doc.to_dict()["statements"].clear()
assert doc.to_sql() == ""

idents = [ident.text for ident in doc.find_all(squonk.Ident)]
assert {"salary", "employees"}.issubset(idents)

Use parse_dict() when you want the raw serde-compatible JSON shape:

tree: squonk.ParseDocumentJson = squonk.parse_dict("select 1")
assert tree["statements"]

Dialect literals and aliases retain their canonical type: type checkers infer squonk.parse("select 1", "pg").dialect as Literal["postgres"]. Validate configuration strings with validate_dialect() before passing them to typed APIs.

Recovering parse keeps good statements and reports bad statements out of band:

result = squonk.parse_recovering("select 1; from broken; select 2")

for diagnostic in result.errors:
    print(diagnostic.kind, diagnostic.source_text(), diagnostic.location())

Tokenization returns discriminated token dictionaries. Trivia capture is opt-in:

tokens = squonk.tokenize("-- lead\nselect a + $1", "postgres", include_trivia=True)

assert tokens["tokens"][0]["kind"] == "Keyword"
assert tokens["tokens"][0]["keyword"] == "select"
assert tokens["trivia"][0]["kind"] == "LineComment"

Rendering and transpilation use Rust's renderer:

assert squonk.render("select 1") == "SELECT 1"
assert squonk.redact("select 123") != "SELECT 123"
assert squonk.transpile("select $1", "postgres", "postgres") == "SELECT $1"

When rendering a Document, the document's dialect is used unless you pass an override:

doc = squonk.parse("select $1", dialect="postgres")
assert squonk.render(doc) == "SELECT $1"

Types

The package ships py.typed plus stubs for the public API. The dict-returning helpers expose TypedDict shapes such as ParseDocumentJson, RecoveredDocumentJson, TokenizeResultJson, TokenJson, TriviaJson, and DiagnosticJson.

The AST itself is represented as serde JSON. Document, Node, Ident, ObjectName, Diagnostic, and Trivia provide ergonomic wrappers without hiding the raw JSON: to_dict() returns Python structures and to_json() returns compact JSON text. The generated squonk.ast module exhaustively types the serialized node graph, while squonk.__schema_version__ identifies its wire-schema version. Generated child-node edges use a bounded JSON object type so mypy and Pyright do not recursively expand the entire AST graph; annotate a known node with its named type from squonk.ast when field-level precision is needed.

ObjectName is schema-aware: true qualified object-name fields wrap as ObjectName, while plain Ident lists such as column lists remain lists of Ident wrappers.

Node.to_sql() renders complete statements, queries, expressions, and data types. Check node.is_renderable first when traversing arbitrary nodes; context-dependent nodes raise UnsupportedNodeRenderError. All library failures derive from SquonkError, with structured subclasses for parsing, dialects, tokenization, rendering, formatting, and serialization.

Examples

Runnable scripts live in examples/:

  • metadata_report.py parses SQL and reports identifiers, table names, source snippets, and canonical SQL.
  • recovering_diagnostics.py shows statement-level recovery with byte-span diagnostics.
  • render_transpile_redact.py shows canonical render, redaction, and source/target dialect rendering.

From the Python crate directory after maturin develop:

cd crates/squonk-python
uv run python examples/metadata_report.py
uv run python examples/recovering_diagnostics.py
uv run python examples/render_transpile_redact.py

Development

From the Python crate directory:

cd crates/squonk-python
uv sync --group dev
uv run maturin develop
uv run pytest
uv run ruff check python
uv run mypy
uv run python -m mypy.stubtest squonk._ast squonk._exceptions squonk._native

The Rust boundary is checked by cargo check -p squonk-python; the Python tests live under crates/squonk-python/python/tests and smoke-run the examples.

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

squonk-1.0.0.tar.gz (2.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

squonk-1.0.0-cp311-abi3-win_amd64.whl (5.7 MB view details)

Uploaded CPython 3.11+Windows x86-64

squonk-1.0.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

squonk-1.0.0-cp311-abi3-macosx_11_0_arm64.whl (4.9 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

squonk-1.0.0-cp311-abi3-macosx_10_12_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

File details

Details for the file squonk-1.0.0.tar.gz.

File metadata

  • Download URL: squonk-1.0.0.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for squonk-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7d6226c9ebe2f47d47ace0056cd9f29755255a26f9c60fce869c46697ee605ab
MD5 5cc352fe021ad325c75c6de1c998e219
BLAKE2b-256 27ce4544ccef8dd6ef52949a01335b7617c7881c136a68a1600af07d5716fcb9

See more details on using hashes here.

Provenance

The following attestation bundles were made for squonk-1.0.0.tar.gz:

Publisher: release-python.yml on moderately-ai/squonk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file squonk-1.0.0-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: squonk-1.0.0-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 5.7 MB
  • Tags: CPython 3.11+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for squonk-1.0.0-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2b665b6d33577ff53339c8ff61c9acd7844b194381831ecc303ce905c919d044
MD5 f8b9a1543ca97d8e3869a8a51108054a
BLAKE2b-256 ad7010b3903406349670f1798b011b71d1082519f91c236334a144ebb4b1b734

See more details on using hashes here.

Provenance

The following attestation bundles were made for squonk-1.0.0-cp311-abi3-win_amd64.whl:

Publisher: release-python.yml on moderately-ai/squonk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file squonk-1.0.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for squonk-1.0.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ae7d1b83e76ef83edc748bb3bc658e1dfa05271f7059ba9094eae0924c44d695
MD5 ac1898efeb8c00b30d3b36a9d52be3a1
BLAKE2b-256 310ebd24a621a2b0b8e20f6a9765fd9fd6ec2d183ac6eaceef598edb95c9f2ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for squonk-1.0.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-python.yml on moderately-ai/squonk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file squonk-1.0.0-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for squonk-1.0.0-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d65ca69a019a6a67de1adf9f1d36dc9a61081ac761c41eb3c3658c7039f98de3
MD5 78cf9023de93678528e2c2c125e9d553
BLAKE2b-256 1c0c886c90698f710e34c35afca82128442ebfe6662228f3d32d740eab04bd73

See more details on using hashes here.

Provenance

The following attestation bundles were made for squonk-1.0.0-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: release-python.yml on moderately-ai/squonk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file squonk-1.0.0-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for squonk-1.0.0-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3d6bf7e50112c3e8f20a853abaf9900f98a2670c8d6040244866759590da83ff
MD5 877115ca06b0c37a8b37fb038fcb4a79
BLAKE2b-256 73f2724ecf72eb2247178143fdf55fd1033e1cd8a176d6f430feaae94134b12d

See more details on using hashes here.

Provenance

The following attestation bundles were made for squonk-1.0.0-cp311-abi3-macosx_10_12_x86_64.whl:

Publisher: release-python.yml on moderately-ai/squonk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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