Skip to content

Commit e3e7d50

Browse files
authored
Add window function examples in code (#16102)
1 parent 4c719af commit e3e7d50

File tree

6 files changed

+189
-6
lines changed

6 files changed

+189
-6
lines changed

datafusion/functions-window/src/lead_lag.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@ static LAG_DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
157157
the value of expression should be retrieved. Defaults to 1.")
158158
.with_argument("default", "The default value if the offset is \
159159
not within the partition. Must be of the same type as expression.")
160+
.with_sql_example(r#"```sql
161+
--Example usage of the lag window function:
162+
SELECT employee_id,
163+
salary,
164+
lag(salary, 1, 0) OVER (ORDER BY employee_id) AS prev_salary
165+
FROM employees;
166+
```
167+
168+
```sql
169+
+-------------+--------+-------------+
170+
| employee_id | salary | prev_salary |
171+
+-------------+--------+-------------+
172+
| 1 | 30000 | 0 |
173+
| 2 | 50000 | 30000 |
174+
| 3 | 70000 | 50000 |
175+
| 4 | 60000 | 70000 |
176+
+-------------+--------+-------------+
177+
```"#)
160178
.build()
161179
});
162180

@@ -175,6 +193,27 @@ static LEAD_DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
175193
forward the value of expression should be retrieved. Defaults to 1.")
176194
.with_argument("default", "The default value if the offset is \
177195
not within the partition. Must be of the same type as expression.")
196+
.with_sql_example(r#"```sql
197+
-- Example usage of lead() :
198+
SELECT
199+
employee_id,
200+
department,
201+
salary,
202+
lead(salary, 1, 0) OVER (PARTITION BY department ORDER BY salary) AS next_salary
203+
FROM employees;
204+
```
205+
206+
```sql
207+
+-------------+-------------+--------+--------------+
208+
| employee_id | department | salary | next_salary |
209+
+-------------+-------------+--------+--------------+
210+
| 1 | Sales | 30000 | 50000 |
211+
| 2 | Sales | 50000 | 70000 |
212+
| 3 | Sales | 70000 | 0 |
213+
| 4 | Engineering | 40000 | 60000 |
214+
| 5 | Engineering | 60000 | 0 |
215+
+-------------+-------------+--------+--------------+
216+
```"#)
178217
.build()
179218
});
180219

datafusion/functions-window/src/nth_value.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ static FIRST_VALUE_DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
135135
"first_value(expression)",
136136
)
137137
.with_argument("expression", "Expression to operate on")
138+
.with_sql_example(r#"```sql
139+
--Example usage of the first_value window function:
140+
SELECT department,
141+
employee_id,
142+
salary,
143+
first_value(salary) OVER (PARTITION BY department ORDER BY salary DESC) AS top_salary
144+
FROM employees;
145+
```
146+
147+
```sql
148+
+-------------+-------------+--------+------------+
149+
| department | employee_id | salary | top_salary |
150+
+-------------+-------------+--------+------------+
151+
| Sales | 1 | 70000 | 70000 |
152+
| Sales | 2 | 50000 | 70000 |
153+
| Sales | 3 | 30000 | 70000 |
154+
| Engineering | 4 | 90000 | 90000 |
155+
| Engineering | 5 | 80000 | 90000 |
156+
+-------------+-------------+--------+------------+
157+
```"#)
138158
.build()
139159
});
140160

@@ -150,6 +170,26 @@ static LAST_VALUE_DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
150170
"last_value(expression)",
151171
)
152172
.with_argument("expression", "Expression to operate on")
173+
.with_sql_example(r#"```sql
174+
-- SQL example of last_value:
175+
SELECT department,
176+
employee_id,
177+
salary,
178+
last_value(salary) OVER (PARTITION BY department ORDER BY salary) AS running_last_salary
179+
FROM employees;
180+
```
181+
182+
```sql
183+
+-------------+-------------+--------+---------------------+
184+
| department | employee_id | salary | running_last_salary |
185+
+-------------+-------------+--------+---------------------+
186+
| Sales | 1 | 30000 | 30000 |
187+
| Sales | 2 | 50000 | 50000 |
188+
| Sales | 3 | 70000 | 70000 |
189+
| Engineering | 4 | 40000 | 40000 |
190+
| Engineering | 5 | 60000 | 60000 |
191+
+-------------+-------------+--------+---------------------+
192+
```"#)
153193
.build()
154194
});
155195

datafusion/functions-window/src/ntile.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,29 @@ pub fn ntile(arg: Expr) -> Expr {
5252
argument(
5353
name = "expression",
5454
description = "An integer describing the number groups the partition should be split into"
55-
)
55+
),
56+
sql_example = r#"```sql
57+
--Example usage of the ntile window function:
58+
SELECT employee_id,
59+
salary,
60+
ntile(4) OVER (ORDER BY salary DESC) AS quartile
61+
FROM employees;
62+
```
63+
64+
```sql
65+
+-------------+--------+----------+
66+
| employee_id | salary | quartile |
67+
+-------------+--------+----------+
68+
| 1 | 90000 | 1 |
69+
| 2 | 85000 | 1 |
70+
| 3 | 80000 | 2 |
71+
| 4 | 70000 | 2 |
72+
| 5 | 60000 | 3 |
73+
| 6 | 50000 | 3 |
74+
| 7 | 40000 | 4 |
75+
| 8 | 30000 | 4 |
76+
+-------------+--------+----------+
77+
```"#
5678
)]
5779
#[derive(Debug)]
5880
pub struct Ntile {

datafusion/functions-window/src/rank.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,26 @@ static RANK_DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
110110
skips ranks for identical values.",
111111

112112
"rank()")
113+
.with_sql_example(r#"```sql
114+
--Example usage of the rank window function:
115+
SELECT department,
116+
salary,
117+
rank() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
118+
FROM employees;
119+
```
120+
121+
```sql
122+
+-------------+--------+------+
123+
| department | salary | rank |
124+
+-------------+--------+------+
125+
| Sales | 70000 | 1 |
126+
| Sales | 50000 | 2 |
127+
| Sales | 50000 | 2 |
128+
| Sales | 30000 | 4 |
129+
| Engineering | 90000 | 1 |
130+
| Engineering | 80000 | 2 |
131+
+-------------+--------+------+
132+
```"#)
113133
.build()
114134
});
115135

@@ -121,6 +141,26 @@ static DENSE_RANK_DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
121141
Documentation::builder(DOC_SECTION_RANKING, "Returns the rank of the current row without gaps. This function ranks \
122142
rows in a dense manner, meaning consecutive ranks are assigned even for identical \
123143
values.", "dense_rank()")
144+
.with_sql_example(r#"```sql
145+
--Example usage of the dense_rank window function:
146+
SELECT department,
147+
salary,
148+
dense_rank() OVER (PARTITION BY department ORDER BY salary DESC) AS dense_rank
149+
FROM employees;
150+
```
151+
152+
```sql
153+
+-------------+--------+------------+
154+
| department | salary | dense_rank |
155+
+-------------+--------+------------+
156+
| Sales | 70000 | 1 |
157+
| Sales | 50000 | 2 |
158+
| Sales | 50000 | 2 |
159+
| Sales | 30000 | 3 |
160+
| Engineering | 90000 | 1 |
161+
| Engineering | 80000 | 2 |
162+
+-------------+--------+------------+
163+
```"#)
124164
.build()
125165
});
126166

@@ -131,6 +171,23 @@ fn get_dense_rank_doc() -> &'static Documentation {
131171
static PERCENT_RANK_DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
132172
Documentation::builder(DOC_SECTION_RANKING, "Returns the percentage rank of the current row within its partition. \
133173
The value ranges from 0 to 1 and is computed as `(rank - 1) / (total_rows - 1)`.", "percent_rank()")
174+
.with_sql_example(r#"```sql
175+
--Example usage of the percent_rank window function:
176+
SELECT employee_id,
177+
salary,
178+
percent_rank() OVER (ORDER BY salary) AS percent_rank
179+
FROM employees;
180+
```
181+
182+
```sql
183+
+-------------+--------+---------------+
184+
| employee_id | salary | percent_rank |
185+
+-------------+--------+---------------+
186+
| 1 | 30000 | 0.00 |
187+
| 2 | 50000 | 0.50 |
188+
| 3 | 70000 | 1.00 |
189+
+-------------+--------+---------------+
190+
```"#)
134191
.build()
135192
});
136193

datafusion/functions-window/src/row_number.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,27 @@ define_udwf_and_expr!(
4444
#[user_doc(
4545
doc_section(label = "Ranking Functions"),
4646
description = "Number of the current row within its partition, counting from 1.",
47-
syntax_example = "row_number()"
47+
syntax_example = "row_number()",
48+
sql_example = r"```sql
49+
--Example usage of the row_number window function:
50+
SELECT department,
51+
salary,
52+
row_number() OVER (PARTITION BY department ORDER BY salary DESC) AS row_num
53+
FROM employees;
54+
```
55+
56+
```sql
57+
+-------------+--------+---------+
58+
| department | salary | row_num |
59+
+-------------+--------+---------+
60+
| Sales | 70000 | 1 |
61+
| Sales | 50000 | 2 |
62+
| Sales | 50000 | 3 |
63+
| Sales | 30000 | 4 |
64+
| Engineering | 90000 | 1 |
65+
| Engineering | 80000 | 2 |
66+
+-------------+--------+---------+
67+
```#"
4868
)]
4969
#[derive(Debug)]
5070
pub struct RowNumber {

docs/source/user-guide/sql/window_functions.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ ntile(expression)
228228

229229
- **expression**: An integer describing the number groups the partition should be split into
230230

231-
#### Exmaple
231+
#### Example
232232

233233
```sql
234234
--Example usage of the ntile window function:
@@ -330,7 +330,7 @@ row_number()
330330
FROM employees;
331331
```
332332

333-
```sql
333+
````sql
334334
+-------------+--------+---------+
335335
| department | salary | row_num |
336336
+-------------+--------+---------+
@@ -341,7 +341,8 @@ row_number()
341341
| Engineering | 90000 | 1 |
342342
| Engineering | 80000 | 2 |
343343
+-------------+--------+---------+
344-
```
344+
```#
345+
345346
346347
## Analytical Functions
347348
@@ -357,7 +358,7 @@ Returns value evaluated at the row that is the first row of the window frame.
357358
358359
```sql
359360
first_value(expression)
360-
```
361+
````
361362

362363
#### Arguments
363364

@@ -433,6 +434,8 @@ last_value(expression)
433434

434435
- **expression**: Expression to operate on
435436

437+
#### Example
438+
436439
```sql
437440
-- SQL example of last_value:
438441
SELECT department,
@@ -468,6 +471,8 @@ lead(expression, offset, default)
468471
- **offset**: Integer. Specifies how many rows forward the value of expression should be retrieved. Defaults to 1.
469472
- **default**: The default value if the offset is not within the partition. Must be of the same type as expression.
470473

474+
#### Example
475+
471476
```sql
472477
-- Example usage of lead() :
473478
SELECT

0 commit comments

Comments
 (0)