Skip to main content

mssql-cdc-pyspark

A platform-agnostic PySpark streaming source for SQL Server Change Data Capture, built on Spark's Python DataSource V2 API, plus a completeness signal (finalized_until) that tells downstream jobs when a period of data is safe to read.

  • 100% PySpark: no JVM connector, no platform-specific APIs. Tested on local Spark 4.2 (the source alone also on local Spark 4.1, without admission control), on a local Spark Connect server (a PySpark 4.2 client, Delta Connect) and on Databricks classic compute (DBR 18.2, dedicated access mode, single node); other Spark 4.1+ runtimes and multi-node clusters are untested. Databricks serverless and Databricks Connect are Spark Connect clients; 0.6.0rc3 ran the whole library on Databricks serverless (environment 4) with the fake backend: stream, chunked bootstrap and backfill, silver, reconcile and finalization; its SQL Server path has not run there, since serverless needs a network path to your SQL Server. Serverless refuses the cache API, which the library works without (tested on the local Connect server with caching refused), takes only trigger={"availableNow": True}, and its network path to your SQL Server is yours to set up (Spark Connect). The metrics need metricsPath on a directory every node sees: a local or FUSE path, such as a Unity Catalog Volume, or a URI pyarrow.fs opens (s3://, gs://, abfss://, hdfs://; tested with file:// URIs, not against a real object store yet). Without a metricsPath every node can write the stream runs, but the facts' metric columns stay NULL and schema change events do not reach them.
  • Offsets are SQL Server commit LSNs, checkpointed by Spark. Supports Trigger.AvailableNow and per-batch limits (maxCommitsPerBatch).
  • Arrow end to end: the default driver (mssql-python) fetches straight into Arrow record batches inside the executors.
  • Fails loudly when CDC cleanup purged changes the stream still needed, or re-snapshots on its own and records the gap (on_data_loss="resnapshot").

Documentation: https://emanuel-luis.github.io/mssql-cdc-pyspark/

Status: 1.x (the version is in pyproject.toml): the public API and the state a stream leaves behind break only in a major release ("Compatibility", below). Streaming-engine behaviour (offsets, checkpoints, AvailableNow, admission control, retention guard) is covered by unit tests against a file-backed CDC simulator. SQL Server behaviour is covered by the lab/ checks, which run locally against Docker and in GitHub Actions against SQL Server 2022; tests/integration runs the source against SQL Server 2022 in Docker (testcontainers). See LAB.md.

Why

With CDC, "the partition has data" no longer means "the partition is complete": a commit can reach the lake after its hour has passed. Pinterest solved this for Flink + Iceberg with partition finalization, a watermark inferred from the event times it observed.

SQL Server CDC can do better, because the source already knows. The capture process writes changes in commit order, one consistent transaction per scan cycle, sys.fn_cdc_get_max_lsn() is the last LSN it processed, and during inactivity it writes "dummy" entries (about every 5 minutes on SQL Server 2022) so that LSN keeps advancing. This project carries that frontier through the pipeline. Details in the design notes.

Architecture: the CDC capture job copies each commit of the source table into its change table and cdc.lsn_time_mapping; the mssql_cdc stream reader on the Spark driver turns commit LSNs into offsets and LSN ranges, read() on the executors reads them as Arrow, delta_sink appends them to bronze with a facts row, and finalization and apply_changes then advance finalized_until and silver, which downstream jobs read.

Source: architecture.d2.

Install

On a Spark platform (Databricks, EMR, Dataproc, Fabric), which ships its own PySpark:

pip install mssql-cdc-pyspark

Locally, with PySpark and Delta: pip install "mssql-cdc-pyspark[spark]". On Linux the default driver needs system libraries that pip does not install; see Installation.

Quick start (local)

Requirements: Docker, uv, Java 17 (21 is untested); on Linux, the driver's system libraries above.

cp .env.example .env               # set MSSQL_SA_PASSWORD
docker compose up -d               # SQL Server 2022 + Agent
uv sync                            # .venv with dev tools (Spark, Delta, mssql-python)

uv run python -m lab.workload setup       # database, tables, CDC
uv run python -m lab.workload seed        # Faker data
uv run python examples/local_pipeline.py  # stream -> Delta bronze -> finalized_until
uv run python -m lab.workload stream --duration 60
uv run python examples/local_pipeline.py  # resumes from the checkpoint

The Quickstart walks through it.

Usage

from mssql_cdc import finalization, stream

options = {
    "connectionString": "Server=host,1433;Database=db;Encrypt=yes",
    "user": "u",
    "password": "p",  # from a secret store
    "captureInstance": "dbo_orders",
    "maxCommitsPerBatch": "500",
}
query = stream(spark, options).to_delta(
    "bronze.orders",
    app_id="orders-v1",
    checkpoint="/Volumes/cat/sch/vol/ckpt/orders",
    facts_table="ops.ingestion_facts",
    trigger={"availableNow": True},
    bootstrap=True,
)
query.awaitTermination()

end = finalization.end_offset_from_progress(query.lastProgress)
finalization.advance(spark, "ops.table_finalization", "bronze.orders", end)

The rest is in the documentation:

Compatibility

From 1.0.0 on, versions follow Semantic Versioning: a patch only fixes, a minor adds and deprecates, and only a major breaks the Python API (each break listed under "Breaking" in CHANGELOG.md, with what to change), after a 1.x minor deprecated what it removes. The state a stream leaves behind never breaks without a migration path, and within 1.x it only grows: the offsets in its checkpoints, the checkpoint layout, and the schemas of the tables it writes, which change only through migrations that run on their own. Every release's "State compatibility" line says what it does to that state. The public API is what the documentation's reference documents (the Python API, the options and the output schema); everything else may change in any release. Dropping support for a Python, Spark, Delta or SQL Server version that has reached its end of life is a minor release, announced in the changelog at least one release before; 1.0 supports Python 3.10 and later (ADR 0021).

Development

uv sync
uv run pytest -q                # engine tests, no SQL Server needed

See Development (setup for Linux and Windows), Architecture, the roadmap, the decision records, and CONTRIBUTING.md.

License

MIT. The default backend's dependency mssql-python pulls in mssql-python-odbc, which holds Microsoft's ODBC Driver 18 binaries under Microsoft's own license. For a license-sensitive install, leave it out (pip install --no-deps mssql-cdc-pyspark, then pip install pyarrow arrow-odbc) and use backend=arrow-odbc with a driver installed separately under its EULA (Installation).

Metadata

Release files for mssql-cdc-pyspark 1.0.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 mssql-cdc-pyspark 1.0.0
File Size Uploaded Provenance Actions
mssql_cdc_pyspark-1.0.0.tar.gz 160.8 kB With provenance Details for mssql_cdc_pyspark-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mssql-cdc-pyspark 1.0.0
File Interpreter ABI Platform Actions
mssql_cdc_pyspark-1.0.0-py3-none-any.whl Python 3 none any Details for mssql_cdc_pyspark-1.0.0-py3-none-any.whl

Total release size: 339.0 kB

Release files / mssql_cdc_pyspark-1.0.0.tar.gz

Download URL mssql_cdc_pyspark-1.0.0.tar.gz
Size 160.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d96e1dc99f27943912d35f04c75e87ec11c307013e401e65796bda5541f7a5a9
BLAKE2b-256 checksum
How to use checksums
c0e741c8aca377bc7666f30f900cf2414d05f4d9bb03c8cfa7147ebc8545f32f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Release files / mssql_cdc_pyspark-1.0.0-py3-none-any.whl

Download URL mssql_cdc_pyspark-1.0.0-py3-none-any.whl
Size 178.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca9d09b97b3b880f38a837a947809cff8d32009cd13d52cafd1b0c9ebf90a838
BLAKE2b-256 checksum
How to use checksums
498ebda2f19f3574d9fc7bc1f44603a08c0d2279c5852e51918759d81cedd56c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Security for mssql-cdc-pyspark 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mssql_cdc_pyspark-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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