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 ... -> <return_type> 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 -> <int || float> internal std.mul
let div_i = left right -> <int || float> internal std.div_i
let div_f = left right -> <int || float> internal std.div_f
let mod = left right -> <int || float> internal std.mod
let add = left<int || float || timestamp || date> right<int || float || timestamp || date> -> <int || float || timestamp || date> internal std.add
let sub = left<int || float || timestamp || date> right<int || float || timestamp || date> -> <int || float || timestamp || date> internal std.sub
let eq = left right -> <bool> internal std.eq
let ne = left right -> <bool> internal std.ne
let gt = left right -> <bool> internal std.gt
let lt = left right -> <bool> internal std.lt
let gte = left right -> <bool> internal std.gte
let lte = left right -> <bool> internal std.lte
let and = left<bool> right<bool> -> <bool> internal std.and
let or = left<bool> right<bool> -> <bool> internal std.or
let coalesce = left right -> internal std.coalesce
let regex_search = text pattern -> <bool> internal std.regex_search
let neg = expr<int || float> -> <int || float> internal std.neg
let not = expr<bool> -> <bool> internal std.not
# Types
## Type primitives
type int
type float
type bool
type text
type date
type time
type timestamp
type `func`
type 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` <relation>
-> <relation> internal from
let select = func
columns <scalar || tuple>
tbl <relation>
-> <relation> internal select
let filter = func
condition <bool>
tbl <relation>
-> <relation> internal filter
let derive = func
columns <scalar || tuple>
tbl <relation>
-> <relation> internal derive
let aggregate = func
columns <scalar || tuple>
tbl <relation>
-> <relation> internal aggregate
let sort = func
by <scalar || tuple>
tbl <relation>
-> <relation> internal sort
let take = func
expr <anytype>
tbl <relation>
-> <relation> internal take
let join = func
`default_db.with` <relation>
condition <bool>
`noresolve.side`:inner
tbl <relation>
-> <relation> internal join
let group = func
by<scalar || tuple>
pipeline <transform>
tbl <relation>
-> <relation> internal group
let window = func
rows:0..0
range:0..0
expanding <bool>:false
rolling <int>:0
pipeline <transform>
tbl <relation>
-> <relation> internal window
let append = `default_db.bottom`<relation> top<relation> -> <relation> internal append
let intersect = `default_db.bottom`<relation> top<relation> -> <relation> (
t = top
join (b = bottom) (tuple_every (tuple_map _eq (tuple_zip t.* b.*)))
select t.*
)
let remove = `default_db.bottom`<relation> top<relation> -> <relation> (
t = top
join side:left (b = bottom) (tuple_every (tuple_map _eq (tuple_zip t.* b.*)))
filter (tuple_every (tuple_map _is_null b.*))
select t.*
)
let loop = func
pipeline <transform>
top <relation>
-> <relation> internal loop
## Aggregate functions
# These return either a scalar when used within `aggregate`, or a column when used anywhere else.
let min = column <array> -> <int || float || null> internal std.min
let max = column <array> -> <int || float || null> internal std.max
let sum = column <array> -> <int || float> internal std.sum
let average = column <array> -> <float || null> internal std.average
let stddev = column <array> -> <float || null> internal std.stddev
let all = column <array> -> <bool> internal std.all
let any = column <array> -> <bool> internal std.any
let concat_array = column <array> -> <text> internal std.concat_array
# Counts number of items in the column.
# Note that the count will include null values.
let count = column<array> -> <int> internal std.count
# Deprecated in favour of filterning input to the [std.count] function (not yet implemented).
@{deprecated}
let count_distinct = column <array> -> internal std.count_distinct
## Window functions
let lag = offset <int> column <array> -> internal std.lag
let lead = offset <int> column <array> -> internal std.lead
let first = column <array> -> internal std.first
let last = column <array> -> internal std.last
let rank = column <array> -> internal std.rank
let rank_dense = column <array> -> internal std.rank_dense
let row_number = column <array> -> internal std.row_number
## Misc functions
let round = n_digits column -> <scalar> internal std.round
let as = `noresolve.type` column -> <scalar> internal std.as
let in = pattern value -> <bool> internal in
## Tuple functions
let tuple_every = func list -> <bool> internal tuple_every
let tuple_map = func fn <func> list -> internal tuple_map
let tuple_zip = func a b -> internal tuple_zip
let _eq = func a -> internal _eq
let _is_null = func a -> _param.a == null
## Misc
let from_text = input<text> `noresolve.format`:csv -> <relation> internal from_text
## String functions
let lower = column -> <text> internal std.lower
let upper = column -> <text> internal std.upper
## File-reading functions, primarily for DuckDB
let read_parquet = source<text> -> <relation> internal std.read_parquet
let read_csv = source<text> -> <relation> internal std.read_csv
## PRQL compiler functions
let prql_version = -> <text> internal prql_version
And a couple of examples:
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
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 target:sql.sqlite
from [{x = 13, y = 5}]
select {
quotient = x / y,
int_quotient = x // y,
}
SQL
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 target:sql.mysql
from [{x = 13, y = 5}]
select {
quotient = x / y,
int_quotient = x // y,
}
SQL
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