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
SELECTsubqueries. - 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
- Create repository named
sql_decomposeron GitHub. - Push code and enable Actions.
- Create a PyPI project
sql-decomposer. - Add
PYPI_API_TOKENas a GitHub Actions secret. - Tag a release (
v0.1.0) to trigger publish workflow.
License
MIT License. See LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70a81e1ca72b62bd61d2047f779e916ee300dd712a5ad8d3e9ef11adcf3e5663
|
|
| MD5 |
86f30afb111ffeffdf26a92864e6f681
|
|
| BLAKE2b-256 |
d7528825c9c7e173710f115e8a2b1429c98bdeaffe84eb1d31d4dda8aaac2c33
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca569591eae5f0f44ec822aa18a54d28f2fa28ac6d7e6366b5c94197054077cd
|
|
| MD5 |
a341464c3b71b274ecca3e90dc808aa3
|
|
| BLAKE2b-256 |
f2bedcfa1a2f5cbedebc06b499c7a82f0a934d5b0ee8e4618b56b097df9b02c5
|