Skip to content

Commit

Permalink
Fix query for non-existing key in nested dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelbo committed Apr 24, 2024
1 parent eef7fcf commit b4e1075
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/realm/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void Collection::get_any(QueryCtrlBlock& ctrl, Mixed val, size_t index)
ctrl.matches.back().push_back(k);
});
}
else {
else if (end_of_path) {
ctrl.matches.back().push_back(Mixed());
}
return;
Expand Down
12 changes: 6 additions & 6 deletions src/realm/query_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,18 +2065,18 @@ class Columns<Mixed> : public SimpleQuerySupport<Mixed> {
}
else {
if (index.initialize()) {
Value<Mixed> destination;
destination.init(false, 1);
SimpleQuerySupport::evaluate(index, destination);
Value<Mixed> dest;
dest.init(false, 1);
SimpleQuerySupport::evaluate(index, dest);

m_ctrl.matches.clear();
if (auto sz = destination.size()) {
if (auto sz = dest.size()) {
for (size_t i = 0; i < sz; i++) {
Collection::get_any(m_ctrl, destination.get(i), 0);
Collection::get_any(m_ctrl, dest.get(i), 0);
}
}
if (!index.set_size(m_ctrl.matches.size())) {
destination.init(true, 0);
dest.init(true, 0);
return;
}
}
Expand Down
13 changes: 10 additions & 3 deletions test/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5353,6 +5353,12 @@ TEST(Parser_NestedMixedDictionaryList)
list_george.add(2);
list_george.add(3);

Obj ringo = persons->create_object_with_primary_key("Ringo");
ringo.set(col_self, ringo.get_key());
ringo.set_collection(col, CollectionType::Dictionary);
auto dict_ringo = ringo.get_dictionary(col);
dict_ringo.insert("Foo", "Bar");

auto q = persons->column<Mixed>(col).path({"instruments", 0, "strings"}) == 6;
CHECK_EQUAL(q.count(), 1);

Expand All @@ -5369,15 +5375,16 @@ TEST(Parser_NestedMixedDictionaryList)
verify_query(test_context, persons, "properties[*] == {3, 2, 1}", 0);
verify_query(test_context, persons, "properties[*] == {1, 2, 3}", 1);
verify_query(test_context, persons, "ANY properties[*] == 2", 1);
verify_query(test_context, persons, "NONE properties[*] == 2", 2);
verify_query(test_context, persons, "NONE properties[*] == 2", 3);
verify_query(test_context, persons, "properties.@keys == 'instruments'", 2);
verify_query(test_context, persons, "properties.@keys == 'pets'", 2);
verify_query(test_context, persons, "properties.@keys == 'tickets'", 1);
verify_query(test_context, persons, "properties.@size == 3", 2);
verify_query(test_context, persons, "properties.instruments.@size == 2", 1);
verify_query(test_context, persons, "properties.@type == 'object'", 2);
verify_query(test_context, persons, "properties.@type == 'object'", 3);
verify_query(test_context, persons, "properties.@type == 'array'", 1);
verify_query(test_context, persons, "properties.@type == 'collection'", 3);
verify_query(test_context, persons, "properties.@type == 'collection'", 4);
verify_query(test_context, persons, "properties.Foo == 'Bar'", 1);
}

TEST(Parser_NestedDictionaryDeep)
Expand Down

0 comments on commit b4e1075

Please sign in to comment.