Skip to content

fix: correct percentile_cont(DISTINCT) accumulation and sliding-window retract - #23913

Open
viirya wants to merge 1 commit into
apache:mainfrom
viirya:fix-distinct-percentile-retract
Open

fix: correct percentile_cont(DISTINCT) accumulation and sliding-window retract#23913
viirya wants to merge 1 commit into
apache:mainfrom
viirya:fix-distinct-percentile-retract

Conversation

@viirya

@viirya viirya commented Jul 27, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

DistinctPercentileContAccumulator reused the shared set-based GenericDistinctBuffer, which doesn't fit it: (1) the buffer asserts a single input column but percentile_cont passes two (value + percentile), so every percentile_cont(DISTINCT ...) panicked; (2) the buffer is a plain HashSet with no multiplicity, so sliding-window retract_batch dropped a value while duplicates were still in the frame.

What changes are included in this PR?

  • Replace the shared buffer in this accumulator with a per-accumulator HashMap<Hashable, usize> count map: update_batch reads only the value column and increments; retract_batch decrements and removes a key only at zero; state/merge_batch keep the same List state shape. Other GenericDistinctBuffer users are untouched.
  • Regression tests in aggregate.slt for the plain distinct query and the sliding-window duplicate-retract case.

Are these changes tested?

Yes — new regression tests; the full aggregate.slt suite passes.

Are there any user-facing changes?

percentile_cont(DISTINCT ...) now works instead of panicking, and returns correct results in sliding windows.

…w retract

`DistinctPercentileContAccumulator` had two bugs:

1. Panic on every distinct query. `update_batch` forwarded all argument
   columns to `GenericDistinctBuffer::update_batch`, which asserts a single
   input array. `percentile_cont` always passes two columns (the value and the
   percentile literal), so any `percentile_cont(DISTINCT x, p)` panicked with
   "DistinctValuesBuffer::update_batch expects only a single input array".

2. Wrong results in sliding windows. The buffer was a plain `HashSet` with no
   per-value multiplicity, so `retract_batch` removed a value entirely when a
   single occurrence left the window frame, even if other rows in the frame
   still carried that value.

Replace the shared set-based buffer with a per-accumulator
`HashMap<Hashable, usize>` count map: `update_batch` reads only the value
column and increments counts, `retract_batch` decrements and removes a key
only at zero, and `state`/`merge_batch` serialize the distinct keys as a List
(unchanged state shape). The other `GenericDistinctBuffer` users (count/sum/
median/variance distinct) are unaffected; only percentile_cont supports
retract, so it is the sole accumulator that needed multiplicity tracking.

Add regression coverage in aggregate.slt for the plain distinct aggregate and
for the sliding-window duplicate-value retract case.

Co-authored-by: Claude Code
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Jul 27, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 41.37931% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.67%. Comparing base (77b172e) to head (3499659).

Files with missing lines Patch % Lines
...afusion/functions-aggregate/src/percentile_cont.rs 41.37% 15 Missing and 2 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #23913   +/-   ##
=======================================
  Coverage   80.67%   80.67%           
=======================================
  Files        1094     1094           
  Lines      371770   371790   +20     
  Branches   371770   371790   +20     
=======================================
+ Hits       299907   299930   +23     
+ Misses      53964    53954   -10     
- Partials    17899    17906    +7     

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

percentile_cont(DISTINCT ...) panics on every query, and returns wrong results in sliding windows

2 participants