format_sql
Introduction#
format_sql is most useful as the second step in a parse-then-reconstruct pipeline: first parse a SQL string into a structured dictionary with parse_sql, optionally inspect or transform the result, and then call format_sql to produce a clean, normalized SQL string.
Usage#
Syntax#
format_sql(parsed_sql_model)Parameters#
| Name | Type | Required | Description |
|---|---|---|---|
| parsed_sql_model | dictionary | Yes | The structured data model returned by parse_sql. |
Returns#
A string containing the SQL statement reconstructed from the provided data model.
Example#
Parse a SQL query representing a slow-query log entry and reconstruct it to verify the round-trip.
Query
print formatted = format_sql(parse_sql('SELECT id, status, uri FROM requests WHERE req_duration_ms > 1000 ORDER BY req_duration_ms DESC'))Output
{
"formatted": "select id, status, uri from requests where req_duration_ms > 1000 order by req_duration_ms desc"
}The query parses the SQL string and then reconstructs it with format_sql, confirming that the parse-and-reconstruct pipeline preserves the original statement's structure.
List of related functions#
- parse_sql: Parses a SQL statement string into a structured dictionary.
format_sqlis the inverse ofparse_sql. - parse_json: Parses a JSON string into a dynamic dictionary. Use
parse_jsonwhen your data contains JSON rather than SQL.
Other query languages#
Splunk SPL users
Splunk has no equivalent to format_sql. There is no native way in SPL to reconstruct a SQL string from a parsed representation. In APL, format_sql takes the output of parse_sql and reconstructs the original SQL statement.
Splunk example
-- No direct Splunk equivalentAPL equivalent
['sample-http-logs']
| take 1
| extend parsed = parse_sql('SELECT id, status FROM logs WHERE status = 500')
| project formatted = format_sql(parsed)ANSI SQL users
ANSI SQL has no built-in function to reconstruct a SQL string from a parsed representation. format_sql is unique to APL and works as the inverse of parse_sql.
SQL example
-- No direct SQL equivalentAPL equivalent
['sample-http-logs']
| take 1
| extend parsed = parse_sql('SELECT id, status FROM logs WHERE status = 500')
| project formatted = format_sql(parsed)