Skip to main content

sqlinclude

Tiny SQL source preprocessor. Expands @include directives and @define variables recursively. No SQL parsing, no opinion about any database. Intended to be piped into a query tool:

sqlinclude analysis.sql | bq query
bq query "$(sqlinclude analysis.sql)"

Written for Unix and Windows alike: it is a single dependency-free Python package and installs a sqlinclude console command (a sqlinclude.exe on Windows).

Install

pipx install sqlinclude

or, into the current environment:

pip install sqlinclude

Requires Python 3.12+.

Directives

Line-oriented, leading whitespace allowed:

@include file.sql
@include "file.sql"
@define name = value        (the `= ` is optional; value is the rest of the
                             line, kept verbatim including quotes)

Include paths are resolved relative to the including file. Includes may nest; cycles are detected and reported as errors.

Variables form a single environment filled in expansion order: the first @define name ... seen anywhere in the tree wins, and later definitions of the same name are ignored (so a parent file's definition always overrides an included fragment's default). Definitions from an included file leak back to the including file. A @name with no definition passes through unchanged -- BigQuery's native @param syntax is never touched.

Each include block is bracketed with -- #line N "file" markers (with forward slashes on every platform) so error messages from downstream tools point at the originating source file and line. Use -n/--no-markers to suppress them.

Example

analysis.sql:

@define start = '2024-01-01'
@define end = '2024-12-31'

WITH users AS (
    @include "users.sql"
),

orders AS (
    @include orders.sql
)

SELECT ... WHERE created BETWEEN @start AND @end

Run:

sqlinclude analysis.sql | bq query
sqlinclude --vars start='2024-06-01' analysis.sql | bq query
sqlinclude --tree analysis.sql          # show the include tree, not SQL
sqlinclude --tree --ascii analysis.sql  # ASCII box characters instead of Unicode

Options

Option Description
file SQL file to preprocess (default: read stdin)
-n, --no-markers do not emit -- #line markers around includes
--vars name=value set a variable (repeatable); wins over @define
-e, --edit open the editor even when no variable is undefined
--no-edit never open the editor; leave undefined @names as-is
-t, --tree print the @include dependency tree instead of SQL
--ascii use ASCII box characters in the include tree
--version show the version

Editing undefined variables

When a variable is undefined, sqlinclude opens the editor named by $VISUAL/$EDITOR on the controlling terminal, even when its output is piped. Edit values, or delete a line to leave that variable undefined. Blank lines and # comments are ignored. On Windows the same variables are honored, falling back to notepad. With -e/--edit the buffer also shows the @include tree. Undefined variables are always reported on stderr; pass --no-edit to silence everything and pass undefined names through untouched.

Development

python -m venv .venv
. .venv/bin/activate
pip install -e ".[dev]"
pytest

License

MIT -- see LICENSE.

Metadata

Release files for sqlinclude 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlinclude 0.1.0
File Size Uploaded
sqlinclude-0.1.0.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlinclude 0.1.0
File Interpreter ABI Platform
sqlinclude-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.5 kB

Release files / sqlinclude-0.1.0.tar.gz

Download URL sqlinclude-0.1.0.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
48c357bb3e9ca225e971f4f1c79f6a75b522ac3b9ad23f73ee87101e1586b7ff
BLAKE2b-256 checksum
How to use checksums
970f7e97d54dfc60ef108a8507fa301d428a68b99d145448f11fd891be53bf93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / sqlinclude-0.1.0-py3-none-any.whl

Download URL sqlinclude-0.1.0-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
911a3b34bb6709b87ac78385e54213b874b2abfbceb3c8f0095086d02d330508
BLAKE2b-256 checksum
How to use checksums
4397e5c8245d6331a3aacf886dc46b1a4aad02e7ae2f69f3e011bff00f9e1d13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release history Release notifications | RSS feed

0.1.1

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page