|
|
@@ -0,0 +1,124 @@
|
|
|
+// v2.18.0 - range filters (GT/GTE/LT/LTE) match only number-to-number or
|
|
|
+// string-to-string. Before 2.18.0 they compared ACROSS JSON types (null <
|
|
|
+// boolean < number < string): an explicit null or `true` matched
|
|
|
+// `year <= 2025`, and the string "1900" matched `year >= 2025` - silent
|
|
|
+// over-matching, found by a consumer whose access gate used a single bound.
|
|
|
+// EQ/NE and sort order are unchanged. Every case runs twice: on a collection
|
|
|
+// without an index (scan) and with one (the index plans must agree).
|
|
|
+
|
|
|
+#include <smartbotic/database/client.hpp>
|
|
|
+
|
|
|
+#include <algorithm>
|
|
|
+#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 + "}";
|
|
|
+}
|
|
|
+
|
|
|
+int main() {
|
|
|
+ Client::Config cfg;
|
|
|
+ cfg.address = "127.0.0.1:9034";
|
|
|
+ cfg.writeRetries = 0;
|
|
|
+ Client c(cfg);
|
|
|
+ c.connect();
|
|
|
+ ck(c.serverSupports(Client::Capability::kTypedRangeFilters), "server reports typed_range_filters");
|
|
|
+ for (std::string coll : {"scan", "idx", "bools"}) {
|
|
|
+ if (coll == "bools") continue;
|
|
|
+ c.insert(coll, {{"year", nullptr}}, "null");
|
|
|
+ c.insert(coll, {{"other", 1}}, "absent");
|
|
|
+ c.insert(coll, {{"year", 2024}}, "n2024");
|
|
|
+ c.insert(coll, {{"year", 2030.5}}, "n2030");
|
|
|
+ c.insert(coll, {{"year", "1900"}}, "s1900");
|
|
|
+ c.insert(coll, {{"year", "2100"}}, "s2100");
|
|
|
+ c.insert(coll, {{"year", true}}, "bool");
|
|
|
+ }
|
|
|
+ ck(c.createIndex("idx", "year"), "index declared");
|
|
|
+
|
|
|
+ for (std::string coll : {"scan", "idx"}) {
|
|
|
+ const std::string t = coll + ": ";
|
|
|
+ auto r = ids(c, coll, {F::lte("year", 2025)});
|
|
|
+ ck(r == std::set<std::string>{"n2024"}, t + "lte 2025 matches only the number below (got " + show(r) + ")");
|
|
|
+ r = ids(c, coll, {F::lte("year", 1900)});
|
|
|
+ ck(r.empty(), t + "lte 1900 matches nothing - not null, not true (got " + show(r) + ")");
|
|
|
+ r = ids(c, coll, {F::gte("year", 2025)});
|
|
|
+ ck(r == std::set<std::string>{"n2030"}, t + "gte 2025 does not match the string \"1900\" (got " + show(r) + ")");
|
|
|
+ r = ids(c, coll, {F::gt("year", 0), F::lt("year", 3000)});
|
|
|
+ ck(r == std::set<std::string>{"n2024", "n2030"}, t + "a numeric pair of bounds (got " + show(r) + ")");
|
|
|
+ r = ids(c, coll, {F::gte("year", "2000")});
|
|
|
+ ck(r == std::set<std::string>{"s2100"}, t + "a string bound compares strings only (got " + show(r) + ")");
|
|
|
+ r = ids(c, coll, {F::lt("year", "2000")});
|
|
|
+ ck(r == std::set<std::string>{"s1900"}, t + "string lt (got " + show(r) + ")");
|
|
|
+ r = ids(c, coll, {F::lte("year", nullptr)});
|
|
|
+ ck(r.empty(), t + "a null bound matches nothing (got " + show(r) + ")");
|
|
|
+ r = ids(c, coll, {F::gte("year", false)});
|
|
|
+ ck(r.empty(), t + "a boolean bound matches nothing (got " + show(r) + ")");
|
|
|
+ // Unchanged: equality, NE, EXISTS.
|
|
|
+ r = ids(c, coll, {F::eq("year", nullptr)});
|
|
|
+ ck(r == std::set<std::string>{"null"}, t + "EQ null still matches the explicit null (got " + show(r) + ")");
|
|
|
+ r = ids(c, coll, {F::ne("year", 2024)});
|
|
|
+ ck(r == std::set<std::string>{"null", "n2030", "s1900", "s2100", "bool"},
|
|
|
+ t + "NE keeps JSON inequality, absent excluded (got " + show(r) + ")");
|
|
|
+ ck(c.count(coll, {F::lte("year", 2025)}) == 1, t + "count agrees with find");
|
|
|
+ // Unchanged: sort order (null lowest, absent last ascending).
|
|
|
+ Client::QueryOptions o;
|
|
|
+ o.sortField = "year";
|
|
|
+ std::string order;
|
|
|
+ for (auto& d : c.find(coll, o)) order += d["_id"].get<std::string>() + " ";
|
|
|
+ ck(order == "null bool n2024 n2030 s1900 s2100 absent ",
|
|
|
+ t + "sort order is unchanged (got " + order + ")");
|
|
|
+ // The ordered range plan (range + sort on the same field).
|
|
|
+ Client::QueryOptions ro;
|
|
|
+ ro.filters = {F::gte("year", 2000)};
|
|
|
+ ro.sortField = "year";
|
|
|
+ order.clear();
|
|
|
+ for (auto& d : c.find(coll, ro)) order += d["_id"].get<std::string>() + " ";
|
|
|
+ ck(order == "n2024 n2030 ", t + "range + sort returns the numbers only, in order (got " + order + ")");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Single-type indexes with a matching null / boolean bound: the index range
|
|
|
+ // plan would serve them, and its ordered variant (range + sort on the same
|
|
|
+ // field) trusts postings WITHOUT re-judging. (true and false carry different
|
|
|
+ // index tags, so the index must hold only one of them to be single-type.)
|
|
|
+ c.insert("trues", {{"flag", true}}, "t1");
|
|
|
+ c.insert("trues", {{"flag", true}}, "t2");
|
|
|
+ c.createIndex("trues", "flag");
|
|
|
+ c.insert("nulls", {{"v", nullptr}}, "n1");
|
|
|
+ c.insert("nulls", {{"v", nullptr}}, "n2");
|
|
|
+ c.createIndex("nulls", "v");
|
|
|
+ {
|
|
|
+ Client::QueryOptions o;
|
|
|
+ o.filters = {F::gte("flag", true)};
|
|
|
+ o.sortField = "flag";
|
|
|
+ auto r = c.find("trues", o);
|
|
|
+ ck(r.empty(), "an all-true index with a boolean bound + sort returns nothing (got " +
|
|
|
+ std::to_string(r.size()) + ")");
|
|
|
+ Client::QueryOptions n;
|
|
|
+ n.filters = {F::lte("v", nullptr)};
|
|
|
+ n.sortField = "v";
|
|
|
+ auto rn = c.find("nulls", n);
|
|
|
+ ck(rn.empty(), "an all-null index with a null bound + sort returns nothing (got " +
|
|
|
+ std::to_string(rn.size()) + ")");
|
|
|
+ }
|
|
|
+
|
|
|
+ std::cout << "typed ranges: " << pass << " passed, " << fail << " failed\n";
|
|
|
+ return fail == 0 ? 0 : 1;
|
|
|
+}
|