Query network path from links

My BD contains the current parent of each node. I need to build a flux query to retrieve the path used by a node. For example, in this topology:

4   3
 \ /
  2
  |
  1   5
   \ /
    0

Path of 3 = 3, 2, 1, 0
Path of 5 = 5, 0

My current solution is below.
Is there a more generic solution to this problem?
A solution not limited to a fix number of links.

import "experimental/array"

getParent = (tables=<-, children) => tables
  |> filter(fn: (r) => 
  if length(arr: children) > 0 then
    r["node"] == children[0]
  else
    false)
  |> findColumn(fn: (key) => true, column: "parent")

links = [
  { node: 5, parent: 0},
  { node: 4, parent: 2},
  { node: 3, parent: 2},
  { node: 2, parent: 1},
  { node: 1, parent: 0},
]

parents0 = [4]

parents1 = array.from(rows: links)
  |> getParent(children: parents0)

parents2 = array.from(rows: links)
  |> getParent(children: parents1)

parents3 = array.from(rows: links)
  |> getParent(children: parents2)

parents4 = array.from(rows: links)
  |> getParent(children: parents3)

parents5 = array.from(rows: links)
  |> getParent(children: parents4)

path = parents0
  |> array.concat(v: parents1)
  |> array.concat(v: parents2)
  |> array.concat(v: parents3)
  |> array.concat(v: parents4)
  |> array.concat(v: parents5)

array.from(rows: links)
  |> filter(fn: (r) => contains(value: r.node, set: path))
  |> yield(name: "Path")

Hello @lemikev,
Wow thats really cool!
Unfortunately without looping I don’t think there is. :confused:

This was also my conclusion but with only 2 weeks of flux behind me, it’s nice to get a confirmation.
Thanks for your reply!