Skip to content

MOD-19665: Port the COUNT, SUM, AVG, MIN and MAX reducers to Rust - #11648

Merged
Itzikvaknin merged 3 commits into
masterfrom
itzik-rust-numeric-reducers
Oct 5, 2026
Merged

Itzikvaknin merged 3 commits into
masterfrom
itzik-rust-numeric-reducers

Conversation

@Itzikvaknin

@Itzikvaknin Itzikvaknin commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

  1. Current: COLLECT is the only reducer implemented in Rust; COUNT, SUM, AVG, MIN and MAX are still C, and each converts every input row through two calls into Rust (RLookupRow_Get, RSValue_ToNumber).
  2. Change: their per-group logic moves to the reducers crate, following COLLECT's split. C keeps the argument parsing and calls a Rust constructor, and the per-group states live in a bumpalo arena instead of the C block allocator. The five reducers share one shape, a state folded row by row, which is a small Accumulator trait. One set of generic vtable callbacks in reducers_ffi serves all of them.
  3. Outcome: internal only; results are unchanged, including NaN for an empty SUM/AVG, ±infinity for an empty MIN/MAX, and how MIN/MAX treat NaN (the comparisons of C's MIN/MAX macros). Further reducers (STDDEV, COUNT_DISTINCT, FIRST_VALUE, ...) can be ported as one Accumulator each.

Which additional issues this PR fixes

  1. N/A

Main objects this PR modified

  1. reducers::accumulator: the Accumulator trait and AccumulatorReducer, a #[repr(C)] wrapper with the Reducer vtable at offset 0, as in COLLECT.
  2. reducers::{count, sum, min_max}: the five reducers.
  3. reducers_ffi::accumulator: CountReducer_Create, SumReducer_Create, MinMaxReducer_Create, and the generic callbacks.
  4. Value::to_number (first commit): the conversion RSValue_ToNumber performs, extracted so Rust code does not go through the C ABI. It is #[inline]: without the hint, the extracted call measured about 4% slower on an APPLY-heavy aggregation.

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Release Notes

  • This PR requires release notes
  • This PR does not require release notes

If a release note is required (bug fix / new feature / enhancement), describe the user impact of this PR in the title.

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-05T11:28:53.668212Z 5498bc1 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.85714% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.53%. Comparing base (d559dd5) to head (5498bc1).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
...ch_rs/c_entrypoint/reducers_ffi/src/accumulator.rs 98.24% 0 Missing and 1 partial ⚠️
...earch_rs/c_entrypoint/value_ffi/src/conversions.rs 0.00% 1 Missing ⚠️
src/redisearch_rs/value/src/lib.rs 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11648      +/-   ##
==========================================
- Coverage   84.12%   83.53%   -0.59%     
==========================================
  Files         643      673      +30     
  Lines       85128    86222    +1094     
  Branches    39311    40483    +1172     
==========================================
+ Hits        71614    72027     +413     
- Misses      13151    13823     +672     
- Partials      363      372       +9     
Flag Coverage Δ
flow 84.55% <100.00%> (-0.06%) ⬇️
unit 58.88% <94.28%> (-0.18%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Itzikvaknin and others added 2 commits October 4, 2026 16:36
`RSValue_ToNumber` and the comparison code each carried a copy of the
conversion (follow references and trios, parse strings with `str_to_float`).
It is now `Value::to_number`, which both use, so Rust callers can convert
without going through the C ABI.

`to_number` is `#[inline]`: `RSValue_ToNumber` converts every arithmetic
operand of `APPLY`/`FILTER`, and without the hint the extracted call made an
APPLY-heavy aggregation about 4% slower in a release build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The per-group logic of the five numeric reducers moves from `count.c`,
`sum.c` and `minmax.c` to the `reducers` crate. As with COLLECT, C keeps
the argument parsing (`RDCR*_New`) and calls a Rust constructor, and the
per-group states live in a `bumpalo` arena owned by the reducer instead
of the C block allocator.

The three reducers share one shape, a per-group state folded row by row,
which is the new `Accumulator` trait. `AccumulatorReducer` runs one, and
`reducers_ffi` wires a single set of generic vtable callbacks for all of
them, so the unsafe glue exists once. The states must not need dropping,
which `AccumulatorReducer::new` checks at compile time, so the vtable sets no
`FreeInstance`. A test drives the vtable with interleaved groups, so Miri
covers the glue.

Behavior is unchanged: numbers are converted as `RSValue_ToNumber` does,
empty groups give NaN for SUM/AVG and infinities for MIN/MAX, and MIN/MAX keep
the comparisons of C's `MIN`/`MAX` macros, including how they treat NaN.
`reducerId` is now 0 for all five (MIN and MAX used to set their own ids), and
`Reducer.srckey` is left unset; nothing reads either.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Itzikvaknin
Itzikvaknin force-pushed the itzik-rust-numeric-reducers branch from 89779c9 to dce1a7c Compare October 4, 2026 13:36

@raz-mon raz-mon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice!
Looks good, 1 comment from initial review

Comment thread src/redisearch_rs/reducers/src/sum.rs Resolved
@sonarqubecloud

sonarqubecloud Bot commented Oct 5, 2026

Copy link
Copy Markdown

@Itzikvaknin
Itzikvaknin requested a review from raz-mon October 5, 2026 11:58
Comment on lines +76 to +77
/// Boxes an [`AccumulatorReducer`] running `accumulator` and wires its vtable.
fn into_c_reducer<A: Accumulator>(accumulator: A) -> *mut ffi::Reducer {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would add some comment here to better explain this function, e.g.,:

/// Wraps a Rust accumulator in the C reducer interface and transfers ownership
/// to the caller. Callbacks are specialized for `A`, so they can recover the
/// concrete reducer and group-state types from C's raw pointers.
The caller must release
/// the allocation exactly once through its `Free` callback, which drops the
/// entire Rust object, including the arena holding all per-group states.

@Itzikvaknin
Itzikvaknin added this pull request to the merge queue Oct 5, 2026
Merged via the queue into master with commit bc123a8 Oct 5, 2026
61 checks passed
@Itzikvaknin
Itzikvaknin deleted the itzik-rust-numeric-reducers branch October 5, 2026 13:47
@oshadmi oshadmi changed the title Port the COUNT, SUM, AVG, MIN and MAX reducers to Rust MOD-19665: Port the COUNT, SUM, AVG, MIN and MAX reducers to Rust Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants