Hi! I’m trying to create a query to fetch the nested list of employees in the org hierarchy under a given employee. Below is the query:
CREATE DISTRIBUTED QUERY Fetch_Reportees_Hierarchy(VERTEX<EMPLOYEE> emplyee) FOR GRAPH employee_manager_hierarchy {
TYPEDEF TUPLE <VERTEX<EMPLOYEE> employee> employee_node;
MapAccum<STRING, employee_node> @@reportee_hierarchy;
BOOL isLeafNode = false;
mgr (EMPLOYEE) = {emplyee};
WHILE isLeafNode == false DO
SetAccum<VERTEX<EMPLOYEE>> @employees = (mgr);
IF mgr.@employees.size() > 0 THEN
FOREACH m in mgr.@employees DO
mgr (ANY) = {m};
COMP = SELECT rep FROM EMPLOYEE:rep - (REPORTS_TO>:e) - mgr
ACCUM @@reportee_hierarchy += (rep.managerLevel -> employee_node(rep))
POST-ACCUM
rep.@employee += rep.employeeId;
END;
ELSE
isLeafNode = true;
END;
However, facing an error saying
(14, 12) Error: no type can be inferred for rep.@employees += rep.employeeId
Here, ‘employeeId’ is the primary ID for the vertex, ‘EMPLOYEE’. And the directed edge ‘REPORTS_TO’ signifies the reporting relationship between employees.
Please help on resolving this issue
Hi - it appears to me that you have answered your own question. employeeId is the primary ID for the vertex, and is therefore an attribute of a vertex, not a vertex. But you seem to be trying to put an attribute into a set of vertexes. Also, there are typos in your example, since you refer to an accumulator of @employee and @employees - there are better examples of queries that handle nested hierarchies in the TigerGraph sample queries
2 Likes
Got this working! Here is the updated query:
CREATE DISTRIBUTED QUERY Fetch_Reportees_Hierarchy(VERTEX<EMPLOYEE> emplyee) FOR GRAPH employee_manager_hierarchy {
TYPEDEF TUPLE <VERTEX<EMPLOYEE> employee> employee_node;
TYPEDEF TUPLE <EDGE<REPORTS_TO> REPORTS_TO> reports_to_edge;
SetAccum<VERTEX<EMPLOYEE>> @@reportee_hierarchy;
SetAccum<VERTEX<EMPLOYEE>> @@temp_reportee_hierarchy;
SetAccum<reports_to_edge> @@reports_to_edges;
SetAccum<reports_to_edge> @@temp_reports_to_edges;
seed_set = {emplyee};
BOOL isLeafNode = false;
WHILE isLeafNode == false DO
comp = SELECT reportee FROM EMPLOYEE:reportee -(REPORTS_TO>:e)- seed_set:s
ACCUM @@temp_reportee_hierarchy += reportee,
@@temp_reports_to_edges += reports_to_edge(e);
IF @@temp_reportee_hierarchy.size() == 0 THEN
isLeafNode = true;
ELSE
seed_set = @@temp_reportee_hierarchy;
@@reportee_hierarchy += @@temp_reportee_hierarchy;
@@reports_to_edges += @@temp_reports_to_edges;
@@temp_reportee_hierarchy.clear();
@@temp_reports_to_edges.clear();
END;
END;
PRINT @@reportee_hierarchy;
PRINT @@reports_to_edges;
}
}
Please let me know if this can be improved in any way
Looks good - you could remove the TYPEDEF for employee_node since it appears that you are not using it