Skip to content

Commit

Permalink
[docs]: migrate lead/lag window function docs to new docs (#13095)
Browse files Browse the repository at this point in the history
* added lead-lag docs

* deleted old
  • Loading branch information
buraksenn authored Oct 24, 2024
1 parent 31701b8 commit 9e636d8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 33 deletions.
58 changes: 55 additions & 3 deletions datafusion/functions-window/src/lead_lag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ use datafusion_common::arrow::array::ArrayRef;
use datafusion_common::arrow::datatypes::DataType;
use datafusion_common::arrow::datatypes::Field;
use datafusion_common::{arrow_datafusion_err, DataFusionError, Result, ScalarValue};
use datafusion_expr::window_doc_sections::DOC_SECTION_ANALYTICAL;
use datafusion_expr::{
Literal, PartitionEvaluator, ReversedUDWF, Signature, TypeSignature, Volatility,
WindowUDFImpl,
Documentation, Literal, PartitionEvaluator, ReversedUDWF, Signature, TypeSignature,
Volatility, WindowUDFImpl,
};
use datafusion_functions_window_common::expr::ExpressionArgs;
use datafusion_functions_window_common::field::WindowUDFFieldArgs;
Expand All @@ -34,7 +35,7 @@ use std::any::Any;
use std::cmp::min;
use std::collections::VecDeque;
use std::ops::{Neg, Range};
use std::sync::Arc;
use std::sync::{Arc, OnceLock};

get_or_init_udwf!(
Lag,
Expand Down Expand Up @@ -147,6 +148,50 @@ impl WindowShift {
}
}

static LAG_DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();

fn get_lag_doc() -> &'static Documentation {
LAG_DOCUMENTATION.get_or_init(|| {
Documentation::builder()
.with_doc_section(DOC_SECTION_ANALYTICAL)
.with_description(
"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).",
)
.with_syntax_example("lag(expression, offset, default)")
.with_argument("expression", "Expression to operate on")
.with_argument("offset", "Integer. Specifies how many rows back \
the value of expression should be retrieved. Defaults to 1.")
.with_argument("default", "The default value if the offset is \
not within the partition. Must be of the same type as expression.")
.build()
.unwrap()
})
}

static LEAD_DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();

fn get_lead_doc() -> &'static Documentation {
LEAD_DOCUMENTATION.get_or_init(|| {
Documentation::builder()
.with_doc_section(DOC_SECTION_ANALYTICAL)
.with_description(
"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).",
)
.with_syntax_example("lead(expression, offset, default)")
.with_argument("expression", "Expression to operate on")
.with_argument("offset", "Integer. Specifies how many rows \
forward the value of expression should be retrieved. Defaults to 1.")
.with_argument("default", "The default value if the offset is \
not within the partition. Must be of the same type as expression.")
.build()
.unwrap()
})
}

impl WindowUDFImpl for WindowShift {
fn as_any(&self) -> &dyn Any {
self
Expand Down Expand Up @@ -212,6 +257,13 @@ impl WindowUDFImpl for WindowShift {
WindowShiftKind::Lead => ReversedUDWF::Reversed(lead_udwf()),
}
}

fn documentation(&self) -> Option<&Documentation> {
match self.kind {
WindowShiftKind::Lag => Some(get_lag_doc()),
WindowShiftKind::Lead => Some(get_lead_doc()),
}
}
}

/// When `lead`/`lag` is evaluated on a `NULL` expression we attempt to
Expand Down
30 changes: 0 additions & 30 deletions docs/source/user-guide/sql/window_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ ntile(expression)

- [cume_dist](#cume_dist)
- [percent_rank](#percent_rank)
- [lag](#lag)
- [lead](#lead)
- [first_value](#first_value)
- [last_value](#last_value)
- [nth_value](#nth_value)
Expand All @@ -206,34 +204,6 @@ Relative rank of the current row: (rank - 1) / (total rows - 1).
percent_rank()
```

### `lag`

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). Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null.

```sql
lag(expression, offset, default)
```

#### Arguments

- **expression**: Expression to operate on
- **offset**: Integer. Specifies how many rows back the value of _expression_ should be retrieved. Defaults to 1.
- **default**: The default value if the offset is not within the partition. Must be of the same type as _expression_.

### `lead`

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). Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null.

```sql
lead(expression, offset, default)
```

#### Arguments

- **expression**: Expression to operate on
- **offset**: Integer. Specifies how many rows forward the value of _expression_ should be retrieved. Defaults to 1.
- **default**: The default value if the offset is not within the partition. Must be of the same type as _expression_.

### `first_value`

Returns value evaluated at the row that is the first row of the window frame.
Expand Down
33 changes: 33 additions & 0 deletions docs/source/user-guide/sql/window_functions_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,36 @@ Number of the current row within its partition, counting from 1.
```
row_number()
```

## Analytical Functions

- [lag](#lag)
- [lead](#lead)

### `lag`

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).

```
lag(expression, offset, default)
```

#### Arguments

- **expression**: Expression to operate on
- **offset**: Integer. Specifies how many rows back the value of expression should be retrieved. Defaults to 1.
- **default**: The default value if the offset is not within the partition. Must be of the same type as expression.

### `lead`

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).

```
lead(expression, offset, default)
```

#### Arguments

- **expression**: Expression to operate on
- **offset**: Integer. Specifies how many rows forward the value of expression should be retrieved. Defaults to 1.
- **default**: The default value if the offset is not within the partition. Must be of the same type as expression.

0 comments on commit 9e636d8

Please sign in to comment.