<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating a hierarchy without recursive statements in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/110510#M9848</link>
    <description>&lt;P&gt;Unfortunately I get the same result as the other methods. It only returns the first level and nothing beyond that. It is probably worth mentioning that I am using Delta Live Tables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Feb 2025 15:43:13 GMT</pubDate>
    <dc:creator>bmhardy</dc:creator>
    <dc:date>2025-02-18T15:43:13Z</dc:date>
    <item>
      <title>Creating a hierarchy without recursive statements</title>
      <link>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/106548#M9844</link>
      <description>&lt;P&gt;I am looking to build a hierarchy from a parent child relationship table, which I would typically use a recursive statement for in SQL Server / Azure SQL. This would mean setting an anchor, most commonly the top record of the tree, and then join back to itself as it recurses through the relationships to build the hierarchichal structure. The problem I have encountered with Databricks is that it doesn't support ANY types of recursive statement, including CTE's and while loops.&lt;/P&gt;&lt;P&gt;The table structure is:&lt;/P&gt;&lt;P&gt;RowId | ParentId | ChildId&lt;/P&gt;&lt;P&gt;The parent Id references the row Id of the parent record. I have tried using SQL and also tried with PySpark yet I am out of ideas. This is a really important part of using Databricks as we need to return near real-time aggregate data. Any suggestions would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2025 19:38:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/106548#M9844</guid>
      <dc:creator>bmhardy</dc:creator>
      <dc:date>2025-01-21T19:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a hierarchy without recursive statements</title>
      <link>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/108280#M9845</link>
      <description>&lt;P&gt;Use a loop to iteratively join the DataFrame with itself to build the hierarchy. Each iteration will add a new level to the hierarchy.&lt;/P&gt;
&lt;P&gt;We also have CTE support&amp;nbsp;&lt;A href="https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-qry-select-cte.html" target="_blank"&gt;https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-qry-select-cte.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Feb 2025 08:00:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/108280#M9845</guid>
      <dc:creator>NandiniN</dc:creator>
      <dc:date>2025-02-01T08:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a hierarchy without recursive statements</title>
      <link>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/108314#M9846</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/132117"&gt;@bmhardy&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;As&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/23233"&gt;@NandiniN&lt;/a&gt;&amp;nbsp;suggested you can use iterative approach to simulate recursion. I think in the databricks roadmap there's a plan to support recursive cte.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sample_data = [
    (1, None, 2),
    (2, 1, 3),
    (3, 2, 4),
    (4, 3, None)
]

columns = ["RowId", "ParentId", "ChildId"]
df = spark.createDataFrame(sample_data, columns)


df.createOrReplaceTempView("HierarchyTable")

# Initialize parameters
i = 1
start_row_id = 1  # The starting RowId (replace with your root node)

# Recursive anchor
df = spark.sql(f"""
SELECT RowId
     , ParentId
     , ChildId
     , 0 AS RecursionLevel
FROM HierarchyTable
WHERE RowId = {start_row_id}
""")

# Register the initial result as a temporary view for recursion
df.createOrReplaceTempView('recursion_df')

# Use loop to simulate recursion
while True:
   
    bill_df = spark.sql(f"""
    SELECT t.RowId
         , t.ParentId
         , t.ChildId
         , {i} AS RecursionLevel
    FROM recursion_df cte
    INNER JOIN HierarchyTable t ON t.ParentId = cte.RowId
    """)

   
    bill_df.createOrReplaceTempView('recursion_df')

   
    df = df.union(bill_df)

    # Check if the recursion has produced any results
    if bill_df.count() == 0:
        df.createOrReplaceTempView("final_df")
        break
    else:
        i += 1  # Increment recursion level


display(df)&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 01 Feb 2025 20:02:08 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/108314#M9846</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2025-02-01T20:02:08Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a hierarchy without recursive statements</title>
      <link>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/108578#M9847</link>
      <description>&lt;P&gt;Thank you, I will give this a try. I'll let you know how it goes.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2025 13:00:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/108578#M9847</guid>
      <dc:creator>bmhardy</dc:creator>
      <dc:date>2025-02-03T13:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a hierarchy without recursive statements</title>
      <link>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/110510#M9848</link>
      <description>&lt;P&gt;Unfortunately I get the same result as the other methods. It only returns the first level and nothing beyond that. It is probably worth mentioning that I am using Delta Live Tables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Feb 2025 15:43:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/creating-a-hierarchy-without-recursive-statements/m-p/110510#M9848</guid>
      <dc:creator>bmhardy</dc:creator>
      <dc:date>2025-02-18T15:43:13Z</dc:date>
    </item>
  </channel>
</rss>

