Skip to main content

sqlfluff-pyspark

Lint and optionally fix SQL embedded in spark.sql(...) calls using SQLFluff.

Installation

pip install sqlfluff-pyspark

Command Line Usage

# Lint spark.sql strings
sqlfluff-pyspark path/to/file.py another_file.py

# Apply fixes (writes changes back to the files)
sqlfluff-pyspark --fix path/to/file.py

Exit codes:

  • 0: Success / no actionable snippets / no violations
  • 1: Lint violations found (printed to stderr) or fixes applied and re-lint failed
  • 1: Unexpected error from sqlfluff invocation

Pre-commit Hook Integration

This project provides pre-commit hooks so you can automatically lint (and optionally fix) spark.sql strings before committing.

Add the following to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/danieltom/sqlfluff-pyspark
    rev: v0.1.0  # or the latest tag
    hooks:
      - id: sqlfluff-pyspark-lint
      # Optional fix hook (will modify files). Normally run separately or in CI.
      # - id: sqlfluff-pyspark-fix

Then install:

pre-commit install

Choosing lint vs fix hook

Use the lint hook locally to keep commits clean. Run the fix hook manually:

pre-commit run sqlfluff-pyspark-fix --all-files

or directly:

sqlfluff-pyspark --fix your_script.py

How It Works

  1. Parses Python source with ast to find calls to spark.sql(...).
  2. Extracts string literals (skips f-strings and non-constant expressions).
  3. Writes each snippet to a temp SQL file and calls sqlfluff on them.
  4. Translates violation line numbers back to original file positions.
  5. For fixes, rewrites the original string literals preserving style (indent, quoting) when possible.

Inline directive sqlfluff: anywhere in the literal will skip linting/fixing for that snippet.

Limitations / Notes

  • F-strings are skipped (dynamic content).
  • Concatenated string literals are supported ("SELECT" + " 1").
  • Multi-line snippets are normalized with indentation preserved.
  • Only supports .py files passed explicitly; it does not auto-discover.

Development

pip install -e .[dev]
pytest -vv

Versioning

The hook rev should match a published tag. If using main, be aware of potential breaking changes.

License

MIT

Metadata

Release files for sqlfluff-pyspark 0.1.1

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

Source distribution (sdist)

Source distribution for sqlfluff-pyspark 0.1.1
File Size Uploaded
sqlfluff_pyspark-0.1.1.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlfluff-pyspark 0.1.1
File Interpreter ABI Platform
sqlfluff_pyspark-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / sqlfluff_pyspark-0.1.1.tar.gz

Download URL sqlfluff_pyspark-0.1.1.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fcce775e1b0a983fbd82ac2a2b5c31010f6fb062f0aa28ae26eb3e3c7917b285
BLAKE2b-256 checksum
How to use checksums
b9b31a6db2515a61b710f4eff14a1afa97ba6eaf27a1312cbee97cf5aea14aaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 2, 2025.

Transparency log

Release files / sqlfluff_pyspark-0.1.1-py3-none-any.whl

Download URL sqlfluff_pyspark-0.1.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93df12e851b32838ea769f61e7d1c41ed74b47048c220e0568aaff301662293a
BLAKE2b-256 checksum
How to use checksums
fdb49999b21dab9bf87932224129a0721d4d0c19906ef36e29fac1441beb6996
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 2, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 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