I want to write a query that groups rows by having overlapping arrays. Consider the following example:
id | name | family_ids
--------------------------
1 | Alice | [f1, f2]
2 | Bob | [f1]
3 | Freddy | [f2, f3]
4 | James | [f3]
5 | Joe | [f4, f5]
6 | Tim | [f5]
Alice and Bob are part of the same family, f1. In Alice's family in law (f2), she's also related to Freddy. And considering Freddy's family in law (f3), James is also related to them.
So, basically, I want to group by the arrays in family_ids having any overlap at all. But, notice that f2 -> f3 should also be discovered, which is not possible with 1 simple group by query.
ids | family_ids
----------------------------
[1, 2, 3, 4] | [f1, f2, f3]
[5, 6] | [f4, f5]
I've been playing around a lot with inner joins, group by t1.family_ids && t2.family_ids but can't seem to find a good performing solution. The table right now has a size of ~100k rows. In the future, this table will grow towards ~500k-1M rows.