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.

Note

Currently the stdlib implementation doesn’t support different DB implementations itself; those need to be built deeper into the compiler. We’ll resolve this at some point. Until then, we’ll only add functions here that are broadly supported by most DBs.

Here’s the source of the current PRQL std:

  1. # Aggregate Functions
  2. func min <scalar || column> column -> null
  3. func max <scalar || column> column -> null
  4. func sum <scalar || column> column -> null
  5. func avg <scalar || column> column -> null
  6. func stddev <scalar || column> column -> null
  7. func average <scalar || column> column -> null
  8. func count <scalar || column> non_null:s"*" -> null
  9. # TODO: Possibly make this into `count distinct:true` (or like `distinct:` as an
  10. # abbreviation of that?)
  11. func count_distinct <scalar || column> column -> null
  12. # Window functions
  13. func lag<column> offset column -> null
  14. func lead<column> offset column -> null
  15. func first<column> offset column -> null
  16. func last<column> offset column -> null
  17. func rank<column> -> null
  18. func rank_dense<column> -> null
  19. func row_number<column> -> null
  20. # Other functions
  21. func round<scalar> n_digits column -> null
  22. func as<scalar> `noresolve.type` column -> null
  23. func in<bool> pattern value -> null
  24. # Transform type definitions
  25. func from<table> `default_db.source`<table> -> null
  26. func select<table> columns<column> tbl<table> -> null
  27. func filter<table> condition<bool> tbl<table> -> null
  28. func derive<table> columns<column> tbl<table> -> null
  29. func aggregate<table> a<column> tbl<table> -> null
  30. func sort<table> by tbl<table> -> null
  31. func take<table> expr tbl<table> -> null
  32. func join<table> `default_db.with`<table> filter `noresolve.side`:inner tbl<table> -> null
  33. func group<table> by pipeline tbl<table> -> null
  34. func window<table> rows:0..0 range:0..0 expanding:false rolling:0 pipeline tbl<table> -> null
  35. func append<table> `default_db.bottom`<table> top<table> -> null
  36. func intersect<table> `default_db.bottom`<table> top<table> -> (
  37. from t = _param.top
  38. join b = _param.bottom (all (map _eq (zip t.* b.*)))
  39. select t.*
  40. )
  41. func remove<table> `default_db.bottom`<table> top<table> -> (
  42. from t = _param.top
  43. join side:left b = _param.bottom (all (map _eq (zip t.* b.*)))
  44. filter (all (map _is_null b.*))
  45. select t.*
  46. )
  47. func loop<table> pipeline top<table> -> null
  48. # List functions
  49. func all<bool> list<list> -> null
  50. func map<list> fn list<list> -> null
  51. func zip<list> a<list> b<list> -> null
  52. func _eq<list> a<list> -> null
  53. func _is_null a -> _param.a == null
  54. # Misc
  55. func from_text<table> input<text> `noresolve.format`:csv -> null
  56. # String functions
  57. func lower <text> column -> null
  58. func upper <text> column -> null
  59. # type primitives
  60. type int
  61. type float
  62. type bool
  63. type text
  64. type date
  65. type time
  66. type timestamp
  67. type table
  68. type column
  69. type list
  70. type scalar
  71. # Source-reading functions, primarily for DuckDB
  72. func read_parquet<table> source<text> -> s"SELECT * FROM read_parquet({source})"
  73. func read_csv<table> source<text> -> s"SELECT * FROM read_csv_auto({source})"

And a couple of examples:

PRQL

  1. from employees
  2. derive [
  3. gross_salary = (salary + payroll_tax | as int),
  4. gross_salary_rounded = (gross_salary | round 0),
  5. time = s"NOW()", # an s-string, given no `now` function exists in PRQL
  6. ]

SQL

  1. SELECT
  2. *,
  3. CAST(salary + payroll_tax AS int) AS gross_salary,
  4. ROUND(CAST(salary + payroll_tax AS int), 0) AS gross_salary_rounded,
  5. NOW() AS time
  6. FROM
  7. employees