Repository navigation
[MOD-14883] c_trie: add iterate_fuzzy walk over the terms trie - #10879
Conversation
There was a problem hiding this comment.
💡 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".
e48cc93 to
f7c2d38
Compare
There was a problem hiding this comment.
💡 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".
f7c2d38 to
0fb2f26
Compare
There was a problem hiding this comment.
💡 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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
1e3977f to
944c8c4
Compare
944c8c4 to
08132f3
Compare
…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.
08132f3 to
4925390
Compare
…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.
|



Describe the changes in the pull request
Current:
c_triewraps the terms trie's prefix, contains and suffix walks forRust callers, but not its edit-distance walk. The port of
Query_EvalFuzzyNodeneedsthat 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 insiders_token, which is the onlycaller today.
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 theread-ahead model into
string_utils::libnuand havers_tokenuse 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 theTrie_IterateFuzzy/TrieIterator_Next/TrieIterator_Freepull loop and handeach match to a callback, matching the shape of the
iterate_containsanditerate_suffixwalks already there.Two things the wrapper has to get right, and which the tests pin:
Trie_IterateFuzzylowercases and decodes them itself and is also where theTRIE_MAX_PREFIXlimit is applied. A pattern over that limit starts no walk,which the returned
FuzzyWalkreports separately from a walk that ran and matchednothing — the caller has to yield no iterator for the first and an empty union for
the second, so the two must not collapse.
FuzzyDistance, not a bareint. It reaches C as the size of astack 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_DISTANCEwhenFT.SPELLCHECKis parsed); a public Rustwrapper is the first place the value can arrive unbounded, so the type carries the
bound.
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.
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
Main objects this PR modified
src/redisearch_rs/c_wrappers/c_trie/src/lib.rs—CTrieRef::iterate_fuzzy,FuzzyWalk,FuzzyDistancesrc/redisearch_rs/c_wrappers/c_trie/tests/iterate.rssrc/redisearch_rs/string_utils/src/libnu.rs(new),src/redisearch_rs/c_wrappers/rs_token/src/lib.rssrc/redisearch_rs/ffi/build.rsMark if applicable
Release Notes