Skip to content

Commit

Permalink
collect
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-ricciardi committed Jun 6, 2024
1 parent 6462af4 commit 9a1f2c2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
4 changes: 2 additions & 2 deletions integration/cypher-tests/tests/with_collect_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn _test_with_collect(mut client: Client) {
create (new)-[:IsFriendOf]->(new2:Person {weight: $weight})
return s", p).await;
let res = client.execute_cypher_request("match (s:Person)-[:IsFriendOf]->(new:Person)-[:IsFriendOf]->(end:Person)
return collect(end)
").await;
with collect(end) as list
return list").await;
println!("{}", res.expect("persons").to_string());
}
1 change: 1 addition & 0 deletions lib/zawgl-cypher-query-model/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl RelationshipResult {
RelationshipResult{name, value, source_nid, target_nid}
}
}

#[derive(Debug, Clone)]
pub enum EvalResultItem {
Node(NodeResult),
Expand Down
90 changes: 57 additions & 33 deletions lib/zawgl-front/src/planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ fn handle_eval(results: &mut Vec::<Vec<PropertyGraph>>, eval_scope: EvalScopeCla
for graph in &matched_graphs {
build_items_combinations(grouping.iter(), &graph, &mut combinations, &mut curr_items)?;
}

let mut eval_result_scope = vec![];

let mut aggregations = HashMap::new();
Expand Down Expand Up @@ -170,21 +169,14 @@ fn handle_eval(results: &mut Vec::<Vec<PropertyGraph>>, eval_scope: EvalScopeCla
}
},
Item::Relationship(rel) => {
if let Some(var) = rel.relationship.get_var() {
if let (Some(var), Some(graph)) = (rel.relationship.get_var(), combination.graph) {
if var == named_item {
row.push(EvalResultItem::Relationship(make_relationship(ret_item.alias.as_ref(), &named_item, rel, combination.graph)?));
row.push(EvalResultItem::Relationship(make_relationship(ret_item.alias.as_ref(), &named_item, rel, graph)?));
}
}
}
}
}
for eval_row in eval_results {
for eval_item in eval_row {
if eval_item.get_name() == named_item {
row.push(eval_item.clone());
}
}
}
}
}
},
Expand All @@ -193,7 +185,7 @@ fn handle_eval(results: &mut Vec::<Vec<PropertyGraph>>, eval_scope: EvalScopeCla
}
}

let graphs = combinations.iter().map(|c| c.graph).collect::<Vec<&PropertyGraph>>();
let graphs = combinations.iter().map(|c| c.graph).collect::<Vec<Option<&PropertyGraph>>>();
for ret_exp in &eval_scope.expressions {
match ret_exp {
EvalScopeExpression::FunctionCall(fun) => {
Expand All @@ -218,7 +210,35 @@ fn handle_eval(results: &mut Vec::<Vec<PropertyGraph>>, eval_scope: EvalScopeCla
}
eval_result_scope.push(row);
}
Ok((matched_graphs, eval_result_scope))

let mut eval_result_scope_join = vec![];

for eval_item_row in eval_results {
let mut row = vec![];
if eval_result_scope.is_empty() {
for eval_item in eval_item_row {
if grouping.contains(&&eval_item.get_name().to_string()) {
row.push(eval_item.clone());
}
}
} else {
for eval_row in &mut eval_result_scope {
for eval_item in eval_item_row {
if grouping.contains(&&eval_item.get_name().to_string()) {
row.push(eval_item.clone());
row.append(eval_row);
}
}
}
}
eval_result_scope_join.push(row);
}

if eval_results.is_empty() {
Ok((matched_graphs, eval_result_scope))
} else {
Ok((matched_graphs, eval_result_scope_join))
}
}

fn handle_match(results: &Vec::<Vec<PropertyGraph>>, graph_engine: &mut GraphEngine, step: &QueryStep, eval_row: &Vec<EvalResultItem>) -> Vec::<Vec<PropertyGraph>> {
Expand Down Expand Up @@ -320,10 +340,12 @@ fn get_properties<'a: 'b, 'b>(graph: &'a PropertyGraph, group: &'b mut Vec::<&'a
}
}

fn compute_sum(args: &Vec<ValueItem>, graphs: &Vec<&PropertyGraph>) -> f64 {
fn compute_sum(args: &Vec<ValueItem>, graphs: &Vec<Option<&PropertyGraph>>) -> f64 {
let mut group = Vec::<&PropertyValue>::new();
for graph in graphs {
get_properties(graph, &mut group, args);
for ograph in graphs {
if let Some(graph) = ograph {
get_properties(graph, &mut group, args);
}
}

let mut sum_value = 0.;
Expand All @@ -333,30 +355,32 @@ fn compute_sum(args: &Vec<ValueItem>, graphs: &Vec<&PropertyGraph>) -> f64 {
sum_value
}

fn build_item_list(args: &Vec<ValueItem>, graphs: &Vec<&PropertyGraph>) -> Result<Vec<EvalResultItem>, CypherError> {
fn build_item_list(args: &Vec<ValueItem>, graphs: &Vec<Option<&PropertyGraph>>) -> Result<Vec<EvalResultItem>, CypherError> {
let mut list = Vec::new();
for graph in graphs {
for node in graph.get_nodes() {
if let Some(var) = node.get_var() {
for arg in args {
if let ValueItem::NamedItem(name) = arg {
if name == var {
list.push(EvalResultItem::Node(make_node(None, &name, node)));
for ograph in graphs {
if let Some(graph) = ograph {
for node in graph.get_nodes() {
if let Some(var) = node.get_var() {
for arg in args {
if let ValueItem::NamedItem(name) = arg {
if name == var {
list.push(EvalResultItem::Node(make_node(None, &name, node)));
}
}
}
}
}
}
for rel in graph.get_edges() {
if let Some(var) = rel.relationship.get_var() {
for arg in args {
if let ValueItem::NamedItem(name) = arg {
if name == var {
list.push(EvalResultItem::Relationship(make_relationship(None, &name, rel, graph)?));
for rel in graph.get_edges() {
if let Some(var) = rel.relationship.get_var() {
for arg in args {
if let ValueItem::NamedItem(name) = arg {
if name == var {
list.push(EvalResultItem::Relationship(make_relationship(None, &name, rel, graph)?));
}
}
}

}

}
}
}
Expand Down Expand Up @@ -409,7 +433,7 @@ enum ItemId {
}

struct Combination<'a> {
graph: &'a PropertyGraph,
graph: Option<&'a PropertyGraph>,
items: Vec<Item<'a>>,
}

Expand Down Expand Up @@ -483,7 +507,7 @@ fn build_items_combinations<'a: 'b, 'b>(mut grouping: Iter<&String>, graph: &'a
build_items_combinations(grouping.clone(), graph, combinations, curr_items)?;
}
} else {
combinations.push(Combination { graph: graph, items: curr_items.to_vec() });
combinations.push(Combination { graph: Some(graph), items: curr_items.to_vec() });
curr_items.clear();
}
Ok(())
Expand Down

0 comments on commit 9a1f2c2

Please sign in to comment.