Reformat SQL Queries for Enhanced Clarity

The cockroach sqlfmtcommand changes the textual formatting ofone or more SQL queries. It recognizes all SQL extensions supported byCockroachDB.

A web interface to this feature is also available.

Warning:

This is an experimental feature. The interface and output are subject to change.

Synopsis

  1. # Use the query formatter interactively:
  2. $ cockroach sqlfmt <flags>
  3. <sql stmt>
  4. CTRL+D
  5. # Reformat a SQL query given on the command line:
  6. $ cockroach sqlfmt <flags> -e "<sql stmt>"
  7. # Reformat a SQL query already stored in a file:
  8. $ cat query.sql | cockroach sqlfmt <flags>

Flags

The sqlfmt command supports the following flags.

FlagDescriptionDefault value
—execute-eReformat the given SQL query, without reading from standard input.N/A
—print-widthDesired column width of the output.80
—tab-widthNumber of spaces occupied by a tab character on the final display device.4
—use-spacesAlways use space characters for formatting; avoid tab characters.Use tabs.
—alignUse vertical alignment during formatting.Do not align vertically.
—no-simplifyAvoid removing optional grouping parentheses during formatting.Remove unnecessary grouping parentheses.

Examples

Reformat a query with constrained column width

Using the interactive query formatter, output with the default column width (80 columns):

  • Start the interactive query formatter:
  1. $ cockroach sqlfmt
  • Press Enter.

  • Run the query:

  1. > CREATE TABLE animals (id INT PRIMARY KEY DEFAULT unique_rowid(), name STRING);
  • Press CTRL+D.
  1. CREATE TABLE animals (
  2. id INT PRIMARY KEY DEFAULT unique_rowid(),
  3. name STRING
  4. )

Using the command line, output with the column width set to 40:

  1. $ cockroach sqlfmt --print-width 40 -e "CREATE TABLE animals (id INT PRIMARY KEY DEFAULT unique_rowid(), name STRING);"
  1. CREATE TABLE animals (
  2. id
  3. INT
  4. PRIMARY KEY
  5. DEFAULT unique_rowid(),
  6. name STRING
  7. )

Reformat a query with vertical alignment

Output with the default vertical alignment:

  1. $ cockroach sqlfmt -e "SELECT winner, round(length / (60 * 5)) AS counter FROM players WHERE build = $1 AND (hero = $2 OR region = $3);"
  1. SELECT
  2. winner, round(length / (60 * 5)) AS counter
  3. FROM
  4. players
  5. WHERE
  6. build = $1 AND (hero = $2 OR region = $3)

Output with vertical alignment:

  1. $ cockroach sqlfmt --align -e "SELECT winner, round(length / (60 * 5)) AS counter FROM players WHERE build = $1 AND (hero = $2 OR region = $3);"
  1. SELECT winner, round(length / (60 * 5)) AS counter
  2. FROM players
  3. WHERE build = $1 AND (hero = $2 OR region = $3);

Reformat a query with simplification of parentheses

Output with the default simplification of parentheses:

  1. $ cockroach sqlfmt -e "SELECT (1 * 2) + 3, (1 + 2) * 3;"
  1. SELECT 1 * 2 + 3, (1 + 2) * 3

Output with no simplification of parentheses:

  1. $ cockroach sqlfmt --no-simplify -e "SELECT (1 * 2) + 3, (1 + 2) * 3;"
  1. SELECT (1 * 2) + 3, (1 + 2) * 3

See also

Was this page helpful?
YesNo