Skip to main content

Extract repeated SQL subqueries into temporary tables using sqlglot.

Project description

sql_decomposer

sql_decomposer extracts repeated SQL subqueries into temporary tables. It can help reduce duplication in large analytical queries and produce a more readable SQL script.

Features

  • Parses SQL safely with sqlglot.
  • Detects repeated SELECT subqueries.
  • Rewrites repeated blocks to SELECT * FROM <temp_table>.
  • Provides a Python API and a CLI.

Installation

From source:

pip install .

From PyPI (after release):

pip install sql-decomposer

CLI usage

python -m sql_decomposer input.sql output.sql --min-count 2 --temp-prefix __temp

Or with console script:

sql-decomposer input.sql output.sql

Options:

  • --dialect: optional sqlglot dialect (postgres, mysql, etc.)
  • --min-count: minimum repetition count to extract (default: 2)
  • --temp-prefix: generated temp table prefix (default: __temp)

Python API

from sql_decomposer import decompose_sql

sql = "SELECT * FROM (SELECT id FROM users) t1 JOIN (SELECT id FROM users) t2 ON t1.id=t2.id"
result = decompose_sql(sql, min_count=2, temp_prefix="tmp")
print(result)

Development

Install dev dependencies:

pip install -e ".[dev]"

Run tests:

pytest

Build artifacts:

python -m build

Validate package metadata:

twine check dist/*

GitHub and PyPI release checklist

  1. Create repository named sql_decomposer on GitHub.
  2. Push code and enable Actions.
  3. Create a PyPI project sql-decomposer.
  4. Add PYPI_API_TOKEN as a GitHub Actions secret.
  5. Tag a release (v0.1.0) to trigger publish workflow.

License

MIT License. See LICENSE.

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

sql_decomposer-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

sql_decomposer-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file sql_decomposer-0.1.0.tar.gz.

File metadata

  • Download URL: sql_decomposer-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for sql_decomposer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 70a81e1ca72b62bd61d2047f779e916ee300dd712a5ad8d3e9ef11adcf3e5663
MD5 86f30afb111ffeffdf26a92864e6f681
BLAKE2b-256 d7528825c9c7e173710f115e8a2b1429c98bdeaffe84eb1d31d4dda8aaac2c33

See more details on using hashes here.

File details

Details for the file sql_decomposer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sql_decomposer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for sql_decomposer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca569591eae5f0f44ec822aa18a54d28f2fa28ac6d7e6366b5c94197054077cd
MD5 a341464c3b71b274ecca3e90dc808aa3
BLAKE2b-256 f2bedcfa1a2f5cbedebc06b499c7a82f0a934d5b0ee8e4618b56b097df9b02c5

See more details on using hashes here.

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