Apply expectations only if column exists
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 05:46 AM
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
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 06:02 AM
To apply expectations only if a column exists in Delta Live Tables (DLT), you can use the @Dlt.expect decorator conditionally within your Python function. Here is a step-by-step approach to achieve this:
- Check if the Column Exists: Before applying the expectation, check if the column exists in the DataFrame.
- Apply Expectations Conditionally: Use the
@Dlt.expectdecorator only if the column is present.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 06:32 AM
Hi Walter, sorry what would it look like in the code?
i can do this in the python function (it does nothing)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2024 06:50 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2025 08:31 AM - edited 07-11-2025 09:00 AM
Hi @Walter_C ,
I wonder that how does the step-by-step approach work.
As far as I know, Decorators (@dlt.expect) are processed when the function code is first read and the pipeline's structure is being defined—this is the parsing phase. But what check if the column exists, looks like is during when Spark is actually processing data—the execution phase. As you can see at below, if column doesn't exist, the lakeflow declarative pipeline failed at initialization phase.
The method proposed by @Hoviedo might not work well, as I try to do the same thing, I found the metrics in UI is disappear.
Before (use the decorator):
After using non-decorator manner: