Skip to content

[MOD-14883] c_trie: add iterate_fuzzy walk over the terms trie - #10879

Merged
gdesmott merged 4 commits into
masterfrom
gd_c_trie_iterate_fuzzy
Aug 13, 2026
Merged

gdesmott merged 4 commits into
masterfrom
gd_c_trie_iterate_fuzzy

Conversation

@gdesmott

@gdesmott gdesmott commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

  1. Current: c_trie wraps the terms trie's prefix, contains and suffix walks for
    Rust callers, but not its edit-distance walk. The port of Query_EvalFuzzyNode needs
    that walk, and has no way to reach it. Separately, the guards around libnu's decoder —
    how far past the end of a sequence it reads (NU_MAX_READAHEAD, nu_seq_len,
    tail_may_overread) — live as private items inside rs_token, which is the only
    caller today.

  2. Change: two commits, split out of [MOD-14883] query_eval: evaluate QN_FUZZY in Rust #10860 so the wrapper lands on its own:

    • string_utils: share the libnu read-ahead model out of rs_token — move the
      read-ahead model into string_utils::libnu and have rs_token use it from there.
      No behaviour change; the items are the same, only their home is.
    • c_trie: add iterate_fuzzy walk over the terms trie — wrap the
      Trie_IterateFuzzy / TrieIterator_Next / TrieIterator_Free pull loop and hand
      each match to a callback, matching the shape of the iterate_contains and
      iterate_suffix walks already there.

    Two things the wrapper has to get right, and which the tests pin:

    • The pattern is passed as raw token bytes rather than runes, because
      Trie_IterateFuzzy lowercases and decodes them itself and is also where the
      TRIE_MAX_PREFIX limit is applied. A pattern over that limit starts no walk,
      which the returned FuzzyWalk reports separately from a walk that ran and matched
      nothing — the caller has to yield no iterator for the first and an empty union for
      the second, so the two must not collapse.
    • The distance is a FuzzyDistance, not a bare int. It reaches C as the size of a
      stack variable-length array, so a negative one is a negative-length allocation and
      a large one exhausts the stack before the walk starts — neither is checkable after
      the fact. C bounds it at its entry points instead (structurally in the grammar,
      and against MAX_LEV_DISTANCE when FT.SPELLCHECK is parsed); a public Rust
      wrapper is the first place the value can arrive unbounded, so the type carries the
      bound.
    • A token is a byte string that nothing validates as UTF-8, so a pattern can end
      mid-sequence, and the C decoder reads a fixed 2–4 bytes off a lead byte without
      checking how many are left. Where that can happen the pattern is handed to C as a
      private zero-padded copy; the length passed stays the pattern's own, so the decode
      and the rune-length threshold are unchanged. Well-formed patterns are decoded in
      place, and an over-long pattern is refused on byte length before anything
      proportional to it is copied.
  3. Outcome: the edit-distance walk is reachable from Rust behind a documented safe
    shape, with the read-ahead model stated once instead of twice. No user-visible change
    — nothing calls the new wrapper yet; [MOD-14883] query_eval: evaluate QN_FUZZY in Rust #10860 is the caller.

Which additional issues this PR fixes

  1. MOD-14883

Main objects this PR modified

  1. src/redisearch_rs/c_wrappers/c_trie/src/lib.rs — CTrieRef::iterate_fuzzy, FuzzyWalk, FuzzyDistance
  2. src/redisearch_rs/c_wrappers/c_trie/tests/iterate.rs
  3. src/redisearch_rs/string_utils/src/libnu.rs (new), src/redisearch_rs/c_wrappers/rs_token/src/lib.rs
  4. src/redisearch_rs/ffi/build.rs

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: db0a288c61

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@gdesmott
gdesmott force-pushed the gd_c_trie_iterate_fuzzy branch 2 times, most recently from e48cc93 to f7c2d38 Compare August 12, 2026 08:01

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f7c2d38cd3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@gdesmott
gdesmott force-pushed the gd_c_trie_iterate_fuzzy branch from f7c2d38 to 0fb2f26 Compare August 12, 2026 08:15

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0fb2f267cb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@codecov

codecov Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.96907% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.23%. Comparing base (1fec8e2) to head (560e7e2).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/redisearch_rs/c_wrappers/c_trie/src/lib.rs 98.36% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10879      +/-   ##
==========================================
- Coverage   83.19%   82.23%   -0.96%     
==========================================
  Files         608      633      +25     
  Lines       81025    82195    +1170     
  Branches    36273    37443    +1170     
