Introduction

The format_sql function converts the structured dictionary produced by parse_sql back into a SQL string. Use it to normalize SQL formatting, validate that a parsed query round-trips correctly, or reconstruct a SQL statement after modifying its parsed representation.

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.

For users of other query languages

If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.

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.

```sql Splunk example -- No direct Splunk 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 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 SQL example -- No direct SQL equivalent ```
['sample-http-logs']
| take 1
| extend parsed = parse_sql('SELECT id, status FROM logs WHERE status = 500')
| project formatted = format_sql(parsed)

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'))

Run in Playground

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.

  • parse_sql: Parses a SQL statement string into a structured dictionary. format_sql is the inverse of parse_sql.
  • parse_json: Parses a JSON string into a dynamic dictionary. Use parse_json when your data contains JSON rather than SQL.

Good afternoon

I'm here to help you with the docs.

I
AIBased on your context