8000 fix for ArangoSearch LIKE operator with escape sequence by gnusi · Pull Request #11279 · arangodb/arangodb · GitHub
[go: up one dir, main page]

Skip to content

fix for ArangoSearch LIKE operator with escape sequence #11279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Si 8000 gn in to your account

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion 3rdParty/iresearch/core/search/wildcard_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@
#include "utils/automaton_utils.hpp"
#include "utils/hash_utils.hpp"

NS_LOCAL

inline irs::bytes_ref unescape(const irs::bytes_ref& in, irs::bstring& out) {
out.reserve(in.size());

bool copy = true;
std::copy_if(in.begin(), in.end(), std::back_inserter(out),
[&copy](irs::byte_type c) {
if (c == irs::WildcardMatch::ESCAPE) {
copy = !copy;
} else {
copy = true;
}
return copy;
});

return out;
}

NS_END

NS_ROOT

DEFINE_FILTER_TYPE(by_wildcard)
Expand All @@ -41,17 +62,24 @@ DEFINE_FACTORY_DEFAULT(by_wildcard)
const order::prepared& order,
boost_t boost,
const string_ref& field,
const bstring& term,
bytes_ref term,
size_t scored_terms_limit) {
bstring buf;
switch (wildcard_type(term)) {
case WildcardType::INVALID:
return prepared::empty();
case WildcardType::TERM_ESCAPED:
term = unescape(term, buf);
[[fallthrough]];
case WildcardType::TERM:
return term_query::make(index, order, boost, field, term);
case WildcardType::MATCH_ALL:
return by_prefix::prepare(index, order, boost, field,
bytes_ref::EMPTY, // empty prefix == match all
scored_terms_limit);
case WildcardType::PREFIX_ESCAPED:
term = unescape(term, buf);
[[fallthrough]];
case WildcardType::PREFIX: {
assert(!term.empty());
const auto* begin = term.c_str();
Expand Down
2 changes: 1 addition & 1 deletion 3rdParty/iresearch/core/search/wildcard_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class IRESEARCH_API by_wildcard final : public by_prefix {
const order::prepared& order,
boost_t boost,
const string_ref& field,
const bstring& term,
bytes_ref term,
size_t scored_terms_limit);

explicit by_wildcard() noexcept;
Expand Down
10 changes: 8 additions & 2 deletions 3rdParty/iresearch/core/utils/wildcard_utils.cpp
8000
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ WildcardType wildcard_type(const bytes_ref& expr) noexcept {
}

bool escaped = false;
bool seen_escaped = false;
size_t num_match_any_string = 0;
size_t num_adjacent_match_any_string = 0;

Expand All @@ -50,17 +51,20 @@ WildcardType wildcard_type(const bytes_ref& expr) noexcept {
case WildcardMatch::ANY_STRING:
num_adjacent_match_any_string += size_t(!escaped);
num_match_any_string += size_t(!escaped);
seen_escaped |= escaped;
escaped = false;
break;
case WildcardMatch::ANY_CHAR:
if (!escaped) {
return WildcardType::WILDCARD;
}
seen_escaped = true;
num_adjacent_match_any_string = 0;
escaped = false;
break;
case WildcardMatch::ESCAPE:
num_adjacent_match_any_string = 0;
seen_escaped |= escaped;
escaped = !escaped;
break;
default:
Expand All @@ -73,15 +77,17 @@ WildcardType wildcard_type(const bytes_ref& expr) noexcept {
}

if (0 == num_match_any_string) {
return WildcardType::TERM;
return seen_escaped ? WildcardType::TERM_ESCAPED
: WildcardType::TERM;
}

if (expr.size() == num_match_any_string) {
return WildcardType::MATCH_ALL;
}

if (num_match_any_string == num_adjacent_match_any_string) {
return WildcardType::PREFIX;
return seen_escaped ? WildcardType::PREFIX_ESCAPED
: WildcardType::PREFIX;
}

return WildcardType::WILDCARD;
Expand Down
12 changes: 7 additions & 5 deletions 3rdParty/iresearch/core/utils/wildcard_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
NS_ROOT

enum class WildcardType {
INVALID = 0, // invalid input sequence
TERM, // foo
MATCH_ALL, // *
PREFIX, // foo*
WILDCARD // f_o*
INVALID = 0, // invalid input sequence
TERM_ESCAPED, // f\*o
TERM, // foo
MATCH_ALL, // *
PREFIX_ESCAPED, // fo\*
PREFIX, // foo*
WILDCARD // f_o*
};

IRESEARCH_API WildcardType wildcard_type(const bytes_ref& pattern) noexcept;
Expand Down
0