Crate prql_compiler

source ·
Expand description

Compiler for PRQL language. Targets SQL and exposes PL and RQ abstract syntax trees.

You probably want to start with compile wrapper function.

For more granular access, refer to this diagram:

           PRQL

   (parse) │ ▲
prql_to_pl │ │ pl_to_prql
           │ │
           ▼ │      json::from_pl
                  ────────►
          PL AST            PL JSON
                  ◄────────
           │        json::to_pl
           │
 (resolve) │
  pl_to_rq │
           │
           │
           ▼        json::from_rq
                  ────────►
          RQ AST            RQ JSON
                  ◄────────
           │        json::to_rq
           │
 rq_to_sql │
           ▼

           SQL

Common use-cases

I want to …

  • … compile PRQL queries at run time, because I cannot commit them into the source tree.

    let sql = prql_compiler::compile(
        "from albums | select [title, artist_id]",
         &prql_compiler::Options::default().no_format()
    )?;
    assert_eq!(&sql[..35], "SELECT title, artist_id FROM albums");
  • … compile PRQL queries to SQL at build time.

    Use prql-compiler-macros crate (unreleased), which can be used like this:

    let sql: &str = prql_to_sql!("from albums | select [title, artist_id]");
  • … compile .prql files to .sql files at build time.

    Call this crate from build.rs. See this example project.

  • … compile, format & debug PRQL from command line.

    $ cargo install prqlc
    $ prqlc compile query.prql
    

Modules

  • Abstract Syntax Tree
  • JSON serialization and deserialization functions
  • Semantic resolver (name resolution, type checking and lowering to RQ)
  • Backend for translating RQ into SQL

Structs

Enums

Statics

Functions