Skip to main content

sparksqlformatter

A SparkSQL formatter in Python based on sql-formatter and its fork sql-formatter-plus, with customizations and extra features.

Used by: pysqlformatter.

Installation

Install using pip

pip install sparksqlformatter

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

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

Use as command-line tool

usage: sparksqlformatter [-h] [-f FILES [FILES ...]] [-i] [--style STYLE]

Formatter for 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.
  --style STYLE         Style configurations for SparkSQL. Can be a path to a style config file or a dictionary.

Style

The --style argument specifies foramtting style. Supported language attributes can be found in style configurations.

There are two ways to specify style:

  • Path to a style config file. E.g.,
$ sparksqlformatter --style="<path_to_config_file>" -f <path_to_file1> <path_to_file2>

The style config file should have section [sparksqlformatter] and key-value pairs specifying attributes. E.g.,

[sparksqlformatter]
reservedKeywordUppercase = False
linesBetweenQueries = 2
  • Dictionary of configurations expressed as key-value pairs. E.g.,
$ sparksqlformatter --style="{'reservedKeywordUppercase': False}" -f <path_to_file1> <path_to_file2>

Use as Python library

Call sparksqlformatter.api.format_query() to format query in string:

>>> from sparksqlformatter import api
>>> query = 'select c1 from t1'
>>> api.format_query(query)
'SELECT\n    c1\nFROM\n    t0'

Call hiveql.formatter.api.format_file() to format query in file:

>>> from sparksqlformatter import api
>>> api.format_file(<path_to_file>, inPlace=False)
...

Style

Formatting style can be specified via the style parameter in the api format functions.

Similar to the command-line tool, there are two ways to create configurations when using sparksqlformatter as a Python library:

  • Path to a style config file
>>> from sparksqlformatter import api
>>> style = '<path_to_config_file>'
>>> query = 'select c1 FROM t0'
>>> api.format_query(query, style)
...
  • Dictionary
>>> from sparksqlformatter import api
>>> style = {'reservedKeywordUppercase': False}
>>> query = 'select c1 FROM t0'
>>> api.format_query(query, style)
'select\n    c1\nfrom\n    t0'

Style configurations

topLevelKeywords

A list of keywords that should start a query block when formatting. E.g.,

SELECT
    [block]
FROM
    [block]

Default to

TOP_LEVEL_KEYWORDS = [
    'ADD', 'AFTER', 'ALTER COLUMN', 'ALTER TABLE', 'CREATE TABLE', 'CROSS JOIN', 'DELETE FROM', 'EXCEPT',
    'FETCH FIRST', 'FROM', 'GROUP BY', 'GO', 'HAVING', 'INNER JOIN', 'INSERT INTO', 'INSERT', 'JOIN',
    'LEFT JOIN', 'LEFT OUTER JOIN', 'LIMIT', 'MODIFY', 'ORDER BY', 'OUTER JOIN', 'PARTITION BY', 'RIGHT JOIN',
    'RIGHT OUTER JOIN', 'SELECT', 'SET CURRENT SCHEMA', 'SET SCHEMA', 'SET', 'UPDATE', 'VALUES', 'WHERE'
]

topLevelKeywordsNoIndent

A list of top-level keywords that should not be indented when formatting. E.g., UNION in

SELECT
    ...
FROM
    ...
UNION
SELECT
    ...
FROM
    ...

Default to

TOP_LEVEL_KEYWORDS_NO_INDENT = ['INTERSECT', 'INTERSECT ALL', 'MINUS', 'UNION', 'UNION ALL']

newlineKeywords

A list of keywords that should start a newline when formatting. E.g., LEFT JOIN in

SELECT
    ...
FROM
    t0
    LEFT JOIN t1 ...
    LEFT JOIN t2 ...

Note that this is less restrictive than topLevelKeywords, since top-level keywords always start a newline. Default to

NEWLINE_KEYWORDS = [
    'AND', 'ELSE', 'LATERAL', 'ON', 'OPTIONS', 'OR', 'PARTITIONED BY', 'THEN', 'USING', 'WHEN', 'XOR'
]

stringTypes

A list of character pairs that enclose strings in the query language. Default to

['""', "''", '{}', '``']

openParens

A list of strings that behave as opening parentheses in the query language regarding block indent level. Default to

['(', '[', 'CASE']

closeParens

A list of strings that behave as closing parentheses in the query language regarding block indent level. Default to

[')', ']', 'END']

lineCommentTypes

A list of prefixes to comments in the query language. Default to

['--']

reservedKeywordUppercase

A boolean indicating whether the keywords should be converted to uppercase when formatting. Default to True.

linesBetweenQueries

An integer that specifies the number of blank lines to put between (sub-)queries when formatting. E.g., with linesBetweenQueries = 1,

WITH t0 AS (
    ...
),

t1 AS (
    ...
)

SELECT
    ...
FROM
    ...

specialWordChars

A list of characters that require special handling when formatting. Default to [].

indent

A string that specifies one indent. Default to four blanks:

'    '

inlineMaxLength

Maximum length of an inline block. Default to 120.

splitOnComma

If true, in cases where a comma separated list in GROUP BY, ORDER BY clauses is too long to fit in a line, split such that all elements are on a single line. Else, will only split at inlineMaxLength.

Metadata

Release files for sparksqlformatter 0.1.12

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

Source distribution (sdist)

Source distribution for sparksqlformatter 0.1.12
File Size Uploaded
sparksqlformatter-0.1.12.tar.gz 26.4 kB Details

Built distribution (wheel)

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

Total release size: 60.7 kB

Release files / sparksqlformatter-0.1.12.tar.gz

Download URL sparksqlformatter-0.1.12.tar.gz
Size 26.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c7d1d62fe9b87be6f204348afef1d894e13b5cf74160e2f553ee6d97fb5fff65
BLAKE2b-256 checksum
How to use checksums
12a510b79b56f3c96bdb5e61d3451b2b82982674fbb35e05d8cb95fbb10ed15d
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 / sparksqlformatter-0.1.12-py3-none-any.whl

Download URL sparksqlformatter-0.1.12-py3-none-any.whl
Size 34.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3afdf46b53be09cced21be5edf16905d67c2f30e6e5aa89e052d05e997fd18b3
BLAKE2b-256 checksum
How to use checksums
772026b6a158fec9c1c6a56de41c280c6814b65b22ec4701f4de3b768b9f5a1f
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.1.12 This release

2 release files

0.1.11

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

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