- 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).
- Mathematical functions
- Deprecated, will be removed in 0.12.0
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 = int
type float = float
type bool = bool
type text = text
type date = date
type time = time
type timestamp = timestamp
type func
= func
type anytype = anytype
Generic array
TODO: an array of anything, not just nulls
type array = [anytype]
Scalar
type scalar = int || float || bool || text || date || time || timestamp || null type tuple = {..anytype}
Range
type range = {start = scalar, end = scalar}
Relation (an array of tuples)
type relation = [tuple]
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..-1
range:0..-1
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 all = 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
Mathematical functions
module math {
let abs = column ->
Misc functions
let as = noresolve.type
column ->
Tuple functions
let tuple_every = func list ->
Misc
let from_text = inputnoresolve.format
:csv ->
Text functions
module text {
let lower = column ->
Date functions
module date {
let to_text = format
File-reading functions, primarily for DuckDB
let read_parquet = source
PRQL compiler functions
module prql
{
let version = ->
Deprecated, will be removed in 0.12.0
let prql_version = ->
And a couple of examples:
#### [PRQL](#prql)
from employees
derive {
gross_salary = (salary + payroll_tax | as int),
gross_salary_rounded = (gross_salary | math.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
```