Skip to content

[MOD-19060] QN_TAG: add Rust APIs to rewrite tag tokens in place - #11606

Merged
gdesmott merged 3 commits into
masterfrom
gdesmott-tag-token-rewrite
Oct 5, 2026
Merged

gdesmott merged 3 commits into
masterfrom
gdesmott-tag-token-rewrite

Conversation

@gdesmott

@gdesmott gdesmott commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

  1. Current: Tag evaluation rewrites its tokens in place: it removes query escapes and lowercases them, which may free and replace the buffer. The Rust query wrappers have no way to do that. QueryNodeMut::token_mut deliberately refuses QN_TOKEN nodes, because query expansion can leave them borrowing read-only, length-delimited strings.
  2. Change: QueryNodeMut::token_node_mut is a new unsafe accessor for exactly the QN_TOKEN nodes token_mut declines. RSTokenMut::normalize_tag wraps tag_strtolower. A new invariant (5) on QueryNodeMut::new records why this is sound under a tag node: expansion skips tag subtrees, and the parser allocates their tokens with the Redis module allocator. The query evaluation FFI entry points pass that invariant on to their callers.
  3. Outcome: Internal-only, with no behavior change. It is a prerequisite for porting QN_TAG evaluation to Rust.

Will be used in #11206

Main objects this PR modified

  1. QueryNodeMut: new token_node_mut, and invariant (5) on new, the soundness argument to check first.
  2. RSTokenMut: new normalize_tag, with its allocator requirement when lowercasing. The constructor contract now states that the terminator byte must be writable.
  3. Tag evaluation tests: every mock token is now Redis-allocated, as invariant (5) requires, so the separate RedisToken child variant is gone.

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

@gdesmott gdesmott changed the title QN_TAG: add Rust APIs to rewrite tag tokens in place [MOD-19060] QN_TAG: add Rust APIs to rewrite tag tokens in place Sep 29, 2026
@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.10983% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.56%. Comparing base (c8f9d94) to head (1372f41).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
src/util/strconv.c 0.00% 4 Missing ⚠️
src/redisearch_rs/string_utils/src/libnu.rs 98.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11606      +/-   ##
==========================================
- Coverage   84.24%   83.56%   -0.69%     
==========================================
  Files         640      665      +25     
  Lines       85448    86724    +1276     
  Branches    39264    40501    +1237     
==========================================
+ Hits        71988    72473     +485     
- Misses      13094    13878     +784     
- Partials      366      373       +7     
Flag Coverage Δ
flow 84.61% <0.00%> (-0.02%) ⬇️
unit 59.03% <97.10%> (-0.34%) ⬇️

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 gdesmott-tag-token-rewrite branch from 32cac0c to 18fc954 Compare September 29, 2026 12:20
Comment on lines +392 to +398
unsafe {
ffi::tag_strtolower(
&raw mut self.tok.str_,
&raw mut self.tok.len,
i32::from(case_sensitive),
)
}

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.

look I know this might bloat the PR scope, but tag_strtolower is a horrible function. Its a conditional tolower that also does things its name doesn't suggest and also forces us into the NUL terminator rewrite... We already have tolower_cow and the builtins for unicode lowering and could easily split out the bit of backslash and whitespace handling into a separate function no?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

so I gave this a shot. It's far more work than anticipated, because unfortunately tags can be invalid utf8 so we need to convert manually but it does work.

See the 3rd commit. That's a significant bit of code but I think it's not that bad, and we may eventually need those at some point anyway so... 🤷

@JonasKruckenberg JonasKruckenberg 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.

The docs here are too verbose and duplicate each other quite a bit, I would centralize and clean this up so we document each invariant only in one place.

I also personally don't really like this whole terminator rewriting business, that just sounds like trouble down the line, but if it's the only good solution then fair enough. the code itself looks pretty solid.

@gdesmott
gdesmott force-pushed the gdesmott-tag-token-rewrite branch from 18fc954 to 3d86e24 Compare September 30, 2026 09:48
Add QueryNodeMut::token_node_mut, which admits exactly the QN_TOKEN nodes
token_mut declines, and extend QueryNodeMut::new with the invariant covering
the tokens tag evaluation rewrites in place. The query evaluation entry points
now pass that invariant on to their callers.

The tag evaluation tests now build every token with the Redis module
allocator, as the parser does, rather than only the one whose lowercased form
outgrows its buffer: on a case-insensitive field the new invariant requires
it, since lowercasing may free and replace the buffer. The separate
RedisToken child variant is therefore gone.
@gdesmott
gdesmott force-pushed the gdesmott-tag-token-rewrite branch from 3d86e24 to 9a8fafb Compare September 30, 2026 12:53
@gdesmott

Copy link
Copy Markdown
Collaborator Author

The docs here are too verbose and duplicate each other quite a bit, I would centralize and clean this up so we document each invariant only in one place.

I already trimmed it actually; did a second pass now.

I also personally don't really like this whole terminator rewriting business, that just sounds like trouble down the line, but if it's the only good solution then fair enough. the code itself looks pretty solid.

Agreed, but I'm afraid this will have to do until we ported all the caller code.

Add RSTokenMut::normalize_tag, wrapping tag_strtolower to remove query
escapes and optionally lowercase a tag token.
@gdesmott
gdesmott force-pushed the gdesmott-tag-token-rewrite branch from 9a8fafb to 4fa63e0 Compare September 30, 2026 14:26
@github-actions github-actions Bot added size:XL and removed size:M labels Sep 30, 2026
@github-actions

Copy link
Copy Markdown

This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@gdesmott
gdesmott force-pushed the gdesmott-tag-token-rewrite branch 2 times, most recently from f704d03 to 14ce805 Compare September 30, 2026 14:51
Normalizing a tag token takes two steps, now each in string_utils:
tag::unescape removes the query escapes, and unicode::tolower_bytes
lowercases the result exactly as the C unicode_tolower does, malformed
UTF-8 included, so query tag values keep matching what indexing
lowercased. tolower_bytes builds on new libnu::nu_utf8_decode and
nu_utf8_encode models of the lax C codec. Property tests compare the
codec, tolower_bytes, and the two steps together against the C
functions on arbitrary input, through new strconv.h test wrappers for
nu_utf8_read and nu_utf8_write.

The one deliberate difference: C removes escapes only up to an interior
NUL yet shortens the length by the escapes it removed, garbling the bytes
after it; unescape keeps them verbatim.

RSTokenMut::normalize_tag now runs the two steps instead of the C
tag_strtolower, writing the result back in place when it fits the
token's buffer and replacing the buffer only when lowercasing makes it
longer.

tag::strtolower, which had no callers outside its tests, is gone; its
tests now cover the two steps.
@gdesmott
gdesmott force-pushed the gdesmott-tag-token-rewrite branch from 14ce805 to 1372f41 Compare October 1, 2026 07:03
@sonarqubecloud

sonarqubecloud Bot commented Oct 1, 2026

Copy link
Copy Markdown

@JonasKruckenberg JonasKruckenberg 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.

gnarly code indeed, but looks all correct to me 👍🏻

@gdesmott
gdesmott added this pull request to the merge queue Oct 5, 2026
Merged via the queue into master with commit d9ca954 Oct 5, 2026
61 checks passed
@gdesmott
gdesmott deleted the gdesmott-tag-token-rewrite branch October 5, 2026 12:31
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