Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Remove func keyword #3430

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,499 changes: 2,499 additions & 0 deletions crates/prql-parser/src/lib.rs

Large diffs are not rendered by default.

120 changes: 55 additions & 65 deletions prqlc/prql-compiler/src/semantic/std.prql
Original file line number Diff line number Diff line change
Expand Up @@ -60,66 +60,57 @@ type tuple = {anytype..}
type relation = [tuple]

## Transform
type transform = (func relation -> relation)
type transform = (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 <scalar>
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 from = `default_db.source` <relation>
\ -> <relation> internal from

let select =
\ columns <scalar || tuple>
\ tbl <relation>
\ -> <relation> internal select

let filter = condition <bool>
\ tbl <relation>
\ -> <relation> internal filter

let derive = columns <scalar || tuple>
\ tbl <relation>
\ -> <relation> internal derive

let aggregate = columns <scalar || tuple>
\ tbl <relation>
\ -> <relation> internal aggregate

let sort = by <scalar || tuple>
\ tbl <relation>
\ -> <relation> internal sort

let take = expr <scalar>
\ tbl <relation>
\ -> <relation> internal take

let join = `default_db.with` <relation>
\ condition <bool>
\ `noresolve.side`:inner
\ tbl <relation>
\ -> <relation> internal join

let group = by<scalar || tuple>
\ pipeline <transform>
\ tbl <relation>
\ -> <relation> internal group

let window = 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> (
Expand All @@ -133,10 +124,9 @@ let remove = `default_db.bottom`<relation> top<relation> -> <relation> (
filter (tuple_every (tuple_map _is_null b.*))
select t.*
)
let loop = func
pipeline <transform>
top <relation>
-> <relation> internal loop
let loop = 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.
Expand Down Expand Up @@ -180,11 +170,11 @@ 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
let tuple_every = list -> <bool> internal tuple_every
let tuple_map = fn <func> list -> internal tuple_map
let tuple_zip = a b -> internal tuple_zip
let _eq = a -> internal _eq
let _is_null = a -> _param.a == null

## Misc
let from_text = input<text> `noresolve.format`:csv -> <relation> internal from_text
Expand Down
2 changes: 2 additions & 0 deletions prqlc/prql-compiler/tests/sql/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,8 @@ fn test_function() {
Literal:
Integer: 42
params:
- name: func
default_value: ~
- name: return_constant
default_value: ~
named_params: []
Expand Down
1 change: 0 additions & 1 deletion prqlc/prqlc-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, std::ops::Range<usize>)>, Error
just("type"),
just("module"),
just("internal"),
just("func"),
))
.then_ignore(end_expr())
.map(|x| x.to_string())
Expand Down
4 changes: 1 addition & 3 deletions prqlc/prqlc-parser/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ pub fn type_expr() -> impl Parser<Token, Expr, Error = PError> {

let ident = ident().map(ExprKind::Ident);

let func = keyword("func").to(ExprKind::Ident(Ident::from_path(vec!["std", "func"])));

let term = literal.or(ident).or(func).map_with_span(into_expr);
let term = literal.or(ident).map_with_span(into_expr);

binary_op_parser(term, operator_or())
.delimited_by(ctrl('<'), ctrl('>'))
Expand Down
4 changes: 1 addition & 3 deletions web/book/src/reference/spec/type-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ function parameter definitions.
As shown, types can be defined by defining expressions and coercing them to set
expressions by using `< >`.

But similar to how both `func` and `let` can be used to define functions (when
we introduce lambda function syntax), let's also introduce syntactic sugar for
type definitions:
Let's also introduce syntactic sugar for type definitions:

```
# these two are equivalent
Expand Down
4 changes: 1 addition & 3 deletions web/book/src/reference/syntax/keywords.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ PRQL uses following keywords:
- **`let`** - variable definition [_more..._](../declarations/variables.md)
- **`into`** - variable definition [_more..._](../declarations/variables.md)
- **`case`** - flow control [_more..._](../syntax/case.md)
- **`type`** - type declaration
- **`func`** - explicit function declaration
[_more..._](../declarations/functions.md)
- **`type`** - type declaration [_more..._](../declarations/functions.md)
- **`module`** - used internally
- **`internal`** - used internally
- **`true`** - boolean [_more..._](./literals.md#booleans)
Expand Down
2 changes: 1 addition & 1 deletion web/book/src/reference/syntax/pipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exceptions where a line break doesn't create a pipeline:
- within a tuple
- within an array
- when the following line is a new statement, which starts with a keyword of
`func`, `let` or `from`
`let` or `from`
- Within a [line wrap](./operators.md#wrapping-lines)

```prql
Expand Down
8 changes: 4 additions & 4 deletions web/website/content/posts/2022-05-19-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ Here's the same query with PRQL:
```prql
prql version:0.3 db:snowflake # PRQL version & database name.

func excess x -> (x - interest_rate) / 252 # Functions are clean and simple.
func if_valid x -> is_valid_price ? x : null
func lag_day x -> group sec_id ( # `group` is used for window partitions too
let excess = x -> (x - interest_rate) / 252 # Functions are clean and simple.
let if_valid = x -> is_valid_price ? x : null
let lag_day = x -> group sec_id ( # `group` is used for window partitions too
sort date
window ( # `window` runs a pipeline over each window
lag 1 x # `lag 1 x` lags the `x` col by 1
)
)

func ret x -> x / (x | lag_day) - 1 + dividend_return
let ret = x -> x / (x | lag_day) - 1 + dividend_return

from prices
join interest_rates (==date)
Expand Down