Repository navigation
[MOD-19060] QN_TAG: add Rust APIs to rewrite tag tokens in place - #11606
Conversation
Codecov Report❌ Patch coverage is
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
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:
|
32cac0c to
18fc954
Compare
| unsafe { | ||
| ffi::tag_strtolower( | ||
| &raw mut self.tok.str_, | ||
| &raw mut self.tok.len, | ||
| i32::from(case_sensitive), | ||
| ) | ||
| } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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... 🤷
There was a problem hiding this comment.
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.
18fc954 to
3d86e24
Compare
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.
3d86e24 to
9a8fafb
Compare
I already trimmed it actually; did a second pass now.
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.
9a8fafb to
4fa63e0
Compare
|
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. |
f704d03 to
14ce805
Compare
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.
14ce805 to
1372f41
Compare
|
JonasKruckenberg
left a comment
There was a problem hiding this comment.
gnarly code indeed, but looks all correct to me 👍🏻



Describe the changes in the pull request
QueryNodeMut::token_mutdeliberately refusesQN_TOKENnodes, because query expansion can leave them borrowing read-only, length-delimited strings.QueryNodeMut::token_node_mutis a newunsafeaccessor for exactly theQN_TOKENnodestoken_mutdeclines.RSTokenMut::normalize_tagwrapstag_strtolower. A new invariant (5) onQueryNodeMut::newrecords 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.QN_TAGevaluation to Rust.Will be used in #11206
Main objects this PR modified
QueryNodeMut: newtoken_node_mut, and invariant (5) onnew, the soundness argument to check first.RSTokenMut: newnormalize_tag, with its allocator requirement when lowercasing. The constructor contract now states that the terminator byte must be writable.RedisTokenchild variant is gone.Mark if applicable
Release Notes