Skip to main content

pyspark-sql-formatter

A formatter for Pyspark code with SQL queries. It relies on Python formatter yapf and SparkSQL formatter sparksqlformatter, both working indepdendently. User can specify configurations for either formatter separately.

The queries should be in the form spark.sql(query) or spark.sql('xxx'). Cases like spark.sql('xxx'.format()), spark.sql('xxx'.replace()) may raise Exceptions.

Installation

Install using pip

pip install pysqlformatter

Install from source

  1. Download source code.
  2. Navigate to the source code directory.
  3. Do python setup.py install or pip install ..

Compatibility

Supports Python 2.7 and 3.6+.

Usage

pysqlformatter can be used as either a command-line tool or a Python library.

Use as command-line tool

usage: pysqlformatter [-h] [-f FILES [FILES ...]] [-i] [--query-names QUERY_NAMES [QUERY_NAMES ...]] [--python-style PYTHON_STYLE] [--sparksql-style SPARKSQL_CONFIG]

Formatter for Pyspark code and SparkSQL queries.

optional arguments:
  -h, --help            show this help message and exit
  -f FILES [FILES ...], --files FILES [FILES ...]
                        Paths to files to format.
  -i, --in-place        Format the files in place.
  --python-style PYTHON_STYLE
                        Style for Python formatting, interface to https://github.com/google/yapf.
  --sparksql-style SPARKSQL_CONFIG
                        Style for SparkSQL formatting, interface to https://github.com/largecats/sparksql-formatter.
  --query-names QUERY_NAMES [QUERY_NAMES ...]
                        String variables with names containing these strings will be formatted as SQL queries. Default to 'query'.

E.g.,

$ pysqlformatter -f <path_to_file> --python-style='pep8' --sparksql-style="{'reservedKeywordUppercase': False}" --query-names query

Or using config files:

$ pysqlformatter -f <path_to_file> --python-style="<path_to_python_style_config_file>" --sparksql-style="<path_to_sparksql_config_file>" --query-names query

Use as Python library

Call pysqlformatter.api.format_script() to format script passed as string:

>>> from pysqlformatter import api
>>> script = '''query = 'select * from t0'\nspark.sql(query)'''
>>> api.format_script(script=script, pythonStyle='pep8', sparksqlConfig=sparksqlConfig(), queryNames=['query'])
"query = '''\nSELECT\n    *\nFROM\n    t0\n'''\nspark.sql(query)\n"

Call pysqlformatter.api.format_file() to format script in file:

>>> from pysqlformatter import api
>>> api.format_file(filePath=<path_to_file>, pythonStyle='pep8', sparksqlConfig=sparksqlConfig(), queryNames=['query'], inPlace=False)
...

Metadata

Release files for pysqlformatter 0.0.7

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

Source distribution (sdist)

Source distribution for pysqlformatter 0.0.7
File Size Uploaded
pysqlformatter-0.0.7.tar.gz 10.6 kB Details

Built distribution (wheel)

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

Total release size: 24.0 kB

Release files / pysqlformatter-0.0.7.tar.gz

Download URL pysqlformatter-0.0.7.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
775e8bd762679b27caf667d2578dc2f41061d64469bfe26603576754478e6b97
BLAKE2b-256 checksum
How to use checksums
0e254e1d98486f2172f59eb3815b1c7240849f715c2aecab50b4f1b7c975134b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

Release files / pysqlformatter-0.0.7-py3-none-any.whl

Download URL pysqlformatter-0.0.7-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee427e3cb0595f8287b489d33563cf90b68d9e675385bcb12f1207644a2ecf84
BLAKE2b-256 checksum
How to use checksums
a5bd787c5b2972c0a2d8fa135f505156f6899e324e1ee19d179d8b526691b4a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

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