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

Parse nested reference `include_fields`.

Harpreet Sangar4bd1619cd630
diff --git a/include/collection_manager.h b/include/collection_manager.h
index c8465fee..874b8055 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -113,6 +113,15 @@ 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,
@@ -210,7 +219,12 @@ public:
Option<bool> delete_preset(const std::string & preset_name);
static void _get_reference_collection_names(const std::string& filter_query,
- std::set<std::string>& reference_collection_names);
+ ref_include_collection_names_t*& reference_collection_names);
+
+ // Separate out the reference includes into `ref_include_fields_vec`.
+ static void _initialize_ref_include_fields_vec(const std::string& filter_query,
+ std::vector<std::string>& include_fields_vec,
+ std::vector<ref_include_fields>& ref_include_fields_vec);
void add_referenced_in_backlog(const std::string& collection_name, reference_pair&& pair);
diff --git a/include/field.h b/include/field.h
index 808c1398..d563404d 100644
--- a/include/field.h
+++ b/include/field.h
@@ -518,7 +518,10 @@ struct ref_include_fields {
std::string collection_name;
std::string fields;
std::string alias;
- bool nest_ref_doc = false;
+ bool nest_ref_doc = true;
+
+ // In case we have nested join.
+ std::vector<ref_include_fields> nested_join_includes;
};
struct hnsw_index_t;
diff --git a/include/string_utils.h b/include/string_utils.h
index 11f16967..0568a29e 100644
--- a/include/string_utils.h
+++ b/include/string_utils.h
@@ -337,4 +337,8 @@ struct StringUtils {
static Option<bool> split_include_fields(const std::string& include_fields, std::vector<std::string>& tokens);
static size_t get_occurence_count(const std::string& str, char symbol);
+
+ static Option<bool> split_reference_include_fields(const std::string& include_fields,
+ size_t& index,
+ std::string& token);
};
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 9f407d3c..7f535745 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -910,7 +910,11 @@ Option<bool> add_unsigned_int_list_param(const std::string& param_name, const st
}
void CollectionManager::_get_reference_collection_names(const std::string& filter_query,
- std::set<std::string>& reference_collection_names) {
+ 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];
@@ -918,7 +922,7 @@ void CollectionManager::_get_reference_collection_names(const std::string& filte
i++;
} else if (c == '&' || c == '|') {
if (i + 1 >= size || (c == '&' && filter_query[i+1] != '&') || (c == '|' && filter_query[i+1] != '|')) {
- reference_collection_names.clear();
+ ref_include->collection_names.clear();
return;
}
i += 2;
@@ -927,14 +931,14 @@ void CollectionManager::_get_reference_collection_names(const std::string& filte
if (c == '$') {
auto open_paren_pos = filter_query.find('(', ++i);
if (open_paren_pos == std::string::npos) {
- reference_collection_names.clear();
+ 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()) {
- reference_collection_names.insert(reference_collection_name);
+ ref_include->collection_names.insert(reference_collection_name);
}
i = open_paren_pos;
@@ -948,17 +952,19 @@ void CollectionManager::_get_reference_collection_names(const std::string& filte
}
if (parenthesis_count != 0) {
- reference_collection_names.clear();
+ 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);
- _get_reference_collection_names(sub_filter_query, reference_collection_names);
+ 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) {
- reference_collection_names.clear();
+ ref_include->collection_names.clear();
return;
}
@@ -976,11 +982,99 @@ void CollectionManager::_get_reference_collection_names(const std::string& filte
}
}
-// Separate out the reference includes into `ref_include_fields_vec`.
-void initialize_ref_include_fields_vec(const std::string& filter_query, std::vector<std::string>& include_fields_vec,
- std::vector<ref_include_fields>& ref_include_fields_vec) {
- std::set<std::string> reference_collection_names;
- CollectionManager::_get_reference_collection_names(filter_query, reference_collection_names);
+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_fields>& ref_include_fields_vec) {
+ // Format: $ref_collection_name(field_1, field_2, $nested_ref_coll(nested_field_1: nested_include_strategy) as nested_ref_alias: 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_fields> nested_ref_include_fields_vec;
+ if (nested_include_pos < closing_parenthesis_pos) {
+ // Nested reference include.
+ // "... $product_variants(title, $inventory(qty:merge) as inventory :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_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_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);
+ }
+
+ // ... $inventory(qty:merge) as inventory ...
+ if (colon_pos < closing_parenthesis_pos) { // Merge strategy is specified.
+ auto include_strategy = include_field_exp.substr(colon_pos + 1, closing_parenthesis_pos - colon_pos - 1);
+ StringUtils::trim(include_strategy);
+ nest_ref_doc = include_strategy == ref_include::nest;
+
+ if (index < colon_pos) {
+ ref_fields += include_field_exp.substr(index, colon_pos - index);
+ }
+ } else if (index < closing_parenthesis_pos) {
+ ref_fields += include_field_exp.substr(index, closing_parenthesis_pos - index);
+ }
+ 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.
+ ref_alias = !ref_alias.empty() ? (StringUtils::trim(ref_alias) + (nest_ref_doc ? "" : ".")) : "";
+
+ ref_include_fields_vec.emplace_back(ref_include_fields{ref_collection_name, ref_fields, ref_alias, nest_ref_doc});
+ ref_include_fields_vec.back().nested_join_includes = std::move(nested_ref_include_fields_vec);
+
+ // Referenced collection in filter_by is already mentioned in ref_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);
+}
+
+void CollectionManager::_initialize_ref_include_fields_vec(const std::string& filter_query,
+ std::vector<std::string>& include_fields_vec,
+ std::vector<ref_include_fields>& ref_include_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;
@@ -995,6 +1089,12 @@ void initialize_ref_include_fields_vec(const std::string& filter_query, std::vec
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_fields_vec);
+ 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);
@@ -1031,13 +1131,24 @@ void initialize_ref_include_fields_vec(const std::string& filter_query, std::vec
continue;
}
- // Referenced collection in filter_query is already mentioned in ref_include_fields.
- reference_collection_names.erase(reference_collection_name);
+ // Referenced collection in filter_by is already mentioned in ref_include_fields.
+ if (ref_include_coll_names != nullptr) {
+ ref_include_coll_names->collection_names.erase(reference_collection_name);
+ }
}
// Get all the fields of the referenced collection in the filter but not mentioned in include_fields.
- for (const auto &reference_collection_name: reference_collection_names) {
- ref_include_fields_vec.emplace_back(ref_include_fields{reference_collection_name, "", "", true});
+ auto ref_includes = std::ref(ref_include_fields_vec);
+ while (ref_include_coll_names != nullptr) {
+ for (const auto &reference_collection_name: ref_include_coll_names->collection_names) {
+ ref_includes.get().emplace_back(ref_include_fields{reference_collection_name, "", "", true});
+ }
+
+ ref_include_coll_names = ref_include_coll_names->nested_include;
+ if (ref_includes.get().empty()) {
+ break;
+ }
+ ref_includes = std::ref(ref_includes.get().front().nested_join_includes);
}
// Since no field of the collection is mentioned in include_fields, get all the fields.
@@ -1431,7 +1542,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
per_page = 0;
}
- initialize_ref_include_fields_vec(filter_query, include_fields_vec, ref_include_fields_vec);
+ _initialize_ref_include_fields_vec(filter_query, include_fields_vec, ref_include_fields_vec);
include_fields.insert(include_fields_vec.begin(), include_fields_vec.end());
exclude_fields.insert(exclude_fields_vec.begin(), exclude_fields_vec.end());
diff --git a/src/string_utils.cpp b/src/string_utils.cpp
index 109c5d09..cd74e96a 100644
--- a/src/string_utils.cpp
+++ b/src/string_utils.cpp
@@ -492,49 +492,87 @@ Option<bool> StringUtils::tokenize_filter_query(const std::string& filter_query,
return Option<bool>(true);
}
-Option<bool> StringUtils::split_include_fields(const std::string& include_fields, std::vector<std::string>& tokens) {
- size_t start = 0, end = 0, size = include_fields.size();
- std::string include_field;
+Option<bool> StringUtils::split_reference_include_fields(const std::string& include_fields,
+ size_t& index,
+ std::string& token) {
+ auto ref_include_error = Option<bool>(400, "Invalid reference in include_fields, expected `$CollectionName(fieldA, ...)`.");
+ auto const& size = include_fields.size();
+ size_t start_index = index;
+ while(++index < size && include_fields[index] != '(') {}
- while (true) {
- auto range_pos = include_fields.find('$', start);
- auto comma_pos = include_fields.find(',', start);
-
- if (range_pos == std::string::npos && comma_pos == std::string::npos) {
- if (start < size - 1) {
- include_field = include_fields.substr(start, size - start);
- include_field = trim(include_field);
- if (!include_field.empty()) {
- tokens.push_back(include_field);
- }
- }
- break;
- } else if (range_pos < comma_pos) {
- end = include_fields.find(')', range_pos);
- if (end == std::string::npos || end < include_fields.find('(', range_pos)) {
- return Option<bool>(400, "Invalid reference in include_fields, expected `$CollectionName(fieldA, ...)`.");
- }
+ if (index >= size) {
+ return ref_include_error;
+ }
+
+ // In case of nested join, the reference include field could have parenthesis inside it.
+ int parenthesis_count = 1;
+ while (++index < size && parenthesis_count > 0) {
+ if (include_fields[index] == '(') {
+ parenthesis_count++;
+ } else if (include_fields[index] == ')') {
+ parenthesis_count--;
+ }
+ }
+
+ if (parenthesis_count != 0) {
+ return ref_include_error;
+ }
- include_field = include_fields.substr(range_pos, (end - range_pos) + 1);
+ // In case of nested reference include, we might end up with one of the following scenarios:
+ // $ref_include( $nested_ref_include(foo :merge)as nest ) as ref
+ // ...^
+ // $ref_include( $nested_ref_include(foo :merge)as nest, bar ) as ref
+ // ...^
+ // $ref_include( $nested_ref_include(foo :merge)as nest :merge ) as ref
+ // ...^
+ auto closing_parenthesis_pos = include_fields.find(')', index);
+ auto comma_pos = include_fields.find(',', index);
+ auto colon_pos = include_fields.find(':', index);
+ auto alias_start_pos = include_fields.find(" as ", index);
+ auto alias_end_pos = std::min(std::min(closing_parenthesis_pos, comma_pos), colon_pos);
+ std::string alias;
+ if (alias_start_pos != std::string::npos && alias_start_pos < alias_end_pos) {
+ alias = include_fields.substr(alias_start_pos, alias_end_pos - alias_start_pos);
+ }
- comma_pos = include_fields.find(',', end);
- auto as_pos = include_fields.find(" as ", end);
- if (as_pos != std::string::npos && as_pos < comma_pos) {
- auto alias = include_fields.substr(as_pos, (comma_pos - as_pos));
- end += alias.size() + 1;
- include_field += (" " + trim(alias));
+ token = include_fields.substr(start_index, index - start_index) + " " + trim(alias);
+ trim(token);
+
+ index = alias_end_pos;
+ return Option<bool>(true);
+}
+
+Option<bool> StringUtils::split_include_fields(const std::string& include_fields, std::vector<std::string>& tokens) {
+ std::string token;
+ auto const& size = include_fields.size();
+ for (size_t i = 0; i < size;) {
+ auto c = include_fields[i];
+ if (c == ' ') {
+ i++;
+ continue;
+ } else if (c == '$') { // Reference include
+ std::string ref_include_token;
+ auto split_op = split_reference_include_fields(include_fields, i, ref_include_token);
+ if (!split_op.ok()) {
+ return split_op;
}
- } else {
- end = comma_pos;
- include_field = include_fields.substr(start, end - start);
+
+ tokens.push_back(ref_include_token);
+ continue;
}
- include_field = trim(include_field);
- if (!include_field.empty()) {
- tokens.push_back(include_field);
+ auto comma_pos = include_fields.find(',', i);
+ token = include_fields.substr(i, (comma_pos == std::string::npos ? size : comma_pos) - i);
+ trim(token);
+ if (!token.empty()) {
+ tokens.push_back(token);
}
- start = end + 1;
+ if (comma_pos == std::string::npos) {
+ break;
+ }
+ i = comma_pos + 1;
+ token.clear();
}
return Option<bool>(true);
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 1bf90dfd..e03c4e88 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -1368,13 +1368,19 @@ TEST_F(CollectionManagerTest, CloneCollection) {
TEST_F(CollectionManagerTest, GetReferenceCollectionNames) {
std::string filter_query = "";
- std::set<std::string> reference_collection_names;
- CollectionManager::_get_reference_collection_names(filter_query, reference_collection_names);
- ASSERT_TRUE(reference_collection_names.empty());
+ 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, reference_collection_names);
- ASSERT_TRUE(reference_collection_names.empty());
+ 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",
@@ -1400,40 +1406,153 @@ TEST_F(CollectionManagerTest, GetReferenceCollectionNames) {
ASSERT_EQ(search_op_bool.error(), "Could not parse the filter query.");
filter_query = "foo:bar";
- CollectionManager::_get_reference_collection_names(filter_query, reference_collection_names);
- ASSERT_TRUE(reference_collection_names.empty());
+ 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, reference_collection_names);
- ASSERT_TRUE(reference_collection_names.empty());
+ 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)";
- std::vector<std::string> result = {"foo"};
- CollectionManager::_get_reference_collection_names(filter_query, reference_collection_names);
- ASSERT_EQ(1, reference_collection_names.size());
- for (const auto &item: result) {
- ASSERT_EQ(1, reference_collection_names.count(item));
- }
- reference_collection_names.clear();
+ 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))";
- result = {"Customers"};
- CollectionManager::_get_reference_collection_names(filter_query, reference_collection_names);
- ASSERT_EQ(1, reference_collection_names.size());
- for (const auto &item: result) {
- ASSERT_EQ(1, reference_collection_names.count(item));
- }
- reference_collection_names.clear();
+ 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))))";
- result = {"product_variants", "inventory", "retailers"};
- CollectionManager::_get_reference_collection_names(filter_query, reference_collection_names);
- ASSERT_EQ(3, reference_collection_names.size());
- for (const auto &item: result) {
- ASSERT_EQ(1, reference_collection_names.count(item));
- }
- reference_collection_names.clear();
+ 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, InitializeRefIncludeFields) {
+ std::string filter_query = "";
+ std::vector<std::string> include_fields_vec;
+ std::vector<ref_include_fields> ref_include_fields_vec;
+ CollectionManager::_initialize_ref_include_fields_vec(filter_query, include_fields_vec, ref_include_fields_vec);
+ ASSERT_TRUE(ref_include_fields_vec.empty());
+
+ filter_query = "$foo(bar:baz)";
+ CollectionManager::_initialize_ref_include_fields_vec(filter_query, include_fields_vec, ref_include_fields_vec);
+ ASSERT_EQ(1, ref_include_fields_vec.size());
+ ASSERT_EQ("foo", ref_include_fields_vec[0].collection_name);
+ ASSERT_TRUE(ref_include_fields_vec[0].fields.empty());
+ ASSERT_TRUE(ref_include_fields_vec[0].alias.empty());
+ ASSERT_TRUE(ref_include_fields_vec[0].nest_ref_doc);
+ ASSERT_TRUE(ref_include_fields_vec[0].nested_join_includes.empty());
+ ref_include_fields_vec.clear();
+
+ filter_query = "$Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";
+ include_fields_vec = {"$Customers(product_price: merge) as customers"};
+ CollectionManager::_initialize_ref_include_fields_vec(filter_query, include_fields_vec, ref_include_fields_vec);
+ ASSERT_EQ(1, ref_include_fields_vec.size());
+ ASSERT_EQ("Customers", ref_include_fields_vec[0].collection_name);
+ ASSERT_EQ("product_price", ref_include_fields_vec[0].fields);
+ ASSERT_EQ("customers.", ref_include_fields_vec[0].alias);
+ ASSERT_FALSE(ref_include_fields_vec[0].nest_ref_doc);
+ ASSERT_TRUE(ref_include_fields_vec[0].nested_join_includes.empty());
+ ref_include_fields_vec.clear();
+
+ filter_query = "$product_variants( $inventory($retailers(location:(33.865,-118.375,100 km))))";
+ include_fields_vec = {"$product_variants(title, $inventory(qty:merge) as inventory: nest) as variants"};
+ CollectionManager::_initialize_ref_include_fields_vec(filter_query, include_fields_vec, ref_include_fields_vec);
+ ASSERT_EQ(1, ref_include_fields_vec.size());
+ ASSERT_EQ("product_variants", ref_include_fields_vec[0].collection_name);
+ ASSERT_EQ("title,", ref_include_fields_vec[0].fields);
+ ASSERT_EQ("variants", ref_include_fields_vec[0].alias);
+ ASSERT_TRUE(ref_include_fields_vec[0].nest_ref_doc);
+
+ auto nested_join_includes = ref_include_fields_vec[0].nested_join_includes;
+ ASSERT_EQ("inventory", nested_join_includes[0].collection_name);
+ ASSERT_EQ("qty", nested_join_includes[0].fields);
+ ASSERT_EQ("inventory.", nested_join_includes[0].alias);
+ ASSERT_FALSE(nested_join_includes[0].nest_ref_doc);
+
+ nested_join_includes = ref_include_fields_vec[0].nested_join_includes[0].nested_join_includes;
+ ASSERT_EQ("retailers", nested_join_includes[0].collection_name);
+ ASSERT_TRUE(nested_join_includes[0].fields.empty());
+ ASSERT_TRUE(nested_join_includes[0].alias.empty());
+ ASSERT_TRUE(nested_join_includes[0].nest_ref_doc);
+ ref_include_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:merge) as inventory,"
+ " $retailers(title): merge) as variants"};
+ CollectionManager::_initialize_ref_include_fields_vec(filter_query, include_fields_vec, ref_include_fields_vec);
+ ASSERT_EQ(1, ref_include_fields_vec.size());
+ ASSERT_EQ("product_variants", ref_include_fields_vec[0].collection_name);
+ ASSERT_EQ("title,", ref_include_fields_vec[0].fields);
+ ASSERT_EQ("variants.", ref_include_fields_vec[0].alias);
+ ASSERT_FALSE(ref_include_fields_vec[0].nest_ref_doc);
+
+ nested_join_includes = ref_include_fields_vec[0].nested_join_includes;
+ ASSERT_EQ("inventory", nested_join_includes[0].collection_name);
+ ASSERT_EQ("qty", nested_join_includes[0].fields);
+ ASSERT_EQ("inventory.", nested_join_includes[0].alias);
+ ASSERT_FALSE(nested_join_includes[0].nest_ref_doc);
+
+ ASSERT_EQ("retailers", nested_join_includes[1].collection_name);
+ ASSERT_EQ("title", nested_join_includes[1].fields);
+ ASSERT_TRUE(nested_join_includes[1].alias.empty());
+ ASSERT_TRUE(nested_join_includes[1].nest_ref_doc);
+ ref_include_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:merge) as inventory, description,"
+ " $retailers(title), foo: merge) as variants"};
+ CollectionManager::_initialize_ref_include_fields_vec(filter_query, include_fields_vec, ref_include_fields_vec);
+ ASSERT_EQ(1, ref_include_fields_vec.size());
+ ASSERT_EQ("product_variants", ref_include_fields_vec[0].collection_name);
+ ASSERT_EQ("title, description, foo", ref_include_fields_vec[0].fields);
+ ASSERT_EQ("variants.", ref_include_fields_vec[0].alias);
+ ASSERT_FALSE(ref_include_fields_vec[0].nest_ref_doc);
+
+ nested_join_includes = ref_include_fields_vec[0].nested_join_includes;
+ ASSERT_EQ("inventory", nested_join_includes[0].collection_name);
+ ASSERT_EQ("qty", nested_join_includes[0].fields);
+ ASSERT_EQ("inventory.", nested_join_includes[0].alias);
+ ASSERT_FALSE(nested_join_includes[0].nest_ref_doc);
+
+ ASSERT_EQ("retailers", nested_join_includes[1].collection_name);
+ ASSERT_EQ("title", nested_join_includes[1].fields);
+ ASSERT_TRUE(nested_join_includes[1].alias.empty());
+ ASSERT_TRUE(nested_join_includes[1].nest_ref_doc);
+ ref_include_fields_vec.clear();
}
TEST_F(CollectionManagerTest, ReferencedInBacklog) {
diff --git a/test/string_utils_test.cpp b/test/string_utils_test.cpp
index bd33a169..44e39bb9 100644
--- a/test/string_utils_test.cpp
+++ b/test/string_utils_test.cpp
@@ -412,10 +412,14 @@ TEST(StringUtilsTest, SplitIncludeFields) {
std::string include_fields;
std::vector<std::string> tokens;
- include_fields = "id, title, count";
+ include_fields = " id, title , count ";
tokens = {"id", "title", "count"};
splitIncludeTestHelper(include_fields, tokens);
+ include_fields = "id, $Collection(title, pref*),count";
+ tokens = {"id", "$Collection(title, pref*)", "count"};
+ splitIncludeTestHelper(include_fields, tokens);
+
include_fields = "id, $Collection(title, pref*), count, ";
tokens = {"id", "$Collection(title, pref*)", "count"};
splitIncludeTestHelper(include_fields, tokens);
@@ -427,4 +431,28 @@ TEST(StringUtilsTest, SplitIncludeFields) {
include_fields = "id, $Collection(title, pref*) as coll , count, ";
tokens = {"id", "$Collection(title, pref*) as coll", "count"};
splitIncludeTestHelper(include_fields, tokens);
+
+ include_fields = "$Collection(title, pref*: merge) as coll";
+ tokens = {"$Collection(title, pref*: merge) as coll"};
+ splitIncludeTestHelper(include_fields, tokens);
+
+ include_fields = "$product_variants(id,$inventory(qty,sku,$retailer(id,title: merge) as retailer_info)) as variants";
+ tokens = {"$product_variants(id,$inventory(qty,sku,$retailer(id,title: merge) as retailer_info)) as variants"};
+ splitIncludeTestHelper(include_fields, tokens);
+}
+
+TEST(StringUtilsTest, SplitReferenceIncludeFields) {
+ std::string include_fields = "$retailer(id,title: merge) as retailer_info:merge) as variants, foo", token;
+ size_t index = 0;
+ auto tokenize_op = StringUtils::split_reference_include_fields(include_fields, index, token);
+ ASSERT_TRUE(tokenize_op.ok());
+ ASSERT_EQ("$retailer(id,title: merge) as retailer_info", token);
+ ASSERT_EQ(":merge) as variants, foo", include_fields.substr(index));
+
+ include_fields = "$inventory(qty,sku,$retailer(id,title: merge) as retailer_info) as inventory) as variants, foo";
+ index = 0;
+ tokenize_op = StringUtils::split_reference_include_fields(include_fields, index, token);
+ ASSERT_TRUE(tokenize_op.ok());
+ ASSERT_EQ("$inventory(qty,sku,$retailer(id,title: merge) as retailer_info) as inventory", token);
+ ASSERT_EQ(") as variants, foo", include_fields.substr(index));
}