Skip to main content

Module formatting

Module formatting 

Source
Expand description

SQL formatting and structural complexity scoring.

Mirrors kumosql.formatting. format_sql lays BigQuery SQL out again from its tokens: keywords, built-in function names, literals and types take the chosen case, clauses start new lines, subqueries and blocks are indented, and every comment is kept in order. Formatting only ever changes whitespace and case: the result is lexed again and compared token for token (and parsed again and compared tree for tree) before it is returned, and anything that would change is left as written. Dataform SQLX is never formatted.

complexity scores a query’s structure from its syntax tree with the same weights and bands as the reference.

Structs§

Complexity
A structural complexity score with its breakdown.
ComplexityMetrics
The counts a complexity score is made of.
FormatDiagnostic
A note about one formatting run (line is -1 for the whole text).
FormatOutcome
What format_with_diagnostics did.
FormatPreferences
Formatting preferences (the reference’s sqlfluff settings).
FormatRule
One formatting rule this formatter applies.
PipelineComplexity
The complexity of a pipeline of tables.

Enums§

CommaPosition
Where a list’s commas go.
ComplexityBand
How complex a score is: < 10 low, < 25 moderate, < 50 high, otherwise very high.
IndentUnit
Indentation with spaces or tabs.
KeywordCase
The case keywords, function names, literals and types take.

Functions§

complexity
Score a query’s structural complexity: a weighted sum of joins (comma joins included), CTEs, subqueries, set operations, CASE expressions (IF included), window functions, AND/OR predicates and maximum SELECT nesting. Bands: < 10 low, < 25 moderate, < 50 high, otherwise very high. SQL that does not parse, and Dataform SQLX, are errors.
format_rules
Every rule this formatter applies, with its code, name and groups.
format_sql
Format BigQuery SQL. Text with no statement that parses, and Dataform SQLX, are errors.
format_statements
Format sql and return it with the number of statements left as written (ones that do not parse).
format_with_diagnostics
Format like the reference’s format_sql rule: never fails, explains what it left alone.
parse_preferences
Validate JSON-shaped preferences; missing keys keep their defaults.
pipeline_complexity
complexity summed over a pipeline’s queries, plus one per table so a pass-through table counts.