==========================================
+ Hits        67405    67595     +190     
- Misses      13274    14254     +980     
  Partials      346      346              
Flag Coverage Δ
flow 84.35% <ø> (-0.02%) ⬇️
unit 55.77% <98.96%> (-0.56%) ⬇️

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.

@gdesmott
gdesmott force-pushed the gd_c_trie_iterate_fuzzy branch 2 times, most recently from 1e3977f to 944c8c4 Compare August 12, 2026 08:44
@LukeMathWalker
LukeMathWalker requested a review from memark August 12, 2026 10:05
@gdesmott
gdesmott force-pushed the gd_c_trie_iterate_fuzzy branch from 944c8c4 to 08132f3 Compare August 12, 2026 11:57
…token

rs_token pads around libnu's decoder because that decoder reads ahead past the
end of a sequence, and the model of how far it can read — NU_MAX_READAHEAD,
nu_seq_len, tail_may_overread — has lived in rs_token as private items.

The fuzzy walk added next has to hand bytes to the same decoder and so needs
the same guards. Move them into string_utils::libnu and have rs_token use them
from there, so the two callers share one copy: a second copy is one a later fix
to the first would leave behind, and being wrong about how far the decoder
reads is a read past the end of a buffer.

No behaviour change — the items are the same, only their home is.
The QN_FUZZY port needs the trie's edit-distance walk from Rust. Wrap the
Trie_IterateFuzzy / TrieIterator_Next / TrieIterator_Free pull loop inside
c_trie and hand each match to a callback, matching the shape of the
iterate_contains and iterate_suffix walks already there.

The pattern is passed as raw token bytes rather than runes: Trie_IterateFuzzy
lowercases and decodes them itself, and it is also where the TRIE_MAX_PREFIX
length limit is applied. A pattern over that limit starts no walk, which the
returned FuzzyWalk reports separately from a walk that ran and matched nothing
— the caller has to yield no iterator for the first and an empty union for the
second, so the two must not collapse.

The per-match edit distance and payload out-params are passed as null, which
both the iterator and the edit-distance filter check for before writing.

Folding the pattern goes through libnu's decoder, so the read-ahead guards
shared out of rs_token in the previous change are used here rather than copied.

The distance is taken as a FuzzyDistance rather than a bare int. It reaches C as
the size of a stack variable-length array, so a negative one is a
negative-length allocation and a large one exhausts the stack before the walk
starts — neither is something a caller can check for afterwards. C bounds it at
the entry points instead, structurally in the grammar and against
MAX_LEV_DISTANCE when FT.SPELLCHECK is parsed; a public Rust wrapper is the
first place the value can arrive unbounded, so the type carries the bound.
@gdesmott
gdesmott force-pushed the gd_c_trie_iterate_fuzzy branch from 08132f3 to 4925390 Compare August 13, 2026 10:19
…ce bound

The largest edit distance an automaton can be built for was spelled twice: as
a bare 4 inside SpellCheckCommand in module.c, and again as FuzzyDistance::MAX
in the Rust c_trie wrapper. Nothing linked the two, so raising one would leave
FT.SPELLCHECK accepting a distance the wrapper refuses.

Move the constant to trie/levenshtein.h, next to the SparseAutomaton whose
stack VLA is what actually bounds it, and have both the command parser and
FuzzyDistance::MAX read it from there.
FuzzyDistance made an out-of-range edit distance unrepresentable, but every
caller had to construct one first and every test had to wrap its literal, for
a range check that only ever mattered at one call site.

Drop the type and take the distance as the i32 it already travels at, checked
before anything reaches the automaton. InvalidFuzzyDistance stays and is now
returned by iterate_fuzzy, so the walk answers with a Result whose Err arm is
the same refusal the type used to encode.
@gdesmott
gdesmott requested a review from memark August 13, 2026 13:19
@gdesmott
gdesmott enabled auto-merge August 13, 2026 13:26
@sonarqubecloud

Copy link
Copy Markdown

@gdesmott
gdesmott added this pull request to the merge queue Aug 13, 2026
Merged via the queue into master with commit 6decad6 Aug 13, 2026
59 checks passed
@gdesmott
gdesmott deleted the gd_c_trie_iterate_fuzzy branch August 13, 2026 14:10
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