Skip to content

Conversation

@mzabaluev
Copy link

Rationale for this change

The PartitionEvaluator implementation for NthValue in DataFusion has a few shortcomings:

  • When nulls are ignored (meaning the count should skip over them), the evaluation collects an array of all valid indices, to select at most one index accordingly to the First/Last/Nth case.
  • The memoize implementation gives up in the same condition, even after performing part of the logic!

What changes are included in this PR?

Use only as much iteration over the valid indices as needed for the function case, without collecting all indices.
The memoize implementation does the right thing for FirstValue with ignore_nulls set to true, or returns early for other function cases.

Are these changes tested?

All existing tests pass for FirstValue/LastValue/NthValue.

Are there any user-facing changes?

No.

Instead of collecting all valid indices per batch in PartitionEvaluator
for NthValue, use the iterator as appropriate for the case.
Even tn the worst case of negative index larger than 1, only a sliding
window of N last valid indices is needed.
Handle the case when FirstValue is called with ignore_nulls set to true,
can prune the partition on the first non-null value.
Also return early for the other function cases in the same condition,
rather than grinding some logic only to discard the results.
@github-actions github-actions bot added the functions Changes to functions implementation label Dec 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant