|
12 | 12 | Unless required by applicable law or agreed to in writing,
|
13 | 13 | software distributed under the License is distributed on an
|
14 | 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15 |
| - KIND, either expressioness or implied. See the License for the |
| 15 | + KIND, either express or implied. See the License for the |
16 | 16 | specific language governing permissions and limitations
|
17 | 17 | under the License.
|
18 | 18 | -->
|
19 | 19 |
|
| 20 | +<!--- |
| 21 | +This file was generated by the dev/update_function_docs.sh script. |
| 22 | +Do not edit it manually as changes will be overwritten. |
| 23 | +Instead, edit the WindowUDFImpl's documentation() function to |
| 24 | +update documentation for an individual UDF or the |
| 25 | +dev/update_function_docs.sh file for updating surrounding text. |
| 26 | +--> |
| 27 | + |
20 | 28 | # Window Functions
|
21 | 29 |
|
22 | 30 | A _window function_ performs a calculation across a set of table rows that are somehow related to the current row.
|
23 |
| - |
24 |
| -Note: this documentation is in the process of being migrated to be [automatically created from the codebase]. |
25 |
| -Please see the [Window Functions (new)](window_functions_new.md) page for |
26 |
| -the rest of the documentation. |
27 |
| - |
28 |
| -[automatically created from the codebase]: https://github.com/apache/datafusion/issues/12740 |
29 |
| - |
30 |
| -Window functions are comparable to the type of calculation that can be done with an aggregate function. However, window functions do not cause rows to become grouped into a single output row like non-window aggregate calls would. Instead, the rows retain their separate identities. Behind the scenes, the window function is able to access more than just the current row of the query result |
| 31 | +This is comparable to the type of calculation that can be done with an aggregate function. |
| 32 | +However, window functions do not cause rows to become grouped into a single output row like non-window aggregate calls would. |
| 33 | +Instead, the rows retain their separate identities. Behind the scenes, the window function is able to access more than just the current row of the query result |
31 | 34 |
|
32 | 35 | Here is an example that shows how to compare each employee's salary with the average salary in his or her department:
|
33 | 36 |
|
@@ -146,45 +149,136 @@ RANGE and GROUPS modes require an ORDER BY clause (with RANGE the ORDER BY must
|
146 | 149 |
|
147 | 150 | All [aggregate functions](aggregate_functions.md) can be used as window functions.
|
148 | 151 |
|
149 |
| -## Analytical functions |
| 152 | +## Ranking Functions |
| 153 | + |
| 154 | +- [cume_dist](#cume_dist) |
| 155 | +- [dense_rank](#dense_rank) |
| 156 | +- [ntile](#ntile) |
| 157 | +- [percent_rank](#percent_rank) |
| 158 | +- [rank](#rank) |
| 159 | +- [row_number](#row_number) |
| 160 | + |
| 161 | +### `cume_dist` |
| 162 | + |
| 163 | +Relative rank of the current row: (number of rows preceding or peer with current row) / (total rows). |
| 164 | + |
| 165 | +``` |
| 166 | +cume_dist() |
| 167 | +``` |
| 168 | + |
| 169 | +### `dense_rank` |
| 170 | + |
| 171 | +Returns the rank of the current row without gaps. This function ranks rows in a dense manner, meaning consecutive ranks are assigned even for identical values. |
| 172 | + |
| 173 | +``` |
| 174 | +dense_rank() |
| 175 | +``` |
| 176 | + |
| 177 | +### `ntile` |
| 178 | + |
| 179 | +Integer ranging from 1 to the argument value, dividing the partition as equally as possible |
| 180 | + |
| 181 | +``` |
| 182 | +ntile(expression) |
| 183 | +``` |
| 184 | + |
| 185 | +#### Arguments |
| 186 | + |
| 187 | +- **expression**: An integer describing the number groups the partition should be split into |
| 188 | + |
| 189 | +### `percent_rank` |
| 190 | + |
| 191 | +Returns the percentage rank of the current row within its partition. The value ranges from 0 to 1 and is computed as `(rank - 1) / (total_rows - 1)`. |
| 192 | + |
| 193 | +``` |
| 194 | +percent_rank() |
| 195 | +``` |
| 196 | + |
| 197 | +### `rank` |
| 198 | + |
| 199 | +Returns the rank of the current row within its partition, allowing gaps between ranks. This function provides a ranking similar to `row_number`, but skips ranks for identical values. |
| 200 | + |
| 201 | +``` |
| 202 | +rank() |
| 203 | +``` |
| 204 | + |
| 205 | +### `row_number` |
| 206 | + |
| 207 | +Number of the current row within its partition, counting from 1. |
| 208 | + |
| 209 | +``` |
| 210 | +row_number() |
| 211 | +``` |
| 212 | + |
| 213 | +## Analytical Functions |
150 | 214 |
|
151 | 215 | - [first_value](#first_value)
|
| 216 | +- [lag](#lag) |
152 | 217 | - [last_value](#last_value)
|
| 218 | +- [lead](#lead) |
153 | 219 | - [nth_value](#nth_value)
|
154 | 220 |
|
155 | 221 | ### `first_value`
|
156 | 222 |
|
157 | 223 | Returns value evaluated at the row that is the first row of the window frame.
|
158 | 224 |
|
159 |
| -```sql |
| 225 | +``` |
160 | 226 | first_value(expression)
|
161 | 227 | ```
|
162 | 228 |
|
163 | 229 | #### Arguments
|
164 | 230 |
|
165 | 231 | - **expression**: Expression to operate on
|
166 | 232 |
|
| 233 | +### `lag` |
| 234 | + |
| 235 | +Returns value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead return default (which must be of the same type as value). |
| 236 | + |
| 237 | +``` |
| 238 | +lag(expression, offset, default) |
| 239 | +``` |
| 240 | + |
| 241 | +#### Arguments |
| 242 | + |
| 243 | +- **expression**: Expression to operate on |
| 244 | +- **offset**: Integer. Specifies how many rows back the value of expression should be retrieved. Defaults to 1. |
| 245 | +- **default**: The default value if the offset is not within the partition. Must be of the same type as expression. |
| 246 | + |
167 | 247 | ### `last_value`
|
168 | 248 |
|
169 | 249 | Returns value evaluated at the row that is the last row of the window frame.
|
170 | 250 |
|
171 |
| -```sql |
| 251 | +``` |
172 | 252 | last_value(expression)
|
173 | 253 | ```
|
174 | 254 |
|
175 | 255 | #### Arguments
|
176 | 256 |
|
177 | 257 | - **expression**: Expression to operate on
|
178 | 258 |
|
| 259 | +### `lead` |
| 260 | + |
| 261 | +Returns value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead return default (which must be of the same type as value). |
| 262 | + |
| 263 | +``` |
| 264 | +lead(expression, offset, default) |
| 265 | +``` |
| 266 | + |
| 267 | +#### Arguments |
| 268 | + |
| 269 | +- **expression**: Expression to operate on |
| 270 | +- **offset**: Integer. Specifies how many rows forward the value of expression should be retrieved. Defaults to 1. |
| 271 | +- **default**: The default value if the offset is not within the partition. Must be of the same type as expression. |
| 272 | + |
179 | 273 | ### `nth_value`
|
180 | 274 |
|
181 | 275 | Returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row.
|
182 | 276 |
|
183 |
| -```sql |
| 277 | +``` |
184 | 278 | nth_value(expression, n)
|
185 | 279 | ```
|
186 | 280 |
|
187 | 281 | #### Arguments
|
188 | 282 |
|
189 | 283 | - **expression**: The name the column of which nth value to retrieve
|
190 |
| -- **n**: Integer. Specifies the _n_ in nth |
| 284 | +- **n**: Integer. Specifies the n in nth |
0 commit comments