8000 fix bug (#11279) · arangodb/arangodb@053bb7f · GitHub
[go: up one dir, main page]

Skip to content

Commit 053bb7f

Browse files
authored
fix bug (#11279)
1 parent e7a6101 commit 053bb7f

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

3rdParty/iresearch/core/search/wildcard_filter.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@
3131
#include "utils/automaton_utils.hpp"
3232
#include "utils/hash_utils.hpp"
3333

34+
NS_LOCAL
35+
36+
inline irs::bytes_ref unescape(const irs::bytes_ref& in, irs::bstring& out) {
37+
out.reserve(in.size());
38+
39+
bool copy = true;
40+
std::copy_if(in.begin(), in.end(), std::back_inserter(out),
41+
[&copy](irs::byte_type c) {
42+
if (c == irs::WildcardMatch::ESCAPE) {
43+
copy = !copy;
44+
} else {
45+
copy = true;
46+
}
47+
return copy;
48+
});
49+
50+
return out;
51+
}
52+
53+
NS_END
54+
3455
NS_ROOT
3556

3657
DEFINE_FILTER_TYPE(by_wildcard)
@@ -41,17 +62,24 @@ DEFINE_FACTORY_DEFAULT(by_wildcard)
4162
const order::prepared& order,
4263
boost_t boost,
4364
const string_ref& field,
44-
const bstring& term,
65+
bytes_ref term,
4566
size_t scored_terms_limit) {
67+
bstring buf;
4668
switch (wildcard_type(term)) {
4769
case WildcardType::INVALID:
4870
return prepared::empty();
71+
case WildcardType::TERM_ESCAPED:
72+
term = unescape(term, buf);
73+
[[fallthrough]];
4974
case WildcardType::TERM:
5075
return term_query::make(index, order, boost, field, term);
5176
case WildcardType::MATCH_ALL:
5277
return by_prefix::prepare(index, order, boost, field,
5378
bytes_ref::EMPTY, // empty prefix == match all
5479
scored_terms_limit);
80+
case WildcardType::PREFIX_ESCAPED:
81+
term = unescape(term, buf);
82+
[[fallthrough]];
5583
case WildcardType::PREFIX: {
5684
assert(!term.empty());
5785
const auto* begin = term.c_str();

3rdParty/iresearch/core/search/wildcard_filter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class IRESEARCH_API by_wildcard final : public by_prefix {
4343
const order::prepared& order,
4444
boost_t boost,
4545
const string_ref& field,
46-
const bstring& term,
46+
bytes_ref term,
4747
size_t scored_terms_limit);
4848

4949
explicit by_wildcard() noexcept;

3rdParty/iresearch/core/utils/wildcard_utils.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ WildcardType wildcard_type(const bytes_ref& expr) noexcept {
3232
}
3333

3434
bool escaped = false;
35+
bool seen_escaped = false;
3536
size_t num_match_any_string = 0;
3637
size_t num_adjacent_match_any_string = 0;
3738

@@ -50,17 +51,20 @@ WildcardType wildcard_type(const bytes_ref& expr) noexcept {
5051
case WildcardMatch::ANY_STRING:
5152
num_adjacent_match_any_string += size_t(!escaped);
5253
num_match_any_string += size_t(!escaped);
54+
seen_escaped |= escaped;
5355
escaped = false;
5456
break;
5557
case WildcardMatch::ANY_CHAR:
5658
if (!escaped) {
5759
return WildcardType::WILDCARD;
5860
}
61+
seen_escaped = true;
5962
num_adjacent_match_any_string = 0;
6063
escaped = false;
6164
break;
6265
case WildcardMatch::ESCAPE:
6366
num_adjacent_match_any_string = 0;
67+
seen_escaped |= escaped;
6468
escaped = !escaped;
6569
break;
6670
default:
@@ -73,15 +77,17 @@ WildcardType wildcard_type(const bytes_ref& expr) noexcept {
7377
}
7478

7579
if (0 == num_match_any_string) {
76-
return WildcardType::TERM;
80+
return seen_escaped ? WildcardType::TERM_ESCAPED
81+
: WildcardType::TERM;
7782
}
7883

7984
if (expr.size() == num_match_any_string) {
8085
return WildcardType::MATCH_ALL;
8186
}
8287

8388
if (num_match_any_string == num_adjacent_match_any_string) {
84-
return WildcardType::PREFIX;
89+
return seen_escaped ? WildcardType::PREFIX_ESCAPED
90+
: WildcardType::PREFIX;
8591
}
8692

8793
return WildcardType::WILDCARD;

3rdParty/iresearch/core/utils/wildcard_utils.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
NS_ROOT
2929

3030
enum class WildcardType {
31-
INVALID = 0, // invalid input sequence
32-
TERM, // foo
33-
MATCH_ALL, // *
34-
PREFIX, // foo*
35-
WILDCARD // f_o*
31+
INVALID = 0, // invalid input sequence
32+
TERM_ESCAPED, // f\*o
33+
TERM, // foo
34+
MATCH_ALL, // *
35+
PREFIX_ESCAPED, // fo\*
36+
PREFIX, // foo*
37+
WILDCARD // f_o*
3638
};
3739

3840
IRESEARCH_API WildcardType wildcard_type(const bytes_ref& pattern) noexcept;

0 commit comments

Comments
 (0)
0