Skip to content

datastx/sqlfmt-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sqlfmt

An opinionated SQL formatter written in Rust. Ported from Python sqlfmt, optimized for Snowflake and DuckDB.

Installation

Download a prebuilt binary

Prebuilt binaries are available for Linux and macOS from GitHub Releases.

Linux (x86_64):

curl -fsSL https://github.com/datastx/sqlfmt-rust/releases/latest/download/sqlfmt-v0.1.0-x86_64-unknown-linux-musl.tar.gz \
  | tar xz
sudo mv sqlfmt-v0.1.0-x86_64-unknown-linux-musl/sqlfmt /usr/local/bin/

Linux (aarch64 / ARM64):

curl -fsSL https://github.com/datastx/sqlfmt-rust/releases/latest/download/sqlfmt-v0.1.0-aarch64-unknown-linux-musl.tar.gz \
  | tar xz
sudo mv sqlfmt-v0.1.0-aarch64-unknown-linux-musl/sqlfmt /usr/local/bin/

macOS (Apple Silicon):

curl -fsSL https://github.com/datastx/sqlfmt-rust/releases/latest/download/sqlfmt-v0.1.0-aarch64-apple-darwin.tar.gz \
  | tar xz
sudo mv sqlfmt-v0.1.0-aarch64-apple-darwin/sqlfmt /usr/local/bin/

macOS (Intel):

curl -fsSL https://github.com/datastx/sqlfmt-rust/releases/latest/download/sqlfmt-v0.1.0-x86_64-apple-darwin.tar.gz \
  | tar xz
sudo mv sqlfmt-v0.1.0-x86_64-apple-darwin/sqlfmt /usr/local/bin/

Verify the download

Each release includes a checksums-sha256.txt file. After downloading, verify:

sha256sum -c checksums-sha256.txt

Build from source

Requires Rust 1.70+.

git clone https://github.com/datastx/sqlfmt-rust.git
cd sqlfmt-rust
cargo install --path .

Usage

Format files or directories in place:

sqlfmt .
sqlfmt queries/
sqlfmt path/to/query.sql

Read from stdin, write to stdout:

echo "SELECT   a,b FROM t WHERE x=1" | sqlfmt -

Check formatting without modifying files (exit code 1 if changes needed):

sqlfmt --check .

Show a diff of what would change:

sqlfmt --diff .

Options

Usage: sqlfmt [OPTIONS] <FILES>...

Arguments:
  <FILES>...  Files or directories to format. Use "-" to read from stdin

Options:
  -l, --line-length <N>       Maximum line length [default: 88]
  -d, --dialect <DIALECT>     SQL dialect: polyglot, duckdb, clickhouse [default: polyglot]
      --check                 Check formatting without writing changes
      --diff                  Show formatting diff
      --fast                  Skip safety equivalence check (faster)
      --no-jinjafmt           Disable Jinja template formatting
      --exclude <PATTERN>     Glob patterns to exclude
  -t, --threads <N>           Number of threads (0 = all cores) [default: 0]
      --single-process        Disable multi-threaded processing
  -v, --verbose               Verbose output
  -q, --quiet                 Quiet output (errors only)
      --no-progressbar        Disable progress bar
      --config <PATH>         Path to config file (pyproject.toml or sqlfmt.toml)
  -h, --help                  Print help
  -V, --version               Print version

Environment variables

You can set environment variables to enable performance options without passing flags on every invocation:

Variable Equivalent flag Description
SQLFMT_FAST=1 --fast Skip the safety equivalence check for faster formatting
SQLFMT_THREADS=N --threads N Number of parallel threads (0 = all cores)

Accepted values for SQLFMT_FAST: 1, true, yes (case-insensitive). CLI flags always take precedence over environment variables.

# Format a large directory as fast as possible
export SQLFMT_FAST=1
export SQLFMT_THREADS=8
sqlfmt .

Configuration file

sqlfmt reads settings from sqlfmt.toml or the [tool.sqlfmt] section of pyproject.toml:

# sqlfmt.toml
line_length = 100
dialect = "duckdb"
exclude = ["migrations/**"]
threads = 4

Supported platforms

Platform Architecture Binary target
Linux x86_64 x86_64-unknown-linux-musl
Linux aarch64 aarch64-unknown-linux-musl
macOS (Apple Silicon) aarch64 aarch64-apple-darwin
macOS (Intel) x86_64 x86_64-apple-darwin

Linux binaries are statically linked (musl) and have no runtime dependencies.

License

Apache-2.0

About

a faster sqlfmt but in rust

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •