airborne12 commented on code in PR #70265:
URL: https://github.com/apache/doris/pull/70265#discussion_r4637644893


##########
be/src/storage/index/inverted/analysis/tokenizer/standard/uax29_tokenizer.cpp:
##########
@@ -0,0 +1,125 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "storage/index/inverted/analysis/tokenizer/standard/uax29_tokenizer.h"
+
+#include <algorithm>
+#include <array>
+#include <cstdint>
+
+#include "common/logging.h"
+#include "storage/index/inverted/analysis/scratch.h"
+#include "storage/index/inverted/analysis/token_batch.h"
+#include "storage/index/inverted/analysis/tokenizer/byte_classifier.h"
+#include "storage/index/inverted/analysis/tokenizer/standard/uax29_table.h"
+#include "storage/index/inverted/analysis/tokenizer/utf8_validator.h"
+#include "util/utf8_check.h"
+
+namespace doris::segment_v2::inverted_index {
+
+namespace {
+
+constexpr std::array<std::string_view, 33> STOP_WORDS = {
+        "a",   "an",    "and",  "are",   "as",    "at",   "be",   "but", "by", 
 "for",  "if",
+        "in",  "into",  "is",   "it",    "no",    "not",  "of",   "on",  "or", 
 "such", "that",
+        "the", "their", "then", "there", "these", "they", "this", "to",  
"was", "will", "with"};
+constexpr size_t LONGEST_STOP_WORD = 5;
+
+// A term of at most 7 bytes as one integer: its bytes from the low end and 
its length in the top
+// byte, so that terms of different lengths never compare equal.
+constexpr uint64_t pack_term(std::string_view term) {
+    uint64_t key = static_cast<uint64_t>(term.size()) << 56;
+    for (size_t i = 0; i < term.size(); ++i) {
+        key |= static_cast<uint64_t>(static_cast<uint8_t>(term[i])) << (8 * i);
+    }
+    return key;
+}
+
+// Sorted for binary search; most tokens leave on the length test before it.
+constexpr std::array<uint64_t, STOP_WORDS.size()> STOP_WORD_KEYS = [] {
+    std::array<uint64_t, STOP_WORDS.size()> keys {};
+    for (size_t i = 0; i < STOP_WORDS.size(); ++i) {
+        keys[i] = pack_term(STOP_WORDS[i]);
+    }
+    std::ranges::sort(keys);
+    return keys;
+}();
+
+} // namespace
+
+std::span<const std::string_view> standard_stop_words() {
+    return STOP_WORDS;
+}
+
+bool is_standard_stop_word(std::string_view term) {
+    return term.size() <= LONGEST_STOP_WORD &&
+           std::ranges::binary_search(STOP_WORD_KEYS, pack_term(term));
+}
+
+Uax29Tokenizer::Uax29Tokenizer(bool lowercase, bool stop_words)
+        : _table(Uax29Table::instance()), _lowercase(lowercase), 
_stop_words(stop_words) {}
+
+void Uax29Tokenizer::reset(std::string_view text) {
+    _pos = 0;
+    // Strict UTF-8 passing decides alone; the lenient rule decides the rest, 
the overlong forms.
+    if (!doris::validate_utf8(text.data(), text.size()) && 
!lenient_utf8_valid(text)) {

Review Comment:
   Confirmed and fixed in 1fe9d63807e.
   
   Reproduction: the new test 
`Uax29TokenizerTest.AnInvalidValueReleasesTheCopyOfALongOne` resets a 
lowercasing standard stream with a 256 KiB valid value, then with `"abc\xFF"`. 
Before the fix, `reserved_bytes()` stayed at 262144 bytes after the invalid 
reset, above `ANALYZER_SCRATCH_HIGH_WATER_BYTES` (65536).
   
   Fix: `Uax29Tokenizer::reset()` now calls 
`release_oversized_scratch(_lowered)` before the early returns, so an invalid 
value releases the copy that the previous value left.
   
   Verification (BE UT, ASAN): the new test passes, and the Uax29, 
Utf8Validator, standard tokenizer, component stream, CLucene adapter, analyzer, 
golden and differential suites pass: 82 tests, 81 passed and 1 opt-in test 
skipped.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to