Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tree Structure and Recursion

Using a PostgreSQL 8.4.14 database, I have a table representing a tree structure like the following example:

CREATE TABLE unit (
    id bigint NOT NULL PRIMARY KEY,
    name varchar(64) NOT NULL,
    parent_id bigint,
    FOREIGN KEY (parent_id) REFERENCES unit (id)
);
INSERT INTO unit VALUES (1, 'parent', NULL), (2, 'child', 1)
                      , (3, 'grandchild A', 2), (4, 'grandchild B', 2);
 id |    name      | parent_id 
----+--------------+-----------
  1 | parent       |          
  2 | child        |         1
  3 | grandchild A |         2
  4 | grandchild B |         2

I want to create an Access Control List for those units, where each unit may have it's own ACL, or is inheriting it from the nearest ancestor with an own ACL.

CREATE TABLE acl (
    unit_id bigint NOT NULL PRIMARY KEY,
    FOREIGN KEY (unit_id) REFERENCES unit (id)
);
INSERT INTO acl VALUES (1), (4);
 unit_id 
---------
       1
       4

I'm using a view to determine if a unit is inheriting it's ACL from an ancestor:

CREATE VIEW inheriting_acl AS
    SELECT u.id AS unit_id, COUNT(a.*) = 0 AS inheriting
    FROM unit AS u
    LEFT JOIN acl AS a ON a.unit_id = u.id
    GROUP BY u.id;
 unit_id | inheriting 
---------+------------
       1 | f
       2 | t
       3 | t
       4 | f

My question is: how can I get the nearest unit which is NOT inheriting the ACL from an ancestor? My expected result should look similar to the following table/view:

 unit_id | acl 
---------+------------
       1 | 1
       2 | 1
       3 | 1
       4 | 4
like image 216
jabu.10245 Avatar asked Nov 01 '12 19:11

jabu.10245


People also ask

What is tree recursion in data structure?

Recursion Tree Method is a pictorial representation of an iteration method which is in the form of a tree where at each level nodes are expanded. 2. In general, we consider the second term in recurrence as root.

How tree is recursive?

Abstract. Recursion tree method is used to solve recurrence relations. Generally, these recurrence relations follow the divide and conquer approach to solve a problem, for example T(n) = T(n-1) + T(n-2) + k, is a recurrence relation as problem size 'n' is dividing into problems of size n-1 and n-2.

What is the example of tree recursion?

Thinking recursively, you might say that you would print out the tree to the left of the root, print out the root, and then print out the tree to the right of the root. That's all there is to it.

What is a recursion structure?

Recursive structure is a simple idea (or shorthand abstraction) with surprising applications beyond science. A structure is recursive if the shape of the whole recurs in the shape of the parts: for example, a circle formed of welded links that are circles themselves.


1 Answers

A query with a recursive CTE could do the job. Requires PostgreSQL 8.4 or later:

WITH RECURSIVE next_in_line AS (
    SELECT u.id AS unit_id, u.parent_id, a.unit_id AS acl
    FROM   unit u
    LEFT   JOIN acl a ON a.unit_id = u.id

    UNION  ALL
    SELECT n.unit_id, u.parent_id, a.unit_id
    FROM   next_in_line n
    JOIN   unit u ON u.id = n.parent_id AND n.acl IS NULL
    LEFT   JOIN acl a ON a.unit_id = u.id
    )
SELECT unit_id, acl
FROM   next_in_line
WHERE  acl IS NOT NULL
ORDER  BY unit_id

The break condition in the second leg of the UNION is n.acl IS NULL. With that, the query stops traversing the the tree as soon as an acl is found.
In the final SELECT we only return the rows where an acl was found. Voilá.

As an aside: It is an anti-pattern to use the generic, non-descriptive id as column name. Sadly, some ORMs do that by default. Call it unit_id and you don't have to use aliases in queries all the time.

like image 59
Erwin Brandstetter Avatar answered Oct 12 '22 00:10

Erwin Brandstetter