'Table Lineage with Recursive CTE using view definition in information schema

I have the problem that in our Snowflake DWH views were built on of top views (& again on top of views). If am now trying to change the underlying table the first view is derived from, I am afraid to break things.

Heres an example:

We have a table with REGISTRATIONS for several countries. Based on that table a view is created only showing data from US (REGISTRATIONS_US). This view is being used to built a view containing US data from lets say 2021 (REGISTRATIONS_US_2021). This view is again being used to build a view containing data from US in 2021 for a specific month, lets say July (REGISTRATIONS_US_202106). It can also happen that a view is used to generate more than one view (see for example REGISTRATIONS_US_NYC in the table provided below).

If I would now change the registrations table all the related views are affected. Therefore I need to find out how the views are related.

The only information I have is the one available in INFORMATION_SCHEMA:

TABLE_NAME VIEW_DEFINITION
REGISTRATIONS_US CREATE VIEW REGISTRATIONS_US AS
SELECT * FROM REGISTRATIONS WHERE market = 'US'
REGISTRATIONS_US_2021 CREATE VIEW REGISTRATIONS_US_2021 AS
SELECT * FROM REGISTRATIONS_US WHERE year = 2021
REGISTRATIONS_US_202106 CREATE VIEW REGISTRATIONS_US_202106 AS
SELECT * FROM REGISTRATIONS_US_2021 WHERE month_id = '202106'
REGISTRATIONS_US_NYC CREATE VIEW REGISTRATIONS_US_NYC AS
SELECT * FROM REGISTRATIONS_US WHERE city = 'NYC'

I would like to gain the following solution:

LINEAGE
REGISTRATIONS_US > REGISTRATIONS_US_NYC
REGISTRATIONS_US > REGISTRATIONS_US_2021 > REGISTRATIONS_US_202106

I guess that should be somehow possible using a recursive CTE. But I just cannot imagine how since most examples of recursive CTEs to be found on Google already show the hierarchy in the table based on ids (typical manager/ employee example).

I hope my question is clear & someone can help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source