You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "comphead (via GitHub)" <gi...@apache.org> on 2026/10/23 15:17:37 UTC
[PR] perf: memoize `LAST_VALUE` and `NTH_VALUE` with `IGNORE NULLS`, gather results with `take` [datafusion]
comphead opened a new pull request, #26664:
URL: https://github.com/apache/datafusion/pull/26664
## Which issue does this PR close?
- Closes #26537.
- Closes #26538.
- Closes #26539.
- Closes #26540.
## Rationale for this change
With `IGNORE NULLS`, `LAST_VALUE` and `NTH_VALUE` over a frame that starts at the first row of the partition never let `BoundedWindowAggExec` prune the partition's rows (#26537, #26539). A 10M-row partition of `LAST_VALUE ... IGNORE NULLS` took 5.6 s and 363 MiB, against 0.75 s and 43 MiB with `RESPECT NULLS`. The quadratic scan of #26538 was fixed by `nth_set_bit_from_end` in #26533, except over long runs of NULLs, which every row still scanned. Forward-filling a 1M-row partition that holds a single non-NULL value took 3.7 s.
`FIRST_VALUE`, `LAST_VALUE` and `NTH_VALUE` also created a `ScalarValue` for every row, which is slow for nested types (#26540).
## What changes are included in this PR?
- `NthValueEvaluator::memoize` moves the frame start with `IGNORE NULLS` too.
- `LAST_VALUE` keeps its last non-NULL result, which it returns for later frames without valid rows, and drops the whole frame.
- `NTH_VALUE(expr, -n)` keeps the rows from the one it selects.
- `FIRST_VALUE` and `NTH_VALUE(expr, n)` are final once they select a row.
- A frame with too few valid rows keeps them, but not the NULLs before them.
- The new `PartitionEvaluator::evaluate_ranges` evaluates the frames of consecutive rows in one call. Its default calls `evaluate` for each frame. `StandardWindowExpr` calls it for functions that use the window frame, in both `WindowAggExec` and `BoundedWindowAggExec`.
- `NthValueEvaluator::evaluate_ranges` gathers the selected rows with one `take`, or repeats its final result. A single frame still goes through `evaluate`, which is cheaper for one-row partitions.
#26540 suggests a method that returns the row index. A method over all frames also covers a final `FIRST_VALUE` result, whose row may already be pruned.
`NTH_VALUE(expr, n)` and `NTH_VALUE(expr, -n)` with `n > 1` still keep every row from the first of the valid rows they need. Runs of NULLs among those rows stay buffered and are scanned for every row. Unlike `LAST_VALUE`, they would need to know how many rows `BoundedWindowAggExec` actually prunes.
## What is the testing strategy for this PR?
New `window.slt` cases cover `NTH_VALUE(v, 4)` and `NTH_VALUE(v, -4)` with `IGNORE NULLS` over growing frames that span many batches, struct arguments, and empty frames. The `IGNORE NULLS` cases from #26533, with runs of 497 NULLs, now run with pruning. A unit test checks where `memoize` moves the frame start for each function. These tests fail if `memoize` keeps one row too few, drops the valid rows of a frame that has too few of them, never finalizes `NTH_VALUE(expr, n)`, or loses the last valid value of `LAST_VALUE`, and if an empty frame returns a row.
Locally, 312 queries over 12 frame shapes and 13 functions, with both NULL treatments and batch sizes of 7, 100 and 8192, returned the same results as `main`.
The queries in `datafusion-cli` (`release-nonlto` profile, Apple M5 Max):
| Query | `main` | This PR |
|---|---:|---:|
| `last_value(v) IGNORE NULLS`, one 10M-row partition, every other value NULL (#26539) | 5.63 s, 363 MiB | 0.67 s, 43 MiB |
| `nth_value(v, -2) IGNORE NULLS`, 300K-row partitions of NULLs (#26538) | 0.38 s | 0.04 s |
| `last_value(v) IGNORE NULLS`, a 1M-row partition with one non-NULL value | 3.67 s | 0.13 s |
| `last_value(st)`, 64-row partitions of a struct (#26540) | 0.25 s | 0.10 s |
| `nth_value(st, 2)`, 64-row partitions of a struct (#26540) | 0.27 s | 0.11 s |
`cargo bench -p datafusion --bench window_query_sql -- built-in`, change against `main`:
| Benchmark | Change |
|---|---:|
| `window empty over` | -74% |
| `window order by` | -12% |
| `window partition by, u64_wide` | -4% to -6% |
| `window partition by, u64_narrow` | -49% |
| `window partition and order by, u64_wide` | -2% to -9% |
| `window partition and order by, u64_narrow` | -13% |
## Are there any user-facing changes?
`PartitionEvaluator` has a new method, `evaluate_ranges`, with a default implementation, so existing implementations keep working.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: memoize `LAST_VALUE` and `NTH_VALUE` with `IGNORE NULLS` [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #26664:
URL: https://github.com/apache/datafusion/pull/26664#discussion_r4366067103
##########
datafusion/functions-window/src/nth_value.rs:
##########
@@ -464,18 +475,55 @@ impl PartitionEvaluator for NthValueEvaluator {
} else {
// FIRST_VALUE, LAST_VALUE, NTH_VALUE window functions take a single column, values will have size 1.
let arr = &values[0];
- let n_range = range.end - range.start;
- if n_range == 0 {
- // We produce None if the window is empty.
- return ScalarValue::try_from(arr.data_type());
- }
- match self.valid_index(arr, range) {
- Some(index) => ScalarValue::try_from_array(arr, index),
- None => ScalarValue::try_from(arr.data_type()),
+ match (self.valid_index(arr, range), &self.last_valid) {
+ (Some(index), _) => ScalarValue::try_from_array(arr, index),
+ (None, Some(last_valid)) => Ok(last_valid.clone()),
Review Comment:
https://github.com/apache/datafusion/issues/26700
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: memoize `LAST_VALUE` and `NTH_VALUE` with `IGNORE NULLS` [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #26664:
URL: https://github.com/apache/datafusion/pull/26664#discussion_r4366049783
##########
datafusion/functions-window/src/nth_value.rs:
##########
@@ -464,18 +475,55 @@ impl PartitionEvaluator for NthValueEvaluator {
} else {
// FIRST_VALUE, LAST_VALUE, NTH_VALUE window functions take a single column, values will have size 1.
let arr = &values[0];
- let n_range = range.end - range.start;
- if n_range == 0 {
- // We produce None if the window is empty.
- return ScalarValue::try_from(arr.data_type());
- }
- match self.valid_index(arr, range) {
- Some(index) => ScalarValue::try_from_array(arr, index),
- None => ScalarValue::try_from(arr.data_type()),
+ match (self.valid_index(arr, range), &self.last_valid) {
+ (Some(index), _) => ScalarValue::try_from_array(arr, index),
+ (None, Some(last_valid)) => Ok(last_valid.clone()),
Review Comment:
running slt for different batch sizes, it would be part of bigger thing
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: memoize `LAST_VALUE` and `NTH_VALUE` with `IGNORE NULLS` [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on PR #26664:
URL: https://github.com/apache/datafusion/pull/26664#issuecomment-6380319377
Thanks for the review @jayzhan211
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: memoize `LAST_VALUE` and `NTH_VALUE` with `IGNORE NULLS` [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #26664:
URL: https://github.com/apache/datafusion/pull/26664#discussion_r4366002680
##########
datafusion/functions-window/src/nth_value.rs:
##########
@@ -464,18 +475,55 @@ impl PartitionEvaluator for NthValueEvaluator {
} else {
// FIRST_VALUE, LAST_VALUE, NTH_VALUE window functions take a single column, values will have size 1.
let arr = &values[0];
- let n_range = range.end - range.start;
- if n_range == 0 {
- // We produce None if the window is empty.
- return ScalarValue::try_from(arr.data_type());
- }
- match self.valid_index(arr, range) {
- Some(index) => ScalarValue::try_from_array(arr, index),
- None => ScalarValue::try_from(arr.data_type()),
+ match (self.valid_index(arr, range), &self.last_valid) {
+ (Some(index), _) => ScalarValue::try_from_array(arr, index),
+ (None, Some(last_valid)) => Ok(last_valid.clone()),
Review Comment:
Thanks, added as `window_ignore_nulls_matrix.slt`, which sweeps `batch_size` over 1, 2, 100 and 8192. At `batch_size = 2` each call evaluates two frames, so the query reaches the `take`/`zip` path in `evaluate_ranges` but not this scalar arm. `batch_size = 1` covers the arm. Each path fails its run if empty frames return NULL.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: memoize `LAST_VALUE` and `NTH_VALUE` with `IGNORE NULLS` [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead merged PR #26664:
URL: https://github.com/apache/datafusion/pull/26664
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: memoize `LAST_VALUE` and `NTH_VALUE` with `IGNORE NULLS` [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #26664:
URL: https://github.com/apache/datafusion/pull/26664#discussion_r4365958695
##########
datafusion/functions-window/src/nth_value.rs:
##########
@@ -464,18 +475,55 @@ impl PartitionEvaluator for NthValueEvaluator {
} else {
// FIRST_VALUE, LAST_VALUE, NTH_VALUE window functions take a single column, values will have size 1.
let arr = &values[0];
- let n_range = range.end - range.start;
- if n_range == 0 {
- // We produce None if the window is empty.
- return ScalarValue::try_from(arr.data_type());
- }
- match self.valid_index(arr, range) {
- Some(index) => ScalarValue::try_from_array(arr, index),
- None => ScalarValue::try_from(arr.data_type()),
+ match (self.valid_index(arr, range), &self.last_valid) {
+ (Some(index), _) => ScalarValue::try_from_array(arr, index),
+ (None, Some(last_valid)) => Ok(last_valid.clone()),
Review Comment:
Returning `last_valid` for an empty frame looks right, since memoize only runs with an unbounded start. But no test reaches it: if this arm returns NULL for an empty range, `window.slt` still passes. The existing tests compute `max(v)` in the same operator, which keeps the early rows buffered, so the frame never becomes empty. Could you add a test like this one? It puts the reference in a separate operator and fails if empty frames return NULL.
```sql
statement ok
set datafusion.execution.batch_size = 2;
# The RANGE frame end stalls after each jump of `k`, so a frame can be empty
# once `memoize` has dropped the rows before it
query II
SELECT count(lv), sum(CASE WHEN lv IS DISTINCT FROM mx THEN 1 ELSE 0 END)
FROM (SELECT mx, LAST_VALUE(v) IGNORE NULLS OVER w AS lv
FROM (SELECT k, v, max(v) OVER w AS mx
FROM (SELECT i + 100 * (i / 10) AS k, CASE WHEN i % 7 < 3 THEN i END AS v
FROM (SELECT value AS i FROM generate_series(1, 200)))
WINDOW w AS (ORDER BY k RANGE BETWEEN UNBOUNDED PRECEDING AND 5 PRECEDING))
WINDOW w AS (ORDER BY k RANGE BETWEEN UNBOUNDED PRECEDING AND 5 PRECEDING));
----
195 0
statement ok
reset datafusion.execution.batch_size;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: memoize `LAST_VALUE` and `NTH_VALUE` with `IGNORE NULLS` [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #26664:
URL: https://github.com/apache/datafusion/pull/26664#discussion_r4366001008
##########
datafusion/functions-window/src/nth_value.rs:
##########
@@ -464,18 +475,55 @@ impl PartitionEvaluator for NthValueEvaluator {
} else {
// FIRST_VALUE, LAST_VALUE, NTH_VALUE window functions take a single column, values will have size 1.
let arr = &values[0];
- let n_range = range.end - range.start;
- if n_range == 0 {
- // We produce None if the window is empty.
- return ScalarValue::try_from(arr.data_type());
- }
- match self.valid_index(arr, range) {
- Some(index) => ScalarValue::try_from_array(arr, index),
- None => ScalarValue::try_from(arr.data_type()),
+ match (self.valid_index(arr, range), &self.last_valid) {
+ (Some(index), _) => ScalarValue::try_from_array(arr, index),
+ (None, Some(last_valid)) => Ok(last_valid.clone()),
Review Comment:
actually it is good to have batchsize matrices everywhere in slt IMO.... 🤔
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: memoize `LAST_VALUE` and `NTH_VALUE` with `IGNORE NULLS` [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #26664:
URL: https://github.com/apache/datafusion/pull/26664#discussion_r4365984675
##########
datafusion/functions-window/src/nth_value.rs:
##########
@@ -464,18 +475,55 @@ impl PartitionEvaluator for NthValueEvaluator {
} else {
// FIRST_VALUE, LAST_VALUE, NTH_VALUE window functions take a single column, values will have size 1.
let arr = &values[0];
- let n_range = range.end - range.start;
- if n_range == 0 {
- // We produce None if the window is empty.
- return ScalarValue::try_from(arr.data_type());
- }
- match self.valid_index(arr, range) {
- Some(index) => ScalarValue::try_from_array(arr, index),
- None => ScalarValue::try_from(arr.data_type()),
+ match (self.valid_index(arr, range), &self.last_valid) {
+ (Some(index), _) => ScalarValue::try_from_array(arr, index),
+ (None, Some(last_valid)) => Ok(last_valid.clone()),
Review Comment:
it would make sense to include matrix in this file
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org