I'm working with hierarchical tree data in DolphinDB and need to solve two specific programming challenges:
- Generate full paths (concatenated IDs and names) for all nodes, including infinite-level leaf nodes.
- Detect cyclic references (e.g., A → B → A).
In MySQL, I've successfully achieved this using recursive CTEs:
with recursive at as(
SELECT id, name,json_array(id) as path_id,json_array(name) as path_name,false as cycle
FROM table1
where pid is null and dir_group = 4
union ALL
SELECT t1.id, t1.name,JSON_MERGE_PRESERVE(at.path_id,json_array(t1.id)) as path_id ,
JSON_MERGE_PRESERVE(at.path_name,json_array(t1.name)) as path_name ,
json_contains(at.path_id,json_array(t1.id)) as cycle
FROM table1 t1 join at on t1.pid=at.id
where t1.pid is not null and not at.cycle)
select * from at
But I'm struggling to find an equivalent approach in DolphinDB. I've tried an iterative approach using loops and temporary tables:
// Sample table structure (adjust columns as needed)
nodes = table(1:0, `id`name`pid, [INT,STRING,INT])
// Initialize result table
result = table(1:0, `id`name`path`isCycle, [INT,STRING,STRING,BOOL])
// Initialize stack (using table)
stack = select id, name, string(id) as path from nodes where pid = NULL
while(size(stack) > 0) {
// Pop last entry (stack behavior)
current = select * from stack limit -1
stack = select * from stack limit size(stack)-1
// Detect cycles
pathParts = split(current.path, ",")
isCycle = sum(pathParts == string(current.id)) > 1
// Store result
result.append!(select id, name, path, isCycle from current)
// Push children (reverse order for DFS)
children = lj(nodes, current, `pid == `id)
children = select * from children order by id desc // Reverse for DFS
stack.append!(select id, name, path + "," + string(id) from children)
}
The iterative approach works for small datasets but fails with deep hierarchies due to performance issues.
Also, I've explored built-in functions like ploop, each, and accumulate, but they don't natively support recursive state passing.
Are there recommended patterns in DolphinDB for recursive hierarchical queries? Or, does DolphinDB have any built-in functions specifically designed for hierarchical data traversal?
My Enviroment:
- DolphinDB version: 3.00.2
- Dataset size: About 1,000,000 rows