Prql

PRQL (Pipelined Relational Query Language) compiler for Elixir, powered by Rust's prqlc.

Features

Installation

Add prql to your list of dependencies in mix.exs:

def deps do
  [
    {:prql, "~> 0.1.0"}
  ]
end

Usage

# Basic usage
{:ok, sql} = Prql.compile("from employees | select {name, age}")
#=> {:ok, "SELECT name, age FROM employees"}

# With options
{:ok, sql} = Prql.compile("from employees | select {name, age}", 
  format: true, 
  target: :postgres
)

# Error handling
{:error, reason} = Prql.compile("invalid prql")
#=> {:error, "PRQL compilation error (unexpected token)"}

# Format PRQL code
{:ok, formatted} = Prql.format("from employees | select {name, age}")
#=> {:ok, "from employees\nselect {name, age}"}

# Raises on error
formatted = Prql.format!("from employees | select {name, age}")

# Handles invalid PRQL
{:error, reason} = Prql.format("invalid prql")

Formatting PRQL

You can format PRQL code using the format/1 and format!/1 functions. These functions take a PRQL query string and return a formatted version of the query with consistent indentation and line breaks.

Options

Error Handling

PRQL compilation errors include helpful error messages with hints. For example:

{:error, reason} = Prql.compile("from employees | select {name,}")
#=> {:error, "Unexpected token (Expected an identifier after .)"}

Development

Building the NIF

The NIF is built automatically when compiling the Elixir code. For development, you can build it manually:

cd native/prql_native
cargo build

Running Tests

mix test

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.