Skip to content

Commit

Permalink
Fix querying with a path into nested collections with wildcards
Browse files Browse the repository at this point in the history
Comparing a collection with a list could fail if there was wildcards
in the path and therefore multiple collections to compare with right
hand list.

Linklist is implicitly having wildcard in the path, so if linklists is
in the path there will be a similar problem. This part is not solved.
  • Loading branch information
jedelbo committed Mar 1, 2024
1 parent b1d32d3 commit 6a7af1b
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 88 deletions.
39 changes: 23 additions & 16 deletions src/realm/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,20 @@ void Collection::get_any(QueryCtrlBlock& ctrl, Mixed val, size_t index)
{
auto path_size = ctrl.path.size() - index;
PathElement& pe = ctrl.path[index];
bool end_of_path = path_size == 1;

if (end_of_path) {
ctrl.matches.emplace_back();
}

if (val.is_type(type_Dictionary) && (pe.is_key() || pe.is_all())) {
auto ref = val.get_ref();
if (!ref)
return;
Array top(ctrl.alloc);
Array top(*ctrl.alloc);
top.init_from_ref(ref);

BPlusTree<StringData> keys(ctrl.alloc);
BPlusTree<StringData> keys(*ctrl.alloc);
keys.set_parent(&top, 0);
keys.init_from_parent();
size_t start = 0;
Expand All @@ -156,25 +162,26 @@ void Collection::get_any(QueryCtrlBlock& ctrl, Mixed val, size_t index)
start = keys.find_first(StringData(pe.get_key()));
if (start == realm::not_found) {
if (pe.get_key() == "@keys") {
ctrl.from_list = true;
ctrl.path_only_unary_keys = false;
REALM_ASSERT(end_of_path);
keys.for_all([&](const auto& k) {
ctrl.matches.insert(k);
ctrl.matches.back().push_back(k);
});
}
return;
}
finish = start + 1;
}
BPlusTree<Mixed> values(ctrl.alloc);
BPlusTree<Mixed> values(*ctrl.alloc);
values.set_parent(&top, 1);
values.init_from_parent();
for (; start < finish; start++) {
val = values.get(start);
if (path_size > 1) {
Collection::get_any(ctrl, val, index + 1);
if (end_of_path) {
ctrl.matches.back().push_back(val);
}
else {
ctrl.matches.insert(val);
Collection::get_any(ctrl, val, index + 1);
}
}
}
Expand All @@ -183,7 +190,7 @@ void Collection::get_any(QueryCtrlBlock& ctrl, Mixed val, size_t index)
auto ref = val.get_ref();
if (!ref)
return;
BPlusTree<Mixed> list(ctrl.alloc);
BPlusTree<Mixed> list(*ctrl.alloc);
list.init_from_ref(ref);
if (size_t sz = list.size()) {
size_t start = 0;
Expand All @@ -199,11 +206,11 @@ void Collection::get_any(QueryCtrlBlock& ctrl, Mixed val, size_t index)
}
for (; start < finish; start++) {
val = list.get(start);
if (path_size > 1) {
Collection::get_any(ctrl, val, index + 1);
if (end_of_path) {
ctrl.matches.back().push_back(val);
}
else {
ctrl.matches.insert(val);
Collection::get_any(ctrl, val, index + 1);
}
}
}
Expand All @@ -214,15 +221,15 @@ void Collection::get_any(QueryCtrlBlock& ctrl, Mixed val, size_t index)
auto col = obj.get_table()->get_column_key(pe.get_key());
if (col) {
val = obj.get_any(col);
if (path_size > 1) {
if (end_of_path) {
ctrl.matches.back().push_back(val);
}
else {
if (val.is_type(type_Link)) {
val = ObjLink(obj.get_target_table(col)->get_key(), val.get<ObjKey>());
}
Collection::get_any(ctrl, val, index + 1);
}
else {
ctrl.matches.insert(val);
}
}
}
}
Expand Down
17 changes: 5 additions & 12 deletions src/realm/collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,11 @@ class Collection {
virtual StablePath get_stable_path() const = 0;

struct QueryCtrlBlock {
QueryCtrlBlock(Path& p, const Table& table, bool is_from_list)
: path(p)
, from_list(is_from_list)
, alloc(table.get_alloc())
, group(table.get_parent_group())
{
}
Path& path;
std::set<Mixed> matches;
bool from_list;
Allocator& alloc;
Group* group;
Path path;
std::vector<std::vector<Mixed>> matches;
bool path_only_unary_keys = false; // Not from list
Allocator* alloc = nullptr;
Group* group = nullptr;
};
static void get_any(QueryCtrlBlock&, Mixed, size_t);
};
Expand Down
4 changes: 4 additions & 0 deletions src/realm/obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,10 @@ Dictionary Obj::get_dictionary(ColKey col_key) const
Obj& Obj::set_collection(ColKey col_key, CollectionType type)
{
REALM_ASSERT(col_key.get_type() == col_type_Mixed);
if ((col_key.is_dictionary() && type == CollectionType::Dictionary) ||
(col_key.is_list() && type == CollectionType::List)) {
return *this;
}
update_if_needed();
Mixed new_val(0, type);

Expand Down
53 changes: 36 additions & 17 deletions src/realm/query_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,19 @@ ColumnDictionaryKeys Columns<Dictionary>::keys()

void Columns<Dictionary>::init_path(const PathElement* begin, const PathElement* end)
{
m_path.clear();
m_path_only_unary_keys = true;
m_ctrl.path.clear();
m_ctrl.path_only_unary_keys = true;
while (begin != end) {
if (begin->is_all()) {
m_path_only_unary_keys = false;
m_ctrl.path_only_unary_keys = false;
}
m_path.emplace_back(std::move(*begin));
m_ctrl.path.emplace_back(std::move(*begin));
++begin;
}
std::move(begin, end, std::back_inserter(m_path));
if (m_path.empty()) {
m_path_only_unary_keys = false;
m_path.push_back(PathElement::AllTag());
std::move(begin, end, std::back_inserter(m_ctrl.path));
if (m_ctrl.path.empty()) {
m_ctrl.path_only_unary_keys = false;
m_ctrl.path.push_back(PathElement::AllTag());
}
}

Expand Down Expand Up @@ -350,6 +350,12 @@ class DictionarySize : public Columns<Dictionary> {
: Columns<Dictionary>(other)
{
}
void reset_path(size_t) override {}
bool more() const override
{
return false;
}

void evaluate(size_t index, ValueBase& destination) override
{
Allocator& alloc = this->m_link_map.get_target_table()->get_alloc();
Expand All @@ -375,37 +381,50 @@ SizeOperator<int64_t> Columns<Dictionary>::size()
return SizeOperator<int64_t>(std::move(ptr));
}

void Columns<Dictionary>::evaluate(size_t index, ValueBase& destination)
void Columns<Dictionary>::reset_path(size_t index)
{
Collection::QueryCtrlBlock ctrl(m_path, *m_link_map.get_target_table(), !m_path_only_unary_keys);

m_ctrl.matches.clear();
if (links_exist()) {
REALM_ASSERT(!m_leaf);
std::vector<ObjKey> links = m_link_map.get_links(index);
auto sz = links.size();
if (!m_link_map.only_unary_links())
ctrl.from_list = true;
m_ctrl.path_only_unary_keys = false;

for (size_t t = 0; t < sz; t++) {
const Obj obj = m_link_map.get_target_table()->get_object(links[t]);
auto val = obj.get_any(m_column_key);
if (!val.is_null()) {
Collection::get_any(ctrl, val, 0);
Collection::get_any(m_ctrl, val, 0);
}
}
}
else {
// Not a link column
REALM_ASSERT(m_leaf);
if (ref_type ref = to_ref(m_leaf->get(index))) {
Collection::get_any(ctrl, {ref, CollectionType::Dictionary}, 0);
Collection::get_any(m_ctrl, {ref, CollectionType::Dictionary}, 0);
}
}
if (m_ctrl.matches.empty()) {
// Make sure we at lease have one empty result
m_ctrl.matches.emplace_back();
}
m_destination_index = 0;
}

bool Columns<Dictionary>::more() const
{
return m_destination_index < m_ctrl.matches.size();
}

void Columns<Dictionary>::evaluate(size_t, ValueBase& destination)
{
// Copy values over
auto sz = ctrl.matches.size();
destination.init(ctrl.from_list || sz == 0, sz);
destination.set(ctrl.matches.begin(), ctrl.matches.end());
auto& matches = m_ctrl.matches[m_destination_index++];
auto sz = matches.size();
destination.init(!m_ctrl.path_only_unary_keys || sz == 0, sz);
destination.set(matches.begin(), matches.end());
}


Expand Down
Loading

0 comments on commit 6a7af1b

Please sign in to comment.