Skip to content

perf: avoid processing discarded rows for negative nth_value - #24453

Open
lyne7-sc wants to merge 4 commits into
apache:mainfrom
lyne7-sc:perf/nth-value-negative-tail
Open

perf: avoid processing discarded rows for negative nth_value#24453
lyne7-sc wants to merge 4 commits into
apache:mainfrom
lyne7-sc:perf/nth-value-negative-tail

Conversation

@lyne7-sc

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • Closes #.

Rationale for this change

For negative n, NTH_VALUE only needs to retain the last n rows. However, each update currently converts and appends every row in the input batch before discarding rows that cannot affect the result.

This adds unnecessary work for large batches, especially when n is small.

What changes are included in this PR?

  • Retain only the required portion of the existing accumulator state.
  • Append only the trailing input rows that can affect the result.
  • Apply the optimization to both trivial and ordered accumulators.

Are these changes tested?

Yes.

Are there any user-facing changes?

No.

Benchmark

group                       main                                     optimized
-----                       ----                                     ---------
nth_value/ordered/n=-1      5154.01     6.4±0.19ms        ? ?/sec    1.00  1233.7±449.90ns        ? ?/sec
nth_value/ordered/n=-10     819.35     6.5±0.25ms        ? ?/sec     1.00      7.9±0.14µs        ? ?/sec
nth_value/ordered/n=-100    87.44     6.4±0.18ms        ? ?/sec      1.00     73.7±1.80µs        ? ?/sec
nth_value/ordered/n=1       1.00   241.0±24.66ns        ? ?/sec      1.04   251.0±22.94ns        ? ?/sec
nth_value/ordered/n=10      1.13  1375.2±281.33ns        ? ?/sec     1.00  1219.0±51.07ns        ? ?/sec
nth_value/ordered/n=100     1.03      8.4±0.74µs        ? ?/sec      1.00      8.2±0.18µs        ? ?/sec
nth_value/trivial/n=-1      6821.05     3.3±0.24ms        ? ?/sec    1.00   485.5±20.29ns        ? ?/sec
nth_value/trivial/n=-10     822.65     3.4±0.33ms        ? ?/sec     1.00      4.1±0.10µs        ? ?/sec
nth_value/trivial/n=-100    82.35     3.3±0.26ms        ? ?/sec      1.00     40.2±4.02µs        ? ?/sec
nth_value/trivial/n=1       1.00     97.0±1.56ns        ? ?/sec      1.03   100.1±10.38ns        ? ?/sec
nth_value/trivial/n=10      1.01   599.9±38.67ns        ? ?/sec      1.00   591.2±22.46ns        ? ?/sec
nth_value/trivial/n=100     1.06      4.5±0.50µs        ? ?/sec      1.00      4.3±0.19µs        ? ?/sec

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Aug 18, 2026
@codecov-commenter

codecov-commenter commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.24%. Comparing base (c429919) to head (0adf1bc).

Files with missing lines Patch % Lines
datafusion/functions-aggregate/src/nth_value.rs 77.77% 0 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24453      +/-   ##
==========================================
- Coverage   81.24%   81.24%   -0.01%     
==========================================
  Files        1113     1113              
  Lines      392744   392744              
  Branches   392744   392744              
==========================================
- Hits       319090   319087       -3     
+ Misses      54900    54897       -3     
- Partials    18754    18760       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants