Skip to content

fix: last value accumulator merge indexing - #23905

Open
peterxcli wants to merge 4 commits into
apache:mainfrom
peterxcli:fix/last-on-merge-batch
Open

fix: last value accumulator merge indexing#23905
peterxcli wants to merge 4 commits into
apache:mainfrom
peterxcli:fix/last-on-merge-batch

Conversation

@peterxcli

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

TrivialLastValueAccumulator::merge_batch filtered out unset states but then read row 0 from the remaining value array. As a result, merging multiple LAST_VALUE partial states returned the first valid state instead of the last one.

What changes are included in this PR?

  • Select the final valid state row when merging unordered LAST_VALUE states.
  • Extend the existing merge test to verify the evaluated result.

Are these changes tested?

Yes:

cargo test -p datafusion-functions-aggregate test_first_last_state_after_merge

@github-actions github-actions Bot added the functions Changes to functions implementation label Jul 26, 2026
@codecov-commenter

codecov-commenter commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 54.54545% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.66%. Comparing base (9facaaa) to head (3a98268).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/functions-aggregate/src/first_last.rs 54.54% 0 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23905      +/-   ##
==========================================
- Coverage   80.67%   80.66%   -0.01%     
==========================================
  Files        1095     1095              
  Lines      372416   372426      +10     
  Branches   372416   372426      +10     
==========================================
- Hits       300434   300431       -3     
- Misses      54055    54060       +5     
- Partials    17927    17935       +8     

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

@peterxcli
peterxcli marked this pull request as ready for review July 26, 2026 12:17
@peterxcli

Copy link
Copy Markdown
Member Author

cc @comphead, @andygrove, tagging you because you involved in the discussion of apache/datafusion-comet#4131, please take a look if you have time, Thanks!
This is a small change.

@comphead

Copy link
Copy Markdown
Contributor

Checking this

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @peterxcli the PR makes sense to me, and likely other accumulators shouldn't be affected.

For the test it would be nice having another one

  #[test]
  fn test_trivial_last_value_merge_all_flags_false() -> Result<()> {
      let mut acc = TrivialLastValueAccumulator::try_new(&DataType::Int64, false)?;
      let states: Vec<ArrayRef> = vec![
          Arc::new(Int64Array::from(vec![None, None])) as ArrayRef,
          Arc::new(BooleanArray::from(vec![false, false])) as ArrayRef,
      ];
      acc.merge_batch(&states)?;
      assert_eq!(acc.evaluate()?, ScalarValue::Int64(None));
      Ok(())
  }

to test is_set flags

@peterxcli
peterxcli requested a review from comphead July 27, 2026 16:56

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @peterxcli
Since this is a core change I'd like to keep it open to let other committers have a chance to look at it

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.

3 participants