<?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: Apply expectations only if column exists in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/101079#M40531</link>
    <description>&lt;P class="_1t7bu9h1 paragraph"&gt;&lt;SPAN&gt;To apply expectations only if a column exists in Delta Live Tables (DLT), you can use the &lt;CODE&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97035"&gt;@Dlt&lt;/a&gt;.expect&lt;/CODE&gt; decorator conditionally within your Python function. Here is a step-by-step approach to achieve this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;STRONG&gt;Check if the Column Exists&lt;/STRONG&gt;: Before applying the expectation, check if the column exists in the DataFrame.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;STRONG&gt;Apply Expectations Conditionally&lt;/STRONG&gt;: Use the &lt;CODE&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97035"&gt;@Dlt&lt;/a&gt;.expect&lt;/CODE&gt; decorator only if the column is present.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
    <pubDate>Thu, 05 Dec 2024 14:02:23 GMT</pubDate>
    <dc:creator>Walter_C</dc:creator>
    <dc:date>2024-12-05T14:02:23Z</dc:date>
    <item>
      <title>Apply expectations only if column exists</title>
      <link>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/101076#M40529</link>
      <description>&lt;P&gt;Hi, is there any way to apply a expectations only if that column exists? I am creating multiple dlt tables with the same python function so i would like to create diferent expectations based in the table name, currently i only can create expectations for those column that exists in all the tables created for the python function&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 13:46:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/101076#M40529</guid>
      <dc:creator>Hoviedo</dc:creator>
      <dc:date>2024-12-05T13:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Apply expectations only if column exists</title>
      <link>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/101079#M40531</link>
      <description>&lt;P class="_1t7bu9h1 paragraph"&gt;&lt;SPAN&gt;To apply expectations only if a column exists in Delta Live Tables (DLT), you can use the &lt;CODE&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97035"&gt;@Dlt&lt;/a&gt;.expect&lt;/CODE&gt; decorator conditionally within your Python function. Here is a step-by-step approach to achieve this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;STRONG&gt;Check if the Column Exists&lt;/STRONG&gt;: Before applying the expectation, check if the column exists in the DataFrame.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;STRONG&gt;Apply Expectations Conditionally&lt;/STRONG&gt;: Use the &lt;CODE&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97035"&gt;@Dlt&lt;/a&gt;.expect&lt;/CODE&gt; decorator only if the column is present.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Thu, 05 Dec 2024 14:02:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/101079#M40531</guid>
      <dc:creator>Walter_C</dc:creator>
      <dc:date>2024-12-05T14:02:23Z</dc:date>
    </item>
    <item>
      <title>Re: Apply expectations only if column exists</title>
      <link>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/101083#M40534</link>
      <description>&lt;P&gt;Hi Walter, sorry what would it look like in the code?&lt;/P&gt;&lt;P&gt;i can do this in the python function (it does nothing)&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; &lt;SPAN&gt;"Distance"&lt;/SPAN&gt; &lt;SPAN&gt;in&lt;/SPAN&gt;&lt;SPAN&gt; df.columns:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dlt.&lt;/SPAN&gt;&lt;SPAN&gt;expect&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"Distance is positive"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"Distance &amp;gt;= 0"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;but i am now sure how can i apply the same with the decorator&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 05 Dec 2024 14:32:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/101083#M40534</guid>
      <dc:creator>Hoviedo</dc:creator>
      <dc:date>2024-12-05T14:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: Apply expectations only if column exists</title>
      <link>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/101084#M40535</link>
      <description>&lt;DIV&gt;def get_changes_from_raw(table_name):&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97035"&gt;@Dlt&lt;/a&gt;.table(&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; name=f"{table_name}_changes",&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; comment=f"New {table_name} data incrementally ingested from cloud object storage landing zone",&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; )&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97035"&gt;@Dlt&lt;/a&gt;.expect("valid_rescued_data", "_rescued_data is null")&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; def read_changes_from_adl2():&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; df = spark.read...&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if "distance" in df.columns:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dlt.expect("distance is positive", "distance &amp;gt;= 0")&lt;/DIV&gt;</description>
      <pubDate>Thu, 05 Dec 2024 14:50:08 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/101084#M40535</guid>
      <dc:creator>Hoviedo</dc:creator>
      <dc:date>2024-12-05T14:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: Apply expectations only if column exists</title>
      <link>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/124937#M47295</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/88823"&gt;@Walter_C&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;I wonder that how does the step-by-step approach work.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;As far as I know,&amp;nbsp;Decorators (@dlt.expect) are processed when the function code is first read and the pipeline's structure is being defined—this is the &lt;STRONG&gt;parsing phase&lt;/STRONG&gt;. But what check if the column exists, looks like is during when Spark is actually processing data—the &lt;STRONG&gt;execution phase&lt;/STRONG&gt;. As you can see at below, if column doesn't exist, the lakeflow declarative pipeline failed at &lt;STRONG&gt;initialization phase.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-07-11 at 11.54.08 AM.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/18138i5DBCF891AE1B0842/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2025-07-11 at 11.54.08 AM.png" alt="Screenshot 2025-07-11 at 11.54.08 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-07-11 at 11.55.40 AM.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/18139i41D64F2910434250/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2025-07-11 at 11.55.40 AM.png" alt="Screenshot 2025-07-11 at 11.55.40 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;The method proposed by&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/70893"&gt;@Hoviedo&lt;/a&gt;&amp;nbsp;might not work well, as I try to do the same thing, I found the metrics in UI is disappear.&lt;BR /&gt; &lt;BR /&gt;Before (use the decorator):&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-07-11 at 11.27.06 AM.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/18135i74BFDFC5B542B8D7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2025-07-11 at 11.27.06 AM.png" alt="Screenshot 2025-07-11 at 11.27.06 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;After using non-decorator manner:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-07-11 at 11.29.40 AM.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/18136i6F54214FAF8C56A7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2025-07-11 at 11.29.40 AM.png" alt="Screenshot 2025-07-11 at 11.29.40 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jul 2025 16:00:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apply-expectations-only-if-column-exists/m-p/124937#M47295</guid>
      <dc:creator>Fevemania</dc:creator>
      <dc:date>2025-07-11T16:00:31Z</dc:date>
    </item>
  </channel>
</rss>

