<?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: Adding if statements or try/catch block in sql based dlt pipelines in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/adding-if-statements-or-try-catch-block-in-sql-based-dlt/m-p/107596#M42853</link>
    <description>&lt;P&gt;Is there a reason why you can't use Autoloader for this?&amp;nbsp; That would only trigger the pipeline when new files arrive.&lt;/P&gt;</description>
    <pubDate>Wed, 29 Jan 2025 13:56:51 GMT</pubDate>
    <dc:creator>Rjdudley</dc:creator>
    <dc:date>2025-01-29T13:56:51Z</dc:date>
    <item>
      <title>Adding if statements or try/catch block in sql based dlt pipelines</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-if-statements-or-try-catch-block-in-sql-based-dlt/m-p/107563#M42836</link>
      <description>&lt;P&gt;We have complete sql based dlt pipelines. Where bronze tables are read from UC volumes. There can be situations when no new data comes so of the endpoints in the UC volume. in that case the SQL code blocks gets failed which results in failing the entire pipelines. Where ideal case should be of whichever endpoints in the UC volume have new data those SQL blocks should run correctly which those where no new file got added. They should haves some if else or try catch block blocks to handle this scenario. How to achieve it&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is how right now&amp;nbsp; for example two code blocks of my SQL based dlt pipeline looks&lt;/P&gt;&lt;P&gt;code block 1&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;CREATE OR REFRESH MATERIALIZED VIEW jamf_inventory_information
COMMENT "Incremental upload of data incoming from source endpoint 1"
AS
SELECT *
FROM read_files(
  '/Volumes/catalog/${schema_path}/catalog_name-${schema_path}/source=source/*/table_name=endpoint/*/*.json'
);&lt;/LI-CODE&gt;&lt;P&gt;code block 2&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;CREATE OR REFRESH MATERIALIZED VIEW jamf_mobile_devices
COMMENT "Incremental upload of data incoming from Jamf for endpoint 2"

AS
SELECT *
FROM read_files(
  '/Volumes/catalog/${schema_path}/catalog_name-${schema_path}/source=source/*/table_name=endpint_2/*/*.json'
);&lt;/LI-CODE&gt;&lt;P&gt;code block 1 with endpoint_1 got new file or it has the files then it runs correctly.&lt;/P&gt;&lt;P&gt;while code block 2 with endpoint_2 has no file or no folder. So idealy that code block should be skipped but if it runs it will fail and raise error.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 11:27:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-if-statements-or-try-catch-block-in-sql-based-dlt/m-p/107563#M42836</guid>
      <dc:creator>ashraf1395</dc:creator>
      <dc:date>2025-01-29T11:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: Adding if statements or try/catch block in sql based dlt pipelines</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-if-statements-or-try-catch-block-in-sql-based-dlt/m-p/107586#M42847</link>
      <description>&lt;P&gt;Hello, thank you for your question.&lt;/P&gt;
&lt;P&gt;Since SQL-based &lt;STRONG&gt;Delta Live Tables (DLT)&lt;/STRONG&gt; pipelines do not natively support &lt;STRONG&gt;IF-ELSE&lt;/STRONG&gt; or &lt;STRONG&gt;TRY-CATCH&lt;/STRONG&gt; constructs, you'll need an approach to &lt;STRONG&gt;gracefully handle missing files&lt;/STRONG&gt;. Here are two recommended solutions:&lt;/P&gt;
&lt;H3&gt;&lt;STRONG&gt;Solution 1: Use &lt;CODE&gt;read_files&lt;/CODE&gt; with &lt;CODE&gt;allow_missing_files = true&lt;/CODE&gt;&lt;/STRONG&gt;&lt;/H3&gt;
&lt;P&gt;The &lt;CODE&gt;read_files&lt;/CODE&gt; function supports an &lt;CODE&gt;allow_missing_files&lt;/CODE&gt; option that prevents failure if the path is empty or missing. Modify your queries as follows:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;CREATE OR REFRESH MATERIALIZED VIEW jamf_mobile_devices
COMMENT "Incremental upload of data incoming from endpoint_2"
AS
SELECT *
FROM read_files(
  '/Volumes/catalog/${schema_path}/catalog_name-${schema_path}/source=source/*/table_name=endpoint_2/*/*.json',
  allow_missing_files =&amp;gt; true  -- Prevents failure when no files exist
);
&lt;/LI-CODE&gt;
&lt;P&gt;This ensures that if no new files exist, the query does not fail and instead returns an &lt;STRONG&gt;empty dataset&lt;/STRONG&gt;.&lt;/P&gt;
&lt;H3&gt;&lt;STRONG&gt;Solution 2: Use &lt;CODE&gt;WHERE&lt;/CODE&gt; with a Metadata Table (Advanced)&lt;/STRONG&gt;&lt;/H3&gt;
&lt;UL&gt;
&lt;LI&gt;Maintain a metadata table tracking available file paths.&lt;/LI&gt;
&lt;LI&gt;Before querying, &lt;STRONG&gt;filter out missing sources&lt;/STRONG&gt; dynamically.&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="markup"&gt;WITH available_sources AS (
  SELECT path FROM file_metadata_table WHERE table_name = 'endpoint_2'
)
CREATE OR REFRESH MATERIALIZED VIEW jamf_mobile_devices AS
SELECT * FROM read_files(
  (SELECT path FROM available_sources)
);
&lt;/LI-CODE&gt;
&lt;P&gt;This prevents querying non-existent sources.&lt;/P&gt;
&lt;P&gt;If &lt;CODE&gt;allow_missing_files =&amp;gt; true&lt;/CODE&gt; works in your case, it is the easiest solution. Otherwise, dynamically maintaining a metadata table for available file paths is a more robust alternative. Let me know if you need more details!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 13:31:05 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-if-statements-or-try-catch-block-in-sql-based-dlt/m-p/107586#M42847</guid>
      <dc:creator>VZLA</dc:creator>
      <dc:date>2025-01-29T13:31:05Z</dc:date>
    </item>
    <item>
      <title>Re: Adding if statements or try/catch block in sql based dlt pipelines</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-if-statements-or-try-catch-block-in-sql-based-dlt/m-p/107596#M42853</link>
      <description>&lt;P&gt;Is there a reason why you can't use Autoloader for this?&amp;nbsp; That would only trigger the pipeline when new files arrive.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 13:56:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-if-statements-or-try-catch-block-in-sql-based-dlt/m-p/107596#M42853</guid>
      <dc:creator>Rjdudley</dc:creator>
      <dc:date>2025-01-29T13:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Adding if statements or try/catch block in sql based dlt pipelines</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-if-statements-or-try-catch-block-in-sql-based-dlt/m-p/107712#M42896</link>
      <description>&lt;P&gt;Yes, using autoloader with file notification mode can be useful here.&lt;/P&gt;
&lt;P&gt;Also you can use the &lt;CODE&gt;IF EXISTS&lt;/CODE&gt; clause to check if the files exist before attempting to create or refresh the materialized view. This will prevent the SQL block from running if there are no new files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2025 07:41:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-if-statements-or-try-catch-block-in-sql-based-dlt/m-p/107712#M42896</guid>
      <dc:creator>Sidhant07</dc:creator>
      <dc:date>2025-01-30T07:41:15Z</dc:date>
    </item>
  </channel>
</rss>

