Skip to main content

Fluent DDL builder using SQLGlot AST

Project description

ddlglot logo

ddlglot

Fluent DDL builder using SQLGlot's AST

CI PyPI version Python versions Docs

Features

  • Fluent API: Chain method calls to build DDL statements
  • Multi-dialect: PostgreSQL, SQLite, DuckDB, Spark, BigQuery, Hive, and more
  • Type Safe: Full type hints and validation
  • DDL Inspection: Inspect table metadata without parsing SQL strings
  • SQLGlot Powered: Leverages SQLGlot's AST for SQL generation

Installation

pip install ddlglot

Quick Start

from ddlglot import create

sql = (
    create("table")
    .name("public.users")
    .column("id", "INT", not_null=True, pk=True)
    .column("name", "VARCHAR(100)")
    .column("active", "BOOLEAN", default=True)
    .sql(dialect="postgres")
)
# CREATE TABLE public.users (
#   id INT NOT NULL PRIMARY KEY,
#   name VARCHAR(100),
#   active BOOLEAN DEFAULT TRUE
# )

Dialect Examples

The same builder generates correct SQL for any dialect:

from ddlglot import create

ddl = (
    create("table")
    .name("events")
    .column("id", "INT")
    .column("event_date", "DATE")
    .column("value", "DOUBLE")
)

print(ddl.sql(dialect="postgres"))
# CREATE TABLE events (id INT, event_date DATE, value DOUBLE PRECISION)

print(ddl.sql(dialect="spark"))
# CREATE TABLE events (id INT, event_date DATE, value DOUBLE)

print(ddl.sql(dialect="bigquery"))
# CREATE TABLE events (id INT64, event_date DATE, value FLOAT64)

Advanced Features

Partitioning and Location (Spark/Delta Lake)

from ddlglot import create

sql = (
    create("table")
    .name("events")
    .using("delta")
    .column("id", "INT")
    .column("event_date", "DATE")
    .partitioned_by("event_date")
    .location("s3://warehouse/events")
    .tblproperties({"delta.autoOptimize.optimizeWrite": True})
    .sql(dialect="spark")
)

DDL Inspection

Inspect the table definition as structured data — no string parsing needed:

from ddlglot import create

ddl = (
    create("table")
    .name("users")
    .column("id", "INT", not_null=True, pk=True)
    .column("name", "VARCHAR(100)")
    .column("score", "INT", default=0)
    .unique_key("name")
    .build()
)

print(ddl.table_name)      # "users"
print(ddl.primary_keys)    # ("id",)
print(ddl.unique_keys)     # (("name",),)
print(ddl.columns[0].pk)   # True
print(ddl.columns[2].default)  # 0

CTAS (CREATE TABLE AS SELECT)

from sqlglot import select
from ddlglot import create

sql = (
    create("table")
    .name("user_summary")
    .column("user_id", "INT")
    .column("total_orders", "INT")
    .as_select(select("user_id", "COUNT(1).AS_(total_orders)").from_("orders"))
    .sql(dialect="postgres")
)

Development

# Install with all dependencies
uv sync --all-extras

# Run tests
uv run pytest tests/ -q

# Lint and format
uv run ruff check src tests
uv run ruff format src tests

# Type check
uv run mypy src

# Build docs
uv run sphinx-build -b html docs docs/_build/html

Documentation

Full documentation is available at: https://alexmarco.github.io/ddlglot/

License

MIT 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

ddlglot-1.0.0.tar.gz (402.9 kB view details)

Uploaded Source

Built Distribution

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

ddlglot-1.0.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ddlglot-1.0.0.tar.gz
  • Upload date:
  • Size: 402.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ddlglot-1.0.0.tar.gz
Algorithm Hash digest
SHA256 86d7952c5b6b8f95177a9549d0e94f30f6b87f953a172a119437fafe14674b46
MD5 ffc5dad5cd1679eb04b9d36edc43ab7d
BLAKE2b-256 e169fb9598495a84dfd287bdfa4d2ef10f9f7d3d60ff9be6c2a92673a141e89e

See more details on using hashes here.

Provenance

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

Publisher: release-please.yml on alexmarco/ddlglot

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

File details

Details for the file ddlglot-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ddlglot-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ddlglot-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89fc25edbbc1e4fcda1aa18c5d0409ac293d5ebece887a1c3443cf69df20620d
MD5 3386158d7b4f9b792cb5e579ebd372a2
BLAKE2b-256 54494b4bea40532635f5264074da148036c675cf133a2438fb24d9dda31a6024

See more details on using hashes here.

Provenance

The following attestation bundles were made for ddlglot-1.0.0-py3-none-any.whl:

Publisher: release-please.yml on alexmarco/ddlglot

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