prequel

An interpreted relational query language that compiles to SQL


Keywords
data-science, database, python, query, sql
License
0BSD
Install
pip install prequel==0.1.8

Documentation

alt text

Preql is an interpreted, relational programming language, that specializes in database queries.

It is designed for use by data engineers, analysts and data scientists.

Preql's main objective is to provide an alternative to SQL, in the form of a high-level programming language, with first-class functions, modules, strict typing, and Python integration.

How does it work?

Preql code is interpreted and gets compiled to SQL at runtime. This way, Preql gains the performance and abilities of SQL, but can also operate as a normal scripting language.

Currently supported dialects are:

  • Postgres
  • MySQL
  • Sqlite
  • BigQuery
  • More... (planned)

For features that are database-specific, or aren't implemented in Preql, there is a SQL() function that provides a convenient escape hatch to write raw SQL code.

Main Features

  • Modern syntax and semantics
    • Interpreted, everything is an object
    • Strong type system with gradual type validation and duck-typing
  • Compiles to SQL
  • Python and Pandas integration
  • Interactive shell (REPL) with auto-completion
  • Runs on Jupyter Notebook

Note: Preql is still work in progress, and isn't yet recommended for use in production.

tests

Learn More

Get started

Simply install via pip:

    pip install -U preql

Then just run the interpreter:

    preql

Requires Python 3.6+

Read more

Quick Example

// Declare a new table
table Continent {
    name: string
    area: int       // kmΒ²
    population: int
}

// Initialize the table, by inserting rows
new Continent("Africa", 30370000, 1287920000)
new Continent("Antarctica", 14000000, 4490)
new Continent("Asia", 44579000, 4545133000)
new Continent("Europe", 10180000, 742648000)
new Continent("North America", 24709000, 587615000)
new Continent("South America", 17840000, 428240000)
new Continent("Australia", 8600000, 41261000)

// Print the continents, ordered by density
print Continent {
    ...                         // Include existing fields
    density: population / area  // Create new a field

} order{^density}

// Print the total land area
print "Total land area:", sum(Continent{area}), "kmΒ²"

//  ========================= Output ==========================

                              table  =7
┏━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┓
┃ id ┃ name          ┃     area ┃ population ┃               density ┃
┑━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━┩
β”‚  3 β”‚ Asia          β”‚ 44579000 β”‚ 4545133000 β”‚     101.9568182328002 β”‚
β”‚  4 β”‚ Europe        β”‚ 10180000 β”‚  742648000 β”‚      72.9516699410609 β”‚
β”‚  1 β”‚ Africa        β”‚ 30370000 β”‚ 1287920000 β”‚     42.40763911755021 β”‚
β”‚  6 β”‚ South America β”‚ 17840000 β”‚  428240000 β”‚    24.004484304932735 β”‚
β”‚  5 β”‚ North America β”‚ 24709000 β”‚  587615000 β”‚    23.781415678497712 β”‚
β”‚  7 β”‚ Australia     β”‚  8600000 β”‚   41261000 β”‚     4.797790697674419 β”‚
β”‚  2 β”‚ Antarctica    β”‚ 14000000 β”‚       4490 β”‚ 0.0003207142857142857 β”‚
β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Total land area: 150278000 kmΒ²

In the background, this table was generated by executing the following compiled SQL code (reformatted):

-- Continent {..., density: population / area} order{ ^density }
WITH subq_1(id, name, area, population, density) AS (
    SELECT id, name, area, population, (CAST(population AS float) / area) AS density
    FROM Continent
    ORDER BY density DESC)
SELECT * FROM subq_1

See more examples in the examples folder.

Interactive Environment

Screenshot.png

License

Preql uses an β€œInterface-Protection Clause” on top of the MIT license.

See: LICENSE

In simple words, it's free for personal use. Also, it can be used for any commercial or non-commercial purpose, as long as your product doesn't base its value on exposing the Preql language itself to your users. Read more