This is an automated email from the ASF dual-hosted git repository.

airborne12 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 769d7c33946 [opt](inverted index) Skip terms without the literal part 
of a leading-wildcard glob (#70373)
769d7c33946 is described below

commit 769d7c339461ae5bd022862594931bc3198009b8
Author: Jack <[email protected]>
AuthorDate: Wed Nov 25 09:42:58 2026 +0800

    [opt](inverted index) Skip terms without the literal part of a 
leading-wildcard glob (#70373)
    
    ### What problem does this PR solve?
    
    Issue Number: None
    
    Related PR: None
    
    Problem Summary: A wildcard that starts with `*` or `?`, such as
    `*cache*`, walks the whole term dictionary and matches every term. Such
    a glob now names its longest literal run as text every match holds, so
    CLucene and SNII skip the other terms before converting or matching
    them, and the matcher compares one-byte characters in place. `*cache*`
    over 100M rows: CPU 26.7 to 14.8 s on V3, 19.6 to 11.0 s on SNII.
---
 .../index/inverted/query/exec/term_pattern.cpp     | 26 ++++++++++
 .../index/inverted/query/exec/term_pattern.h       | 12 +++--
 .../inverted/query/engine/wildcard_query_test.cpp  | 34 +++++++++++++
 .../snii/reader/snii_index_source_test.cpp         | 17 +++++++
 .../inverted/snii/snii_b2_t08_wildcard_test.cpp    | 57 ++++++++++++++++++++++
 5 files changed, 143 insertions(+), 3 deletions(-)

diff --git a/be/src/storage/index/inverted/query/exec/term_pattern.cpp 
b/be/src/storage/index/inverted/query/exec/term_pattern.cpp
index 7ac229fad27..79ce086071f 100644
--- a/be/src/storage/index/inverted/query/exec/term_pattern.cpp
+++ b/be/src/storage/index/inverted/query/exec/term_pattern.cpp
@@ -57,6 +57,24 @@ std::string regexp_enumeration_prefix(std::string_view 
pattern) {
     return smallest;
 }
 
+// The longest run of a glob without '*' or '?', such as "cde" for "*ab?cde*". 
Every term the glob
+// matches holds it as consecutive bytes: the matcher consumes a literal run 
one character at a time
+// against consecutive text characters, and a valid UTF-8 run cannot match 
starting inside another
+// character.
+std::string_view longest_literal_run(std::string_view pattern) {
+    std::string_view longest;
+    size_t start = 0;
+    for (size_t i = 0; i <= pattern.size(); ++i) {
+        if (i == pattern.size() || pattern[i] == '*' || pattern[i] == '?') {
+            if (i - start > longest.size()) {
+                longest = pattern.substr(start, i - start);
+            }
+            start = i + 1;
+        }
+    }
+    return longest;
+}
+
 int on_match(unsigned int /*id*/, unsigned long long /*from*/, unsigned long 
long /*to*/,
              unsigned int /*flags*/, void* context) {
     *static_cast<bool*>(context) = true;
@@ -91,6 +109,14 @@ Status TermPattern::create(TermPatternKind kind, 
std::string_view pattern, TermP
                     "wildcard pattern is not valid UTF-8");
         }
         out->_enumeration_prefix = pattern.substr(0, 
pattern.find_first_of("*?"));
+        // A glob that starts with '*' or '?' enumerates the whole dictionary: 
"*cache*" visits
+        // about two million terms of a 6M-row TextBench segment to keep three 
of them. The literal
+        // run lets a format drop the others with a substring search before it 
decodes, converts or
+        // matches them. A glob with a prefix already stops where the prefix 
ends, and CLucene
+        // checks the prefix only after this text, so it keeps none.
+        if (out->_enumeration_prefix.empty()) {
+            out->_text = longest_literal_run(pattern);
+        }
         return Status::OK();
     }
     if (is_hyperscan_regexp_expensive(pattern)) {
diff --git a/be/src/storage/index/inverted/query/exec/term_pattern.h 
b/be/src/storage/index/inverted/query/exec/term_pattern.h
index 862b2235b62..0442292bfea 100644
--- a/be/src/storage/index/inverted/query/exec/term_pattern.h
+++ b/be/src/storage/index/inverted/query/exec/term_pattern.h
@@ -93,7 +93,12 @@ public:
                 ++p;
                 t += text_width;
             } else if (p < _pattern.size() && width(_pattern[p]) == text_width 
&&
-                       std::memcmp(_pattern.data() + p, text.data() + t, 
text_width) == 0) {
+                       // A one-byte character is compared in place: memcmp of 
a runtime length is a
+                       // library call per character, and '*cache*' tries 'c' 
at every position of
+                       // every term it is matched against.
+                       (text_width == 1 ? _pattern[p] == text[t]
+                                        : std::memcmp(_pattern.data() + p, 
text.data() + t,
+                                                      text_width) == 0)) {
                 p += text_width;
                 t += text_width;
             } else if (star_p != std::string_view::npos) {
@@ -136,8 +141,9 @@ public:
 
     const std::string& enumeration_prefix() const { return 
_enumeration_prefix; }
 
-    // Text every matching term holds, empty when the pattern names none. A 
format may skip the
-    // terms without it before decoding them.
+    // Text every matching term holds, or empty. A format skips the terms 
without it before decoding
+    // them: kSuffix and kContains take their text, and a glob that enumerates 
the whole dictionary
+    // takes its longest literal run ("cache" for "*cache*").
     const std::string& required_text() const { return _text; }
 
     // False when no term can match, as for a regular expression Hyperscan 
cannot compile.
diff --git 
a/be/test/storage/index/inverted/query/engine/wildcard_query_test.cpp 
b/be/test/storage/index/inverted/query/engine/wildcard_query_test.cpp
index 5f3faf0ecd3..16a799b29a1 100644
--- a/be/test/storage/index/inverted/query/engine/wildcard_query_test.cpp
+++ b/be/test/storage/index/inverted/query/engine/wildcard_query_test.cpp
@@ -19,9 +19,11 @@
 #include <gtest/gtest.h>
 
 #include <algorithm>
+#include <iterator>
 #include <memory>
 #include <roaring/roaring.hh>
 #include <string>
+#include <vector>
 
 #include "common/status.h"
 #include "io/fs/local_file_system.h"
@@ -31,6 +33,7 @@
 #include "storage/index/inverted/clucene/spi/clucene_index_source.h"
 #include "storage/index/inverted/common/string_helper.h"
 #include "storage/index/inverted/query/engine/expand_query/expand_query.h"
+#include "storage/index/inverted/query/exec/term_pattern.h"
 #include "storage/index/inverted/query/scoring/collection_similarity.h"
 #include "storage/index/inverted/reader/collection_statistics.h"
 
@@ -543,4 +546,35 @@ TEST_F(WildcardQueryV2Test, 
test_wildcard_query_move_semantics) {
     ASSERT_NE(weight2, nullptr);
 }
 
+// A glob from the start of the dictionary skips the terms without its longest 
literal run before
+// it converts them, and still expands to every term it matches, in dictionary 
order.
+TEST_F(WildcardQueryV2Test, 
test_wildcard_leading_glob_expands_to_every_matching_term) {
+    auto* dir = FSDirectory::getDirectory(kTestDir.c_str());
+    auto reader_holder = 
make_shared_reader(lucene::index::IndexReader::open(dir, true));
+    ASSERT_TRUE(reader_holder != nullptr);
+    auto source = clucene_index_source(reader_holder, 
StringHelper::to_wstring("field"), nullptr);
+    const auto expand = [&](const std::string& glob) {
+        index_query::TermPattern pattern;
+        
EXPECT_TRUE(index_query::TermPattern::create(index_query::TermPatternKind::kWildcard,
 glob,
+                                                     &pattern)
+                            .ok());
+        std::vector<std::string> terms;
+        EXPECT_TRUE(source->expand_terms(pattern, 0, &terms).ok());
+        return terms;
+    };
+
+    const std::vector<std::string> dictionary = expand("*");
+    EXPECT_NE(std::ranges::find(dictionary, "cardboard"), dictionary.end());
+    for (const std::string& glob :
+         std::vector<std::string> {"*card*", "*an?a*", "?pp*", "*t*t*", 
"*123*", "*ion", "*zz*"}) {
+        index_query::WildcardMatcher matcher(glob);
+        std::vector<std::string> expected;
+        std::ranges::copy_if(dictionary, std::back_inserter(expected),
+                             [&](const std::string& term) { return 
matcher(term); });
+        EXPECT_EQ(expand(glob), expected) << glob;
+    }
+
+    _CLDECDELETE(dir);
+}
+
 } // namespace doris::segment_v2
\ No newline at end of file
diff --git 
a/be/test/storage/index/inverted/snii/reader/snii_index_source_test.cpp 
b/be/test/storage/index/inverted/snii/reader/snii_index_source_test.cpp
index c2aaa88ece5..152611f54e7 100644
--- a/be/test/storage/index/inverted/snii/reader/snii_index_source_test.cpp
+++ b/be/test/storage/index/inverted/snii/reader/snii_index_source_test.cpp
@@ -20,6 +20,7 @@
 #include <gtest/gtest.h>
 
 #include <algorithm>
+#include <iterator>
 #include <memory>
 #include <numeric>
 #include <span>
@@ -188,6 +189,22 @@ TEST_F(SniiIndexSourceTest, ExpandsTermsInDictionaryOrder) 
{
     EXPECT_TRUE(expand(TermPatternKind::kPrefix, "zzz", 0).empty());
 }
 
+// A glob from the start of the dictionary skips the terms without its longest 
literal run before it
+// reads their entries, and still expands to every term it matches, in 
dictionary order.
+TEST_F(SniiIndexSourceTest, AGlobFromTheStartExpandsToEveryTermItMatches) {
+    using index_query::TermPatternKind;
+    const std::vector<std::string> dictionary = 
expand(TermPatternKind::kWildcard, "*", 0);
+    ASSERT_FALSE(dictionary.empty());
+    for (const std::string& glob :
+         std::vector<std::string> {"*rde*", "*ar?e_*", "?rder", "*e*", 
"*_left", "*a*a*", "*zz*"}) {
+        index_query::WildcardMatcher matcher(glob);
+        std::vector<std::string> expected;
+        std::ranges::copy_if(dictionary, std::back_inserter(expected),
+                             [&](const std::string& term) { return 
matcher(term); });
+        EXPECT_EQ(expand(TermPatternKind::kWildcard, glob, 0), expected) << 
glob;
+    }
+}
+
 TEST_F(SniiIndexSourceTest, ScoredPostingsCarryFrequenciesAndPositions) {
     auto cursor = open("repeat", true, true);
     ASSERT_NE(cursor, nullptr);
diff --git a/be/test/storage/index/inverted/snii/snii_b2_t08_wildcard_test.cpp 
b/be/test/storage/index/inverted/snii/snii_b2_t08_wildcard_test.cpp
index be3a4b58e2c..689ead64603 100644
--- a/be/test/storage/index/inverted/snii/snii_b2_t08_wildcard_test.cpp
+++ b/be/test/storage/index/inverted/snii/snii_b2_t08_wildcard_test.cpp
@@ -220,6 +220,63 @@ TEST(SniiWildcardQueryTest, 
InvalidUtf8PatternReturnsInvalidArgument) {
                         .is<doris::ErrorCode::INVALID_ARGUMENT>());
 }
 
+// W-REQUIRED: a glob that enumerates the whole dictionary names its longest 
literal run as the
+// text every matching term holds; a glob with a prefix stops where the prefix 
ends and names none.
+TEST(SniiWildcardQueryTest, AGlobFromTheStartRequiresItsLongestLiteralRun) {
+    const auto required_text = [](std::string_view glob) {
+        index_query::TermPattern pattern;
+        
assert_ok(index_query::TermPattern::create(index_query::TermPatternKind::kWildcard,
 glob,
+                                                   &pattern));
+        return pattern.required_text();
+    };
+    EXPECT_EQ(required_text("*cache*"), "cache");
+    EXPECT_EQ(required_text("*ab?cde*"), "cde");
+    EXPECT_EQ(required_text("?ache*"), "ache");
+    EXPECT_EQ(required_text("*ch*ed*"), "ch");
+    EXPECT_EQ(required_text("*猫火*"), "猫火");
+    EXPECT_EQ(required_text("*"), "");
+    EXPECT_EQ(required_text("?*?"), "");
+    EXPECT_EQ(required_text("ca*che*"), "");
+    EXPECT_EQ(required_text("cache"), "");
+}
+
+// W-REQUIRED-SOUND: every term a glob matches holds the glob's required text, 
so a format that
+// skips the terms without it drops no match. Exhaustive over multi-byte and 
malformed pieces, which
+// the matcher reads as code points and as bytes.
+TEST(SniiWildcardQueryTest, EveryMatchingTermHoldsTheRequiredText) {
+    const auto concatenations = [](const std::vector<std::string>& pieces, 
size_t max_pieces) {
+        std::vector<std::string> out {""};
+        size_t level_begin = 0;
+        for (size_t count = 1; count <= max_pieces; ++count) {
+            const size_t level_end = out.size();
+            for (size_t i = level_begin; i < level_end; ++i) {
+                for (const std::string& piece : pieces) {
+                    out.push_back(out[i] + piece);
+                }
+            }
+            level_begin = level_end;
+        }
+        return out;
+    };
+    const std::vector<std::string> globs = concatenations({"a", "b", "猫", "*", 
"?"}, 4);
+    const std::vector<std::string> terms =
+            concatenations({"a", "b", "猫", std::string("\xff", 1), 
std::string("\xe7\x8c", 2)}, 4);
+    size_t matches = 0;
+    for (const std::string& glob : globs) {
+        index_query::TermPattern pattern;
+        
assert_ok(index_query::TermPattern::create(index_query::TermPatternKind::kWildcard,
 glob,
+                                                   &pattern));
+        for (const std::string& term : terms) {
+            if (pattern.matches(term)) {
+                ++matches;
+                EXPECT_NE(term.find(pattern.required_text()), 
std::string::npos)
+                        << "glob=\"" << glob << "\" term=\"" << term << "\"";
+            }
+        }
+    }
+    EXPECT_GT(matches, 0U);
+}
+
 // W-CONSEC-STAR: consecutive '*' degrade gracefully.
 TEST(SniiWildcardQueryTest, ConsecutiveStars) {
     index_query::WildcardMatcher matcher("**a**");


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

Reply via email to