OrderBy in Expand has two members to differ vertices Vs edges

This commit is contained in:
jeremy 2022-11-22 16:47:25 +01:00
parent 6801d6ff09
commit 3a171376d7
5 changed files with 58 additions and 47 deletions

View File

@ -403,7 +403,9 @@ struct ExpandOneRequest {
std::vector<std::string> vertex_expressions;
std::vector<std::string> edge_expressions;
std::vector<OrderBy> order_by;
std::vector<OrderBy> order_by_vertices;
std::vector<OrderBy> order_by_edges;
// Limit the edges or the vertices?
std::optional<size_t> limit;
std::vector<std::string> filters;

View File

@ -495,11 +495,11 @@ std::array<std::vector<EdgeAccessor>, 2> GetEdgesFromVertex(const VertexAccessor
}
std::vector<Element<EdgeAccessor>> OrderByEdges(DbAccessor &dba, std::vector<EdgeAccessor> &iterable,
std::vector<msgs::OrderBy> &order_bys,
std::vector<msgs::OrderBy> &order_by_edges,
const VertexAccessor &vertex_acc) {
std::vector<Ordering> ordering;
ordering.reserve(order_bys.size());
std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) {
ordering.reserve(order_by_edges.size());
std::transform(order_by_edges.begin(), order_by_edges.end(), std::back_inserter(ordering), [](const auto &order_by) {
if (memgraph::msgs::OrderingDirection::ASCENDING == order_by.direction) {
return Ordering::ASC;
}
@ -510,8 +510,8 @@ std::vector<Element<EdgeAccessor>> OrderByEdges(DbAccessor &dba, std::vector<Edg
std::vector<Element<EdgeAccessor>> ordered;
for (auto it = iterable.begin(); it != iterable.end(); ++it) {
std::vector<TypedValue> properties_order_by;
properties_order_by.reserve(order_bys.size());
std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(properties_order_by),
properties_order_by.reserve(order_by_edges.size());
std::transform(order_by_edges.begin(), order_by_edges.end(), std::back_inserter(properties_order_by),
[&dba, &vertex_acc, &it](const auto &order_by) {
return ComputeExpression(dba, vertex_acc, *it, order_by.expression.expression,
expr::identifier_node_symbol, expr::identifier_edge_symbol);

View File

@ -122,33 +122,27 @@ concept VerticesIt = utils::SameAsAnyOf<T, VerticesIterable, std::vector<VertexA
template <VerticesIt TIterable>
std::vector<Element<VertexAccessor>> OrderByVertices(DbAccessor &dba, TIterable &iterable,
std::vector<msgs::OrderBy> &original_order_bys) {
auto order_bys = original_order_bys;
auto it_to_remove = std::remove_if(order_bys.begin(), order_bys.end(), [](const auto &order_by) {
// We only want to keep OrderBys not impliying edges-ordering
return std::string::npos != order_by.expression.expression.find(expr::identifier_edge_symbol);
});
order_bys.erase(it_to_remove, order_bys.end());
std::vector<msgs::OrderBy> &order_by_vertices) {
std::vector<Ordering> ordering;
ordering.reserve(order_bys.size());
std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(ordering), [](const auto &order_by) {
switch (order_by.direction) {
case memgraph::msgs::OrderingDirection::ASCENDING:
return Ordering::ASC;
case memgraph::msgs::OrderingDirection::DESCENDING:
return Ordering::DESC;
default:
LOG_FATAL("Unknown ordering direction");
}
});
ordering.reserve(order_by_vertices.size());
std::transform(order_by_vertices.begin(), order_by_vertices.end(), std::back_inserter(ordering),
[](const auto &order_by) {
switch (order_by.direction) {
case memgraph::msgs::OrderingDirection::ASCENDING:
return Ordering::ASC;
case memgraph::msgs::OrderingDirection::DESCENDING:
return Ordering::DESC;
default:
LOG_FATAL("Unknown ordering direction");
}
});
std::vector<Element<VertexAccessor>> ordered;
for (auto it = iterable.begin(); it != iterable.end(); ++it) {
std::vector<TypedValue> properties_order_by;
properties_order_by.reserve(order_bys.size());
properties_order_by.reserve(order_by_vertices.size());
std::transform(order_bys.begin(), order_bys.end(), std::back_inserter(properties_order_by),
std::transform(order_by_vertices.begin(), order_by_vertices.end(), std::back_inserter(properties_order_by),
[&dba, &it](const auto &order_by) {
return ComputeExpression(dba, *it, std::nullopt /*e_acc*/, order_by.expression.expression,
expr::identifier_node_symbol, expr::identifier_edge_symbol);

View File

@ -506,10 +506,10 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) {
vertex_accessors.emplace_back(src_vertex_acc_opt.value());
}
if (!req.order_by.empty()) {
if (!req.order_by_vertices.empty()) {
// Can we do differently to avoid this? We need OrderByElements but currently it returns vector<Element>, so this
// workaround is here to avoid more duplication later
auto local_sorted_vertices = OrderByVertices(dba, vertex_accessors, req.order_by);
auto local_sorted_vertices = OrderByVertices(dba, vertex_accessors, req.order_by_vertices);
vertex_accessors.clear();
std::transform(local_sorted_vertices.begin(), local_sorted_vertices.end(), std::back_inserter(vertex_accessors),
[](auto &vertex) { return vertex.object_acc; });
@ -532,7 +532,7 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) {
std::optional<msgs::ExpandOneResultRow> maybe_result;
if (req.order_by.empty()) {
if (req.order_by_vertices.empty()) {
const auto *schema = shard_->GetSchema(shard_->PrimaryLabel());
MG_ASSERT(schema);
maybe_result =
@ -540,8 +540,8 @@ msgs::ReadResponses ShardRsm::HandleRead(msgs::ExpandOneRequest &&req) {
} else {
auto [in_edge_accessors, out_edge_accessors] = GetEdgesFromVertex(src_vertex_acc, req.direction);
const auto in_ordered_edges = OrderByEdges(dba, in_edge_accessors, req.order_by, src_vertex_acc);
const auto out_ordered_edges = OrderByEdges(dba, out_edge_accessors, req.order_by, src_vertex_acc);
const auto in_ordered_edges = OrderByEdges(dba, in_edge_accessors, req.order_by_edges, src_vertex_acc);
const auto out_ordered_edges = OrderByEdges(dba, out_edge_accessors, req.order_by_edges, src_vertex_acc);
std::vector<EdgeAccessor> in_edge_ordered_accessors;
std::transform(in_ordered_edges.begin(), in_ordered_edges.end(), std::back_inserter(in_edge_ordered_accessors),

View File

@ -531,7 +531,8 @@ void AttemptToExpandOneWithWrongEdgeType(ShardClient &client, uint64_t src_verte
std::optional<std::vector<PropertyId>> edge_properties = {};
std::vector<std::string> expressions;
std::vector<msgs::OrderBy> order_by = {};
std::vector<msgs::OrderBy> order_by_vertices = {};
std::vector<msgs::OrderBy> order_by_edges = {};
std::optional<size_t> limit = {};
std::vector<std::string> filter = {};
@ -543,7 +544,8 @@ void AttemptToExpandOneWithWrongEdgeType(ShardClient &client, uint64_t src_verte
expand_one_req.vertex_expressions = expressions;
expand_one_req.filters = filter;
expand_one_req.limit = limit;
expand_one_req.order_by = order_by;
expand_one_req.order_by_vertices = order_by_vertices;
expand_one_req.order_by_edges = order_by_edges;
expand_one_req.src_vertex_properties = src_vertex_properties;
expand_one_req.src_vertices = {src_vertex};
expand_one_req.transaction_id.logical_id = GetTransactionId();
@ -586,7 +588,8 @@ void AttemptToExpandOneSimple(ShardClient &client, uint64_t src_vertex_val, Edge
std::optional<std::vector<PropertyId>> edge_properties = {};
std::vector<std::string> expressions;
std::vector<msgs::OrderBy> order_by = {};
std::vector<msgs::OrderBy> order_by_vertices = {};
std::vector<msgs::OrderBy> order_by_edges = {};
std::optional<size_t> limit = {};
std::vector<std::string> filter = {};
@ -598,7 +601,8 @@ void AttemptToExpandOneSimple(ShardClient &client, uint64_t src_vertex_val, Edge
expand_one_req.vertex_expressions = expressions;
expand_one_req.filters = filter;
expand_one_req.limit = limit;
expand_one_req.order_by = order_by;
expand_one_req.order_by_vertices = order_by_vertices;
expand_one_req.order_by_edges = order_by_edges;
expand_one_req.src_vertex_properties = src_vertex_properties;
expand_one_req.src_vertices = {src_vertex};
expand_one_req.transaction_id.logical_id = GetTransactionId();
@ -642,7 +646,8 @@ void AttemptToExpandOneWithUniqueEdges(ShardClient &client, uint64_t src_vertex_
std::optional<std::vector<PropertyId>> edge_properties = {};
std::vector<std::string> expressions;
std::vector<msgs::OrderBy> order_by = {};
std::vector<msgs::OrderBy> order_by_vertices = {};
std::vector<msgs::OrderBy> order_by_edges = {};
std::optional<size_t> limit = {};
std::vector<std::string> filter = {};
@ -654,7 +659,8 @@ void AttemptToExpandOneWithUniqueEdges(ShardClient &client, uint64_t src_vertex_
expand_one_req.vertex_expressions = expressions;
expand_one_req.filters = filter;
expand_one_req.limit = limit;
expand_one_req.order_by = order_by;
expand_one_req.order_by_vertices = order_by_vertices;
expand_one_req.order_by_edges = order_by_edges;
expand_one_req.src_vertex_properties = src_vertex_properties;
expand_one_req.src_vertices = {src_vertex};
expand_one_req.only_unique_neighbor_rows = true;
@ -700,9 +706,11 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_
// Edge properties to look for
std::optional<std::vector<PropertyId>> edge_properties = {};
std::vector<msgs::OrderBy> order_by = {
{msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::ASCENDING},
std::vector<msgs::OrderBy> order_by_vertices = {
{msgs::Expression{"MG_SYMBOL_NODE.prop1"}, msgs::OrderingDirection::ASCENDING}};
std::vector<msgs::OrderBy> order_by_edges = {
{msgs::Expression{"MG_SYMBOL_EDGE.prop4"}, msgs::OrderingDirection::DESCENDING}};
size_t limit = 1;
std::vector<std::string> filters = {"MG_SYMBOL_NODE.prop1 != -1"};
@ -713,7 +721,8 @@ void AttemptToExpandOneLimitAndOrderBy(ShardClient &client, uint64_t src_vertex_
expand_one_req.edge_types = {edge_type};
expand_one_req.filters = filters;
expand_one_req.limit = limit;
expand_one_req.order_by = order_by;
expand_one_req.order_by_vertices = order_by_vertices;
expand_one_req.order_by_edges = order_by_edges;
expand_one_req.src_vertex_properties = src_vertex_properties;
expand_one_req.src_vertices = {src_vertex, other_src_vertex};
expand_one_req.transaction_id.logical_id = GetTransactionId();
@ -780,7 +789,8 @@ void AttemptToExpandOneWithSpecifiedSrcVertexProperties(ShardClient &client, uin
std::optional<std::vector<PropertyId>> edge_properties = {};
std::vector<std::string> expressions;
std::vector<msgs::OrderBy> order_by = {};
std::vector<msgs::OrderBy> order_by_vertices = {};
std::vector<msgs::OrderBy> order_by_edges = {};
std::optional<size_t> limit = {};
std::vector<std::string> filter = {};
@ -792,7 +802,8 @@ void AttemptToExpandOneWithSpecifiedSrcVertexProperties(ShardClient &client, uin
expand_one_req.vertex_expressions = expressions;
expand_one_req.filters = filter;
expand_one_req.limit = limit;
expand_one_req.order_by = order_by;
expand_one_req.order_by_vertices = order_by_vertices;
expand_one_req.order_by_edges = order_by_edges;
expand_one_req.src_vertex_properties = src_vertex_properties;
expand_one_req.src_vertices = {src_vertex};
expand_one_req.transaction_id.logical_id = GetTransactionId();
@ -840,7 +851,8 @@ void AttemptToExpandOneWithSpecifiedEdgeProperties(ShardClient &client, uint64_t
std::optional<std::vector<PropertyId>> edge_properties = {specified_edge_prop};
std::vector<std::string> expressions;
std::vector<msgs::OrderBy> order_by = {};
std::vector<msgs::OrderBy> order_by_vertices = {};
std::vector<msgs::OrderBy> order_by_edges = {};
std::optional<size_t> limit = {};
std::vector<std::string> filter = {};
@ -852,7 +864,8 @@ void AttemptToExpandOneWithSpecifiedEdgeProperties(ShardClient &client, uint64_t
expand_one_req.vertex_expressions = expressions;
expand_one_req.filters = filter;
expand_one_req.limit = limit;
expand_one_req.order_by = order_by;
expand_one_req.order_by_vertices = order_by_vertices;
expand_one_req.order_by_edges = order_by_edges;
expand_one_req.src_vertex_properties = src_vertex_properties;
expand_one_req.src_vertices = {src_vertex};
expand_one_req.transaction_id.logical_id = GetTransactionId();
@ -899,7 +912,8 @@ void AttemptToExpandOneWithFilters(ShardClient &client, uint64_t src_vertex_val,
std::optional<std::vector<PropertyId>> edge_properties = {};
std::vector<std::string> expressions;
std::vector<msgs::OrderBy> order_by = {};
std::vector<msgs::OrderBy> order_by_vertices = {};
std::vector<msgs::OrderBy> order_by_edges = {};
std::optional<size_t> limit = {};
std::vector<std::string> filter = {};
@ -911,7 +925,8 @@ void AttemptToExpandOneWithFilters(ShardClient &client, uint64_t src_vertex_val,
expand_one_req.vertex_expressions = expressions;
expand_one_req.filters = {filter_expr1};
expand_one_req.limit = limit;
expand_one_req.order_by = order_by;
expand_one_req.order_by_vertices = order_by_vertices;
expand_one_req.order_by_edges = order_by_edges;
expand_one_req.src_vertex_properties = src_vertex_properties;
expand_one_req.src_vertices = {src_vertex};
expand_one_req.transaction_id.logical_id = GetTransactionId();