-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Je/query index stage 1 #6715
Je/query index stage 1 #6715
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
#include <realm/collection.hpp> | ||
#include <realm/bplustree.hpp> | ||
#include <realm/array_key.hpp> | ||
#include <realm/array_string.hpp> | ||
#include <realm/array_mixed.hpp> | ||
|
||
namespace realm { | ||
|
||
|
@@ -91,6 +93,48 @@ void check_for_last_unresolved(BPlusTree<ObjKey>* tree) | |
|
||
Collection::~Collection() {} | ||
|
||
Mixed Collection::get_any(Mixed val, Path::const_iterator begin, Path::const_iterator end, Allocator& alloc) | ||
{ | ||
auto path_size = end - begin; | ||
if (val.is_type(type_Dictionary) && begin->is_key()) { | ||
Array top(alloc); | ||
top.init_from_ref(val.get_ref()); | ||
|
||
BPlusTree<StringData> keys(alloc); | ||
keys.set_parent(&top, 0); | ||
keys.init_from_parent(); | ||
size_t ndx = keys.find_first(StringData(begin->get_key())); | ||
if (ndx != realm::not_found) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to see some tests where the key is found and there is more levels in the path, but the dictionary key contains a simple primitive value and not another nested level. In that case, we should return Null early and not recurse further (also for lists). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add some tests. We will always make a recursive call if the key is found, but that call will return quickly as non of the tests for type will succeed. Do you think this is a problem? Otherwise we would have to do the tests twice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems fine to me 👍 |
||
BPlusTree<Mixed> values(alloc); | ||
values.set_parent(&top, 1); | ||
values.init_from_parent(); | ||
val = values.get(ndx); | ||
if (path_size > 1) { | ||
val = Collection::get_any(val, begin + 1, end, alloc); | ||
} | ||
return val; | ||
} | ||
} | ||
if (val.is_type(type_List) && begin->is_ndx()) { | ||
ArrayMixed list(alloc); | ||
list.init_from_ref(val.get_ref()); | ||
if (size_t sz = list.size()) { | ||
auto idx = begin->get_ndx(); | ||
if (idx == size_t(-1)) { | ||
idx = sz - 1; | ||
} | ||
if (idx < sz) { | ||
val = list.get(idx); | ||
} | ||
if (path_size > 1) { | ||
val = Collection::get_any(val, begin + 1, end, alloc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a problem (probably we are never going to hit it though), in case of nested collections that are too deep, we might run out of stack space. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any clue when that would be a problem? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is up to how long is the nested collection... List<List<List<List..... will result in N recursive call proportional to the amount of collections that are nested. Most probably we will never run into this problem in practise, and there are also others part in the code when this can manifest earlier than at this point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a check that nesting level will not exceed 100. |
||
} | ||
return val; | ||
} | ||
} | ||
return {}; | ||
} | ||
|
||
std::pair<std::string, std::string> CollectionBase::get_open_close_strings(size_t link_depth, | ||
JSONOutputMode output_mode) const | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -537,7 +537,7 @@ Query EqualityNode::visit(ParserDriver* drv) | |
else if (right->has_single_value() && (left_type == right_type || left_type == type_Mixed)) { | ||
Mixed val = right->get_mixed(); | ||
const ObjPropertyBase* prop = dynamic_cast<const ObjPropertyBase*>(left.get()); | ||
if (prop && !prop->links_exist()) { | ||
if (prop && !prop->links_exist() && !prop->has_path()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only managing simple values, right? No links? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No links and no path. |
||
auto col_key = prop->column_key(); | ||
if (val.is_null()) { | ||
switch (op) { | ||
|
@@ -848,17 +848,29 @@ std::unique_ptr<Subexpr> PropertyNode::visit(ParserDriver* drv, DataType) | |
} | ||
std::unique_ptr<Subexpr> subexpr{drv->column(m_link_chain, path)}; | ||
|
||
if (!path->at_end()) { | ||
if (path->is_identifier()) { | ||
auto trailing = path->next_identifier(); | ||
Path indexes; | ||
while (!path->at_end()) { | ||
indexes.emplace_back(std::move(*(path->current_path_elem++))); | ||
} | ||
|
||
if (!indexes.empty()) { | ||
const PathElement& first_index = indexes.front(); | ||
if (indexes.size() > 1 && subexpr->get_type() != type_Mixed) { | ||
throw InvalidQueryError("Only Property of type 'any' can have nested collections"); | ||
} | ||
if (auto mixed = dynamic_cast<Columns<Mixed>*>(subexpr.get())) { | ||
mixed->path(indexes); | ||
} | ||
else if (first_index.is_key()) { | ||
auto trailing = first_index.get_key(); | ||
if (auto dict = dynamic_cast<Columns<Dictionary>*>(subexpr.get())) { | ||
if (trailing == "@values") { | ||
} | ||
else if (trailing == "@keys") { | ||
subexpr = std::make_unique<ColumnDictionaryKeys>(*dict); | ||
} | ||
else { | ||
dict->key(trailing); | ||
dict->key(indexes); | ||
} | ||
} | ||
else { | ||
|
@@ -879,9 +891,29 @@ std::unique_ptr<Subexpr> PropertyNode::visit(ParserDriver* drv, DataType) | |
} | ||
} | ||
else { | ||
throw InvalidQueryError("Index not supported"); | ||
// This must be an integer index | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we assert this somehow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Certainly |
||
auto ok = false; | ||
if (indexes.size() == 1) { | ||
if (auto coll = dynamic_cast<ColumnListBase*>(subexpr.get())) { | ||
ok = coll->index(first_index); | ||
} | ||
} | ||
else { | ||
if (auto coll = dynamic_cast<Columns<Lst<Mixed>>*>(subexpr.get())) { | ||
ok = coll->indexes(indexes); | ||
} | ||
} | ||
if (!ok) { | ||
throw InvalidQueryError(util::format("Property '%1.%2' does not support index '%3'", | ||
m_link_chain.get_current_table()->get_class_name(), identifier, | ||
first_index)); | ||
} | ||
} | ||
} | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove... commented code |
||
if (!path->path_elems.back().index.is_null()) { | ||
} | ||
*/ | ||
if (post_op) { | ||
return post_op->visit(drv, subexpr.get()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4269
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