#prql #sql #language #pipeline #compile #compiler #replace

bin+lib prqlc

PRQL is a modern language for transforming data — a simple, powerful, pipelined SQL replacement

23 unstable releases (8 breaking)

new 0.12.2 Jun 10, 2024
0.11.3 Feb 11, 2024
0.11.1 Dec 26, 2023
0.10.1 Nov 14, 2023
0.6.1 Mar 13, 2023

#50 in Database interfaces

Download history 472/week @ 2024-02-23 538/week @ 2024-03-01 209/week @ 2024-03-08 2075/week @ 2024-03-15 2614/week @ 2024-03-22 2313/week @ 2024-03-29 1867/week @ 2024-04-05 1518/week @ 2024-04-12 1891/week @ 2024-04-19 1689/week @ 2024-04-26 1820/week @ 2024-05-03 3541/week @ 2024-05-10 2551/week @ 2024-05-17 2114/week @ 2024-05-24 2168/week @ 2024-05-31 1957/week @ 2024-06-07

9,311 downloads per month
Used in 7 crates (5 directly)

Apache-2.0

1MB
21K SLoC

PRQL compiler

prqlc is the reference implementation of a compiler from PRQL to SQL, written in Rust. It also serves as the CLI.

For more on PRQL, check out the PRQL website or the PRQL repo.

CLI

prqlc serves as a CLI for the PRQL compiler. It is a single, dependency-free binary that compiles PRQL into SQL.

Usage

prqlc compile

This command works as a filter that compiles a PRQL string into an SQL string.

$ echo 'from employees | filter has_dog | select salary' | prqlc compile

SELECT
  salary
FROM
  employees
WHERE
  has_dog

A PRQL query can be executed with CLI tools compatible with SQL, such as DuckDB CLI.

$ curl -fsL https://raw.githubusercontent.com/PRQL/prql/0.8.1/prql-compiler/tests/integration/data/chinook/albums.csv -o albums.csv
$ echo 'from `albums.csv` | take 3' | prqlc compile | duckdb
┌──────────┬───────────────────────────────────────┬───────────┐
 album_id │                 title                 │ artist_id │
  int64   │                varchar                │   int64   │
├──────────┼───────────────────────────────────────┼───────────┤
        1 │ For Those About To Rock We Salute You │         1 │
        2 │ Balls to the Wall                     │         2 │
        3 │ Restless and Wild                     │         2 │
└──────────┴───────────────────────────────────────┴───────────┘

Executing this command without any argument will start interactive mode, allowing a PRQL query to be written interactively. In this mode, after writing PRQL and press Ctrl-d (Linux, macOS) or Ctrl-z (Windows) to display the compiled SQL.

prqlc compile

Just like when using it as a filter, SQL string output can be passed to the DuckDB CLI and similar tools.

$ prqlc compile | duckdb
Enter PRQL, then press ctrl-d to compile:

from `albums.csv`
take 3
┌──────────┬───────────────────────────────────────┬───────────┐
 album_id │                 title                 │ artist_id │
  int64   │                varchar                │   int64   │
├──────────┼───────────────────────────────────────┼───────────┤
        1 │ For Those About To Rock We Salute You │         1 │
        2 │ Balls to the Wall                     │         2 │
        3 │ Restless and Wild                     │         2 │
└──────────┴───────────────────────────────────────┴───────────┘

Installation

Packaging status

via Homebrew (macOS, Linux)

brew install prqlc

via winget (Windows)

winget install prqlc

From GitHub release page

Precompiled binaries are available for Linux, macOS, and Windows on the PRQL release page.

From source

# From crates.io
cargo install prqlc
# From a local PRQL repository
cargo install --path prqlc/prqlc

Shell completions

The prqlc shell-completion command prints a shell completion script for supported shells, and saving the printed scripts to files makes for shells to load completions for each session.

Bash

For Linux:

prqlc shell-completion bash >/etc/bash_completion.d/prqlc

For macOS:

prqlc shell-completion bash >/usr/local/etc/bash_completion.d/prqlc

fish

prqlc shell-completion fish >~/.config/fish/completions/prqlc.fish

PowerShell

mkdir -Path (Split-Path -Parent $profile) -ErrorAction SilentlyContinue
prqlc shell-completion powershell >path/to/prqlc.ps1
echo 'Invoke-Expression -Command path/to/prqlc.ps1' >>$profile

zsh

prqlc shell-completion zsh >"${fpath[1]}/_prqlc"

Ensure that the following lines are present in ~/.zshrc:

autoload -U compinit
compinit -i

Helpers

Cheat sheets for prqlc are available on various websites and with various tools.

Library

For more usage examples and the library documentation, check out the prqlc documentation.

Library installation

cargo add prqlc

Examples

Compile a PRQL string to a SQLite dialect string:

// In a file src/main.rs

use prqlc::{compile, Options, DisplayOptions, Target, sql::Dialect};

let prql = "from employees | select {name, age}";
let opts = &Options {
    format: false,
    target: Target::Sql(Some(Dialect::SQLite)),
    signature_comment: false,
    display: DisplayOptions::Plain,
    ..Default::default()
};
let sql = compile(&prql, opts).unwrap();
assert_eq!("SELECT name, age FROM employees", sql);

Dependencies

~8–33MB
~506K SLoC