ClickHouse/docs/en/sql-reference/window-functions/index.md

61 lines
2.8 KiB
Markdown
Raw Normal View History

2021-02-04 18:14:02 +00:00
---
toc_priority: 62
toc_title: Window Functions
---
# [experimental] Window Functions
2021-01-28 17:39:32 +00:00
!!! warning "Warning"
2021-06-02 11:36:36 +00:00
This is an experimental feature that is currently in development and is not ready for general use. It will change in unpredictable backwards-incompatible ways in the future releases. Set `allow_experimental_window_functions = 1` to enable it.
2021-01-28 17:39:32 +00:00
2021-02-12 14:22:03 +00:00
ClickHouse supports the standard grammar for defining windows and window functions. The following features are currently supported:
2021-01-28 17:39:32 +00:00
2021-02-12 14:22:03 +00:00
| Feature | Support or workaround |
| --------| ----------|
2021-02-15 19:42:10 +00:00
| ad hoc window specification (`count(*) over (partition by id order by time desc)`) | supported |
2021-02-15 19:40:55 +00:00
| expressions involving window functions, e.g. `(count(*) over ()) / 2)` | not supported, wrap in a subquery ([feature request](https://github.com/ClickHouse/ClickHouse/issues/19857)) |
2021-02-15 19:42:10 +00:00
| `WINDOW` clause (`select ... from table window w as (partiton by id)`) | supported |
| `ROWS` frame | supported |
| `RANGE` frame | supported, the default |
2021-02-15 19:56:26 +00:00
| `INTERVAL` syntax for `DateTime` `RANGE OFFSET` frame | not supported, specify the number of seconds instead |
2021-02-15 19:42:10 +00:00
| `GROUPS` frame | not supported |
2021-02-12 14:22:03 +00:00
| Calculating aggregate functions over a frame (`sum(value) over (order by time)`) | all aggregate functions are supported |
2021-02-15 19:42:10 +00:00
| `rank()`, `dense_rank()`, `row_number()` | supported |
2021-03-18 23:05:43 +00:00
| `lag/lead(value, offset)` | Not supported. Workarounds: |
| | 1) replace with `any(value) over (.... rows between <offset> preceding and <offset> preceding)`, or `following` for `lead`|
2021-03-25 15:49:01 +00:00
| | 2) use `lagInFrame/leadInFrame`, which are analogous, but respect the window frame. To get behavior identical to `lag/lead`, use `rows between unbounded preceding and unbounded following` |
2021-01-28 17:39:32 +00:00
## References
### GitHub Issues
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
The roadmap for the initial support of window functions is [in this issue](https://github.com/ClickHouse/ClickHouse/issues/18097).
All GitHub issues related to window funtions have the [comp-window-functions](https://github.com/ClickHouse/ClickHouse/labels/comp-window-functions) tag.
### Tests
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
These tests contain the examples of the currently supported grammar:
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
https://github.com/ClickHouse/ClickHouse/blob/master/tests/performance/window_functions.xml
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
https://github.com/ClickHouse/ClickHouse/blob/master/tests/queries/0_stateless/01591_window_functions.sql
### Postgres Docs
2021-02-12 14:37:22 +00:00
2021-02-04 18:16:26 +00:00
https://www.postgresql.org/docs/current/sql-select.html#SQL-WINDOW
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
https://www.postgresql.org/docs/devel/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
https://www.postgresql.org/docs/devel/functions-window.html
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
https://www.postgresql.org/docs/devel/tutorial-window.html
### MySQL Docs
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
https://dev.mysql.com/doc/refman/8.0/en/window-functions-usage.html
2021-02-12 14:37:22 +00:00
2021-01-28 17:39:32 +00:00
https://dev.mysql.com/doc/refman/8.0/en/window-functions-frames.html