Skip to content

Commit 51fc08d

Browse files
TCeasonBohuTANG
andauthored
add: rand window support respect|ignore nulls (#966)
* add: rand window support respect|ignore nulls * Update last-value.md * Update first-value.md * Update nth-value.md * fmt * modify note and title * merge --------- Co-authored-by: Bohu <[email protected]>
1 parent 9477bcf commit 51fc08d

File tree

3 files changed

+121
-12
lines changed

3 files changed

+121
-12
lines changed

docs/en/sql-reference/20-sql-functions/08-window-functions/first-value.md

+41-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ title: FIRST_VALUE
44

55
import FunctionDescription from '@site/src/components/FunctionDescription';
66

7-
<FunctionDescription description="Introduced: v1.1.50"/>
7+
<FunctionDescription description="Introduced: v1.2.568"/>
88

9-
Returns the first value from an ordered group of values.
9+
When `IGNORE NULLS` is used with FIRST_VALUE, the function returns the first value in the frame that is not NULL (or NULL if all values are NULL). If not specified, the default is RESPECT NULLS.
1010

1111
See also:
1212

@@ -16,7 +16,7 @@ See also:
1616
## Syntax
1717

1818
```sql
19-
FIRST_VALUE(expression) OVER ([PARTITION BY partition_expression] ORDER BY order_expression [window_frame])
19+
FIRST_VALUE(expression) [ { IGNORE | RESPECT } NULLS ] OVER ([PARTITION BY partition_expression] ORDER BY order_expression [window_frame])
2020
```
2121

2222
For the syntax of window frame, see [Window Frame Syntax](index.md#window-frame-syntax).
@@ -52,4 +52,41 @@ employee_id | first_name | last_name | salary | highest_salary_first_name
5252
3 | David | Johnson | 5500.00 | Mary
5353
1 | John | Doe | 5000.00 | Mary
5454
5 | Michael | Brown | 4500.00 | Mary
55-
```
55+
56+
```
57+
58+
### Returning NON-NULLs with IGNORE NULLS
59+
60+
```sql
61+
CREATE or replace TABLE example AS SELECT * FROM (VALUES
62+
(0, 1, 614),
63+
(1, 1, null),
64+
(2, 1, null),
65+
(3, 1, 639),
66+
(4, 1, 2027)
67+
) tbl(id, user_id, order_id);
68+
69+
70+
SELECT
71+
id,
72+
user_id,
73+
order_id,
74+
FIRST_VALUE (order_id) IGNORE nulls over (
75+
PARTITION BY user_id
76+
ORDER BY
77+
id ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING
78+
) AS last_order_id
79+
FROM
80+
example
81+
82+
┌───────────────────────────────────────────────────────┐
83+
│ id │ user_id │ order_id │ last_order_id │
84+
├───────┼─────────┼──────────────────┼──────────────────┤
85+
01614614
86+
11NULL614
87+
21NULL639
88+
31639639
89+
412027639
90+
└───────────────────────────────────────────────────────┘
91+
92+
```

docs/en/sql-reference/20-sql-functions/08-window-functions/last-value.md

+40-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ title: LAST_VALUE
44

55
import FunctionDescription from '@site/src/components/FunctionDescription';
66

7-
<FunctionDescription description="Introduced: v1.1.50"/>
7+
<FunctionDescription description="Introduced: v1.2.568"/>
88

9-
Returns the last value from an ordered group of values.
9+
When `IGNORE NULLS` is used with LAST_VALUE, the function returns the last value in the frame that is not NULL (or NULL if all values are NULL). If not specified, the default is RESPECT NULLS.
1010

1111
See also:
1212

@@ -16,7 +16,7 @@ See also:
1616
## Syntax
1717

1818
```sql
19-
LAST_VALUE(expression) OVER ([PARTITION BY partition_expression] ORDER BY order_expression [window_frame])
19+
LAST_VALUE(expression) [ { IGNORE | RESPECT } NULLS ] OVER ([PARTITION BY partition_expression] ORDER BY order_expression [window_frame])
2020
```
2121

2222
For the syntax of window frame, see [Window Frame Syntax](index.md#window-frame-syntax).
@@ -51,4 +51,40 @@ employee_id | first_name | last_name | salary | lowest_salary_first_name
5151
3 | David | Johnson | 5500.00 | Michael
5252
1 | John | Doe | 5000.00 | Michael
5353
5 | Michael | Brown | 4500.00 | Michael
54-
```
54+
```
55+
56+
### Returning NON-NULLs with IGNORE NULLS
57+
58+
```sql
59+
CREATE or replace TABLE example AS SELECT * FROM (VALUES
60+
(0, 1, 614),
61+
(1, 1, null),
62+
(2, 1, null),
63+
(3, 1, 639),
64+
(4, 1, 2027)
65+
) tbl(id, user_id, order_id);
66+
67+
68+
SELECT
69+
id,
70+
user_id,
71+
order_id,
72+
LAST_VALUE (order_id) IGNORE NULLS over (
73+
PARTITION BY user_id
74+
ORDER BY
75+
id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
76+
) AS last_order_id
77+
FROM
78+
example
79+
80+
┌───────────────────────────────────────────────────────┐
81+
│ id │ user_id │ order_id │ last_order_id │
82+
├───────┼─────────┼──────────────────┼──────────────────┤
83+
01614NULL
84+
11NULL614
85+
21NULL614
86+
31639614
87+
412027639
88+
└───────────────────────────────────────────────────────┘
89+
90+
```

docs/en/sql-reference/20-sql-functions/08-window-functions/nth-value.md

+40-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ title: NTH_VALUE
44

55
import FunctionDescription from '@site/src/components/FunctionDescription';
66

7-
<FunctionDescription description="Introduced: v1.1.50"/>
7+
<FunctionDescription description="Introduced: v1.2.568"/>
88

9-
Returns the Nth value from an ordered group of values.
9+
Returns expr evaluated at the nth row (among rows with a non-null value of expr if `IGNORE NULLS` is set) of the window frame (counting from 1); NULL if no such row.
1010

1111
See also:
1212

@@ -16,7 +16,7 @@ See also:
1616
## Syntax
1717

1818
```sql
19-
NTH_VALUE(expression, n) OVER ([PARTITION BY partition_expression] ORDER BY order_expression [window_frame])
19+
NTH_VALUE(expression, n) [ { IGNORE | RESPECT } NULLS ] OVER ([PARTITION BY partition_expression] ORDER BY order_expression [window_frame])
2020
```
2121

2222
For the syntax of window frame, see [Window Frame Syntax](index.md#window-frame-syntax).
@@ -51,4 +51,40 @@ employee_id | first_name | last_name | salary | second_highest_salary_first_nam
5151
3 | David | Johnson | 5500.00 | Jane
5252
1 | John | Doe | 5000.00 | Jane
5353
5 | Michael | Brown | 4500.00 | Jane
54-
```
54+
```
55+
56+
### Returning NON-NULLs with IGNORE NULLS
57+
58+
```sql
59+
CREATE or replace TABLE example AS SELECT * FROM (VALUES
60+
(0, 1, 614),
61+
(1, 1, null),
62+
(2, 1, null),
63+
(3, 1, 639),
64+
(4, 1, 2027)
65+
) tbl(id, user_id, order_id);
66+
67+
68+
SELECT
69+
id,
70+
user_id,
71+
order_id,
72+
NTH_VALUE (order_id, 2) IGNORE NULLS over (
73+
PARTITION BY user_id
74+
ORDER BY
75+
id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
76+
) AS last_order_id
77+
FROM
78+
example
79+
80+
┌───────────────────────────────────────────────────────┐
81+
│ id │ user_id │ order_id │ last_order_id │
82+
├───────┼─────────┼──────────────────┼──────────────────┤
83+
01614NULL
84+
11NULLNULL
85+
21NULLNULL
86+
31639NULL
87+
412027639
88+
└───────────────────────────────────────────────────────┘
89+
90+
```

0 commit comments

Comments
 (0)