Skip to main content

Composition of SQL files

Project description

Test Coverage Stable Version Pre-release Version PyPI - Python Version PyPI Downloads

SQLCompose

Composable SQL for Python — designed to make large queries easier to structure, reuse, and maintain.

🚀 Why this exists

As SQL queries grow, they tend to become difficult to manage:

  • Large queries turn into monolithic files
  • Logic gets duplicated across transformations
  • Small changes become risky and time-consuming
  • Reusability is limited

👉 Over time, SQL becomes harder to understand, evolve, and maintain.

This project introduces a simple idea:

Treat SQL as composable building blocks instead of monolithic scripts.

✨ Features

  • 🧩 Split SQL into reusable components
  • 🔗 Compose queries from smaller building blocks
  • ♻️ Reduce duplication across pipelines
  • 🧠 Improve readability and structure
  • ⚡ Lightweight and framework-agnostic

📦 Use cases

This library is especially useful when working with:

  • Data pipelines
  • ETL/ELT workflows
  • Analytics transformations
  • Data warehouse queries
  • Systems with repeated SQL logic

👉 Particularly valuable in environments where SQL is a core part of the architecture.

🏗 Core idea

Traditional SQL workflows rely on large, self-contained query files.

SQLCompose takes a different approach:

  • Break queries into smaller, focused pieces
  • Reuse common logic across multiple queries
  • Assemble final queries through composition

👉 This enables a more modular and maintainable way of working with SQL.

🔄 Reusability & maintainability

By introducing composition:

  • Shared logic can be defined once and reused
  • Changes can be made in one place instead of many
  • Query structure becomes easier to reason about

👉 This reduces both duplication and long-term maintenance cost.

🧠 Design philosophy

This project is built around a few key principles:

1. Modularity over monoliths

Large SQL files should be broken into smaller, understandable units.

2. Reusability by design

Common logic should be shareable across queries and pipelines.

3. Simplicity over abstraction

The goal is not to hide SQL, but to organize it better.

4. Fit into existing workflows

The library works alongside existing tools and data platforms.

🔗 What this enables

With composable SQL, you can:

  • Build more maintainable data pipelines
  • Reduce duplication across transformations
  • Standardize common query patterns
  • Improve collaboration across teams

👉 Particularly useful in growing data platforms.

⚖️ Trade-offs

Focus ✅ Not a goal ❌
SQL composability Full query engine abstraction
Maintainability Replacing SQL with another DSL
Simplicity Complex orchestration frameworks

👉 The goal is not to replace SQL — but to make it easier to structure at scale.

🧠 Context

This library fits into a broader focus on:

  • Data engineering workflows
  • Clean architecture principles
  • Composable systems
  • Reducing duplication in large codebases

🎯 When to use

Use this library if you:

  • Work with large or growing SQL codebases
  • Reuse logic across multiple queries
  • Maintain data pipelines or transformations
  • Want cleaner, more modular SQL

🚫 When not to use

This library may not be necessary if:

  • Your SQL queries are small and simple
  • Reusability is not a concern
  • Query complexity is low

🔗 Related projects

Part of a broader focus on:

  • Runtime abstractions
  • Developer tooling
  • Structured Python systems

👉 https://github.com/apmadsen

🤝 Contributing

Feedback, ideas, and contributions are welcome!

⚙️ Examples

1. Execute the script with the filename as an argument and output to the console:

sqlcompose query.sql

2. Pipe data into application and output to a file

cat query.sql | sqlcompose > output.sql

3. Execute the script with SQL string as argument

sqlcompose 'select * from $INCLUDE(included-query1.sql)'

NOTE: Different consoles have different limitations, so you may have to switch from single to double quotes to allow for using the dollar sign.

4. Import it in another python application or package

from sqlcompose import load, loads
# method 1 : loading from a file
sql1 = load("query.sql")

# method 2 : loading from an SQL string
sql2 = loads("""
    select *
      from dataset.table main
inner join $INCLUDE(other.sql) other
        on other.field = main.field
  """)

Preparing SQL scripts

Insert a $INCLUDE(filename) where the reference to the file should be in the resulting SQL, keeping in mind that references are loaded relative to the file loaded or the current working dir in case of an SQL string.

--main-query.sql
select * from $INCLUDE(includes\included-query2.sql)
--included-query1.sql
select 1 as test
--included-query2.sql
select * from $INCLUDE(included-query1.sql)
union all
select * from $INCLUDE(nested\included-query3.sql)
--nested\included-query3.sql
select 1 as test

Which outputs:

WITH Q_1_1 AS (
  WITH Q_2_1 AS (
    --includes\included-query1.sql
    select 1 as test
  ), Q_2_2 AS (
    --includes\nested\included-query3.sql
    select 1 as test
  ), Q_2 AS (
    --includes\included-query2.sql
    select * from Q_2_1
    union all
    select * from Q_2_2
  )
  SELECT * FROM Q_2
), Q_1 AS (
  --test\main-query.sql
  select * from Q_1_1
)
SELECT * FROM Q_1

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

sqlcompose-0.0.4.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

sqlcompose-0.0.4-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlcompose-0.0.4.tar.gz.

File metadata

  • Download URL: sqlcompose-0.0.4.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlcompose-0.0.4.tar.gz
Algorithm Hash digest
SHA256 cef0e498af2170311b65526bfcf1516db6fcec5adcc3fa4f611e02c89741f28e
MD5 bfa9938a1f98122aeeb4842f3892c97f
BLAKE2b-256 44872d990783ca1a2b93ac9aba2a1e8e9b3af6abdef4cdf930d8cc2bce756a26

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcompose-0.0.4.tar.gz:

Publisher: python-publish.yml on apmadsen/sqlcompose

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

File details

Details for the file sqlcompose-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: sqlcompose-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlcompose-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 abdff2dd36467b3f83ae9d80d98eb75ced10780e305a713407defcb4f24a9655
MD5 ea5d44a5421a46acb3c6578f98ffeeff
BLAKE2b-256 1aa84a8b3c3237c572fa10980cac2cb0ca444007129bbcc6f4637255905ec7db

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcompose-0.0.4-py3-none-any.whl:

Publisher: python-publish.yml on apmadsen/sqlcompose

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