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: add more window functions #534

Merged
merged 15 commits into from
Sep 8, 2023
Merged
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
138 changes: 138 additions & 0 deletions extensions/functions_arithmetic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1654,3 +1654,141 @@ window_functions:
decomposable: NONE
return: i64?
window_type: PARTITION
- name: "first_value"
description: >
Returns the first value in the window.
impls:
- args:
- value: any1
name: expression
nullability: DECLARED_OUTPUT
decomposable: NONE
return: any1
window_type: PARTITION
- name: "last_value"
description: >
Returns the last value in the window.
impls:
- args:
- value: any1
name: expression
nullability: DECLARED_OUTPUT
decomposable: NONE
return: any1
window_type: PARTITION
- name: "nth_value"
description: >
Returns a value from the nth row based on the `window_offset`. `window_offset` should
be a positive integer. If the value of the `window_offset` is outside the range
of the window, `null` is returned.

The `on_domain_error` option governs behavior in cases where `window_offset` is not
a positive integer or `null`.
impls:
- args:
- value: any1
name: expression
- value: i32
name: window_offset
options:
on_domain_error:
values: [ NAN, ERROR ]
nullability: DECLARED_OUTPUT
decomposable: NONE
return: any1?
window_type: PARTITION
- name: "lead"
description: >
Return a value from a following row based on a specified physical offset.
This allows you to compare a value in the current row against a following row.

The `expression` is evaluated against a row that comes after the current row based
on the `row_offset`. The `row_offset` should be a positive integer and is set to
1 if not specified explicitly. If the `row_offset` is negative, the expression
will be evaluated against a row coming before the current row, similar to the `lag`
function. A `row_offset` of `null` will return `null`. The function returns the
`default` input value if `row_offset` goes beyond the scope of the window.
If a `default` value is not specified, it is set to `null`.

Example comparing the sales of the current year to the following year.
`row_offset` of 1.
| year | sales | next_year_sales |
| 2019 | 20.50 | 30.00 |
| 2020 | 30.00 | 45.99 |
| 2021 | 45.99 | null |
impls:
- args:
- value: any1
name: expression
nullability: DECLARED_OUTPUT
decomposable: NONE
return: any1?
window_type: PARTITION
- args:
- value: any1
name: expression
- value: i32
name: row_offset
nullability: DECLARED_OUTPUT
decomposable: NONE
return: any1?
window_type: PARTITION
- args:
- value: any1
name: expression
- value: i32
name: row_offset
- value: any1
name: default
nullability: DECLARED_OUTPUT
decomposable: NONE
return: any1?
window_type: PARTITION
- name: "lag"
description: >
Return a column value from a previous row based on a specified physical offset.
This allows you to compare a value in the current row against a previous row.

The `expression` is evaluated against a row that comes before the current row based
on the `row_offset`. The `expression` can be a column, expression or subquery that
evaluates to a single value. The `row_offset` should be a positive integer and is set to
1 if not specified explicitly. If the `row_offset` is negative, the expression will
be evaluated against a row coming after the current row, similar to the `lead` function.
A `row_offset` of `null` will return `null`. The function returns the `default`
input value if `row_offset` goes beyond the scope of the partition. If a `default`
value is not specified, it is set to `null`.

Example comparing the sales of the current year to the previous year.
`row_offset` of 1.
| year | sales | previous_year_sales |
| 2019 | 20.50 | null |
| 2020 | 30.00 | 20.50 |
| 2021 | 45.99 | 30.00 |
impls:
- args:
- value: any1
name: expression
nullability: DECLARED_OUTPUT
decomposable: NONE
return: any1?
window_type: PARTITION
- args:
- value: any1
name: expression
- value: i32
name: row_offset
nullability: DECLARED_OUTPUT
decomposable: NONE
return: any1?
window_type: PARTITION
- args:
- value: any1
name: expression
- value: i32
name: row_offset
- value: any1
name: default
nullability: DECLARED_OUTPUT
decomposable: NONE
return: any1?
window_type: PARTITION
Loading