SQLFormat  type: sql_format#

class lumen.transforms.sql.SQLFormat(*, parameters, comments, error_level, identify, optimize, pretty, read, unsupported_level, write, controls, name)#

Format SQL expressions with parameterized replacements.

This transform allows for replacing placeholders in SQL queries using either Python string format-style placeholders {name} or sqlglot-style placeholders :name.

Parameters:
  • sql_expr (str) – The SQL expression template with placeholders to be formatted.

  • parameters (dict) – Dictionary of parameter names and values to replace in the SQL template.

  • py_format (bool) – Whether to convert Python format-style placeholders {name} to sqlglot placeholders :name before parsing. Default is True.


Parameters#

comments

type: bool
default: False
Whether to include comments in the output SQL

error_level

type: sqlglot.ErrorLevel
default: <ErrorLevel.RAISE: 'RAISE'>
Error level for parsing

identify

type: bool
default: False
Delimit all identifiers, e.g. turn FROM database.table into FROM "database"."table".This is useful for dialects that don’t support unquoted identifiers.

optimize

type: bool
default: False
Whether to optimize the generated SQL query; may produce invalid results, especially withduckdb’s read_* functions.

parameters

type: dict
default: {}
Dictionary of parameter names and values to replace in the SQL template.

pretty

type: bool
default: False
Prettify output SQL, i.e. add newlines and indentation

read

type: str
default: None
Source dialect for parsing; if None, automatically detects

unsupported_level

type: sqlglot.ErrorLevel
default: <ErrorLevel.WARN: 'WARN'>
When using to_sql, how to handle unsupported dialect features.

write

type: str
default: None
Target dialect for output; if None, defaults to read dialect


Methods#

SQLFormat.apply(sql_in: str) str#

Apply the formatting to the input SQL, replacing placeholders with values.

Parameters:

sql_in (str) – The input SQL query to format. This is used as a base query that will have the formatted sql_expr applied to it, typically as a subquery.

Returns:

The formatted SQL query with all placeholders replaced.

Return type:

str

SQLFormat.parse_sql(sql_in: str) Expression#

Parse SQL string into sqlglot AST.

Parameters:

sql_in (string) – SQL string to parse

Returns:

Parsed SQL expression

Return type:

sqlglot.Expression

SQLFormat.to_spec(context: dict[str, Any] | None = None) dict[str, Any]#

Exports the full specification to reconstruct this component.

Return type:

Resolved and instantiated Component object

SQLFormat.to_sql(expression: Expression) str#

Convert sqlglot expression back to SQL string.

Parameters:

expression (sqlglot.Expression) – Expression to convert to SQL

Returns:

SQL string representation

Return type:

string