format_sql

This page explains how to use the format_sql function in APL.

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.

Usage

Syntax

format_sql(parsed_sql_model)

Parameters

NameTypeRequiredDescription
parsed_sql_modeldictionaryYesThe 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.

Other query languages