- Standard library
- let my_func = param1 param2 … ->
body_expr - ```
- Where:
- *
my_func
is the name of the function - *
param1
is the first parameter optionally followed by a type in “< … >” - *
param2
etc. follow the same pattern as param1 - *
<return_type>
is the type of result wrapped in “< … >” - *
body_expr
defines the function body that creates the result. - It can be PRQL code or
internal ...
to indicate internal compiler code. - Operators
- Types
- TODO: an array of anything, not just nulls
- Functions
- These return either a scalar when used within
aggregate
, or a column when used anywhere else. - Counts number of items in the column.
- Note that the count will include null values.
- Deprecated in favour of filterning input to the [std.count] function (not yet implemented).
Standard library
The standard library currently contains commonly used functions that are used in SQL. It’s not yet as broad as we’d like, and we’re very open to expanding it.
Currently s-strings are an escape-hatch for any function that isn’t in our standard library. If we find ourselves using them for something frequently, raise an issue and we’ll add it to the stdlib.
Here’s the source of the current PRQL std:
Note
PRQL 0.9.0 has started supporting different DB implementations for standard library functions. The source is the std.sql.
# The PRQL standard library defines the following functions and transforms.
# The definitions are whitespace insensitive, and have this form:
#
#
let my_func = param1 param2 … -> body_expr
```
#
Where:
* my_func
is the name of the function
* param1
is the first parameter optionally followed by a type in “< … >”
* param2
etc. follow the same pattern as param1
* <return_type>
is the type of result wrapped in “< … >”
* body_expr
defines the function body that creates the result.
It can be PRQL code or internal ...
to indicate internal compiler code.
Operators
let mul = left right ->
let neg = expr
Types
Type primitives
type int
type float
type bool
type text
type date
type time
type timestamp
type func
Generic array
TODO: an array of anything, not just nulls
type array = [null]
Scalar
type scalar = int || float || bool || text || date || time || timestamp || null type tuple_of_scalars = {scalar..}
Relation (an array of tuples)
type relation = [tuple_of_scalars]
Transform
type transform = (func relation -> relation)
Functions
Relational transforms
let from = func
default_db.source
let select = func
columns
let filter = func
condition
let derive = func
columns
let aggregate = func
columns
let sort = func
by
let take = func
expr
let join = func
default_db.with
noresolve.side
:inner
tbl
let group = func
by
let window = func
rows:0..0
range:0..0
expanding
let append = default_db.bottom
default_db.bottom
default_db.bottom
Aggregate functions
These return either a scalar when used within aggregate
, or a column when used anywhere else.
let min = column
let max = column
let sum = column
let average = column
let stddev = column
let every = column
let any = column
let concat_array = column
Counts number of items in the column.
Note that the count will include null values.
let count = column
Deprecated in favour of filterning input to the [std.count] function (not yet implemented).
@{deprecated}
let count_distinct = column
Window functions
let lag = offset
Misc functions
let round = n_digits column -> noresolve.type
column ->
Tuple functions
let tuple_every = func list ->
Misc
let from_text = inputnoresolve.format
:csv ->
String functions
let lower = column ->
File-reading functions, primarily for DuckDB
let read_parquet = source
And a couple of examples:
#### [PRQL](#prql)
from employees
derive {
gross_salary = (salary + payroll_tax | as int),
gross_salary_rounded = (gross_salary | round 0),
time = s”NOW()”, # an s-string, given no now
function exists in PRQL
}
#### [SQL](#sql)
SELECT *, CAST(salary + payroll_tax AS int) AS gross_salary, ROUND(CAST(salary + payroll_tax AS int), 0) AS gross_salary_rounded, NOW() AS time FROM employees
Example of different implementations of division and integer division:
#### [PRQL](#prql-1)
prql target:sql.sqlite
from [{x = 13, y = 5}] select { quotient = x / y, int_quotient = x // y, }
#### [SQL](#sql-1)
WITH table_0 AS ( SELECT 13 AS x, 5 AS y ) SELECT (x 1.0 / y) AS quotient, ROUND(ABS(x / y) - 0.5) SIGN(x) * SIGN(y) AS int_quotient FROM table_0
#### [PRQL](#prql-2)
prql target:sql.mysql
from [{x = 13, y = 5}] select { quotient = x / y, int_quotient = x // y, }
#### [SQL](#sql-2)
WITH table_0 AS ( SELECT 13 AS x, 5 AS y ) SELECT (x / y) AS quotient, (x DIV y) AS int_quotient FROM table_0
```