Skip to content

Commit 5f0ba13

Browse files
authored
docs: document negative n behavior for nth_value (#24557)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Follow-up to #24453. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. Please explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the implementation. "COUNT(DISTINCT) returns wrong results when the column contains nulls" is the user-visible problem. --> `nth_value` supports negative `n`, but the user documentation only describes positive, one-based positions. This behavior was discussed while reviewing #24453, where a documentation follow-up was proposed. Negative `n` is a DataFusion extension that counts backward from the end of the ordered group or window frame. It provides the behavior of SQL-standard `from last`. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR. --> - Document positive and negative `n` for the aggregate `nth_value`. - Document positive and negative `n` for the window `nth_value`. - Update the generated aggregate and window function documentation. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes. Documentation formatting was checked. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please add the `api change` label. --> Documentation only;
1 parent a4e071d commit 5f0ba13

4 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎datafusion/functions-aggregate/src/nth_value.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ pub fn nth_value(
8585
),
8686
argument(
8787
name = "n",
88-
description = "The position (nth) of the value to retrieve, based on the ordering."
88+
description = "The position of the value to retrieve. Positive values count from the first \
89+
value, starting at 1; negative values count backward from the last value, where -1 \
90+
returns the last value."
8991
)
9092
)]
9193
/// Expression for a `NTH_VALUE(..., ... ORDER BY ...)` aggregation. In a multi

‎datafusion/functions-window/src/nth_value.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ static NTH_VALUE_DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
217217
)
218218
.with_argument(
219219
"n",
220-
"Integer. Specifies the row number (starting from 1) in the window frame.",
220+
"Integer position in the window frame. Positive values count from the first \
221+
row, starting at 1; negative values count backward from the last row, where -1 \
222+
returns the last row.",
221223
)
222224
.with_sql_example(
223225
r#"

‎docs/source/user-guide/sql/aggregate_functions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ nth_value(expression, n ORDER BY expression)
705705
#### Arguments
706706

707707
- **expression**: The column or expression to retrieve the nth value from.
708-
- **n**: The position (nth) of the value to retrieve, based on the ordering.
708+
- **n**: The position of the value to retrieve. Positive values count from the first value, starting at 1; negative values count backward from the last value, where -1 returns the last value.
709709

710710
#### Example
711711

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ nth_value(expression, n)
498498
#### Arguments
499499

500500
- **expression**: The column from which to retrieve the nth value.
501-
- **n**: Integer. Specifies the row number (starting from 1) in the window frame.
501+
- **n**: Integer position in the window frame. Positive values count from the first row, starting at 1; negative values count backward from the last row, where -1 returns the last row.
502502

503503
#### Example
504504

0 commit comments

Comments
 (0)