Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Merge join conditions of embedded `filter_by` and query `filter_by` (#1935)

Harpreet Sangar61dccfe0e35c
diff --git a/include/collection_manager.h b/include/collection_manager.h
index a5d3d547..7392e4c9 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -112,15 +112,6 @@ public:
CollectionManager(CollectionManager const&) = delete;
void operator=(CollectionManager const&) = delete;
- struct ref_include_collection_names_t {
- std::set<std::string> collection_names;
- ref_include_collection_names_t* nested_include = nullptr;
-
- ~ref_include_collection_names_t() {
- delete nested_include;
- }
- };
-
static Collection* init_collection(const nlohmann::json & collection_meta,
const uint32_t collection_next_seq_id,
Store* store,
@@ -227,15 +218,6 @@ public:
Option<bool> delete_preset(const std::string & preset_name);
- static void _get_reference_collection_names(const std::string& filter_query,
- ref_include_collection_names_t*& reference_collection_names);
-
- // Separate out the reference includes and excludes into `ref_include_exclude_fields_vec`.
- static Option<bool> initialize_ref_include_exclude_fields_vec(const std::string& filter_query,
- std::vector<std::string>& include_fields_vec,
- std::vector<std::string>& exclude_fields_vec,
- std::vector<ref_include_exclude_fields>& ref_include_exclude_fields_vec);
-
void add_referenced_in_backlog(const std::string& collection_name, reference_info_t&& ref_info);
std::map<std::string, std::set<reference_info_t>> _get_referenced_in_backlog() const;
diff --git a/include/join.h b/include/join.h
index 5805a282..500abeda 100644
--- a/include/join.h
+++ b/include/join.h
@@ -23,6 +23,15 @@ struct reference_info_t {
}
};
+struct ref_include_collection_names_t {
+ std::set<std::string> collection_names;
+ ref_include_collection_names_t* nested_include = nullptr;
+
+ ~ref_include_collection_names_t() {
+ delete nested_include;
+ }
+};
+
class Join {
public:
@@ -43,4 +52,20 @@ public:
const std::map<std::string, reference_filter_result_t>& reference_filter_results,
const std::vector<ref_include_exclude_fields>& ref_include_exclude_fields_vec,
const nlohmann::json& original_doc);
+
+ static Option<bool> parse_reference_filter(const std::string& filter_query, std::queue<std::string>& tokens, size_t& index,
+ std::set<std::string>& ref_collection_names);
+
+ static Option<bool> split_reference_include_exclude_fields(const std::string& include_exclude_fields,
+ size_t& index, std::string& token);
+
+ static void get_reference_collection_names(const std::string& filter_query, ref_include_collection_names_t*& ref_include);
+
+ // Separate out the reference includes and excludes into `ref_include_exclude_fields_vec`.
+ static Option<bool> initialize_ref_include_exclude_fields_vec(const std::string& filter_query,
+ std::vector<std::string>& include_fields_vec,
+ std::vector<std::string>& exclude_fields_vec,
+ std::vector<ref_include_exclude_fields>& ref_include_exclude_fields_vec);
+
+ [[nodiscard]] static bool merge_join_conditions(string& embedded_filter, string& query_filter);
};
\ No newline at end of file
diff --git a/include/string_utils.h b/include/string_utils.h
index e3d834e6..4907781f 100644
--- a/include/string_utils.h
+++ b/include/string_utils.h
@@ -338,7 +338,4 @@ struct StringUtils {
std::vector<std::string>& tokens);
static size_t get_occurence_count(const std::string& str, char symbol);
-
- static Option<bool> split_reference_include_exclude_fields(const std::string& include_fields,
- size_t& index, std::string& token);
};
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index ab5dede0..e855bae3 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -1,6 +1,7 @@
#include "auth_manager.h"
#include <openssl/evp.h>
#include <regex>
+#include <join.h>
constexpr const char* AuthManager::DOCUMENTS_SEARCH_ACTION;
constexpr const uint64_t api_key_t::FAR_FUTURE_TIMESTAMP;
@@ -402,6 +403,17 @@ bool AuthManager::add_item_to_params(std::map<std::string, std::string>& req_par
if(req_params.count(item.key()) == 0) {
req_params[item.key()] = str_value;
} else if(item.key() == "filter_by") {
+ auto& embedded_param = str_value;
+ auto& query_param = req_params[item.key()];
+
+ // Join follows $collection_name(<join_condition>) pattern. There might be false-positive matches with this
+ // regular expression like, "(field: foo$) || (field: bar)" but that is acceptable.
+ const std::regex join_pattern(R"(\$.+\(.+\))");
+ if (std::regex_search(embedded_param, join_pattern) && std::regex_search(query_param, join_pattern) &&
+ !Join::merge_join_conditions(embedded_param, query_param)) {
+ return false;
+ }
+
if(!req_params[item.key()].empty() && !str_value.empty()) {
req_params[item.key()] = "(" + req_params[item.key()] + ") && (" + str_value + ")";
} else if(req_params[item.key()].empty() && !str_value.empty()) {
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 1b152d0e..20fd6a5d 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -1159,397 +1159,6 @@ Option<bool> add_unsigned_int_list_param(const std::string& param_name, const st
return Option<bool>(true);
}
-void CollectionManager::_get_reference_collection_names(const std::string& filter_query,
- ref_include_collection_names_t*& ref_include) {
- if (ref_include == nullptr) {
- ref_include = new ref_include_collection_names_t();
- }
-
- auto size = filter_query.size();
- for (uint32_t i = 0; i < size;) {
- auto c = filter_query[i];
- if (c == ' ' || c == '(' || c == ')') {
- i++;
- } else if (c == '&' || c == '|') {
- if (i + 1 >= size || (c == '&' && filter_query[i+1] != '&') || (c == '|' && filter_query[i+1] != '|')) {
- ref_include->collection_names.clear();
- return;
- }
- i += 2;
- } else {
- // Reference filter would start with $ symbol.
- if (c == '$') {
- auto open_paren_pos = filter_query.find('(', ++i);
- if (open_paren_pos == std::string::npos) {
- ref_include->collection_names.clear();
- return;
- }
-
- auto reference_collection_name = filter_query.substr(i, open_paren_pos - i);
- StringUtils::trim(reference_collection_name);
- if (!reference_collection_name.empty()) {
- ref_include->collection_names.insert(reference_collection_name);
- }
-
- i = open_paren_pos;
- int parenthesis_count = 1;
- while (++i < size && parenthesis_count > 0) {
- if (filter_query[i] == '(') {
- parenthesis_count++;
- } else if (filter_query[i] == ')') {
- parenthesis_count--;
- }
- }
-
- if (parenthesis_count != 0) {
- ref_include->collection_names.clear();
- return;
- }
-
- // Need to process the filter expression inside parenthesis in case of nested join.
- auto sub_filter_query = filter_query.substr(open_paren_pos + 1, i - open_paren_pos - 2);
- if (sub_filter_query.find('$') != std::string::npos) {
- _get_reference_collection_names(sub_filter_query, ref_include->nested_include);
- }
- } else {
- while (i + 1 < size && filter_query[++i] != ':');
- if (i >= size) {
- ref_include->collection_names.clear();
- return;
- }
-
- bool in_backtick = false;
- do {
- c = filter_query[++i];
- if (c == '`') {
- in_backtick = !in_backtick;
- }
- } while (i < size && (in_backtick || (c != '(' && c != ')' &&
- !(c == '&' && filter_query[i + 1] == '&') &&
- !(c == '|' && filter_query[i + 1] == '|'))));
- }
- }
- }
-}
-
-Option<bool> parse_nested_exclude(const std::string& exclude_field_exp,
- std::unordered_map<std::string, std::string>& ref_excludes) {
- // Format: $ref_collection_name(field_1, field_2, $nested_ref_coll(nested_field_1))
- size_t index = 0;
- while (index < exclude_field_exp.size()) {
- auto parenthesis_index = exclude_field_exp.find('(');
- auto ref_collection_name = exclude_field_exp.substr(index + 1, parenthesis_index - index - 1);
- std::string ref_fields;
-
- index = parenthesis_index + 1;
- auto nested_exclude_pos = exclude_field_exp.find('$', parenthesis_index);
- auto closing_parenthesis_pos = exclude_field_exp.find(')', parenthesis_index);
- size_t comma_pos;
- if (nested_exclude_pos < closing_parenthesis_pos) {
- // Nested reference exclude.
- // "... $product_variants(title, $inventory(qty)) ..."
- do {
- ref_fields += exclude_field_exp.substr(index, nested_exclude_pos - index);
- StringUtils::trim(ref_fields);
- index = nested_exclude_pos;
- std::string nested_exclude_field_exp;
- auto split_op = StringUtils::split_reference_include_exclude_fields(exclude_field_exp, index,
- nested_exclude_field_exp);
- if (!split_op.ok()) {
- return split_op;
- }
-
- auto parse_op = parse_nested_exclude(nested_exclude_field_exp, ref_excludes);
- if (!parse_op.ok()) {
- return parse_op;
- }
-
- nested_exclude_pos = exclude_field_exp.find('$', index);
- closing_parenthesis_pos = exclude_field_exp.find(')', index);
- comma_pos = exclude_field_exp.find(',', index);
- index = std::min(closing_parenthesis_pos, comma_pos) + 1;
- } while (index < exclude_field_exp.size() && nested_exclude_pos < closing_parenthesis_pos);
- }
-
- // ... $inventory(qty) ...
- if (index < closing_parenthesis_pos) {
- ref_fields += exclude_field_exp.substr(index, closing_parenthesis_pos - index);
- }
- StringUtils::trim(ref_fields);
-
- ref_excludes[ref_collection_name] = ref_fields;
- index = closing_parenthesis_pos + 1;
- }
-
- return Option<bool>(true);
-}
-
-Option<bool> parse_ref_include_parameters(const std::string& include_field_exp, const std::string& parameters,
- ref_include::strategy_enum& strategy_enum) {
- std::vector<std::string> parameters_map;
- StringUtils::split(parameters, parameters_map, ",");
- for (const auto &item: parameters_map) {
- std::vector<std::string> parameter_pair;
- StringUtils::split(item, parameter_pair, ":");
- if (parameter_pair.size() != 2) {
- continue;
- }
- auto const& key = StringUtils::trim(parameter_pair[0]);
- if (key == ref_include::strategy_key) {
- auto const& include_strategy = StringUtils::trim(parameter_pair[1]);
-
- auto string_to_enum_op = ref_include::string_to_enum(include_strategy);
- if (!string_to_enum_op.ok()) {
- return Option<bool>(400, "Error parsing `" + include_field_exp + "`: " + string_to_enum_op.error());
- }
- strategy_enum = string_to_enum_op.get();
- } else {
- return Option<bool>(400, "Unknown reference `include_fields` parameter: `" + key + "`.");
- }
- }
-
- return Option<bool>(true);
-}
-
-Option<bool> parse_nested_include(const std::string& include_field_exp,
- CollectionManager::ref_include_collection_names_t* const ref_include_coll_names,
- std::vector<ref_include_exclude_fields>& ref_include_exclude_fields_vec) {
- // Format: $ref_collection_name(field_1, field_2, $nested_ref_coll(nested_field_1, strategy: nested_include_strategy) as nested_ref_alias, strategy: include_strategy) as ref_alias
- size_t index = 0;
- while (index < include_field_exp.size()) {
- auto parenthesis_index = include_field_exp.find('(');
- auto ref_collection_name = include_field_exp.substr(index + 1, parenthesis_index - index - 1);
- bool nest_ref_doc = true;
- std::string ref_fields, ref_alias;
-
- index = parenthesis_index + 1;
- auto nested_include_pos = include_field_exp.find('$', parenthesis_index);
- auto closing_parenthesis_pos = include_field_exp.find(')', parenthesis_index);
- auto colon_pos = include_field_exp.find(':', index);
- size_t comma_pos;
- std::vector<ref_include_exclude_fields> nested_ref_include_exclude_fields_vec;
- if (nested_include_pos < closing_parenthesis_pos) {
- // Nested reference include.
- // "... $product_variants(title, $inventory(qty, strategy:merge) as inventory, strategy :nest) as variants ..."
- do {
- ref_fields += include_field_exp.substr(index, nested_include_pos - index);
- StringUtils::trim(ref_fields);
- index = nested_include_pos;
- std::string nested_include_field_exp;
- auto split_op = StringUtils::split_reference_include_exclude_fields(include_field_exp, index,
- nested_include_field_exp);
- if (!split_op.ok()) {
- return split_op;
- }
-
- auto parse_op = parse_nested_include(nested_include_field_exp,
- ref_include_coll_names == nullptr ? nullptr : ref_include_coll_names->nested_include,
- nested_ref_include_exclude_fields_vec);
- if (!parse_op.ok()) {
- return parse_op;
- }
-
- nested_include_pos = include_field_exp.find('$', index);
- closing_parenthesis_pos = include_field_exp.find(')', index);
- colon_pos = include_field_exp.find(':', index);
- comma_pos = include_field_exp.find(',', index);
- index = std::min(std::min(closing_parenthesis_pos, colon_pos), comma_pos) + 1;
- } while(index < include_field_exp.size() && nested_include_pos < closing_parenthesis_pos);
- }
-
- if (index < closing_parenthesis_pos) {
- ref_fields += include_field_exp.substr(index, closing_parenthesis_pos - index);
- }
- index = closing_parenthesis_pos;
-
- // ... $inventory(qty, strategy:merge) as inventory
- auto strategy_enum = ref_include::nest;
- if (colon_pos < closing_parenthesis_pos) {
- auto const& parameters_start = ref_fields.rfind(',', colon_pos);
- std::string parameters;
- if (parameters_start == std::string::npos) {
- parameters = ref_fields;
- ref_fields.clear();
- } else {
- parameters = ref_fields.substr(parameters_start + 1);
- ref_fields = ref_fields.substr(0, parameters_start);
- }
-
- auto parse_params_op = parse_ref_include_parameters(include_field_exp, parameters, strategy_enum);
- if (!parse_params_op.ok()) {
- return parse_params_op;
- }
- }
- StringUtils::trim(ref_fields);
-
- auto as_pos = include_field_exp.find(" as ", index);
- comma_pos = include_field_exp.find(',', index);
- if (as_pos != std::string::npos && as_pos < comma_pos) {
- ref_alias = include_field_exp.substr(as_pos + 4, comma_pos - as_pos - 4);
- }
-
- // For an alias `foo`,
- // In case of "merge" reference doc, we need append `foo.` to all the top level keys of reference doc.
- // In case of "nest" reference doc, `foo` becomes the key with reference doc as value.
- nest_ref_doc = strategy_enum == ref_include::nest || strategy_enum == ref_include::nest_array;
- ref_alias = !ref_alias.empty() ? (StringUtils::trim(ref_alias) + (nest_ref_doc ? "" : ".")) : "";
-
- ref_include_exclude_fields_vec.emplace_back(ref_include_exclude_fields{ref_collection_name, ref_fields, "",
- ref_alias, strategy_enum});
- ref_include_exclude_fields_vec.back().nested_join_includes = std::move(nested_ref_include_exclude_fields_vec);
-
- // Referenced collection in filter_by is already mentioned in include_fields.
- if (ref_include_coll_names != nullptr) {
- ref_include_coll_names->collection_names.erase(ref_collection_name);
- }
- if (comma_pos == std::string::npos) {
- break;
- }
- index = comma_pos + 1;
- }
-
- return Option<bool>(true);
-}
-
-Option<bool> CollectionManager::initialize_ref_include_exclude_fields_vec(const std::string& filter_query,
- std::vector<std::string>& include_fields_vec,
- std::vector<std::string>& exclude_fields_vec,
- std::vector<ref_include_exclude_fields>& ref_include_exclude_fields_vec) {
- ref_include_collection_names_t* ref_include_coll_names = nullptr;
- CollectionManager::_get_reference_collection_names(filter_query, ref_include_coll_names);
- std::unique_ptr<CollectionManager::ref_include_collection_names_t> guard(ref_include_coll_names);
-
- std::vector<std::string> result_include_fields_vec;
- auto wildcard_include_all = true;
- for (auto const& include_field_exp: include_fields_vec) {
- if (include_field_exp[0] != '$') {
- if (include_field_exp == "*") {
- continue;
- }
-
- wildcard_include_all = false;
- result_include_fields_vec.emplace_back(include_field_exp);
- continue;
- }
-
- // Nested reference include.
- if (include_field_exp.find('$', 1) != std::string::npos) {
- auto parse_op = parse_nested_include(include_field_exp, ref_include_coll_names, ref_include_exclude_fields_vec);
- if (!parse_op.ok()) {
- return parse_op;
- }
- continue;
- }
-
- // Format: $ref_collection_name(field_1, field_2: include_strategy) as ref_alias
- auto as_pos = include_field_exp.find(" as ");
- auto ref_include = include_field_exp.substr(0, as_pos);
- auto alias = (as_pos == std::string::npos) ? "" :
- include_field_exp.substr(as_pos + 4, include_field_exp.size() - (as_pos + 4));
-
- auto parenthesis_index = ref_include.find('(');
- auto ref_collection_name = ref_include.substr(1, parenthesis_index - 1);
- auto ref_fields = ref_include.substr(parenthesis_index + 1, ref_include.size() - parenthesis_index - 2);
-
- auto strategy_enum = ref_include::nest;
- auto colon_pos = ref_fields.find(':');
- if (colon_pos != std::string::npos) {
- auto const& parameters_start = ref_fields.rfind(',', colon_pos);
- std::string parameters;
- if (parameters_start == std::string::npos) {
- parameters = ref_fields;
- ref_fields.clear();
- } else {
- parameters = ref_fields.substr(parameters_start + 1);
- ref_fields = ref_fields.substr(0, parameters_start);
- }
-
- auto parse_params_op = parse_ref_include_parameters(include_field_exp, parameters, strategy_enum);
- if (!parse_params_op.ok()) {
- return parse_params_op;
- }
- }
-
- // For an alias `foo`,
- // In case of "merge" reference doc, we need append `foo.` to all the top level keys of reference doc.
- // In case of "nest" reference doc, `foo` becomes the key with reference doc as value.
- auto const& nest_ref_doc = strategy_enum == ref_include::nest || strategy_enum == ref_include::nest_array;
- auto ref_alias = !alias.empty() ? (StringUtils::trim(alias) + (nest_ref_doc ? "" : ".")) : "";
- ref_include_exclude_fields_vec.emplace_back(ref_include_exclude_fields{ref_collection_name, ref_fields, "",
- ref_alias, strategy_enum});
-
- // Referenced collection in filter_by is already mentioned in include_fields.
- if (ref_include_coll_names != nullptr) {
- ref_include_coll_names->collection_names.erase(ref_collection_name);
- }
- }
-
- // Get all the fields of the referenced collection mentioned in the filter_by but not in include_fields.
- auto references = std::ref(ref_include_exclude_fields_vec);
- while (ref_include_coll_names != nullptr) {
- for (const auto &reference_collection_name: ref_include_coll_names->collection_names) {
- references.get().emplace_back(ref_include_exclude_fields{reference_collection_name, "", "", ""});
- }
-
- ref_include_coll_names = ref_include_coll_names->nested_include;
- if (references.get().empty()) {
- break;
- }
- references = std::ref(references.get().front().nested_join_includes);
- }
-
- std::unordered_map<std::string, std::string> ref_excludes;
- std::vector<std::string> result_exclude_fields_vec;
- for (const auto& exclude_field_exp: exclude_fields_vec) {
- if (exclude_field_exp[0] != '$') {
- result_exclude_fields_vec.emplace_back(exclude_field_exp);
- continue;
- }
-
- // Nested reference exclude.
- if (exclude_field_exp.find('$', 1) != std::string::npos) {
- auto parse_op = parse_nested_exclude(exclude_field_exp, ref_excludes);
- if (!parse_op.ok()) {
- return parse_op;
- }
- continue;
- }
-
- // Format: $ref_collection_name(field_1, field_2)
- auto parenthesis_index = exclude_field_exp.find('(');
- auto ref_collection_name = exclude_field_exp.substr(1, parenthesis_index - 1);
- auto ref_fields = exclude_field_exp.substr(parenthesis_index + 1, exclude_field_exp.size() - parenthesis_index - 2);
- if (!ref_fields.empty()) {
- ref_excludes[ref_collection_name] = ref_fields;
- }
- }
-
- if (!ref_excludes.empty()) {
- references = std::ref(ref_include_exclude_fields_vec);
- while (!references.get().empty()) {
- for (auto& ref_include_exclude: references.get()) {
- if (ref_excludes.count(ref_include_exclude.collection_name) == 0) {
- continue;
- }
-
- ref_include_exclude.exclude_fields = ref_excludes[ref_include_exclude.collection_name];
- }
-
- references = std::ref(references.get().front().nested_join_includes);
- }
- }
-
- // Since no field of the collection being searched is mentioned in include_fields, include all the fields.
- if (wildcard_include_all) {
- result_include_fields_vec.clear();
- }
-
- include_fields_vec = std::move(result_include_fields_vec);
- exclude_fields_vec = std::move(result_exclude_fields_vec);
-
- return Option<bool>(true);
-}
-
Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& req_params,
nlohmann::json& embedded_params,
std::string& results_json_str,
@@ -1971,8 +1580,8 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
per_page = 0;
}
- auto initialize_op = initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec, exclude_fields_vec,
- ref_include_exclude_fields_vec);
+ auto initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec, exclude_fields_vec,
+ ref_include_exclude_fields_vec);
if (!initialize_op.ok()) {
return initialize_op;
}
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 0c81112b..b455ca3b 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -1084,10 +1084,8 @@ bool get_export_documents(const std::shared_ptr<http_req>& req, const std::share
}
}
- auto initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query,
- include_fields_vec,
- exclude_fields_vec,
- export_state->ref_include_exclude_fields_vec);
+ auto initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec, exclude_fields_vec,
+ export_state->ref_include_exclude_fields_vec);
if (!initialize_op.ok()) {
res->set(initialize_op.code(), initialize_op.error());
req->last_chunk_aggregate = true;
diff --git a/src/join.cpp b/src/join.cpp
index 7379d973..71a7add6 100644
--- a/src/join.cpp
+++ b/src/join.cpp
@@ -685,4 +685,699 @@ Option<bool> Join::include_references(nlohmann::json& doc, const uint32_t& seq_i
}
return Option<bool>(true);
-}
\ No newline at end of file
+}
+
+Option<bool> parse_reference_filter_helper(const std::string& filter_query, size_t& index, std::string& ref_coll_name,
+ std::string& join) {
+ auto error = Option<bool>(400, "Could not parse the reference filter: `" + filter_query.substr(index) + "`.");
+
+ if (index >= filter_query.size() || filter_query[index] != '$') {
+ return error;
+ }
+
+ auto const start_index = index;
+ auto size = filter_query.size();
+ auto parenthesis_pos = filter_query.find('(', index + 1);
+ if (parenthesis_pos == std::string::npos) {
+ return error;
+ }
+
+ index = parenthesis_pos;
+ ref_coll_name = filter_query.substr(start_index + 1, parenthesis_pos - start_index - 1);
+ StringUtils::trim(ref_coll_name);
+
+ // The reference filter could have parenthesis inside it. $Foo((X && Y) || Z)
+ int parenthesis_count = 1;
+ while (++index < size && parenthesis_count > 0) {
+ if (filter_query[index] == '(') {
+ parenthesis_count++;
+ } else if (filter_query[index] == ')') {
+ parenthesis_count--;
+ }
+ }
+
+ if (parenthesis_count != 0) {
+ return error;
+ }
+
+ join = filter_query.substr(start_index, index - start_index);
+ return Option<bool>(true);
+}
+
+Option<bool> Join::parse_reference_filter(const std::string& filter_query, std::queue<std::string>& tokens, size_t& index,
+ std::set<std::string>& ref_collection_names) {
+ std::string ref_coll_name, join;
+ auto parse_op = parse_reference_filter_helper(filter_query, index, ref_coll_name, join);
+ if (!parse_op.ok()) {
+ return parse_op;
+ }
+
+ auto it = ref_collection_names.find(ref_coll_name);
+ if (it == ref_collection_names.end()) {
+ ref_collection_names.insert(ref_coll_name);
+ } else {
+ return Option<bool>(400, "More than one joins found for collection `" + ref_coll_name + "` in the `filter_by`." +=
+ " Instead of providing separate join conditions like "
+ "`$customer_product_prices(customer_id:=customer_a) && "
+ "$customer_product_prices(custom_price:<100)`,"
+ " the join condition should be provided as a single filter expression like"
+ " `$customer_product_prices(customer_id:=customer_a && custom_price:<100)`");
+ }
+
+ tokens.push(join);
+ return Option<bool>(true);
+}
+
+Option<bool> Join::split_reference_include_exclude_fields(const std::string& include_exclude_fields,
+ size_t& index, std::string& token) {
+ auto ref_include_error = Option<bool>(400, "Invalid reference `" + include_exclude_fields + "` in include_fields/"
+ "exclude_fields, expected `$CollectionName(fieldA, ...)`.");
+ auto const& size = include_exclude_fields.size();
+ size_t start_index = index;
+ while(++index < size && include_exclude_fields[index] != '(') {}
+
+ if (index >= size) {
+ return ref_include_error;
+ }
+
+ // In case of nested join, the reference include/exclude field could have parenthesis inside it.
+ int parenthesis_count = 1;
+ while (++index < size && parenthesis_count > 0) {
+ if (include_exclude_fields[index] == '(') {
+ parenthesis_count++;
+ } else if (include_exclude_fields[index] == ')') {
+ parenthesis_count--;
+ }
+ }
+
+ if (parenthesis_count != 0) {
+ return ref_include_error;
+ }
+
+ // In case of nested reference include, we might end up with one of the following scenarios:
+ // $ref_include( $nested_ref_include(foo, strategy:merge)as nest ) as ref
+ // ...^
+ // $ref_include( $nested_ref_include(foo, strategy:merge)as nest, bar ) as ref
+ // ...^
+ auto closing_parenthesis_pos = include_exclude_fields.find(')', index);
+ auto comma_pos = include_exclude_fields.find(',', index);
+ auto alias_start_pos = include_exclude_fields.find(" as ", index);
+ auto alias_end_pos = std::min(closing_parenthesis_pos, comma_pos);
+ std::string alias;
+ if (alias_start_pos != std::string::npos && alias_start_pos < alias_end_pos) {
+ alias = include_exclude_fields.substr(alias_start_pos, alias_end_pos - alias_start_pos);
+ }
+
+ token = include_exclude_fields.substr(start_index, index - start_index) + " " + StringUtils::trim(alias);
+ StringUtils::trim(token);
+
+ index = alias_end_pos;
+ return Option<bool>(true);
+}
+
+// Returns tri-state: Error while parsing filter_query (-1), Join not found (0), Join found (1)
+int8_t skip_index_to_join(const std::string& filter_query, size_t& i) {
+ auto const size = filter_query.size();
+ while (i < size) {
+ auto c = filter_query[i];
+ if (c == ' ' || c == '(' || c == ')') {
+ i++;
+ } else if (c == '&' || c == '|') {
+ if (i + 1 >= size || (c == '&' && filter_query[i + 1] != '&') || (c == '|' && filter_query[i + 1] != '|')) {
+ return -1;
+ }
+ i += 2;
+ } else {
+ // Reference filter would start with $ symbol.
+ if (c == '$') {
+ return 1;
+ } else {
+ while (i + 1 < size && filter_query[++i] != ':');
+ if (i >= size) {
+ return -1;
+ }
+
+ bool in_backtick = false;
+ do {
+ c = filter_query[++i];
+ if (c == '`') {
+ in_backtick = !in_backtick;
+ }
+ } while (i < size && (in_backtick || (c != '(' && c != ')' &&
+ !(c == '&' && filter_query[i + 1] == '&') &&
+ !(c == '|' && filter_query[i + 1] == '|'))));
+ }
+ }
+ }
+
+ return 0;
+}
+
+void Join::get_reference_collection_names(const std::string& filter_query,
+ ref_include_collection_names_t*& ref_include) {
+ if (ref_include == nullptr) {
+ ref_include = new ref_include_collection_names_t();
+ }
+
+ auto size = filter_query.size();
+ for (size_t i = 0; i < size;) {
+ auto const result = skip_index_to_join(filter_query, i);
+ if (result == -1) {
+ ref_include->collection_names.clear();
+ return;
+ } else if (result == 0) {
+ break;
+ }
+
+ auto c = filter_query[i];
+ // Reference filter would start with $ symbol.
+ if (c == '$') {
+ auto open_paren_pos = filter_query.find('(', ++i);
+ if (open_paren_pos == std::string::npos) {
+ ref_include->collection_names.clear();
+ return;
+ }
+
+ auto reference_collection_name = filter_query.substr(i, open_paren_pos - i);
+ StringUtils::trim(reference_collection_name);
+ if (!reference_collection_name.empty()) {
+ ref_include->collection_names.insert(reference_collection_name);
+ }
+
+ i = open_paren_pos;
+ int parenthesis_count = 1;
+ while (++i < size && parenthesis_count > 0) {
+ if (filter_query[i] == '(') {
+ parenthesis_count++;
+ } else if (filter_query[i] == ')') {
+ parenthesis_count--;
+ }
+ }
+
+ if (parenthesis_count != 0) {
+ ref_include->collection_names.clear();
+ return;
+ }
+
+ // Need to process the filter expression inside parenthesis in case of nested join.
+ auto sub_filter_query = filter_query.substr(open_paren_pos + 1, i - open_paren_pos - 2);
+ if (sub_filter_query.find('$') != std::string::npos) {
+ get_reference_collection_names(sub_filter_query, ref_include->nested_include);
+ }
+ }
+ }
+}
+
+Option<bool> parse_nested_exclude(const std::string& exclude_field_exp,
+ std::unordered_map<std::string, std::string>& ref_excludes) {
+ // Format: $ref_collection_name(field_1, field_2, $nested_ref_coll(nested_field_1))
+ size_t index = 0;
+ while (index < exclude_field_exp.size()) {
+ auto parenthesis_index = exclude_field_exp.find('(');
+ auto ref_collection_name = exclude_field_exp.substr(index + 1, parenthesis_index - index - 1);
+ std::string ref_fields;
+
+ index = parenthesis_index + 1;
+ auto nested_exclude_pos = exclude_field_exp.find('$', parenthesis_index);
+ auto closing_parenthesis_pos = exclude_field_exp.find(')', parenthesis_index);
+ size_t comma_pos;
+ if (nested_exclude_pos < closing_parenthesis_pos) {
+ // Nested reference exclude.
+ // "... $product_variants(title, $inventory(qty)) ..."
+ do {
+ ref_fields += exclude_field_exp.substr(index, nested_exclude_pos - index);
+ StringUtils::trim(ref_fields);
+ index = nested_exclude_pos;
+ std::string nested_exclude_field_exp;
+ auto split_op = Join::split_reference_include_exclude_fields(exclude_field_exp, index,
+ nested_exclude_field_exp);
+ if (!split_op.ok()) {
+ return split_op;
+ }
+
+ auto parse_op = parse_nested_exclude(nested_exclude_field_exp, ref_excludes);
+ if (!parse_op.ok()) {
+ return parse_op;
+ }
+
+ nested_exclude_pos = exclude_field_exp.find('$', index);
+ closing_parenthesis_pos = exclude_field_exp.find(')', index);
+ comma_pos = exclude_field_exp.find(',', index);
+ index = std::min(closing_parenthesis_pos, comma_pos) + 1;
+ } while (index < exclude_field_exp.size() && nested_exclude_pos < closing_parenthesis_pos);
+ }
+
+ // ... $inventory(qty) ...
+ if (index < closing_parenthesis_pos) {
+ ref_fields += exclude_field_exp.substr(index, closing_parenthesis_pos - index);
+ }
+ StringUtils::trim(ref_fields);
+
+ ref_excludes[ref_collection_name] = ref_fields;
+ index = closing_parenthesis_pos + 1;
+ }
+
+ return Option<bool>(true);
+}
+
+Option<bool> parse_ref_include_parameters(const std::string& include_field_exp, const std::string& parameters,
+ ref_include::strategy_enum& strategy_enum) {
+ std::vector<std::string> parameters_map;
+ StringUtils::split(parameters, parameters_map, ",");
+ for (const auto &item: parameters_map) {
+ std::vector<std::string> parameter_pair;
+ StringUtils::split(item, parameter_pair, ":");
+ if (parameter_pair.size() != 2) {
+ continue;
+ }
+ auto const& key = StringUtils::trim(parameter_pair[0]);
+ if (key == ref_include::strategy_key) {
+ auto const& include_strategy = StringUtils::trim(parameter_pair[1]);
+
+ auto string_to_enum_op = ref_include::string_to_enum(include_strategy);
+ if (!string_to_enum_op.ok()) {
+ return Option<bool>(400, "Error parsing `" + include_field_exp + "`: " + string_to_enum_op.error());
+ }
+ strategy_enum = string_to_enum_op.get();
+ } else {
+ return Option<bool>(400, "Unknown reference `include_fields` parameter: `" + key + "`.");
+ }
+ }
+
+ return Option<bool>(true);
+}
+
+Option<bool> parse_nested_include(const std::string& include_field_exp,
+ ref_include_collection_names_t* const ref_include_coll_names,
+ std::vector<ref_include_exclude_fields>& ref_include_exclude_fields_vec) {
+ // Format: $ref_collection_name(field_1, field_2, $nested_ref_coll(nested_field_1, strategy: nested_include_strategy) as nested_ref_alias, strategy: include_strategy) as ref_alias
+ size_t index = 0;
+ while (index < include_field_exp.size()) {
+ auto parenthesis_index = include_field_exp.find('(');
+ auto ref_collection_name = include_field_exp.substr(index + 1, parenthesis_index - index - 1);
+ bool nest_ref_doc = true;
+ std::string ref_fields, ref_alias;
+
+ index = parenthesis_index + 1;
+ auto nested_include_pos = include_field_exp.find('$', parenthesis_index);
+ auto closing_parenthesis_pos = include_field_exp.find(')', parenthesis_index);
+ auto colon_pos = include_field_exp.find(':', index);
+ size_t comma_pos;
+ std::vector<ref_include_exclude_fields> nested_ref_include_exclude_fields_vec;
+ if (nested_include_pos < closing_parenthesis_pos) {
+ // Nested reference include.
+ // "... $product_variants(title, $inventory(qty, strategy:merge) as inventory, strategy :nest) as variants ..."
+ do {
+ ref_fields += include_field_exp.substr(index, nested_include_pos - index);
+ StringUtils::trim(ref_fields);
+ index = nested_include_pos;
+ std::string nested_include_field_exp;
+ auto split_op = Join::split_reference_include_exclude_fields(include_field_exp, index,
+ nested_include_field_exp);
+ if (!split_op.ok()) {
+ return split_op;
+ }
+
+ auto parse_op = parse_nested_include(nested_include_field_exp,
+ ref_include_coll_names == nullptr ? nullptr : ref_include_coll_names->nested_include,
+ nested_ref_include_exclude_fields_vec);
+ if (!parse_op.ok()) {
+ return parse_op;
+ }
+
+ nested_include_pos = include_field_exp.find('$', index);
+ closing_parenthesis_pos = include_field_exp.find(')', index);
+ colon_pos = include_field_exp.find(':', index);
+ comma_pos = include_field_exp.find(',', index);
+ index = std::min(std::min(closing_parenthesis_pos, colon_pos), comma_pos) + 1;
+ } while(index < include_field_exp.size() && nested_include_pos < closing_parenthesis_pos);
+ }
+
+ if (index < closing_parenthesis_pos) {
+ ref_fields += include_field_exp.substr(index, closing_parenthesis_pos - index);
+ }
+ index = closing_parenthesis_pos;
+
+ // ... $inventory(qty, strategy:merge) as inventory
+ auto strategy_enum = ref_include::nest;
+ if (colon_pos < closing_parenthesis_pos) {
+ auto const& parameters_start = ref_fields.rfind(',', colon_pos);
+ std::string parameters;
+ if (parameters_start == std::string::npos) {
+ parameters = ref_fields;
+ ref_fields.clear();
+ } else {
+ parameters = ref_fields.substr(parameters_start + 1);
+ ref_fields = ref_fields.substr(0, parameters_start);
+ }
+
+ auto parse_params_op = parse_ref_include_parameters(include_field_exp, parameters, strategy_enum);
+ if (!parse_params_op.ok()) {
+ return parse_params_op;
+ }
+ }
+ StringUtils::trim(ref_fields);
+
+ auto as_pos = include_field_exp.find(" as ", index);
+ comma_pos = include_field_exp.find(',', index);
+ if (as_pos != std::string::npos && as_pos < comma_pos) {
+ ref_alias = include_field_exp.substr(as_pos + 4, comma_pos - as_pos - 4);
+ }
+
+ // For an alias `foo`,
+ // In case of "merge" reference doc, we need append `foo.` to all the top level keys of reference doc.
+ // In case of "nest" reference doc, `foo` becomes the key with reference doc as value.
+ nest_ref_doc = strategy_enum == ref_include::nest || strategy_enum == ref_include::nest_array;
+ ref_alias = !ref_alias.empty() ? (StringUtils::trim(ref_alias) + (nest_ref_doc ? "" : ".")) : "";
+
+ ref_include_exclude_fields_vec.emplace_back(ref_include_exclude_fields{ref_collection_name, ref_fields, "",
+ ref_alias, strategy_enum});
+ ref_include_exclude_fields_vec.back().nested_join_includes = std::move(nested_ref_include_exclude_fields_vec);
+
+ // Referenced collection in filter_by is already mentioned in include_fields.
+ if (ref_include_coll_names != nullptr) {
+ ref_include_coll_names->collection_names.erase(ref_collection_name);
+ }
+ if (comma_pos == std::string::npos) {
+ break;
+ }
+ index = comma_pos + 1;
+ }
+
+ return Option<bool>(true);
+}
+
+Option<bool> Join::initialize_ref_include_exclude_fields_vec(const std::string& filter_query,
+ std::vector<std::string>& include_fields_vec,
+ std::vector<std::string>& exclude_fields_vec,
+ std::vector<ref_include_exclude_fields>& ref_include_exclude_fields_vec) {
+ ref_include_collection_names_t* ref_include_coll_names = nullptr;
+ get_reference_collection_names(filter_query, ref_include_coll_names);
+ std::unique_ptr<ref_include_collection_names_t> guard(ref_include_coll_names);
+
+ std::vector<std::string> result_include_fields_vec;
+ auto wildcard_include_all = true;
+ for (auto const& include_field_exp: include_fields_vec) {
+ if (include_field_exp[0] != '$') {
+ if (include_field_exp == "*") {
+ continue;
+ }
+
+ wildcard_include_all = false;
+ result_include_fields_vec.emplace_back(include_field_exp);
+ continue;
+ }
+
+ // Nested reference include.
+ if (include_field_exp.find('$', 1) != std::string::npos) {
+ auto parse_op = parse_nested_include(include_field_exp, ref_include_coll_names, ref_include_exclude_fields_vec);
+ if (!parse_op.ok()) {
+ return parse_op;
+ }
+ continue;
+ }
+
+ // Format: $ref_collection_name(field_1, field_2: include_strategy) as ref_alias
+ auto as_pos = include_field_exp.find(" as ");
+ auto ref_include = include_field_exp.substr(0, as_pos);
+ auto alias = (as_pos == std::string::npos) ? "" :
+ include_field_exp.substr(as_pos + 4, include_field_exp.size() - (as_pos + 4));
+
+ auto parenthesis_index = ref_include.find('(');
+ auto ref_collection_name = ref_include.substr(1, parenthesis_index - 1);
+ auto ref_fields = ref_include.substr(parenthesis_index + 1, ref_include.size() - parenthesis_index - 2);
+
+ auto strategy_enum = ref_include::nest;
+ auto colon_pos = ref_fields.find(':');
+ if (colon_pos != std::string::npos) {
+ auto const& parameters_start = ref_fields.rfind(',', colon_pos);
+ std::string parameters;
+ if (parameters_start == std::string::npos) {
+ parameters = ref_fields;
+ ref_fields.clear();
+ } else {
+ parameters = ref_fields.substr(parameters_start + 1);
+ ref_fields = ref_fields.substr(0, parameters_start);
+ }
+
+ auto parse_params_op = parse_ref_include_parameters(include_field_exp, parameters, strategy_enum);
+ if (!parse_params_op.ok()) {
+ return parse_params_op;
+ }
+ }
+
+ // For an alias `foo`,
+ // In case of "merge" reference doc, we need append `foo.` to all the top level keys of reference doc.
+ // In case of "nest" reference doc, `foo` becomes the key with reference doc as value.
+ auto const& nest_ref_doc = strategy_enum == ref_include::nest || strategy_enum == ref_include::nest_array;
+ auto ref_alias = !alias.empty() ? (StringUtils::trim(alias) + (nest_ref_doc ? "" : ".")) : "";
+ ref_include_exclude_fields_vec.emplace_back(ref_include_exclude_fields{ref_collection_name, ref_fields, "",
+ ref_alias, strategy_enum});
+
+ // Referenced collection in filter_by is already mentioned in include_fields.
+ if (ref_include_coll_names != nullptr) {
+ ref_include_coll_names->collection_names.erase(ref_collection_name);
+ }
+ }
+
+ // Get all the fields of the referenced collection mentioned in the filter_by but not in include_fields.
+ auto references = std::ref(ref_include_exclude_fields_vec);
+ while (ref_include_coll_names != nullptr) {
+ for (const auto &reference_collection_name: ref_include_coll_names->collection_names) {
+ references.get().emplace_back(ref_include_exclude_fields{reference_collection_name, "", "", ""});
+ }
+
+ ref_include_coll_names = ref_include_coll_names->nested_include;
+ if (references.get().empty()) {
+ break;
+ }
+ references = std::ref(references.get().front().nested_join_includes);
+ }
+
+ std::unordered_map<std::string, std::string> ref_excludes;
+ std::vector<std::string> result_exclude_fields_vec;
+ for (const auto& exclude_field_exp: exclude_fields_vec) {
+ if (exclude_field_exp[0] != '$') {
+ result_exclude_fields_vec.emplace_back(exclude_field_exp);
+ continue;
+ }
+
+ // Nested reference exclude.
+ if (exclude_field_exp.find('$', 1) != std::string::npos) {
+ auto parse_op = parse_nested_exclude(exclude_field_exp, ref_excludes);
+ if (!parse_op.ok()) {
+ return parse_op;
+ }
+ continue;
+ }
+
+ // Format: $ref_collection_name(field_1, field_2)
+ auto parenthesis_index = exclude_field_exp.find('(');
+ auto ref_collection_name = exclude_field_exp.substr(1, parenthesis_index - 1);
+ auto ref_fields = exclude_field_exp.substr(parenthesis_index + 1, exclude_field_exp.size() - parenthesis_index - 2);
+ if (!ref_fields.empty()) {
+ ref_excludes[ref_collection_name] = ref_fields;
+ }
+ }
+
+ if (!ref_excludes.empty()) {
+ references = std::ref(ref_include_exclude_fields_vec);
+ while (!references.get().empty()) {
+ for (auto& ref_include_exclude: references.get()) {
+ if (ref_excludes.count(ref_include_exclude.collection_name) == 0) {
+ continue;
+ }
+
+ ref_include_exclude.exclude_fields = ref_excludes[ref_include_exclude.collection_name];
+ }
+
+ references = std::ref(references.get().front().nested_join_includes);
+ }
+ }
+
+ // Since no field of the collection being searched is mentioned in include_fields, include all the fields.
+ if (wildcard_include_all) {
+ result_include_fields_vec.clear();
+ }
+
+ include_fields_vec = std::move(result_include_fields_vec);
+ exclude_fields_vec = std::move(result_exclude_fields_vec);
+
+ return Option<bool>(true);
+}
+
+// If joins to the same collection are found in both `embedded_filter` and `query_filter`, remove the join from
+// `embedded_filter` and merge its join condition with the `query_filter` join in the following manner:
+// `$JoinCollectionName((<embedded_join_condition>) && <query_join_condition>)`
+bool Join::merge_join_conditions(string& embedded_filter, string& query_filter) {
+ std::unordered_map<std::string, std::string> coll_name_to_embedded_join;
+ for (size_t i = 0; i < embedded_filter.size();) {
+ auto const result = skip_index_to_join(embedded_filter, i);
+ if (result == -1) {
+ return false;
+ } else if (result == 0) {
+ break;
+ }
+
+ std::string ref_coll_name, join;
+ if (!parse_reference_filter_helper(embedded_filter, i, ref_coll_name, join).ok()) {
+ return false;
+ }
+
+ if (coll_name_to_embedded_join.find(ref_coll_name) != coll_name_to_embedded_join.end()) {
+ // Multiple joins to the same collection found.
+ return false;
+ }
+
+ coll_name_to_embedded_join[ref_coll_name] = join;
+ }
+
+ if (coll_name_to_embedded_join.empty()) { // No join found in the embedded filter_by.
+ return true;
+ }
+
+ std::set<std::string> query_join_coll_names;
+ for (size_t i = 0; i < query_filter.size();) {
+ auto const result = skip_index_to_join(query_filter, i);
+ if (result == -1) {
+ return false;
+ } else if (result == 0) {
+ break;
+ }
+
+ // Merge join conditions
+ auto const& join_start_index = i;
+ auto const q_parenthesis_pos = query_filter.find('(', i + 1);
+ if (q_parenthesis_pos == std::string::npos) {
+ return false;
+ }
+
+ auto ref_coll_name = query_filter.substr(join_start_index + 1, q_parenthesis_pos - join_start_index - 1);
+ StringUtils::trim(ref_coll_name);
+ if (query_join_coll_names.find(ref_coll_name) != query_join_coll_names.end()) {
+ // Multiple joins to the same collection found.
+ return false;
+ }
+
+ auto it = coll_name_to_embedded_join.find(ref_coll_name);
+ if (it != coll_name_to_embedded_join.end()) {
+ auto const& embedded_join = it->second;
+
+ auto const e_parenthesis_pos = embedded_join.find('(');
+ if (e_parenthesis_pos == std::string::npos) {
+ return false;
+ }
+ auto const embedded_join_condition = embedded_join.substr(e_parenthesis_pos + 1,
+ embedded_join.size() - e_parenthesis_pos - 2);
+ query_filter.insert(q_parenthesis_pos + 1, ("(" + embedded_join_condition + ") && "));
+
+ query_join_coll_names.insert(ref_coll_name);
+ }
+
+ std::string join;
+ if (!parse_reference_filter_helper(query_filter, i, ref_coll_name, join).ok()) {
+ return false;
+ }
+ }
+
+ // Erase the embedded joins that were merged into query filter.
+ for (const auto& ref_coll_name: query_join_coll_names) {
+ auto it = coll_name_to_embedded_join.find(ref_coll_name);
+ if (it == coll_name_to_embedded_join.end()) {
+ return false;
+ }
+
+ auto const& embedded_join = it->second;
+ // In a complex embedded filter expression, there can be following cases:
+ // 1. (Join && ... / (Join || ...
+ // 2. ... && Join) / ... || Join)
+ // 3. ... && (Join) && ... / ... || (Join) || ...
+ auto const& join_start_index = embedded_filter.find(embedded_join);
+ if (join_start_index == std::string::npos) {
+ return false;
+ }
+
+ // i and j point to start and end index of the join respectively. We will move i to left and j to right to probe
+ // the embedded filter expression to check which case this join falls into.
+ size_t i = join_start_index, j = join_start_index + embedded_join.size() - 1;
+ while (i > 0 && embedded_filter[--i] == ' ');
+ while (j < embedded_filter.size() && embedded_filter[++j] == ' ');
+
+ if (i == 0 && j >= embedded_filter.size()) {
+ // Embedded filter had only one expression.
+ embedded_filter.clear();
+ continue;
+ }
+
+ bool is_join_enclosed = embedded_filter[i] == '(' && embedded_filter[j] == ')';
+ if (is_join_enclosed) {
+ // ... ( Join ) ...
+ // Still need to move both i and j.
+ while (i > 0 && embedded_filter[--i] == ' ');
+ while (j < embedded_filter.size() && embedded_filter[++j] == ' ');
+
+ if (i == 0 && j >= embedded_filter.size()) {
+ // Embedded join was enclosed within parentheses.
+ embedded_filter.clear();
+ continue;
+ } else if (embedded_filter[i] == '(' && embedded_filter[j] == ')') {
+ // Join enclosed inside multiple parenthesis.
+ return false;
+ }
+ }
+
+ // Smallest filter expression will have both field name and value that are only 1 character long like, f:v
+ // So there has to be at least 5 characters after join in a complex filter expression like, &&f:v or before
+ // join like, f:v||
+
+ // Case 1.
+ // Either join is the first expression in embedded filter like, Join ...
+ // or it is the first expression in a sub-expression like, ... ( Join ... ) ...
+ if ((i == 0 || embedded_filter[i] == '(') && j + 4 < embedded_filter.size()) {
+ if ((embedded_filter[j] == '&' && embedded_filter[j + 1] == '&') ||
+ (embedded_filter[j] == '|' && embedded_filter[j + 1] == '|')) {
+ j++;
+ while (j < embedded_filter.size() && embedded_filter[++j] == ' ');
+
+ (is_join_enclosed || embedded_filter[i] == '$') ? embedded_filter.erase(0, j) :
+ embedded_filter.erase(i + 1, j - i - 1);
+ } else {
+ return false;
+ }
+ }
+
+ // Case 2.
+ // Either join is the last expression in embedded filter like, ... Join
+ // or it is the last expression in a sub-expression like, ... ( ... Join ) ...
+ else if ((j >= embedded_filter.size() || embedded_filter[j] == ')') && i > 4) {
+ if ((embedded_filter[i] == '&' && embedded_filter[i - 1] == '&') ||
+ (embedded_filter[i] == '|' && embedded_filter[i - 1] == '|')) {
+ i--;
+ while (i > 0 && embedded_filter[--i] == ' ');
+
+ embedded_filter.erase(i + 1, j - i - 1);
+ } else {
+ return false;
+ }
+ }
+
+ // Case 3.
+ // Join is in between filter expressions like, ... && Join && ...
+ else if (i > 4 && j + 4 < embedded_filter.size()) {
+ if ((embedded_filter[i] == '&' && embedded_filter[i - 1] == '&' &&
+ embedded_filter[j] == '&' && embedded_filter[j + 1] == '&') ||
+ (embedded_filter[i] == '|' && embedded_filter[i - 1] == '|' &&
+ embedded_filter[j] == '|' && embedded_filter[j + 1] == '|')) {
+ j++;
+
+ embedded_filter.erase(i + 1, j - i);
+ } else {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ return true;
+}
diff --git a/src/string_utils.cpp b/src/string_utils.cpp
index 0d99714c..449fa1be 100644
--- a/src/string_utils.cpp
+++ b/src/string_utils.cpp
@@ -7,6 +7,7 @@
#include <map>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <join.h>
#include "logger.h"
StringUtils::StringUtils() {
@@ -378,54 +379,6 @@ Option<bool> parse_multi_valued_geopoint_filter(const std::string& filter_query,
return Option<bool>(true);
}
-Option<bool> parse_reference_filter(const std::string& filter_query, std::queue<std::string>& tokens, size_t& index,
- std::set<std::string>& ref_collection_names) {
- auto error = Option<bool>(400, "Could not parse the reference filter: `" + filter_query.substr(index) + "`.");
-
- if (index >= filter_query.size() || filter_query[index] != '$') {
- return error;
- }
-
- auto const start_index = index;
- auto size = filter_query.size();
- auto parenthesis_pos = filter_query.find('(', index + 1);
- if (parenthesis_pos == std::string::npos) {
- return error;
- }
-
- index = parenthesis_pos;
- std::string ref_coll_name = filter_query.substr(start_index + 1, parenthesis_pos - start_index - 1);
- StringUtils::trim(ref_coll_name);
- auto it = ref_collection_names.find(ref_coll_name);
- if (it == ref_collection_names.end()) {
- ref_collection_names.insert(ref_coll_name);
- } else {
- return Option<bool>(400, "More than one joins found for collection `" + ref_coll_name + "` in the `filter_by`." +=
- " Instead of providing separate join conditions like "
- "`$customer_product_prices(customer_id:=customer_a) && "
- "$customer_product_prices(custom_price:<100)`,"
- " the join condition should be provided as a single filter expression like"
- " `$customer_product_prices(customer_id:=customer_a && custom_price:<100)`");
- }
-
- // The reference filter could have parenthesis inside it. $Foo((X && Y) || Z)
- int parenthesis_count = 1;
- while (++index < size && parenthesis_count > 0) {
- if (filter_query[index] == '(') {
- parenthesis_count++;
- } else if (filter_query[index] == ')') {
- parenthesis_count--;
- }
- }
-
- if (parenthesis_count != 0) {
- return error;
- }
-
- tokens.push(filter_query.substr(start_index, index - start_index));
- return Option<bool>(true);
-}
-
Option<bool> StringUtils::tokenize_filter_query(const std::string& filter_query, std::queue<std::string>& tokens) {
std::set<std::string> ref_collection_names;
auto size = filter_query.size();
@@ -457,7 +410,7 @@ Option<bool> StringUtils::tokenize_filter_query(const std::string& filter_query,
} else {
// Reference filter would start with $ symbol.
if (c == '$') {
- auto op = parse_reference_filter(filter_query, tokens, i, ref_collection_names);
+ auto op = Join::parse_reference_filter(filter_query, tokens, i, ref_collection_names);
if (!op.ok()) {
return op;
}
@@ -509,53 +462,6 @@ Option<bool> StringUtils::tokenize_filter_query(const std::string& filter_query,
return Option<bool>(true);
}
-Option<bool> StringUtils::split_reference_include_exclude_fields(const std::string& include_exclude_fields,
- size_t& index, std::string& token) {
- auto ref_include_error = Option<bool>(400, "Invalid reference `" + include_exclude_fields + "` in include_fields/"
- "exclude_fields, expected `$CollectionName(fieldA, ...)`.");
- auto const& size = include_exclude_fields.size();
- size_t start_index = index;
- while(++index < size && include_exclude_fields[index] != '(') {}
-
- if (index >= size) {
- return ref_include_error;
- }
-
- // In case of nested join, the reference include/exclude field could have parenthesis inside it.
- int parenthesis_count = 1;
- while (++index < size && parenthesis_count > 0) {
- if (include_exclude_fields[index] == '(') {
- parenthesis_count++;
- } else if (include_exclude_fields[index] == ')') {
- parenthesis_count--;
- }
- }
-
- if (parenthesis_count != 0) {
- return ref_include_error;
- }
-
- // In case of nested reference include, we might end up with one of the following scenarios:
- // $ref_include( $nested_ref_include(foo, strategy:merge)as nest ) as ref
- // ...^
- // $ref_include( $nested_ref_include(foo, strategy:merge)as nest, bar ) as ref
- // ...^
- auto closing_parenthesis_pos = include_exclude_fields.find(')', index);
- auto comma_pos = include_exclude_fields.find(',', index);
- auto alias_start_pos = include_exclude_fields.find(" as ", index);
- auto alias_end_pos = std::min(closing_parenthesis_pos, comma_pos);
- std::string alias;
- if (alias_start_pos != std::string::npos && alias_start_pos < alias_end_pos) {
- alias = include_exclude_fields.substr(alias_start_pos, alias_end_pos - alias_start_pos);
- }
-
- token = include_exclude_fields.substr(start_index, index - start_index) + " " + trim(alias);
- trim(token);
-
- index = alias_end_pos;
- return Option<bool>(true);
-}
-
Option<bool> StringUtils::split_include_exclude_fields(const std::string& include_exclude_fields,
std::vector<std::string>& tokens) {
std::string token;
@@ -567,7 +473,7 @@ Option<bool> StringUtils::split_include_exclude_fields(const std::string& includ
continue;
} else if (c == '$') { // Reference include/exclude
std::string ref_include_token;
- auto split_op = split_reference_include_exclude_fields(include_exclude_fields, i, ref_include_token);
+ auto split_op = Join::split_reference_include_exclude_fields(include_exclude_fields, i, ref_include_token);
if (!split_op.ok()) {
return split_op;
}
diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp
index 625511d1..6fddda51 100644
--- a/test/collection_join_test.cpp
+++ b/test/collection_join_test.cpp
@@ -5,6 +5,7 @@
#include <algorithm>
#include <collection_manager.h>
#include "collection.h"
+#include <join.h>
class CollectionJoinTest : public ::testing::Test {
protected:
@@ -7237,6 +7238,237 @@ TEST_F(CollectionJoinTest, FilterByReferenceAlias) {
ASSERT_EQ(1, res_obj["hits"][0]["document"]["Customers_alias"].count("product_price"));
}
+TEST_F(CollectionJoinTest, EmbeddedParamsJoin) {
+ std::string embedded_filter = "$Customers(customer_id:customer_a)",
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_TRUE(embedded_filter.empty());
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+
+ {
+ embedded_filter = "($Customers(customer_id:customer_a) )";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_TRUE(embedded_filter.empty());
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+
+ embedded_filter = " ( $Customers(customer_id:customer_a) ) ";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_TRUE(embedded_filter.empty());
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+
+ embedded_filter = " ( $Customers((x:2 || y:4) && z: 10) ) ";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_TRUE(embedded_filter.empty());
+ ASSERT_EQ("$Customers(((x:2 || y:4) && z: 10) && product_price:<100)", query_filter);
+ }
+
+ {
+ embedded_filter = "$Customers(customer_id:customer_a) && field:foo";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_EQ("field:foo", embedded_filter);
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+
+ embedded_filter = "( $Customers(customer_id:customer_a) ) && field:foo";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_EQ("field:foo", embedded_filter);
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+
+ embedded_filter = "($Customers(customer_id:customer_a))&&field:foo";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_EQ("field:foo", embedded_filter);
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+
+ embedded_filter = "($Customers(customer_id:customer_a)&&field:foo)";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_EQ("(field:foo)", embedded_filter);
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+ }
+
+ {
+ embedded_filter = "field:foo && $Customers(customer_id:customer_a) ";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_EQ("field:foo", embedded_filter);
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+
+ embedded_filter = "field:foo && ( $Customers(customer_id:customer_a) )";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_EQ("field:foo", embedded_filter);
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+
+ embedded_filter = "field:foo&&($Customers(customer_id:customer_a) )";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_EQ("field:foo", embedded_filter);
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+ }
+
+ {
+ embedded_filter = " ( $Customers(customer_id:customer_a) && $foo(field:value))";
+ query_filter = "$Customers(product_price:<100) && $foo(bar:baz)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_TRUE(embedded_filter.empty());
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100) && $foo((field:value) && bar:baz)", query_filter);
+
+ embedded_filter = "$Customers(customer_id:customer_a) && $foo(field:value)";
+ query_filter = "$Customers(product_price:<100) && $foo(bar:baz)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_TRUE(embedded_filter.empty());
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100) && $foo((field:value) && bar:baz)", query_filter);
+
+ embedded_filter = "$Customers(customer_id:customer_a)&&$foo( field:value )";
+ query_filter = "$Customers(product_price:<100) && $foo(bar:baz)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_TRUE(embedded_filter.empty());
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100) && $foo(( field:value ) && bar:baz)", query_filter);
+ }
+
+ {
+ embedded_filter = "field:value && ( $Customers(customer_id:customer_a) ) && foo:bar";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_EQ("field:value && foo:bar", embedded_filter);
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+
+ embedded_filter = "field:value&&$Customers(customer_id:customer_a)&&foo:bar";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_TRUE(Join::merge_join_conditions(embedded_filter, query_filter));
+ ASSERT_EQ("field:value&&foo:bar", embedded_filter);
+ ASSERT_EQ("$Customers((customer_id:customer_a) && product_price:<100)", query_filter);
+ }
+
+ {
+ embedded_filter = " (( $Customers(customer_id:customer_a) )) ";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_FALSE(Join::merge_join_conditions(embedded_filter, query_filter));
+
+ embedded_filter = "field:value && $Customers(customer_id:customer_a) || foo:bar";
+ query_filter = "$Customers(product_price:<100)";
+ ASSERT_FALSE(Join::merge_join_conditions(embedded_filter, query_filter));
+ }
+
+ auto schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_id", "type": "string"},
+ {"name": "product_name", "type": "string"},
+ {"name": "product_description", "type": "string"},
+ {"name": "rating", "type": "int32"}
+ ]
+ })"_json;
+ std::vector<nlohmann::json> documents = {
+ R"({
+ "product_id": "product_a",
+ "product_name": "shampoo",
+ "product_description": "Our new moisturizing shampoo is perfect for those with dry or damaged hair.",
+ "rating": "2"
+ })"_json,
+ R"({
+ "product_id": "product_b",
+ "product_name": "soap",
+ "product_description": "Introducing our all-natural, organic soap bar made with essential oils and botanical ingredients.",
+ "rating": "4"
+ })"_json
+ };
+ auto collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ for (auto const &json: documents) {
+ auto add_op = collection_create_op.get()->add(json.dump());
+ if (!add_op.ok()) {
+ LOG(INFO) << add_op.error();
+ }
+ ASSERT_TRUE(add_op.ok());
+ }
+
+ schema_json =
+ R"({
+ "name": "Customers",
+ "fields": [
+ {"name": "customer_id", "type": "string"},
+ {"name": "customer_name", "type": "string"},
+ {"name": "product_price", "type": "float"},
+ {"name": "product_id", "type": "string", "reference": "Products.product_id"}
+ ]
+ })"_json;
+ documents = {
+ R"({
+ "customer_id": "customer_a",
+ "customer_name": "Joe",
+ "product_price": 143,
+ "product_id": "product_a"
+ })"_json,
+ R"({
+ "customer_id": "customer_a",
+ "customer_name": "Joe",
+ "product_price": 73.5,
+ "product_id": "product_b"
+ })"_json,
+ R"({
+ "customer_id": "customer_b",
+ "customer_name": "Dan",
+ "product_price": 75,
+ "product_id": "product_a"
+ })"_json,
+ R"({
+ "customer_id": "customer_b",
+ "customer_name": "Dan",
+ "product_price": 140,
+ "product_id": "product_b"
+ })"_json
+ };
+ collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ for (auto const &json: documents) {
+ auto add_op = collection_create_op.get()->add(json.dump());
+ if (!add_op.ok()) {
+ LOG(INFO) << add_op.error();
+ }
+ ASSERT_TRUE(add_op.ok());
+ }
+
+ std::map<std::string, std::string> req_params = {
+ {"collection", "Products"},
+ {"q", "*"},
+ {"filter_by", "$Customers(product_price:<100)"},
+ };
+ nlohmann::json embedded_params = R"({
+ "filter_by": "$Customers(customer_id:customer_a) "
+ })"_json;
+ std::string json_res;
+ auto now_ts = std::chrono::duration_cast<std::chrono::microseconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+
+ auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_TRUE(search_op.ok());
+
+ nlohmann::json res_obj = nlohmann::json::parse(json_res);
+ ASSERT_EQ(1, res_obj["found"].get<size_t>());
+ ASSERT_EQ(1, res_obj["hits"].size());
+ // No fields are mentioned in `include_fields`, should include all fields of Products and Customers by default.
+ ASSERT_EQ(6, res_obj["hits"][0]["document"].size());
+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("id"));
+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_id"));
+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_name"));
+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_description"));
+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("rating"));
+ // Default strategy of reference includes is nest. No alias was provided, collection name becomes the field name.
+ ASSERT_EQ(5, res_obj["hits"][0]["document"]["Customers"].size());
+ ASSERT_EQ("customer_a", res_obj["hits"][0]["document"]["Customers"]["customer_id"]);
+ ASSERT_EQ(1, res_obj["hits"][0]["document"]["Customers"].count("customer_name"));
+ ASSERT_EQ(1, res_obj["hits"][0]["document"]["Customers"].count("id"));
+ ASSERT_EQ(1, res_obj["hits"][0]["document"]["Customers"].count("product_id"));
+ ASSERT_EQ(73.5, res_obj["hits"][0]["document"]["Customers"]["product_price"]);
+}
+
TEST_F(CollectionJoinTest, QueryByReference) {
auto schema_json =
R"({
@@ -7333,3 +7565,341 @@ TEST_F(CollectionJoinTest, QueryByReference) {
ASSERT_FALSE(search_op.ok());
ASSERT_EQ("Could not find `$Customers(customer_name` field in the schema.", search_op.error());
}
+
+TEST_F(CollectionJoinTest, GetReferenceCollectionNames) {
+ std::string filter_query = "";
+ ref_include_collection_names_t* ref_includes = nullptr;
+ Join::get_reference_collection_names(filter_query, ref_includes);
+ ASSERT_TRUE(ref_includes->collection_names.empty());
+ ASSERT_EQ(nullptr, ref_includes->nested_include);
+ delete ref_includes;
+ ref_includes = nullptr;
+
+ filter_query = "foo";
+ Join::get_reference_collection_names(filter_query, ref_includes);
+ ASSERT_TRUE(ref_includes->collection_names.empty());
+ ASSERT_EQ(nullptr, ref_includes->nested_include);
+ delete ref_includes;
+ ref_includes = nullptr;
+
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string"}
+ ]
+ })"_json;
+ auto create_op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(create_op.ok());
+
+ std::map<std::string, std::string> req_params = {
+ {"collection", "coll1"},
+ {"q", "*"},
+ {"filter_by", "title"},
+ };
+ nlohmann::json embedded_params;
+ std::string json_res;
+ auto now_ts = std::chrono::duration_cast<std::chrono::microseconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+
+ auto search_op_bool = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_FALSE(search_op_bool.ok());
+ ASSERT_EQ(search_op_bool.error(), "Could not parse the filter query.");
+
+ filter_query = "foo:bar";
+ Join::get_reference_collection_names(filter_query, ref_includes);
+ ASSERT_TRUE(ref_includes->collection_names.empty());
+ ASSERT_EQ(nullptr, ref_includes->nested_include);
+ delete ref_includes;
+ ref_includes = nullptr;
+
+ filter_query = "$foo(bar:baz) & age: <5";
+ Join::get_reference_collection_names(filter_query, ref_includes);
+ ASSERT_TRUE(ref_includes->collection_names.empty());
+ ASSERT_EQ(nullptr, ref_includes->nested_include);
+ delete ref_includes;
+ ref_includes = nullptr;
+
+ filter_query = "$foo(bar:baz)";
+ Join::get_reference_collection_names(filter_query, ref_includes);
+ ASSERT_EQ(1, ref_includes->collection_names.size());
+ ASSERT_EQ(1, ref_includes->collection_names.count("foo"));
+ ASSERT_EQ(nullptr, ref_includes->nested_include);
+ delete ref_includes;
+ ref_includes = nullptr;
+
+ filter_query = "((age: <5 || age: >10) && category:= [shoes]) &&"
+ " $Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";
+ Join::get_reference_collection_names(filter_query, ref_includes);
+ ASSERT_EQ(1, ref_includes->collection_names.size());
+ ASSERT_EQ(1, ref_includes->collection_names.count("Customers"));
+ ASSERT_EQ(nullptr, ref_includes->nested_include);
+ delete ref_includes;
+ ref_includes = nullptr;
+
+ filter_query = "$product_variants( $inventory($retailers(location:(33.865,-118.375,100 km))))";
+ Join::get_reference_collection_names(filter_query, ref_includes);
+ ASSERT_EQ(1, ref_includes->collection_names.size());
+ ASSERT_EQ(1, ref_includes->collection_names.count("product_variants"));
+ ASSERT_EQ(1, ref_includes->nested_include->collection_names.size());
+ ASSERT_EQ(1, ref_includes->nested_include->collection_names.count("inventory"));
+ ASSERT_EQ(1, ref_includes->nested_include->nested_include->collection_names.size());
+ ASSERT_EQ(1, ref_includes->nested_include->nested_include->collection_names.count("retailers"));
+ ASSERT_EQ(nullptr, ref_includes->nested_include->nested_include->nested_include);
+ delete ref_includes;
+ ref_includes = nullptr;
+
+ filter_query = "$product_variants( $inventory(id:*) && $retailers(location:(33.865,-118.375,100 km)))";
+ Join::get_reference_collection_names(filter_query, ref_includes);
+ ASSERT_EQ(1, ref_includes->collection_names.size());
+ ASSERT_EQ(1, ref_includes->collection_names.count("product_variants"));
+ ASSERT_EQ(2, ref_includes->nested_include->collection_names.size());
+ ASSERT_EQ(1, ref_includes->nested_include->collection_names.count("inventory"));
+ ASSERT_EQ(1, ref_includes->nested_include->collection_names.count("retailers"));
+ ASSERT_EQ(nullptr, ref_includes->nested_include->nested_include);
+ delete ref_includes;
+ ref_includes = nullptr;
+}
+
+TEST_F(CollectionJoinTest, InitializeRefIncludeExcludeFields) {
+ std::string filter_query = "";
+ std::vector<std::string> include_fields_vec, exclude_fields_vec;
+ std::vector<ref_include_exclude_fields> ref_include_exclude_fields_vec;
+ auto initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_TRUE(ref_include_exclude_fields_vec.empty());
+
+ filter_query = "$foo(bar:baz)";
+ exclude_fields_vec = {"$foo(bar)"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("foo", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].include_fields.empty());
+ ASSERT_EQ("bar", ref_include_exclude_fields_vec[0].exclude_fields);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].alias.empty());
+ ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].nested_join_includes.empty());
+ ref_include_exclude_fields_vec.clear();
+ exclude_fields_vec.clear();
+
+ filter_query = "";
+ include_fields_vec = {"$Customers(product_price, strategy: foo) as customers"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_FALSE(initialize_op.ok());
+ ASSERT_EQ("Error parsing `$Customers(product_price, strategy: foo) as customers`: Unknown include strategy `foo`. "
+ "Valid options are `merge`, `nest`, `nest_array`.", initialize_op.error());
+
+ include_fields_vec = {"$Customers(product_price, foo: bar) as customers"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_FALSE(initialize_op.ok());
+ ASSERT_EQ("Unknown reference `include_fields` parameter: `foo`.", initialize_op.error());
+
+ filter_query = "$Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";
+ include_fields_vec = {"$Customers(product_price, strategy: merge) as customers"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("Customers", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_EQ("product_price", ref_include_exclude_fields_vec[0].include_fields);
+ ASSERT_EQ("customers.", ref_include_exclude_fields_vec[0].alias);
+ ASSERT_EQ(ref_include::merge, ref_include_exclude_fields_vec[0].strategy);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].nested_join_includes.empty());
+ ref_include_exclude_fields_vec.clear();
+
+ filter_query = "$Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";
+ include_fields_vec = {"$Customers(product_price, strategy: nest_array) as customers"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("Customers", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_EQ("product_price", ref_include_exclude_fields_vec[0].include_fields);
+ ASSERT_EQ("customers", ref_include_exclude_fields_vec[0].alias);
+ ASSERT_EQ(ref_include::nest_array, ref_include_exclude_fields_vec[0].strategy);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].nested_join_includes.empty());
+ ref_include_exclude_fields_vec.clear();
+
+ filter_query = "$product_variants( $inventory($retailers(location:(33.865,-118.375,100 km))))";
+ include_fields_vec = {"$product_variants(id,$inventory(qty,sku,$retailers(id,title)))"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_EQ("id,", ref_include_exclude_fields_vec[0].include_fields);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].alias.empty());
+ ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
+
+ auto nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
+ ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
+ ASSERT_EQ("qty,sku,", nested_include_excludes[0].include_fields);
+ ASSERT_TRUE(nested_include_excludes[0].alias.empty());
+ ASSERT_EQ(ref_include::nest, nested_include_excludes[0].strategy);
+
+ nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes[0].nested_join_includes;
+ ASSERT_EQ("retailers", nested_include_excludes[0].collection_name);
+ ASSERT_EQ("id,title", nested_include_excludes[0].include_fields);
+ ASSERT_TRUE(nested_include_excludes[0].alias.empty());
+ ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
+ ref_include_exclude_fields_vec.clear();
+
+ filter_query = "$product_variants( $inventory($retailers(location:(33.865,-118.375,100 km))))";
+ include_fields_vec = {"$product_variants(title, $inventory(qty, strategy:merge) as inventory, strategy: nest) as variants"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_EQ("title", ref_include_exclude_fields_vec[0].include_fields);
+ ASSERT_EQ("variants", ref_include_exclude_fields_vec[0].alias);
+ ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
+
+ nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
+ ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
+ ASSERT_EQ("qty", nested_include_excludes[0].include_fields);
+ ASSERT_EQ("inventory.", nested_include_excludes[0].alias);
+ ASSERT_EQ(ref_include::merge, nested_include_excludes[0].strategy);
+
+ nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes[0].nested_join_includes;
+ ASSERT_EQ("retailers", nested_include_excludes[0].collection_name);
+ ASSERT_TRUE(nested_include_excludes[0].include_fields.empty());
+ ASSERT_TRUE(nested_include_excludes[0].alias.empty());
+ ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
+ ref_include_exclude_fields_vec.clear();
+
+ filter_query = "$product_variants( $inventory(id:*) && $retailers(location:(33.865,-118.375,100 km)))";
+ include_fields_vec = {"$product_variants(title, $inventory(qty, strategy:merge) as inventory,"
+ " $retailers(title), strategy: merge) as variants"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_EQ("title", ref_include_exclude_fields_vec[0].include_fields);
+ ASSERT_EQ("variants.", ref_include_exclude_fields_vec[0].alias);
+ ASSERT_EQ(ref_include::merge, ref_include_exclude_fields_vec[0].strategy);
+
+ nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
+ ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
+ ASSERT_EQ("qty", nested_include_excludes[0].include_fields);
+ ASSERT_EQ("inventory.", nested_include_excludes[0].alias);
+ ASSERT_EQ(ref_include::merge, nested_include_excludes[0].strategy);
+
+ ASSERT_EQ("retailers", nested_include_excludes[1].collection_name);
+ ASSERT_EQ("title", nested_include_excludes[1].include_fields);
+ ASSERT_TRUE(nested_include_excludes[1].alias.empty());
+ ASSERT_EQ(ref_include::nest, nested_include_excludes[1].strategy);
+ ref_include_exclude_fields_vec.clear();
+
+ filter_query = "$product_variants( $inventory(id:*) && $retailers(location:(33.865,-118.375,100 km)))";
+ include_fields_vec = {"$product_variants(title, $inventory(qty, strategy:merge) as inventory, description,"
+ " $retailers(title), foo, strategy: merge) as variants"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_EQ("title, description, foo", ref_include_exclude_fields_vec[0].include_fields);
+ ASSERT_EQ("variants.", ref_include_exclude_fields_vec[0].alias);
+ ASSERT_EQ(ref_include::merge, ref_include_exclude_fields_vec[0].strategy);
+
+ nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
+ ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
+ ASSERT_EQ("qty", nested_include_excludes[0].include_fields);
+ ASSERT_EQ("inventory.", nested_include_excludes[0].alias);
+ ASSERT_EQ(ref_include::merge, nested_include_excludes[0].strategy);
+
+ ASSERT_EQ("retailers", nested_include_excludes[1].collection_name);
+ ASSERT_EQ("title", nested_include_excludes[1].include_fields);
+ ASSERT_TRUE(nested_include_excludes[1].alias.empty());
+ ASSERT_EQ(ref_include::nest, nested_include_excludes[1].strategy);
+ ref_include_exclude_fields_vec.clear();
+
+ filter_query = "$Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";
+ include_fields_vec.clear();
+ exclude_fields_vec = {"$Customers(product_price)"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("Customers", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].include_fields.empty());
+ ASSERT_EQ("product_price", ref_include_exclude_fields_vec[0].exclude_fields);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].alias.empty());
+ ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].nested_join_includes.empty());
+ ref_include_exclude_fields_vec.clear();
+
+ filter_query = "$product_variants( $inventory(id:*) && $retailers(location:(33.865,-118.375,100 km)))";
+ include_fields_vec.clear();
+ exclude_fields_vec = {"$product_variants(title, $inventory(qty), description, $retailers(title), foo)"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].include_fields.empty());
+ ASSERT_EQ("title, description, foo", ref_include_exclude_fields_vec[0].exclude_fields);
+ ASSERT_TRUE(ref_include_exclude_fields_vec[0].alias.empty());
+ ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
+
+ nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
+ ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
+ ASSERT_TRUE(nested_include_excludes[0].include_fields.empty());
+ ASSERT_EQ("qty", nested_include_excludes[0].exclude_fields);
+ ASSERT_TRUE(nested_include_excludes[0].alias.empty());
+ ASSERT_EQ(ref_include::nest, nested_include_excludes[0].strategy);
+
+ ASSERT_EQ("retailers", nested_include_excludes[1].collection_name);
+ ASSERT_TRUE(nested_include_excludes[1].include_fields.empty());
+ ASSERT_EQ("title", nested_include_excludes[1].exclude_fields);
+ ASSERT_TRUE(nested_include_excludes[1].alias.empty());
+ ASSERT_EQ(ref_include::nest, nested_include_excludes[1].strategy);
+ ref_include_exclude_fields_vec.clear();
+
+ filter_query = "$product_variants( $inventory($retailers(location:(33.865,-118.375,100 km))))";
+ include_fields_vec = {"$product_variants(title, $inventory(qty, strategy:merge) as inventory, strategy: nest) as variants"};
+ exclude_fields_vec = {"$product_variants(title, $inventory(qty, $retailers(title)))"};
+ initialize_op = Join::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
+ exclude_fields_vec,
+ ref_include_exclude_fields_vec);
+ ASSERT_TRUE(initialize_op.ok());
+ ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
+ ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
+ ASSERT_EQ("title", ref_include_exclude_fields_vec[0].include_fields);
+ ASSERT_EQ("title,", ref_include_exclude_fields_vec[0].exclude_fields);
+ ASSERT_EQ("variants", ref_include_exclude_fields_vec[0].alias);
+ ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
+
+ nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
+ ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
+ ASSERT_EQ("qty", nested_include_excludes[0].include_fields);
+ ASSERT_EQ("qty,", nested_include_excludes[0].exclude_fields);
+ ASSERT_EQ("inventory.", nested_include_excludes[0].alias);
+ ASSERT_EQ(ref_include::merge, nested_include_excludes[0].strategy);
+
+ nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes[0].nested_join_includes;
+ ASSERT_EQ("retailers", nested_include_excludes[0].collection_name);
+ ASSERT_TRUE(nested_include_excludes[0].include_fields.empty());
+ ASSERT_EQ("title", nested_include_excludes[0].exclude_fields);
+ ASSERT_TRUE(nested_include_excludes[0].alias.empty());
+ ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
+ ref_include_exclude_fields_vec.clear();
+}
\ No newline at end of file
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 02b2505a..33867e67 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -1538,344 +1538,6 @@ TEST_F(CollectionManagerTest, CloneCollection) {
ASSERT_EQ('?', coll2->get_token_separators().at(1));
}
-TEST_F(CollectionManagerTest, GetReferenceCollectionNames) {
- std::string filter_query = "";
- CollectionManager::ref_include_collection_names_t* ref_includes = nullptr;
- CollectionManager::_get_reference_collection_names(filter_query, ref_includes);
- ASSERT_TRUE(ref_includes->collection_names.empty());
- ASSERT_EQ(nullptr, ref_includes->nested_include);
- delete ref_includes;
- ref_includes = nullptr;
-
- filter_query = "foo";
- CollectionManager::_get_reference_collection_names(filter_query, ref_includes);
- ASSERT_TRUE(ref_includes->collection_names.empty());
- ASSERT_EQ(nullptr, ref_includes->nested_include);
- delete ref_includes;
- ref_includes = nullptr;
-
- nlohmann::json schema = R"({
- "name": "coll1",
- "fields": [
- {"name": "title", "type": "string"}
- ]
- })"_json;
- auto create_op = collectionManager.create_collection(schema);
- ASSERT_TRUE(create_op.ok());
-
- std::map<std::string, std::string> req_params = {
- {"collection", "coll1"},
- {"q", "*"},
- {"filter_by", "title"},
- };
- nlohmann::json embedded_params;
- std::string json_res;
- auto now_ts = std::chrono::duration_cast<std::chrono::microseconds>(
- std::chrono::system_clock::now().time_since_epoch()).count();
-
- auto search_op_bool = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
- ASSERT_FALSE(search_op_bool.ok());
- ASSERT_EQ(search_op_bool.error(), "Could not parse the filter query.");
-
- filter_query = "foo:bar";
- CollectionManager::_get_reference_collection_names(filter_query, ref_includes);
- ASSERT_TRUE(ref_includes->collection_names.empty());
- ASSERT_EQ(nullptr, ref_includes->nested_include);
- delete ref_includes;
- ref_includes = nullptr;
-
- filter_query = "$foo(bar:baz) & age: <5";
- CollectionManager::_get_reference_collection_names(filter_query, ref_includes);
- ASSERT_TRUE(ref_includes->collection_names.empty());
- ASSERT_EQ(nullptr, ref_includes->nested_include);
- delete ref_includes;
- ref_includes = nullptr;
-
- filter_query = "$foo(bar:baz)";
- CollectionManager::_get_reference_collection_names(filter_query, ref_includes);
- ASSERT_EQ(1, ref_includes->collection_names.size());
- ASSERT_EQ(1, ref_includes->collection_names.count("foo"));
- ASSERT_EQ(nullptr, ref_includes->nested_include);
- delete ref_includes;
- ref_includes = nullptr;
-
- filter_query = "((age: <5 || age: >10) && category:= [shoes]) &&"
- " $Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";
- CollectionManager::_get_reference_collection_names(filter_query, ref_includes);
- ASSERT_EQ(1, ref_includes->collection_names.size());
- ASSERT_EQ(1, ref_includes->collection_names.count("Customers"));
- ASSERT_EQ(nullptr, ref_includes->nested_include);
- delete ref_includes;
- ref_includes = nullptr;
-
- filter_query = "$product_variants( $inventory($retailers(location:(33.865,-118.375,100 km))))";
- CollectionManager::_get_reference_collection_names(filter_query, ref_includes);
- ASSERT_EQ(1, ref_includes->collection_names.size());
- ASSERT_EQ(1, ref_includes->collection_names.count("product_variants"));
- ASSERT_EQ(1, ref_includes->nested_include->collection_names.size());
- ASSERT_EQ(1, ref_includes->nested_include->collection_names.count("inventory"));
- ASSERT_EQ(1, ref_includes->nested_include->nested_include->collection_names.size());
- ASSERT_EQ(1, ref_includes->nested_include->nested_include->collection_names.count("retailers"));
- ASSERT_EQ(nullptr, ref_includes->nested_include->nested_include->nested_include);
- delete ref_includes;
- ref_includes = nullptr;
-
- filter_query = "$product_variants( $inventory(id:*) && $retailers(location:(33.865,-118.375,100 km)))";
- CollectionManager::_get_reference_collection_names(filter_query, ref_includes);
- ASSERT_EQ(1, ref_includes->collection_names.size());
- ASSERT_EQ(1, ref_includes->collection_names.count("product_variants"));
- ASSERT_EQ(2, ref_includes->nested_include->collection_names.size());
- ASSERT_EQ(1, ref_includes->nested_include->collection_names.count("inventory"));
- ASSERT_EQ(1, ref_includes->nested_include->collection_names.count("retailers"));
- ASSERT_EQ(nullptr, ref_includes->nested_include->nested_include);
- delete ref_includes;
- ref_includes = nullptr;
-}
-
-TEST_F(CollectionManagerTest, InitializeRefIncludeExcludeFields) {
- std::string filter_query = "";
- std::vector<std::string> include_fields_vec, exclude_fields_vec;
- std::vector<ref_include_exclude_fields> ref_include_exclude_fields_vec;
- auto initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_TRUE(ref_include_exclude_fields_vec.empty());
-
- filter_query = "$foo(bar:baz)";
- exclude_fields_vec = {"$foo(bar)"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("foo", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].include_fields.empty());
- ASSERT_EQ("bar", ref_include_exclude_fields_vec[0].exclude_fields);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].alias.empty());
- ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].nested_join_includes.empty());
- ref_include_exclude_fields_vec.clear();
- exclude_fields_vec.clear();
-
- filter_query = "";
- include_fields_vec = {"$Customers(product_price, strategy: foo) as customers"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_FALSE(initialize_op.ok());
- ASSERT_EQ("Error parsing `$Customers(product_price, strategy: foo) as customers`: Unknown include strategy `foo`. "
- "Valid options are `merge`, `nest`, `nest_array`.", initialize_op.error());
-
- include_fields_vec = {"$Customers(product_price, foo: bar) as customers"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_FALSE(initialize_op.ok());
- ASSERT_EQ("Unknown reference `include_fields` parameter: `foo`.", initialize_op.error());
-
- filter_query = "$Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";
- include_fields_vec = {"$Customers(product_price, strategy: merge) as customers"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("Customers", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_EQ("product_price", ref_include_exclude_fields_vec[0].include_fields);
- ASSERT_EQ("customers.", ref_include_exclude_fields_vec[0].alias);
- ASSERT_EQ(ref_include::merge, ref_include_exclude_fields_vec[0].strategy);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].nested_join_includes.empty());
- ref_include_exclude_fields_vec.clear();
-
- filter_query = "$Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";
- include_fields_vec = {"$Customers(product_price, strategy: nest_array) as customers"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("Customers", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_EQ("product_price", ref_include_exclude_fields_vec[0].include_fields);
- ASSERT_EQ("customers", ref_include_exclude_fields_vec[0].alias);
- ASSERT_EQ(ref_include::nest_array, ref_include_exclude_fields_vec[0].strategy);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].nested_join_includes.empty());
- ref_include_exclude_fields_vec.clear();
-
- filter_query = "$product_variants( $inventory($retailers(location:(33.865,-118.375,100 km))))";
- include_fields_vec = {"$product_variants(id,$inventory(qty,sku,$retailers(id,title)))"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_EQ("id,", ref_include_exclude_fields_vec[0].include_fields);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].alias.empty());
- ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
-
- auto nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
- ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
- ASSERT_EQ("qty,sku,", nested_include_excludes[0].include_fields);
- ASSERT_TRUE(nested_include_excludes[0].alias.empty());
- ASSERT_EQ(ref_include::nest, nested_include_excludes[0].strategy);
-
- nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes[0].nested_join_includes;
- ASSERT_EQ("retailers", nested_include_excludes[0].collection_name);
- ASSERT_EQ("id,title", nested_include_excludes[0].include_fields);
- ASSERT_TRUE(nested_include_excludes[0].alias.empty());
- ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
- ref_include_exclude_fields_vec.clear();
-
- filter_query = "$product_variants( $inventory($retailers(location:(33.865,-118.375,100 km))))";
- include_fields_vec = {"$product_variants(title, $inventory(qty, strategy:merge) as inventory, strategy: nest) as variants"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_EQ("title", ref_include_exclude_fields_vec[0].include_fields);
- ASSERT_EQ("variants", ref_include_exclude_fields_vec[0].alias);
- ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
-
- nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
- ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
- ASSERT_EQ("qty", nested_include_excludes[0].include_fields);
- ASSERT_EQ("inventory.", nested_include_excludes[0].alias);
- ASSERT_EQ(ref_include::merge, nested_include_excludes[0].strategy);
-
- nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes[0].nested_join_includes;
- ASSERT_EQ("retailers", nested_include_excludes[0].collection_name);
- ASSERT_TRUE(nested_include_excludes[0].include_fields.empty());
- ASSERT_TRUE(nested_include_excludes[0].alias.empty());
- ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
- ref_include_exclude_fields_vec.clear();
-
- filter_query = "$product_variants( $inventory(id:*) && $retailers(location:(33.865,-118.375,100 km)))";
- include_fields_vec = {"$product_variants(title, $inventory(qty, strategy:merge) as inventory,"
- " $retailers(title), strategy: merge) as variants"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_EQ("title", ref_include_exclude_fields_vec[0].include_fields);
- ASSERT_EQ("variants.", ref_include_exclude_fields_vec[0].alias);
- ASSERT_EQ(ref_include::merge, ref_include_exclude_fields_vec[0].strategy);
-
- nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
- ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
- ASSERT_EQ("qty", nested_include_excludes[0].include_fields);
- ASSERT_EQ("inventory.", nested_include_excludes[0].alias);
- ASSERT_EQ(ref_include::merge, nested_include_excludes[0].strategy);
-
- ASSERT_EQ("retailers", nested_include_excludes[1].collection_name);
- ASSERT_EQ("title", nested_include_excludes[1].include_fields);
- ASSERT_TRUE(nested_include_excludes[1].alias.empty());
- ASSERT_EQ(ref_include::nest, nested_include_excludes[1].strategy);
- ref_include_exclude_fields_vec.clear();
-
- filter_query = "$product_variants( $inventory(id:*) && $retailers(location:(33.865,-118.375,100 km)))";
- include_fields_vec = {"$product_variants(title, $inventory(qty, strategy:merge) as inventory, description,"
- " $retailers(title), foo, strategy: merge) as variants"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_EQ("title, description, foo", ref_include_exclude_fields_vec[0].include_fields);
- ASSERT_EQ("variants.", ref_include_exclude_fields_vec[0].alias);
- ASSERT_EQ(ref_include::merge, ref_include_exclude_fields_vec[0].strategy);
-
- nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
- ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
- ASSERT_EQ("qty", nested_include_excludes[0].include_fields);
- ASSERT_EQ("inventory.", nested_include_excludes[0].alias);
- ASSERT_EQ(ref_include::merge, nested_include_excludes[0].strategy);
-
- ASSERT_EQ("retailers", nested_include_excludes[1].collection_name);
- ASSERT_EQ("title", nested_include_excludes[1].include_fields);
- ASSERT_TRUE(nested_include_excludes[1].alias.empty());
- ASSERT_EQ(ref_include::nest, nested_include_excludes[1].strategy);
- ref_include_exclude_fields_vec.clear();
-
- filter_query = "$Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";
- include_fields_vec.clear();
- exclude_fields_vec = {"$Customers(product_price)"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("Customers", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].include_fields.empty());
- ASSERT_EQ("product_price", ref_include_exclude_fields_vec[0].exclude_fields);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].alias.empty());
- ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].nested_join_includes.empty());
- ref_include_exclude_fields_vec.clear();
-
- filter_query = "$product_variants( $inventory(id:*) && $retailers(location:(33.865,-118.375,100 km)))";
- include_fields_vec.clear();
- exclude_fields_vec = {"$product_variants(title, $inventory(qty), description, $retailers(title), foo)"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].include_fields.empty());
- ASSERT_EQ("title, description, foo", ref_include_exclude_fields_vec[0].exclude_fields);
- ASSERT_TRUE(ref_include_exclude_fields_vec[0].alias.empty());
- ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
-
- nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
- ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
- ASSERT_TRUE(nested_include_excludes[0].include_fields.empty());
- ASSERT_EQ("qty", nested_include_excludes[0].exclude_fields);
- ASSERT_TRUE(nested_include_excludes[0].alias.empty());
- ASSERT_EQ(ref_include::nest, nested_include_excludes[0].strategy);
-
- ASSERT_EQ("retailers", nested_include_excludes[1].collection_name);
- ASSERT_TRUE(nested_include_excludes[1].include_fields.empty());
- ASSERT_EQ("title", nested_include_excludes[1].exclude_fields);
- ASSERT_TRUE(nested_include_excludes[1].alias.empty());
- ASSERT_EQ(ref_include::nest, nested_include_excludes[1].strategy);
- ref_include_exclude_fields_vec.clear();
-
- filter_query = "$product_variants( $inventory($retailers(location:(33.865,-118.375,100 km))))";
- include_fields_vec = {"$product_variants(title, $inventory(qty, strategy:merge) as inventory, strategy: nest) as variants"};
- exclude_fields_vec = {"$product_variants(title, $inventory(qty, $retailers(title)))"};
- initialize_op = CollectionManager::initialize_ref_include_exclude_fields_vec(filter_query, include_fields_vec,
- exclude_fields_vec,
- ref_include_exclude_fields_vec);
- ASSERT_TRUE(initialize_op.ok());
- ASSERT_EQ(1, ref_include_exclude_fields_vec.size());
- ASSERT_EQ("product_variants", ref_include_exclude_fields_vec[0].collection_name);
- ASSERT_EQ("title", ref_include_exclude_fields_vec[0].include_fields);
- ASSERT_EQ("title,", ref_include_exclude_fields_vec[0].exclude_fields);
- ASSERT_EQ("variants", ref_include_exclude_fields_vec[0].alias);
- ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
-
- nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes;
- ASSERT_EQ("inventory", nested_include_excludes[0].collection_name);
- ASSERT_EQ("qty", nested_include_excludes[0].include_fields);
- ASSERT_EQ("qty,", nested_include_excludes[0].exclude_fields);
- ASSERT_EQ("inventory.", nested_include_excludes[0].alias);
- ASSERT_EQ(ref_include::merge, nested_include_excludes[0].strategy);
-
- nested_include_excludes = ref_include_exclude_fields_vec[0].nested_join_includes[0].nested_join_includes;
- ASSERT_EQ("retailers", nested_include_excludes[0].collection_name);
- ASSERT_TRUE(nested_include_excludes[0].include_fields.empty());
- ASSERT_EQ("title", nested_include_excludes[0].exclude_fields);
- ASSERT_TRUE(nested_include_excludes[0].alias.empty());
- ASSERT_EQ(ref_include::nest, ref_include_exclude_fields_vec[0].strategy);
- ref_include_exclude_fields_vec.clear();
-}
-
TEST_F(CollectionManagerTest, ReferencedInBacklog) {
auto referenced_ins_backlog = collectionManager._get_referenced_in_backlog();
ASSERT_EQ(1, referenced_ins_backlog.count("Products"));
diff --git a/test/string_utils_test.cpp b/test/string_utils_test.cpp
index 0eab9472..dd4e6713 100644
--- a/test/string_utils_test.cpp
+++ b/test/string_utils_test.cpp
@@ -3,6 +3,7 @@
#include <iconv.h>
#include <unicode/translit.h>
#include <json.hpp>
+#include <join.h>
TEST(StringUtilsTest, ShouldJoinString) {
std::vector<std::string> parts = {"foo", "bar", "baz", "bazinga"};
@@ -452,28 +453,28 @@ TEST(StringUtilsTest, SplitIncludeExcludeFields) {
TEST(StringUtilsTest, SplitReferenceIncludeExcludeFields) {
std::string include_fields = "$retailer(id,title,strategy:merge) as retailer_info, strategy:merge) as variants, foo", token;
size_t index = 0;
- auto tokenize_op = StringUtils::split_reference_include_exclude_fields(include_fields, index, token);
+ auto tokenize_op = Join::split_reference_include_exclude_fields(include_fields, index, token);
ASSERT_TRUE(tokenize_op.ok());
ASSERT_EQ("$retailer(id,title,strategy:merge) as retailer_info", token);
ASSERT_EQ(", strategy:merge) as variants, foo", include_fields.substr(index));
include_fields = "$inventory(qty,sku,$retailer(id,title, strategy : merge) as retailer_info) as inventory) as variants, foo";
index = 0;
- tokenize_op = StringUtils::split_reference_include_exclude_fields(include_fields, index, token);
+ tokenize_op = Join::split_reference_include_exclude_fields(include_fields, index, token);
ASSERT_TRUE(tokenize_op.ok());
ASSERT_EQ("$inventory(qty,sku,$retailer(id,title, strategy : merge) as retailer_info) as inventory", token);
ASSERT_EQ(") as variants, foo", include_fields.substr(index));
std::string exclude_fields = "$Collection(title), $product_variants(id,$inventory(qty,sku,$retailer(id,title)))";
index = 0;
- tokenize_op = StringUtils::split_reference_include_exclude_fields(exclude_fields, index, token);
+ tokenize_op = Join::split_reference_include_exclude_fields(exclude_fields, index, token);
ASSERT_TRUE(tokenize_op.ok());
ASSERT_EQ("$Collection(title)", token);
ASSERT_EQ(", $product_variants(id,$inventory(qty,sku,$retailer(id,title)))", exclude_fields.substr(index));
exclude_fields = "$inventory(qty,sku,$retailer(id,title)), foo)";
index = 0;
- tokenize_op = StringUtils::split_reference_include_exclude_fields(exclude_fields, index, token);
+ tokenize_op = Join::split_reference_include_exclude_fields(exclude_fields, index, token);
ASSERT_TRUE(tokenize_op.ok());
ASSERT_EQ("$inventory(qty,sku,$retailer(id,title))", token);
ASSERT_EQ(", foo)", exclude_fields.substr(index));