Add path and weight to variable expand filter (#1434)
Co-authored-by: Aidar Samerkhanov <aidar.samerkhanov@memgraph.io>
This commit is contained in:
parent
14f92b4a0f
commit
3ccd78ac71
@ -1818,6 +1818,10 @@ class EdgeAtom : public memgraph::query::PatternAtom {
|
||||
memgraph::query::Identifier *inner_edge{nullptr};
|
||||
/// Argument identifier for the destination node of the edge.
|
||||
memgraph::query::Identifier *inner_node{nullptr};
|
||||
/// Argument identifier for the currently-accumulated path.
|
||||
memgraph::query::Identifier *accumulated_path{nullptr};
|
||||
/// Argument identifier for the weight of the currently-accumulated path.
|
||||
memgraph::query::Identifier *accumulated_weight{nullptr};
|
||||
/// Evaluates the result of the lambda.
|
||||
memgraph::query::Expression *expression{nullptr};
|
||||
|
||||
@ -1825,6 +1829,8 @@ class EdgeAtom : public memgraph::query::PatternAtom {
|
||||
Lambda object;
|
||||
object.inner_edge = inner_edge ? inner_edge->Clone(storage) : nullptr;
|
||||
object.inner_node = inner_node ? inner_node->Clone(storage) : nullptr;
|
||||
object.accumulated_path = accumulated_path ? accumulated_path->Clone(storage) : nullptr;
|
||||
object.accumulated_weight = accumulated_weight ? accumulated_weight->Clone(storage) : nullptr;
|
||||
object.expression = expression ? expression->Clone(storage) : nullptr;
|
||||
return object;
|
||||
}
|
||||
|
@ -1978,6 +1978,15 @@ antlrcpp::Any CypherMainVisitor::visitRelationshipPattern(MemgraphCypher::Relati
|
||||
edge_lambda.inner_edge = storage_->Create<Identifier>(traversed_edge_variable);
|
||||
auto traversed_node_variable = std::any_cast<std::string>(lambda->traversed_node->accept(this));
|
||||
edge_lambda.inner_node = storage_->Create<Identifier>(traversed_node_variable);
|
||||
if (lambda->accumulated_path) {
|
||||
auto accumulated_path_variable = std::any_cast<std::string>(lambda->accumulated_path->accept(this));
|
||||
edge_lambda.accumulated_path = storage_->Create<Identifier>(accumulated_path_variable);
|
||||
|
||||
if (lambda->accumulated_weight) {
|
||||
auto accumulated_weight_variable = std::any_cast<std::string>(lambda->accumulated_weight->accept(this));
|
||||
edge_lambda.accumulated_weight = storage_->Create<Identifier>(accumulated_weight_variable);
|
||||
}
|
||||
}
|
||||
edge_lambda.expression = std::any_cast<Expression *>(lambda->expression()->accept(this));
|
||||
return edge_lambda;
|
||||
};
|
||||
@ -2002,6 +2011,15 @@ antlrcpp::Any CypherMainVisitor::visitRelationshipPattern(MemgraphCypher::Relati
|
||||
// In variable expansion inner variables are mandatory.
|
||||
anonymous_identifiers.push_back(&edge->filter_lambda_.inner_edge);
|
||||
anonymous_identifiers.push_back(&edge->filter_lambda_.inner_node);
|
||||
|
||||
// TODO: In what use case do we need accumulated path and weight here?
|
||||
if (edge->filter_lambda_.accumulated_path) {
|
||||
anonymous_identifiers.push_back(&edge->filter_lambda_.accumulated_path);
|
||||
|
||||
if (edge->filter_lambda_.accumulated_weight) {
|
||||
anonymous_identifiers.push_back(&edge->filter_lambda_.accumulated_weight);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (edge->type_ == EdgeAtom::Type::WEIGHTED_SHORTEST_PATH ||
|
||||
@ -2013,9 +2031,21 @@ antlrcpp::Any CypherMainVisitor::visitRelationshipPattern(MemgraphCypher::Relati
|
||||
// Add mandatory inner variables for filter lambda.
|
||||
anonymous_identifiers.push_back(&edge->filter_lambda_.inner_edge);
|
||||
anonymous_identifiers.push_back(&edge->filter_lambda_.inner_node);
|
||||
if (edge->filter_lambda_.accumulated_path) {
|
||||
anonymous_identifiers.push_back(&edge->filter_lambda_.accumulated_path);
|
||||
|
||||
if (edge->filter_lambda_.accumulated_weight) {
|
||||
anonymous_identifiers.push_back(&edge->filter_lambda_.accumulated_weight);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Other variable expands only have the filter lambda.
|
||||
edge->filter_lambda_ = visit_lambda(relationshipLambdas[0]);
|
||||
if (edge->filter_lambda_.accumulated_weight) {
|
||||
throw SemanticException(
|
||||
"Accumulated weight in filter lambda can be used only with "
|
||||
"shortest paths expansion.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
|
@ -175,7 +175,7 @@ relationshipDetail : '[' ( name=variable )? ( relationshipTypes )? ( variableExp
|
||||
| '[' ( name=variable )? ( relationshipTypes )? ( variableExpansion )? relationshipLambda ( total_weight=variable )? (relationshipLambda )? ']'
|
||||
| '[' ( name=variable )? ( relationshipTypes )? ( variableExpansion )? (properties )* ( relationshipLambda total_weight=variable )? (relationshipLambda )? ']';
|
||||
|
||||
relationshipLambda: '(' traversed_edge=variable ',' traversed_node=variable '|' expression ')';
|
||||
relationshipLambda: '(' traversed_edge=variable ',' traversed_node=variable ( ',' accumulated_path=variable )? ( ',' accumulated_weight=variable )? '|' expression ')';
|
||||
|
||||
variableExpansion : '*' (BFS | WSHORTEST | ALLSHORTEST)? ( expression )? ( '..' ( expression )? )? ;
|
||||
|
||||
|
@ -658,8 +658,16 @@ bool SymbolGenerator::PreVisit(EdgeAtom &edge_atom) {
|
||||
scope.in_edge_range = false;
|
||||
scope.in_pattern = false;
|
||||
if (edge_atom.filter_lambda_.expression) {
|
||||
VisitWithIdentifiers(edge_atom.filter_lambda_.expression,
|
||||
{edge_atom.filter_lambda_.inner_edge, edge_atom.filter_lambda_.inner_node});
|
||||
std::vector<Identifier *> filter_lambda_identifiers{edge_atom.filter_lambda_.inner_edge,
|
||||
edge_atom.filter_lambda_.inner_node};
|
||||
if (edge_atom.filter_lambda_.accumulated_path) {
|
||||
filter_lambda_identifiers.emplace_back(edge_atom.filter_lambda_.accumulated_path);
|
||||
|
||||
if (edge_atom.filter_lambda_.accumulated_weight) {
|
||||
filter_lambda_identifiers.emplace_back(edge_atom.filter_lambda_.accumulated_weight);
|
||||
}
|
||||
}
|
||||
VisitWithIdentifiers(edge_atom.filter_lambda_.expression, filter_lambda_identifiers);
|
||||
} else {
|
||||
// Create inner symbols, but don't bind them in scope, since they are to
|
||||
// be used in the missing filter expression.
|
||||
@ -668,6 +676,17 @@ bool SymbolGenerator::PreVisit(EdgeAtom &edge_atom) {
|
||||
auto *inner_node = edge_atom.filter_lambda_.inner_node;
|
||||
inner_node->MapTo(
|
||||
symbol_table_->CreateSymbol(inner_node->name_, inner_node->user_declared_, Symbol::Type::VERTEX));
|
||||
if (edge_atom.filter_lambda_.accumulated_path) {
|
||||
auto *accumulated_path = edge_atom.filter_lambda_.accumulated_path;
|
||||
accumulated_path->MapTo(
|
||||
symbol_table_->CreateSymbol(accumulated_path->name_, accumulated_path->user_declared_, Symbol::Type::PATH));
|
||||
|
||||
if (edge_atom.filter_lambda_.accumulated_weight) {
|
||||
auto *accumulated_weight = edge_atom.filter_lambda_.accumulated_weight;
|
||||
accumulated_weight->MapTo(symbol_table_->CreateSymbol(
|
||||
accumulated_weight->name_, accumulated_weight->user_declared_, Symbol::Type::NUMBER));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (edge_atom.weight_lambda_.expression) {
|
||||
VisitWithIdentifiers(edge_atom.weight_lambda_.expression,
|
||||
|
@ -1138,6 +1138,11 @@ class ExpandVariableCursor : public Cursor {
|
||||
edges_it_.emplace_back(edges_.back().begin());
|
||||
}
|
||||
|
||||
if (self_.filter_lambda_.accumulated_path_symbol) {
|
||||
// Add initial vertex of path to the accumulated path
|
||||
frame[self_.filter_lambda_.accumulated_path_symbol.value()] = Path(vertex);
|
||||
}
|
||||
|
||||
// reset the frame value to an empty edge list
|
||||
auto *pull_memory = context.evaluation_context.memory;
|
||||
frame[self_.common_.edge_symbol] = TypedValue::TVector(pull_memory);
|
||||
@ -1234,6 +1239,13 @@ class ExpandVariableCursor : public Cursor {
|
||||
// Skip expanding out of filtered expansion.
|
||||
frame[self_.filter_lambda_.inner_edge_symbol] = current_edge.first;
|
||||
frame[self_.filter_lambda_.inner_node_symbol] = current_vertex;
|
||||
if (self_.filter_lambda_.accumulated_path_symbol) {
|
||||
MG_ASSERT(frame[self_.filter_lambda_.accumulated_path_symbol.value()].IsPath(),
|
||||
"Accumulated path must be path");
|
||||
Path &accumulated_path = frame[self_.filter_lambda_.accumulated_path_symbol.value()].ValuePath();
|
||||
accumulated_path.Expand(current_edge.first);
|
||||
accumulated_path.Expand(current_vertex);
|
||||
}
|
||||
if (self_.filter_lambda_.expression && !EvaluateFilter(evaluator, self_.filter_lambda_.expression)) continue;
|
||||
|
||||
// we are doing depth-first search, so place the current
|
||||
@ -1546,6 +1558,13 @@ class SingleSourceShortestPathCursor : public query::plan::Cursor {
|
||||
#endif
|
||||
frame[self_.filter_lambda_.inner_edge_symbol] = edge;
|
||||
frame[self_.filter_lambda_.inner_node_symbol] = vertex;
|
||||
if (self_.filter_lambda_.accumulated_path_symbol) {
|
||||
MG_ASSERT(frame[self_.filter_lambda_.accumulated_path_symbol.value()].IsPath(),
|
||||
"Accumulated path must have Path type");
|
||||
Path &accumulated_path = frame[self_.filter_lambda_.accumulated_path_symbol.value()].ValuePath();
|
||||
accumulated_path.Expand(edge);
|
||||
accumulated_path.Expand(vertex);
|
||||
}
|
||||
|
||||
if (self_.filter_lambda_.expression) {
|
||||
TypedValue result = self_.filter_lambda_.expression->Accept(evaluator);
|
||||
@ -1607,6 +1626,11 @@ class SingleSourceShortestPathCursor : public query::plan::Cursor {
|
||||
const auto &vertex = vertex_value.ValueVertex();
|
||||
processed_.emplace(vertex, std::nullopt);
|
||||
|
||||
if (self_.filter_lambda_.accumulated_path_symbol) {
|
||||
// Add initial vertex of path to the accumulated path
|
||||
frame[self_.filter_lambda_.accumulated_path_symbol.value()] = Path(vertex);
|
||||
}
|
||||
|
||||
expand_from_vertex(vertex);
|
||||
|
||||
// go back to loop start and see if we expanded anything
|
||||
@ -1677,6 +1701,10 @@ class SingleSourceShortestPathCursor : public query::plan::Cursor {
|
||||
namespace {
|
||||
|
||||
void CheckWeightType(TypedValue current_weight, utils::MemoryResource *memory) {
|
||||
if (current_weight.IsNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!current_weight.IsNumeric() && !current_weight.IsDuration()) {
|
||||
throw QueryRuntimeException("Calculated weight must be numeric or a Duration, got {}.", current_weight.type());
|
||||
}
|
||||
@ -1694,6 +1722,34 @@ void CheckWeightType(TypedValue current_weight, utils::MemoryResource *memory) {
|
||||
}
|
||||
}
|
||||
|
||||
void ValidateWeightTypes(const TypedValue &lhs, const TypedValue &rhs) {
|
||||
if ((lhs.IsNumeric() && rhs.IsNumeric()) || (lhs.IsDuration() && rhs.IsDuration())) {
|
||||
return;
|
||||
}
|
||||
throw QueryRuntimeException(utils::MessageWithLink(
|
||||
"All weights should be of the same type, either numeric or a Duration. Please update the weight "
|
||||
"expression or the filter expression.",
|
||||
"https://memgr.ph/wsp"));
|
||||
}
|
||||
|
||||
TypedValue CalculateNextWeight(const std::optional<memgraph::query::plan::ExpansionLambda> &weight_lambda,
|
||||
const TypedValue &total_weight, ExpressionEvaluator evaluator) {
|
||||
if (!weight_lambda) {
|
||||
return {};
|
||||
}
|
||||
auto *memory = evaluator.GetMemoryResource();
|
||||
TypedValue current_weight = weight_lambda->expression->Accept(evaluator);
|
||||
CheckWeightType(current_weight, memory);
|
||||
|
||||
if (total_weight.IsNull()) {
|
||||
return current_weight;
|
||||
}
|
||||
|
||||
ValidateWeightTypes(current_weight, total_weight);
|
||||
|
||||
return TypedValue(current_weight, memory) + total_weight;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
class ExpandWeightedShortestPathCursor : public query::plan::Cursor {
|
||||
@ -1722,7 +1778,6 @@ class ExpandWeightedShortestPathCursor : public query::plan::Cursor {
|
||||
auto expand_pair = [this, &evaluator, &frame, &create_state, &context](
|
||||
const EdgeAccessor &edge, const VertexAccessor &vertex, const TypedValue &total_weight,
|
||||
int64_t depth) {
|
||||
auto *memory = evaluator.GetMemoryResource();
|
||||
#ifdef MG_ENTERPRISE
|
||||
if (license::global_license_checker.IsEnterpriseValidFast() && context.auth_checker &&
|
||||
!(context.auth_checker->Has(vertex, storage::View::OLD,
|
||||
@ -1731,32 +1786,31 @@ class ExpandWeightedShortestPathCursor : public query::plan::Cursor {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
frame[self_.weight_lambda_->inner_edge_symbol] = edge;
|
||||
frame[self_.weight_lambda_->inner_node_symbol] = vertex;
|
||||
TypedValue next_weight = CalculateNextWeight(self_.weight_lambda_, total_weight, evaluator);
|
||||
|
||||
if (self_.filter_lambda_.expression) {
|
||||
frame[self_.filter_lambda_.inner_edge_symbol] = edge;
|
||||
frame[self_.filter_lambda_.inner_node_symbol] = vertex;
|
||||
if (self_.filter_lambda_.accumulated_path_symbol) {
|
||||
MG_ASSERT(frame[self_.filter_lambda_.accumulated_path_symbol.value()].IsPath(),
|
||||
"Accumulated path must be path");
|
||||
Path &accumulated_path = frame[self_.filter_lambda_.accumulated_path_symbol.value()].ValuePath();
|
||||
accumulated_path.Expand(edge);
|
||||
accumulated_path.Expand(vertex);
|
||||
|
||||
if (self_.filter_lambda_.accumulated_weight_symbol) {
|
||||
frame[self_.filter_lambda_.accumulated_weight_symbol.value()] = next_weight;
|
||||
}
|
||||
}
|
||||
|
||||
if (!EvaluateFilter(evaluator, self_.filter_lambda_.expression)) return;
|
||||
}
|
||||
|
||||
frame[self_.weight_lambda_->inner_edge_symbol] = edge;
|
||||
frame[self_.weight_lambda_->inner_node_symbol] = vertex;
|
||||
|
||||
TypedValue current_weight = self_.weight_lambda_->expression->Accept(evaluator);
|
||||
|
||||
CheckWeightType(current_weight, memory);
|
||||
|
||||
auto next_state = create_state(vertex, depth);
|
||||
|
||||
TypedValue next_weight = std::invoke([&] {
|
||||
if (total_weight.IsNull()) {
|
||||
return current_weight;
|
||||
}
|
||||
|
||||
ValidateWeightTypes(current_weight, total_weight);
|
||||
|
||||
return TypedValue(current_weight, memory) + total_weight;
|
||||
});
|
||||
|
||||
auto found_it = total_cost_.find(next_state);
|
||||
if (found_it != total_cost_.end() && (found_it->second.IsNull() || (found_it->second <= next_weight).ValueBool()))
|
||||
return;
|
||||
@ -1796,6 +1850,10 @@ class ExpandWeightedShortestPathCursor : public query::plan::Cursor {
|
||||
// Skip expansion for such nodes.
|
||||
if (node.IsNull()) continue;
|
||||
}
|
||||
if (self_.filter_lambda_.accumulated_path_symbol) {
|
||||
// Add initial vertex of path to the accumulated path
|
||||
frame[self_.filter_lambda_.accumulated_path_symbol.value()] = Path(vertex);
|
||||
}
|
||||
if (self_.upper_bound_) {
|
||||
upper_bound_ = EvaluateInt(&evaluator, self_.upper_bound_, "Max depth in weighted shortest path expansion");
|
||||
upper_bound_set_ = true;
|
||||
@ -1808,12 +1866,17 @@ class ExpandWeightedShortestPathCursor : public query::plan::Cursor {
|
||||
"Maximum depth in weighted shortest path expansion must be at "
|
||||
"least 1.");
|
||||
|
||||
frame[self_.weight_lambda_->inner_edge_symbol] = TypedValue();
|
||||
frame[self_.weight_lambda_->inner_node_symbol] = vertex;
|
||||
TypedValue current_weight =
|
||||
CalculateNextWeight(self_.weight_lambda_, /* total_weight */ TypedValue(), evaluator);
|
||||
|
||||
// Clear existing data structures.
|
||||
previous_.clear();
|
||||
total_cost_.clear();
|
||||
yielded_vertices_.clear();
|
||||
|
||||
pq_.emplace(TypedValue(), 0, vertex, std::nullopt);
|
||||
pq_.emplace(current_weight, 0, vertex, std::nullopt);
|
||||
// We are adding the starting vertex to the set of yielded vertices
|
||||
// because we don't want to yield paths that end with the starting
|
||||
// vertex.
|
||||
@ -1913,15 +1976,6 @@ class ExpandWeightedShortestPathCursor : public query::plan::Cursor {
|
||||
// Keeps track of vertices for which we yielded a path already.
|
||||
utils::pmr::unordered_set<VertexAccessor> yielded_vertices_;
|
||||
|
||||
static void ValidateWeightTypes(const TypedValue &lhs, const TypedValue &rhs) {
|
||||
if (!((lhs.IsNumeric() && lhs.IsNumeric()) || (rhs.IsDuration() && rhs.IsDuration()))) {
|
||||
throw QueryRuntimeException(utils::MessageWithLink(
|
||||
"All weights should be of the same type, either numeric or a Duration. Please update the weight "
|
||||
"expression or the filter expression.",
|
||||
"https://memgr.ph/wsp"));
|
||||
}
|
||||
}
|
||||
|
||||
// Priority queue comparator. Keep lowest weight on top of the queue.
|
||||
class PriorityQueueComparator {
|
||||
public:
|
||||
@ -1979,36 +2033,32 @@ class ExpandAllShortestPathsCursor : public query::plan::Cursor {
|
||||
// queue.
|
||||
auto expand_vertex = [this, &evaluator, &frame](const EdgeAccessor &edge, const EdgeAtom::Direction direction,
|
||||
const TypedValue &total_weight, int64_t depth) {
|
||||
auto *memory = evaluator.GetMemoryResource();
|
||||
|
||||
auto const &next_vertex = direction == EdgeAtom::Direction::IN ? edge.From() : edge.To();
|
||||
|
||||
// Evaluate current weight
|
||||
frame[self_.weight_lambda_->inner_edge_symbol] = edge;
|
||||
frame[self_.weight_lambda_->inner_node_symbol] = next_vertex;
|
||||
TypedValue next_weight = CalculateNextWeight(self_.weight_lambda_, total_weight, evaluator);
|
||||
|
||||
// If filter expression exists, evaluate filter
|
||||
if (self_.filter_lambda_.expression) {
|
||||
frame[self_.filter_lambda_.inner_edge_symbol] = edge;
|
||||
frame[self_.filter_lambda_.inner_node_symbol] = next_vertex;
|
||||
if (self_.filter_lambda_.accumulated_path_symbol) {
|
||||
MG_ASSERT(frame[self_.filter_lambda_.accumulated_path_symbol.value()].IsPath(),
|
||||
"Accumulated path must be path");
|
||||
Path &accumulated_path = frame[self_.filter_lambda_.accumulated_path_symbol.value()].ValuePath();
|
||||
accumulated_path.Expand(edge);
|
||||
accumulated_path.Expand(next_vertex);
|
||||
|
||||
if (self_.filter_lambda_.accumulated_weight_symbol) {
|
||||
frame[self_.filter_lambda_.accumulated_weight_symbol.value()] = next_weight;
|
||||
}
|
||||
}
|
||||
|
||||
if (!EvaluateFilter(evaluator, self_.filter_lambda_.expression)) return;
|
||||
}
|
||||
|
||||
// Evaluate current weight
|
||||
frame[self_.weight_lambda_->inner_edge_symbol] = edge;
|
||||
frame[self_.weight_lambda_->inner_node_symbol] = next_vertex;
|
||||
|
||||
TypedValue current_weight = self_.weight_lambda_->expression->Accept(evaluator);
|
||||
|
||||
CheckWeightType(current_weight, memory);
|
||||
|
||||
TypedValue next_weight = std::invoke([&] {
|
||||
if (total_weight.IsNull()) {
|
||||
return current_weight;
|
||||
}
|
||||
|
||||
ValidateWeightTypes(current_weight, total_weight);
|
||||
|
||||
return TypedValue(current_weight, memory) + total_weight;
|
||||
});
|
||||
|
||||
auto found_it = visited_cost_.find(next_vertex);
|
||||
// Check if the vertex has already been processed.
|
||||
if (found_it != visited_cost_.end()) {
|
||||
@ -2200,7 +2250,17 @@ class ExpandAllShortestPathsCursor : public query::plan::Cursor {
|
||||
traversal_stack_.clear();
|
||||
total_cost_.clear();
|
||||
|
||||
expand_from_vertex(*start_vertex, TypedValue(), 0);
|
||||
if (self_.filter_lambda_.accumulated_path_symbol) {
|
||||
// Add initial vertex of path to the accumulated path
|
||||
frame[self_.filter_lambda_.accumulated_path_symbol.value()] = Path(*start_vertex);
|
||||
}
|
||||
|
||||
frame[self_.weight_lambda_->inner_edge_symbol] = TypedValue();
|
||||
frame[self_.weight_lambda_->inner_node_symbol] = *start_vertex;
|
||||
TypedValue current_weight =
|
||||
CalculateNextWeight(self_.weight_lambda_, /* total_weight */ TypedValue(), evaluator);
|
||||
|
||||
expand_from_vertex(*start_vertex, current_weight, 0);
|
||||
visited_cost_.emplace(*start_vertex, 0);
|
||||
frame[self_.common_.edge_symbol] = TypedValue::TVector(memory);
|
||||
}
|
||||
@ -2252,15 +2312,6 @@ class ExpandAllShortestPathsCursor : public query::plan::Cursor {
|
||||
// Stack indicating the traversal level.
|
||||
utils::pmr::list<utils::pmr::list<DirectedEdge>> traversal_stack_;
|
||||
|
||||
static void ValidateWeightTypes(const TypedValue &lhs, const TypedValue &rhs) {
|
||||
if (!((lhs.IsNumeric() && lhs.IsNumeric()) || (rhs.IsDuration() && rhs.IsDuration()))) {
|
||||
throw QueryRuntimeException(utils::MessageWithLink(
|
||||
"All weights should be of the same type, either numeric or a Duration. Please update the weight "
|
||||
"expression or the filter expression.",
|
||||
"https://memgr.ph/wsp"));
|
||||
}
|
||||
}
|
||||
|
||||
// Priority queue comparator. Keep lowest weight on top of the queue.
|
||||
class PriorityQueueComparator {
|
||||
public:
|
||||
|
@ -917,12 +917,18 @@ struct ExpansionLambda {
|
||||
Symbol inner_node_symbol;
|
||||
/// Expression used in lambda during expansion.
|
||||
Expression *expression;
|
||||
/// Currently expanded accumulated path symbol.
|
||||
std::optional<Symbol> accumulated_path_symbol;
|
||||
/// Currently expanded accumulated weight symbol.
|
||||
std::optional<Symbol> accumulated_weight_symbol;
|
||||
|
||||
ExpansionLambda Clone(AstStorage *storage) const {
|
||||
ExpansionLambda object;
|
||||
object.inner_edge_symbol = inner_edge_symbol;
|
||||
object.inner_node_symbol = inner_node_symbol;
|
||||
object.expression = expression ? expression->Clone(storage) : nullptr;
|
||||
object.accumulated_path_symbol = accumulated_path_symbol;
|
||||
object.accumulated_weight_symbol = accumulated_weight_symbol;
|
||||
return object;
|
||||
}
|
||||
};
|
||||
|
@ -74,6 +74,13 @@ std::vector<Expansion> NormalizePatterns(const SymbolTable &symbol_table, const
|
||||
// Remove symbols which are bound by lambda arguments.
|
||||
collector.symbols_.erase(symbol_table.at(*edge->filter_lambda_.inner_edge));
|
||||
collector.symbols_.erase(symbol_table.at(*edge->filter_lambda_.inner_node));
|
||||
if (edge->filter_lambda_.accumulated_path) {
|
||||
collector.symbols_.erase(symbol_table.at(*edge->filter_lambda_.accumulated_path));
|
||||
|
||||
if (edge->filter_lambda_.accumulated_weight) {
|
||||
collector.symbols_.erase(symbol_table.at(*edge->filter_lambda_.accumulated_weight));
|
||||
}
|
||||
}
|
||||
if (edge->type_ == EdgeAtom::Type::WEIGHTED_SHORTEST_PATH ||
|
||||
edge->type_ == EdgeAtom::Type::ALL_SHORTEST_PATHS) {
|
||||
collector.symbols_.erase(symbol_table.at(*edge->weight_lambda_.inner_edge));
|
||||
@ -295,6 +302,13 @@ void Filters::CollectPatternFilters(Pattern &pattern, SymbolTable &symbol_table,
|
||||
prop_pair.second->Accept(collector);
|
||||
collector.symbols_.emplace(symbol_table.at(*atom->filter_lambda_.inner_node));
|
||||
collector.symbols_.emplace(symbol_table.at(*atom->filter_lambda_.inner_edge));
|
||||
if (atom->filter_lambda_.accumulated_path) {
|
||||
collector.symbols_.emplace(symbol_table.at(*atom->filter_lambda_.accumulated_path));
|
||||
|
||||
if (atom->filter_lambda_.accumulated_weight) {
|
||||
collector.symbols_.emplace(symbol_table.at(*atom->filter_lambda_.accumulated_weight));
|
||||
}
|
||||
}
|
||||
// First handle the inline property filter.
|
||||
auto *property_lookup = storage.Create<PropertyLookup>(atom->filter_lambda_.inner_edge, prop_pair.first);
|
||||
auto *prop_equal = storage.Create<EqualOperator>(property_lookup, prop_pair.second);
|
||||
|
@ -171,6 +171,11 @@ class IndexLookupRewriter final : public HierarchicalLogicalOperatorVisitor {
|
||||
if (expand.common_.existing_node) {
|
||||
return true;
|
||||
}
|
||||
if (expand.type_ == EdgeAtom::Type::BREADTH_FIRST && expand.filter_lambda_.accumulated_path_symbol) {
|
||||
// When accumulated path is used, we cannot use ST shortest path algorithm.
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<ScanAll> indexed_scan;
|
||||
ScanAll dst_scan(expand.input(), expand.common_.node_symbol, storage::View::OLD);
|
||||
// With expand to existing we only get real gains with BFS, because we use a
|
||||
|
@ -705,9 +705,9 @@ class RuleBasedPlanner {
|
||||
std::optional<Symbol> total_weight;
|
||||
|
||||
if (edge->type_ == EdgeAtom::Type::WEIGHTED_SHORTEST_PATH || edge->type_ == EdgeAtom::Type::ALL_SHORTEST_PATHS) {
|
||||
weight_lambda.emplace(ExpansionLambda{symbol_table.at(*edge->weight_lambda_.inner_edge),
|
||||
symbol_table.at(*edge->weight_lambda_.inner_node),
|
||||
edge->weight_lambda_.expression});
|
||||
weight_lambda.emplace(ExpansionLambda{.inner_edge_symbol = symbol_table.at(*edge->weight_lambda_.inner_edge),
|
||||
.inner_node_symbol = symbol_table.at(*edge->weight_lambda_.inner_node),
|
||||
.expression = edge->weight_lambda_.expression});
|
||||
|
||||
total_weight.emplace(symbol_table.at(*edge->total_weight_));
|
||||
}
|
||||
@ -715,12 +715,28 @@ class RuleBasedPlanner {
|
||||
ExpansionLambda filter_lambda;
|
||||
filter_lambda.inner_edge_symbol = symbol_table.at(*edge->filter_lambda_.inner_edge);
|
||||
filter_lambda.inner_node_symbol = symbol_table.at(*edge->filter_lambda_.inner_node);
|
||||
if (edge->filter_lambda_.accumulated_path) {
|
||||
filter_lambda.accumulated_path_symbol = symbol_table.at(*edge->filter_lambda_.accumulated_path);
|
||||
|
||||
if (edge->filter_lambda_.accumulated_weight) {
|
||||
filter_lambda.accumulated_weight_symbol = symbol_table.at(*edge->filter_lambda_.accumulated_weight);
|
||||
}
|
||||
}
|
||||
{
|
||||
// Bind the inner edge and node symbols so they're available for
|
||||
// inline filtering in ExpandVariable.
|
||||
bool inner_edge_bound = bound_symbols.insert(filter_lambda.inner_edge_symbol).second;
|
||||
bool inner_node_bound = bound_symbols.insert(filter_lambda.inner_node_symbol).second;
|
||||
MG_ASSERT(inner_edge_bound && inner_node_bound, "An inner edge and node can't be bound from before");
|
||||
if (filter_lambda.accumulated_path_symbol) {
|
||||
bool accumulated_path_bound = bound_symbols.insert(*filter_lambda.accumulated_path_symbol).second;
|
||||
MG_ASSERT(accumulated_path_bound, "The accumulated path can't be bound from before");
|
||||
|
||||
if (filter_lambda.accumulated_weight_symbol) {
|
||||
bool accumulated_weight_bound = bound_symbols.insert(*filter_lambda.accumulated_weight_symbol).second;
|
||||
MG_ASSERT(accumulated_weight_bound, "The accumulated weight can't be bound from before");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Join regular filters with lambda filter expression, so that they
|
||||
// are done inline together. Semantic analysis should guarantee that
|
||||
@ -731,15 +747,34 @@ class RuleBasedPlanner {
|
||||
// filtering (they use the inner symbols. If they were not collected,
|
||||
// we have to remove them manually because no other filter-extraction
|
||||
// will ever bind them again.
|
||||
filters.erase(
|
||||
std::remove_if(filters.begin(), filters.end(),
|
||||
[e = filter_lambda.inner_edge_symbol, n = filter_lambda.inner_node_symbol](FilterInfo &fi) {
|
||||
return utils::Contains(fi.used_symbols, e) || utils::Contains(fi.used_symbols, n);
|
||||
}),
|
||||
filters.end());
|
||||
std::vector<Symbol> inner_symbols = {filter_lambda.inner_edge_symbol, filter_lambda.inner_node_symbol};
|
||||
if (filter_lambda.accumulated_path_symbol) {
|
||||
inner_symbols.emplace_back(*filter_lambda.accumulated_path_symbol);
|
||||
|
||||
if (filter_lambda.accumulated_weight_symbol) {
|
||||
inner_symbols.emplace_back(*filter_lambda.accumulated_weight_symbol);
|
||||
}
|
||||
}
|
||||
|
||||
filters.erase(std::remove_if(filters.begin(), filters.end(),
|
||||
[&inner_symbols](FilterInfo &fi) {
|
||||
for (const auto &symbol : inner_symbols) {
|
||||
if (utils::Contains(fi.used_symbols, symbol)) return true;
|
||||
}
|
||||
return false;
|
||||
}),
|
||||
filters.end());
|
||||
|
||||
// Unbind the temporarily bound inner symbols for filtering.
|
||||
bound_symbols.erase(filter_lambda.inner_edge_symbol);
|
||||
bound_symbols.erase(filter_lambda.inner_node_symbol);
|
||||
if (filter_lambda.accumulated_path_symbol) {
|
||||
bound_symbols.erase(*filter_lambda.accumulated_path_symbol);
|
||||
|
||||
if (filter_lambda.accumulated_weight_symbol) {
|
||||
bound_symbols.erase(*filter_lambda.accumulated_weight_symbol);
|
||||
}
|
||||
}
|
||||
|
||||
if (total_weight) {
|
||||
bound_symbols.insert(*total_weight);
|
||||
|
@ -72,8 +72,9 @@ void AddNextExpansions(const Symbol &node_symbol, const Matching &matching, cons
|
||||
// We are not expanding from node1, so flip the expansion.
|
||||
DMG_ASSERT(expansion.node2 && symbol_table.at(*expansion.node2->identifier_) == node_symbol,
|
||||
"Expected node_symbol to be bound in node2");
|
||||
if (expansion.edge->type_ != EdgeAtom::Type::BREADTH_FIRST) {
|
||||
if (expansion.edge->type_ != EdgeAtom::Type::BREADTH_FIRST && !expansion.edge->filter_lambda_.accumulated_path) {
|
||||
// BFS must *not* be flipped. Doing that changes the BFS results.
|
||||
// When filter lambda uses accumulated path, path must not be flipped.
|
||||
std::swap(expansion.node1, expansion.node2);
|
||||
expansion.is_flipped = true;
|
||||
if (expansion.direction != EdgeAtom::Direction::BOTH) {
|
||||
|
@ -699,3 +699,75 @@ Feature: Match
|
||||
Then the result should be
|
||||
| date(n.time) |
|
||||
| 2021-10-05 |
|
||||
|
||||
Scenario: Variable expand with filter by size of accumulated path
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:Person {id: 1})-[:KNOWS]->(:Person {id: 2})-[:KNOWS]->(:Person {id: 3})-[:KNOWS]->(:Person {id: 4});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path = (:Person {id: 1})-[* (e, n, p | size(p) < 4)]->(:Person {id: 4}) RETURN path
|
||||
"""
|
||||
Then the result should be
|
||||
| path |
|
||||
| <(:Person{id:1})-[:KNOWS]->(:Person{id:2})-[:KNOWS]->(:Person{id:3})-[:KNOWS]->(:Person{id:4})> |
|
||||
|
||||
Scenario: Variable expand with filter by last edge type of accumulated path
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:Person {id: 1})-[:KNOWS]->(:Person {id: 2})-[:KNOWS]->(:Person {id: 3})-[:KNOWS]->(:Person {id: 4});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path = (:Person {id: 1})-[* (e, n, p | type(relationships(p)[-1]) = 'KNOWS')]->(:Person {id: 4}) RETURN path
|
||||
"""
|
||||
Then the result should be
|
||||
| path |
|
||||
| <(:Person{id:1})-[:KNOWS]->(:Person{id:2})-[:KNOWS]->(:Person{id:3})-[:KNOWS]->(:Person{id:4})> |
|
||||
|
||||
Scenario: Variable expand with too restricted filter by size of accumulated path
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:Person {id: 1})-[:KNOWS]->(:Person {id: 2})-[:KNOWS]->(:Person {id: 3})-[:KNOWS]->(:Person {id: 4});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path = (:Person {id: 1})-[* (e, n, p | size(p) < 3)]->(:Person {id: 4}) RETURN path
|
||||
"""
|
||||
Then the result should be empty
|
||||
|
||||
Scenario: Variable expand with too restricted filter by last edge type of accumulated path
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:Person {id: 1})-[:KNOWS]->(:Person {id: 2})-[:KNOWS]->(:Person {id: 3})-[:KNOWS]->(:Person {id: 4});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path = (:Person {id: 1})-[* (e, n, p | type(relationships(p)[-1]) = 'Invalid')]->(:Person {id: 4}) RETURN path
|
||||
"""
|
||||
Then the result should be empty
|
||||
|
||||
Scenario: Test DFS variable expand with filter by edge type1
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[* (e, n, p | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1'))]->(:label3) RETURN path;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> |
|
||||
|
||||
Scenario: Test DFS variable expand with filter by edge type2
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[* (e, n, p | NOT(type(e)='type2' AND type(last(relationships(p))) = 'type2'))]->(:label3) RETURN path;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path |
|
||||
| <(:label1 {id: 1})-[:type1 {id: 1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})> |
|
||||
|
@ -203,3 +203,103 @@ Feature: All Shortest Path
|
||||
Then the result should be:
|
||||
| total_cost |
|
||||
| 20.3 |
|
||||
|
||||
Scenario: Test match AllShortest with accumulated path filtered by order of ids
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})-[:type1 {id: 3}]->(:label4 {id: 4});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH pth=(:label1)-[*ALLSHORTEST (r, n | r.id) total_weight (e,n,p | e.id > 0 and (nodes(p)[-1]).id > (nodes(p)[-2]).id)]->(:label4) RETURN pth, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| pth | total_weight |
|
||||
| <(:label1{id:1})-[:type1{id:1}]->(:label2{id:2})-[:type1{id:2}]->(:label3{id:3})-[:type1{id:3}]->(:label4{id:4})> | 6 |
|
||||
|
||||
Scenario: Test match AllShortest with accumulated path filtered by edge type1
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*ALLSHORTEST (r, n | r.id) total_weight (e, n, p | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1'))]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> | 10 |
|
||||
|
||||
Scenario: Test match AllShortest with accumulated path filtered by edge type2
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*ALLSHORTEST (r, n | r.id) total_weight (e, n, p | NOT(type(e)='type2' AND type(last(relationships(p))) = 'type2'))]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type1 {id: 1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})> | 3 |
|
||||
|
||||
Scenario: Test match AllShortest with accumulated path filtered by edge type1 and accumulated weight based on edge
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*ALLSHORTEST (r, n | r.id) total_weight (e, n, p, w | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1') AND w > 0)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> | 10 |
|
||||
|
||||
Scenario: Test match AllShortest with accumulated path filtered by edge type1 and accumulated weight based on edge too restricted
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*ALLSHORTEST (r, n | r.id) total_weight (e, n, p, w | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1') AND w < 10)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be empty
|
||||
|
||||
Scenario: Test match AllShortest with accumulated path filtered by edge type1 and accumulated weight based on vertex is int
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*ALLSHORTEST (r, n | n.id) total_weight (e, n, p, w | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1') AND w > 0)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> | 4 |
|
||||
|
||||
Scenario: Test match allShortest with accumulated path filtered by edge type1 and accumulated weight based on vertex and edge are ints
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*ALLSHORTEST (r, n | n.id + coalesce(r.id, 0)) total_weight (e, n, p, w | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1') AND w > 0)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> | 14 |
|
||||
|
||||
Scenario: Test match AllShortest with accumulated path filtered by edge type1 and accumulated weight based on vertex and edge are doubles
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1.5}]->(:label2 {id: 2})-[:type1 {id: 2.1}]->(:label3 {id: 3})-[:type1 {id: 3.4}]->(:label4 {id: 4});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*ALLSHORTEST (r, n | n.id + coalesce(r.id, 0)) total_weight (e, n, p, w | w > 0)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type1 {id: 1.5}]->(:label2 {id: 2})-[:type1 {id: 2.1}]->(:label3 {id: 3})> | 9.6 |
|
||||
|
||||
Scenario: Test match AllShortest with accumulated path filtered by order of ids and accumulated weight based on both vertex and edge is duration
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:station {name: "A", arrival: localTime("08:00"), departure: localTime("08:15")})-[:ride {id: 1, duration: duration("PT1H5M")}]->(:station {name: "B", arrival: localtime("09:20"), departure: localTime("09:30")})-[:ride {id: 2, duration: duration("PT30M")}]->(:station {name: "C", arrival: localTime("10:00"), departure: localTime("10:20")});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:station {name:"A"})-[*ALLSHORTEST (r, v | v.departure - v.arrival + coalesce(r.duration, duration("PT0M"))) total_weight (r,n,p,w | (nodes(p)[-1]).name > (nodes(p)[-2]).name AND not(w is null))]->(:station {name:"C"}) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:station {arrival: 08:00:00.000000000, departure: 08:15:00.000000000, name: 'A'})-[:ride {duration: PT1H5M, id: 1}]->(:station {arrival: 09:20:00.000000000, departure: 09:30:00.000000000, name: 'B'})-[:ride {duration: PT30M, id: 2}]->(:station {arrival: 10:00:00.000000000, departure: 10:20:00.000000000, name: 'C'})> | PT2H20M |
|
||||
|
@ -121,3 +121,95 @@ Feature: Bfs
|
||||
Then the result should be:
|
||||
| p |
|
||||
| <(:Node {id: 2})-[:LINK {date: '2023-03'}]->(:Node {id: 3})> |
|
||||
|
||||
Scenario: Test BFS variable expand with filter by last edge type of accumulated path
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH pth=(:label1)-[*BFS (e,n,p | type(relationships(p)[-1]) = 'type1')]->(:label3) return pth;
|
||||
"""
|
||||
Then the result should be:
|
||||
| pth |
|
||||
| <(:label1{id:1})-[:type1{id:1}]->(:label2{id:2})-[:type1{id:2}]->(:label3{id:3})> |
|
||||
|
||||
Scenario: Test BFS variable expand with restict filter by last edge type of accumulated path
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH pth=(:label1)-[*BFS (e,n,p | type(relationships(p)[-1]) = 'type2')]->(:label2) return pth;
|
||||
"""
|
||||
Then the result should be empty
|
||||
|
||||
Scenario: Test BFS variable expand with filter by size of accumulated path
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH pth=(:label1)-[*BFS (e,n,p | size(p) < 3)]->(:label3) return pth;
|
||||
"""
|
||||
Then the result should be:
|
||||
| pth |
|
||||
| <(:label1{id:1})-[:type1{id:1}]->(:label2{id:2})-[:type1{id:2}]->(:label3{id:3})> |
|
||||
|
||||
Scenario: Test BFS variable expand with restict filter by size of accumulated path
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH pth=(:label1)-[*BFS (e,n,p | size(p) < 2)]->(:label3) return pth;
|
||||
"""
|
||||
Then the result should be empty
|
||||
|
||||
Scenario: Test BFS variable expand with filter by order of ids in accumulated path when target vertex is indexed
|
||||
Given graph "graph_index"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH pth=(:label1)-[*BFS (e,n,p | (nodes(p)[-1]).id > (nodes(p)[-2]).id)]->(:label4) return pth;
|
||||
"""
|
||||
Then the result should be:
|
||||
| pth |
|
||||
| <(:label1 {id: 1})-[:type1 {id: 1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})-[:type1 {id: 3}]->(:label4 {id: 4})> |
|
||||
|
||||
Scenario: Test BFS variable expand with filter by order of ids in accumulated path when target vertex is NOT indexed
|
||||
Given graph "graph_index"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH pth=(:label1)-[*BFS (e,n,p | (nodes(p)[-1]).id > (nodes(p)[-2]).id)]->(:label3) return pth;
|
||||
"""
|
||||
Then the result should be:
|
||||
| pth |
|
||||
| <(:label1 {id: 1})-[:type1 {id: 1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})> |
|
||||
|
||||
Scenario: Test BFS variable expand with filter by edge type1
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*BFS (e, n, p | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1'))]->(:label3) RETURN path;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> |
|
||||
|
||||
Scenario: Test BFS variable expand with filter by edge type2
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*BFS (e, n, p | NOT(type(e)='type2' AND type(last(relationships(p))) = 'type2'))]->(:label3) RETURN path;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path |
|
||||
| <(:label1 {id: 1})-[:type1 {id: 1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})> |
|
||||
|
@ -155,3 +155,103 @@ Feature: Weighted Shortest Path
|
||||
MATCH (n {a:'0'})-[le *wShortest 10 (e, n | e.w ) w]->(m) RETURN m.a, size(le) as s, w
|
||||
"""
|
||||
Then an error should be raised
|
||||
|
||||
Scenario: Test match wShortest with accumulated path filtered by order of ids
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})-[:type1 {id: 3}]->(:label4 {id: 4});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH pth=(:label1)-[*WSHORTEST (r, n | r.id) total_weight (e,n,p | e.id > 0 and (nodes(p)[-1]).id > (nodes(p)[-2]).id)]->(:label4) RETURN pth, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| pth | total_weight |
|
||||
| <(:label1{id:1})-[:type1{id:1}]->(:label2{id:2})-[:type1{id:2}]->(:label3{id:3})-[:type1{id:3}]->(:label4{id:4})> | 6 |
|
||||
|
||||
Scenario: Test match wShortest with accumulated path filtered by edge type1
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*WSHORTEST (r, n | r.id) total_weight (e, n, p | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1'))]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> | 10 |
|
||||
|
||||
Scenario: Test match wShortest with accumulated path filtered by edge type2
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*WSHORTEST (r, n | r.id) total_weight (e, n, p | NOT(type(e)='type2' AND type(last(relationships(p))) = 'type2'))]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type1 {id: 1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})> | 3 |
|
||||
|
||||
Scenario: Test match wShortest with accumulated path filtered by edge type1 and accumulated weight based on edge
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*WSHORTEST (r, n | r.id) total_weight (e, n, p, w | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1') AND w > 0)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> | 10 |
|
||||
|
||||
Scenario: Test match wShortest with accumulated path filtered by edge type1 and accumulated weight based on edge too restricted
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*WSHORTEST (r, n | r.id) total_weight (e, n, p, w | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1') AND w < 10)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be empty
|
||||
|
||||
Scenario: Test match wShortest with accumulated path filtered by edge type1 and accumulated weight based on vertex is int
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*WSHORTEST (r, n | n.id) total_weight (e, n, p, w | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1') AND w > 0)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> | 4 |
|
||||
|
||||
Scenario: Test match wShortest with accumulated path filtered by edge type1 and accumulated weight based on vertex and edge are ints
|
||||
Given graph "graph_edges"
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*WSHORTEST (r, n | n.id + coalesce(r.id, 0)) total_weight (e, n, p, w | NOT(type(e)='type1' AND type(last(relationships(p))) = 'type1') AND w > 0)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type2 {id: 10}]->(:label3 {id: 3})> | 14 |
|
||||
|
||||
Scenario: Test match wShortest with accumulated path filtered by edge type1 and accumulated weight based on vertex and edge are doubles
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1.5}]->(:label2 {id: 2})-[:type1 {id: 2.1}]->(:label3 {id: 3})-[:type1 {id: 3.4}]->(:label4 {id: 4});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:label1)-[*WSHORTEST (r, n | n.id + coalesce(r.id, 0)) total_weight (e, n, p, w | w > 0)]->(:label3) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:label1 {id: 1})-[:type1 {id: 1.5}]->(:label2 {id: 2})-[:type1 {id: 2.1}]->(:label3 {id: 3})> | 9.6 |
|
||||
|
||||
Scenario: Test match wShortest with accumulated path filtered by order of ids and accumulated weight based on both vertex and edge is duration
|
||||
Given an empty graph
|
||||
And having executed:
|
||||
"""
|
||||
CREATE (:station {name: "A", arrival: localTime("08:00"), departure: localTime("08:15")})-[:ride {id: 1, duration: duration("PT1H5M")}]->(:station {name: "B", arrival: localtime("09:20"), departure: localTime("09:30")})-[:ride {id: 2, duration: duration("PT30M")}]->(:station {name: "C", arrival: localTime("10:00"), departure: localTime("10:20")});
|
||||
"""
|
||||
When executing query:
|
||||
"""
|
||||
MATCH path=(:station {name:"A"})-[*WSHORTEST (r, v | v.departure - v.arrival + coalesce(r.duration, duration("PT0M"))) total_weight (r,n,p,w | (nodes(p)[-1]).name > (nodes(p)[-2]).name AND not(w is null))]->(:station {name:"C"}) RETURN path, total_weight;
|
||||
"""
|
||||
Then the result should be:
|
||||
| path | total_weight |
|
||||
| <(:station {arrival: 08:00:00.000000000, departure: 08:15:00.000000000, name: 'A'})-[:ride {duration: PT1H5M, id: 1}]->(:station {arrival: 09:20:00.000000000, departure: 09:30:00.000000000, name: 'B'})-[:ride {duration: PT30M, id: 2}]->(:station {arrival: 10:00:00.000000000, departure: 10:20:00.000000000, name: 'C'})> | PT2H20M |
|
||||
|
@ -0,0 +1,2 @@
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})-[:type1 {id: 3}]->(:label4 {id: 4});
|
||||
MATCH (n :label1), (m :label3) CREATE (n)-[:type2 {id: 10}]->(m);
|
@ -0,0 +1,2 @@
|
||||
CREATE INDEX ON :label4;
|
||||
CREATE (:label1 {id: 1})-[:type1 {id:1}]->(:label2 {id: 2})-[:type1 {id: 2}]->(:label3 {id: 3})-[:type1 {id:3}]->(:label4 {id: 4});
|
@ -1925,6 +1925,41 @@ TEST_P(CypherMainVisitorTest, MatchBfsReturn) {
|
||||
ASSERT_TRUE(eq);
|
||||
}
|
||||
|
||||
TEST_P(CypherMainVisitorTest, MatchBfsFilterByPathReturn) {
|
||||
auto &ast_generator = *GetParam();
|
||||
{
|
||||
const auto *query = dynamic_cast<CypherQuery *>(
|
||||
ast_generator.ParseQuery("MATCH pth=(r:type1 {id: 1})<-[*BFS ..10 (e, n, p | startNode(relationships(e)[-1]) = "
|
||||
"c:type2)]->(:type3 {id: 3}) RETURN pth;"));
|
||||
ASSERT_TRUE(query);
|
||||
ASSERT_TRUE(query->single_query_);
|
||||
const auto *match = dynamic_cast<Match *>(query->single_query_->clauses_[0]);
|
||||
ASSERT_TRUE(match);
|
||||
ASSERT_EQ(match->patterns_.size(), 1U);
|
||||
ASSERT_EQ(match->patterns_[0]->atoms_.size(), 3U);
|
||||
auto *bfs = dynamic_cast<EdgeAtom *>(match->patterns_[0]->atoms_[1]);
|
||||
ASSERT_TRUE(bfs);
|
||||
EXPECT_TRUE(bfs->IsVariable());
|
||||
EXPECT_EQ(bfs->filter_lambda_.inner_edge->name_, "e");
|
||||
EXPECT_TRUE(bfs->filter_lambda_.inner_edge->user_declared_);
|
||||
EXPECT_EQ(bfs->filter_lambda_.inner_node->name_, "n");
|
||||
EXPECT_TRUE(bfs->filter_lambda_.inner_node->user_declared_);
|
||||
EXPECT_EQ(bfs->filter_lambda_.accumulated_path->name_, "p");
|
||||
EXPECT_TRUE(bfs->filter_lambda_.accumulated_path->user_declared_);
|
||||
EXPECT_EQ(bfs->filter_lambda_.accumulated_weight, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(CypherMainVisitorTest, SemanticExceptionOnBfsFilterByWeight) {
|
||||
auto &ast_generator = *GetParam();
|
||||
{
|
||||
ASSERT_THROW(ast_generator.ParseQuery(
|
||||
"MATCH pth=(:type1 {id: 1})<-[*BFS ..10 (e, n, p, w | startNode(relationships(e)[-1] AND w > 0) = "
|
||||
"c:type2)]->(:type3 {id: 3}) RETURN pth;"),
|
||||
SemanticException);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(CypherMainVisitorTest, MatchVariableLambdaSymbols) {
|
||||
auto &ast_generator = *GetParam();
|
||||
auto *query = dynamic_cast<CypherQuery *>(ast_generator.ParseQuery("MATCH () -[*]- () RETURN *"));
|
||||
@ -1981,6 +2016,57 @@ TEST_P(CypherMainVisitorTest, MatchWShortestReturn) {
|
||||
EXPECT_TRUE(shortest->total_weight_->user_declared_);
|
||||
}
|
||||
|
||||
TEST_P(CypherMainVisitorTest, MatchWShortestFilterByPathReturn) {
|
||||
auto &ast_generator = *GetParam();
|
||||
{
|
||||
const auto *query = dynamic_cast<CypherQuery *>(
|
||||
ast_generator.ParseQuery("MATCH pth=()-[r:type1 *wShortest 10 (we, wn | 42) total_weight "
|
||||
"(e, n, p | startNode(relationships(e)[-1]) = c:type3)]->(:type2) RETURN pth"));
|
||||
ASSERT_TRUE(query);
|
||||
ASSERT_TRUE(query->single_query_);
|
||||
const auto *match = dynamic_cast<Match *>(query->single_query_->clauses_[0]);
|
||||
ASSERT_TRUE(match);
|
||||
ASSERT_EQ(match->patterns_.size(), 1U);
|
||||
ASSERT_EQ(match->patterns_[0]->atoms_.size(), 3U);
|
||||
auto *shortestPath = dynamic_cast<EdgeAtom *>(match->patterns_[0]->atoms_[1]);
|
||||
ASSERT_TRUE(shortestPath);
|
||||
EXPECT_TRUE(shortestPath->IsVariable());
|
||||
EXPECT_EQ(shortestPath->filter_lambda_.inner_edge->name_, "e");
|
||||
EXPECT_TRUE(shortestPath->filter_lambda_.inner_edge->user_declared_);
|
||||
EXPECT_EQ(shortestPath->filter_lambda_.inner_node->name_, "n");
|
||||
EXPECT_TRUE(shortestPath->filter_lambda_.inner_node->user_declared_);
|
||||
EXPECT_EQ(shortestPath->filter_lambda_.accumulated_path->name_, "p");
|
||||
EXPECT_TRUE(shortestPath->filter_lambda_.accumulated_path->user_declared_);
|
||||
EXPECT_EQ(shortestPath->filter_lambda_.accumulated_weight, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(CypherMainVisitorTest, MatchWShortestFilterByPathWeightReturn) {
|
||||
auto &ast_generator = *GetParam();
|
||||
{
|
||||
const auto *query = dynamic_cast<CypherQuery *>(ast_generator.ParseQuery(
|
||||
"MATCH pth=()-[r:type1 *wShortest 10 (we, wn | 42) total_weight "
|
||||
"(e, n, p, w | startNode(relationships(e)[-1]) = c:type3 AND w < 50)]->(:type2) RETURN pth"));
|
||||
ASSERT_TRUE(query);
|
||||
ASSERT_TRUE(query->single_query_);
|
||||
const auto *match = dynamic_cast<Match *>(query->single_query_->clauses_[0]);
|
||||
ASSERT_TRUE(match);
|
||||
ASSERT_EQ(match->patterns_.size(), 1U);
|
||||
ASSERT_EQ(match->patterns_[0]->atoms_.size(), 3U);
|
||||
auto *shortestPath = dynamic_cast<EdgeAtom *>(match->patterns_[0]->atoms_[1]);
|
||||
ASSERT_TRUE(shortestPath);
|
||||
EXPECT_TRUE(shortestPath->IsVariable());
|
||||
EXPECT_EQ(shortestPath->filter_lambda_.inner_edge->name_, "e");
|
||||
EXPECT_TRUE(shortestPath->filter_lambda_.inner_edge->user_declared_);
|
||||
EXPECT_EQ(shortestPath->filter_lambda_.inner_node->name_, "n");
|
||||
EXPECT_TRUE(shortestPath->filter_lambda_.inner_node->user_declared_);
|
||||
EXPECT_EQ(shortestPath->filter_lambda_.accumulated_path->name_, "p");
|
||||
EXPECT_TRUE(shortestPath->filter_lambda_.accumulated_path->user_declared_);
|
||||
EXPECT_EQ(shortestPath->filter_lambda_.accumulated_weight->name_, "w");
|
||||
EXPECT_TRUE(shortestPath->filter_lambda_.accumulated_weight->user_declared_);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(CypherMainVisitorTest, MatchWShortestNoFilterReturn) {
|
||||
auto &ast_generator = *GetParam();
|
||||
auto *query =
|
||||
|
Loading…
Reference in New Issue
Block a user