|
|
@@ -0,0 +1,135 @@
|
|
|
+// v2.20.0 - REGEX filters run on RE2. Reported by smartbotic-vectorapi from a
|
|
|
+// consumer's measurements: on std::regex (1) a pattern that did not compile
|
|
|
+// was swallowed and answered "no match", (2) `(?i)` was one such pattern,
|
|
|
+// (3) a character class over non-ASCII letters matched bytes, not characters
|
|
|
+// (`sz[áÁ]mfejt` never matched, `[öÖ]` matched text with neither), and
|
|
|
+// (4) a caller-supplied pattern backtracked exponentially per row of a scan.
|
|
|
+
|
|
|
+#include <smartbotic/database/client.hpp>
|
|
|
+
|
|
|
+#include <chrono>
|
|
|
+#include <iostream>
|
|
|
+#include <set>
|
|
|
+#include <string>
|
|
|
+
|
|
|
+using namespace smartbotic::database;
|
|
|
+using F = Client::Filter;
|
|
|
+static int pass = 0, fail = 0;
|
|
|
+static void ck(bool c, const std::string& m) {
|
|
|
+ if (c) { ++pass; } else { ++fail; std::cerr << "FAIL: " << m << "\n"; }
|
|
|
+}
|
|
|
+
|
|
|
+static std::set<std::string> ids(Client& c, const std::string& coll, std::vector<F> f) {
|
|
|
+ Client::QueryOptions o;
|
|
|
+ o.filters = std::move(f);
|
|
|
+ std::set<std::string> out;
|
|
|
+ for (auto& d : c.find(coll, o)) out.insert(d["_id"].get<std::string>());
|
|
|
+ return out;
|
|
|
+}
|
|
|
+
|
|
|
+static std::string show(const std::set<std::string>& s) {
|
|
|
+ std::string r;
|
|
|
+ for (const auto& x : s) r += (r.empty() ? "" : " ") + x;
|
|
|
+ return "{" + r + "}";
|
|
|
+}
|
|
|
+
|
|
|
+// Runs `call`, expecting INVALID_ARGUMENT with reason invalid_pattern naming `field`.
|
|
|
+template <typename Fn>
|
|
|
+static void refused(Fn call, const std::string& field, const std::string& what) {
|
|
|
+ try {
|
|
|
+ call();
|
|
|
+ ck(false, what + ": refused (it was answered)");
|
|
|
+ } catch (const Client::Error& e) {
|
|
|
+ ck(e.code() == Client::ErrorCode::InvalidArgument, what + ": InvalidArgument (" + e.what() + ")");
|
|
|
+ const auto* d = Client::errorDetail(e);
|
|
|
+ ck(d && d->reason == "invalid_pattern", what + ": reason invalid_pattern");
|
|
|
+ ck(d && d->fields == std::vector<std::string>{field}, what + ": fields names " + field);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static double seconds(std::chrono::steady_clock::time_point t0) {
|
|
|
+ return std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
|
|
|
+}
|
|
|
+
|
|
|
+int main() {
|
|
|
+ Client::Config cfg;
|
|
|
+ cfg.address = "127.0.0.1:9036";
|
|
|
+ cfg.writeRetries = 0;
|
|
|
+ cfg.timeoutMs = 20000;
|
|
|
+ Client c(cfg);
|
|
|
+ ck(c.connect(), "connect");
|
|
|
+ ck(c.serverSupports(Client::Capability::kRe2RegexFilters), "server reports re2_regex_filters");
|
|
|
+
|
|
|
+ c.createCollection("t", 0, false, 0, 2);
|
|
|
+ c.insert("t", {{"text", "Dolgozói bérszámfejtés"}, {"_vector", std::vector<float>{1, 0}}}, "hu");
|
|
|
+ c.insert("t", {{"text", "öröm és bánat"}, {"_vector", std::vector<float>{0, 1}}}, "oe");
|
|
|
+ c.insert("t", {{"text", "plain ascii 2024"}, {"_vector", std::vector<float>{1, 1}}}, "ascii");
|
|
|
+ c.insert("t", {{"text", 42}, {"_vector", std::vector<float>{1, 0}}}, "num");
|
|
|
+ // The backtracking subject: many a's and a failing end.
|
|
|
+ c.insert("t", {{"text", std::string(5000, 'a') + "b"}, {"_vector", std::vector<float>{0, 1}}}, "redos");
|
|
|
+ // A long subject (a recursive engine can overflow the stack on it).
|
|
|
+ std::string longAb;
|
|
|
+ for (int i = 0; i < 200000; ++i) longAb += (i % 2 ? 'b' : 'a');
|
|
|
+ c.insert("t", {{"text", longAb}, {"_vector", std::vector<float>{1, 1}}}, "long");
|
|
|
+
|
|
|
+ // (1) an invalid pattern is refused, not answered with nothing - on every read that filters.
|
|
|
+ refused([&] { (void)ids(c, "t", {F::regex("text", "dolgoz(")}); }, "text", "find");
|
|
|
+ refused([&] { (void)c.count("t", {F::regex("text", "dolgoz(")}); }, "text", "count");
|
|
|
+ refused([&] { (void)c.similaritySearch("t", {1, 0}, 3, 0.0f, {F::regex("text", "[unclosed")}); },
|
|
|
+ "text", "similaritySearch");
|
|
|
+ refused([&] { (void)ids(c, "t", {F::eq("x", 1), F::regex("text", "(?=look)ahead")}); },
|
|
|
+ "text", "find, lookahead (RE2 has none)");
|
|
|
+ refused([&] { (void)ids(c, "t", {F::regex("text", "(a)\\1")}); }, "text", "find, backreference");
|
|
|
+ ck(!c.createView("badview", "t", {}, {}, {F::regex("text", "dolgoz(")}), "a view with an invalid pattern is refused");
|
|
|
+ ck(!c.getViewInfo("badview"), "the refused view does not exist");
|
|
|
+
|
|
|
+ // (2) (?i) is a flag now.
|
|
|
+ auto r = ids(c, "t", {F::regex("text", "(?i)dolgoz")});
|
|
|
+ ck(r == std::set<std::string>{"hu"}, "(?i)dolgoz matches Dolgozói (got " + show(r) + ")");
|
|
|
+ r = ids(c, "t", {F::regex("text", "dolgoz")});
|
|
|
+ ck(r.empty(), "without (?i) the match stays case-sensitive (got " + show(r) + ")");
|
|
|
+ r = ids(c, "t", {F::regex("text", "(?i)BÉRSZÁMFEJTÉS")});
|
|
|
+ ck(r == std::set<std::string>{"hu"}, "(?i) folds non-ASCII letters (got " + show(r) + ")");
|
|
|
+
|
|
|
+ // (3) a character class matches a character.
|
|
|
+ r = ids(c, "t", {F::regex("text", "sz[áÁ]mfejt")});
|
|
|
+ ck(r == std::set<std::string>{"hu"}, "sz[áÁ]mfejt matches számfejt (got " + show(r) + ")");
|
|
|
+ r = ids(c, "t", {F::regex("text", "[öÖ]")});
|
|
|
+ ck(r == std::set<std::string>{"oe"}, "[öÖ] matches only text containing ö (got " + show(r) + ")");
|
|
|
+ r = ids(c, "t", {F::regex("text", "^b.nat")});
|
|
|
+ ck(r.empty(), "anchored ^ still anchors at the start (got " + show(r) + ")");
|
|
|
+ r = ids(c, "t", {F::regex("text", "b.nat$")});
|
|
|
+ ck(r == std::set<std::string>{"oe"}, ". is one character, á included (got " + show(r) + ")");
|
|
|
+
|
|
|
+ // Unchanged: search semantics, anchors, classes, non-strings never match.
|
|
|
+ r = ids(c, "t", {F::regex("text", "\\d{4}$")});
|
|
|
+ ck(r == std::set<std::string>{"ascii"}, "\\d{4}$ (got " + show(r) + ")");
|
|
|
+ r = ids(c, "t", {F::regex("text", "^plain")});
|
|
|
+ ck(r == std::set<std::string>{"ascii"}, "^plain (got " + show(r) + ")");
|
|
|
+ r = ids(c, "t", {F::regex("text", "4")});
|
|
|
+ ck(r == std::set<std::string>{"ascii"}, "a number value never matches a regex (got " + show(r) + ")");
|
|
|
+ ck(c.count("t", {F::regex("text", "(?i)dolgoz")}) == 1, "count agrees with find");
|
|
|
+ const auto s = c.similaritySearch("t", {1, 0}, 5, -1.0f, {F::regex("text", "(?i)^dolgoz")});
|
|
|
+ ck(s.size() == 1 && s[0].id == "hu", "a filtered similarity search uses the same engine");
|
|
|
+ ck(c.createView("goodview", "t", {}, {}, {F::regex("text", "(?i)számfejt")}), "a view with a valid pattern");
|
|
|
+ r = ids(c, "goodview", {});
|
|
|
+ ck(r == std::set<std::string>{"hu"}, "the view's pattern applies (got " + show(r) + ")");
|
|
|
+
|
|
|
+ // (4) linear time: patterns that backtrack exponentially on std::regex.
|
|
|
+ auto t0 = std::chrono::steady_clock::now();
|
|
|
+ r = ids(c, "t", {F::regex("text", "(a+)+$")});
|
|
|
+ double el = seconds(t0);
|
|
|
+ ck(r.empty(), "(a+)+$ matches nothing here (got " + show(r) + ")");
|
|
|
+ ck(el < 2.0, "(a+)+$ over a 5001-byte failing subject is fast (" + std::to_string(el) + " s)");
|
|
|
+ t0 = std::chrono::steady_clock::now();
|
|
|
+ r = ids(c, "t", {F::regex("text", "^(a|b)*c$")});
|
|
|
+ el = seconds(t0);
|
|
|
+ ck(r.empty(), "^(a|b)*c$ matches nothing");
|
|
|
+ ck(el < 2.0, "^(a|b)*c$ over a 200000-byte subject is fast, no stack overflow (" + std::to_string(el) + " s)");
|
|
|
+ r = ids(c, "t", {F::regex("text", "^(ab)+$")});
|
|
|
+ ck(r == std::set<std::string>{"long"}, "^(ab)+$ matches the long row (got " + show(r) + ")");
|
|
|
+ ck(c.healthCheck(), "the server is still up");
|
|
|
+
|
|
|
+ std::cout << pass << " passed, " << fail << " failed\n";
|
|
|
+ return fail ? 1 : 0;
|
|
|
+}
|