<?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: how to use rules dynamically in LDP in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/146053#M52657</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/193958"&gt;@IM_01&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Yes, you can store your rules in a delta table. For the sake of example this table is filled with static rules, but of course you can insert to that table in "dynamic" way.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;CREATE OR REPLACE TABLE
  rules
AS SELECT
  col1 AS name,
  col2 AS constraint,
  col3 AS tag
FROM (
  VALUES
  ("website_not_null","Website IS NOT NULL","validity"),
  ("fresh_data","to_date(updateTime,'M/d/yyyy h:m:s a') &amp;gt; '2010-01-01'","maintained"),
  ("social_media_access","NOT(Facebook IS NULL AND Twitter IS NULL AND Youtube IS NULL)","maintained")
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Then you can read those rules in following way:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark import pipelines as dp
from pyspark.sql.functions import expr, col

def get_rules(tag):
  """
    loads data quality rules from a table
    :param tag: tag to match
    :return: dictionary of rules that matched the tag
  """
  df = spark.read.table("rules").filter(col("tag") == tag).collect()
  return {
      row['name']: row['constraint']
      for row in df
  }

@dp.table
@dp.expect_all_or_drop(get_rules('validity'))
def raw_farmers_market():
  return (
    spark.read.format('csv').option("header", "true")
      .load('/databricks-datasets/data.gov/farmers_markets_geographic_data/data-001/')
  )

@dp.table
@dp.expect_all_or_drop(get_rules('maintained'))
def organic_farmers_market():
  return (
    spark.read.table("raw_farmers_market")
      .filter(expr("Organic = 'Y'"))
  )&lt;/LI-CODE&gt;&lt;P&gt;Check following page for more details:&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/ldp/expectation-patterns?language=SQL#portable-and-reusable-expectations" target="_blank"&gt;Expectation recommendations and advanced patterns | Databricks on AWS&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 30 Jan 2026 13:17:35 GMT</pubDate>
    <dc:creator>szymon_dybczak</dc:creator>
    <dc:date>2026-01-30T13:17:35Z</dc:date>
    <item>
      <title>how to use rules dynamically in LDP</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/146012#M52656</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I see there is a way to store rules in table &amp;amp; use them in python while implementing LDPs how to use the generate/ read rules dynamically in SQL way of implementing LDPs. Could you please help me with this&lt;/P&gt;&lt;P&gt;#DLT&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jan 2026 10:37:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/146012#M52656</guid>
      <dc:creator>IM_01</dc:creator>
      <dc:date>2026-01-30T10:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rules dynamically in LDP</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/146053#M52657</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/193958"&gt;@IM_01&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Yes, you can store your rules in a delta table. For the sake of example this table is filled with static rules, but of course you can insert to that table in "dynamic" way.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;CREATE OR REPLACE TABLE
  rules
AS SELECT
  col1 AS name,
  col2 AS constraint,
  col3 AS tag
FROM (
  VALUES
  ("website_not_null","Website IS NOT NULL","validity"),
  ("fresh_data","to_date(updateTime,'M/d/yyyy h:m:s a') &amp;gt; '2010-01-01'","maintained"),
  ("social_media_access","NOT(Facebook IS NULL AND Twitter IS NULL AND Youtube IS NULL)","maintained")
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Then you can read those rules in following way:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark import pipelines as dp
from pyspark.sql.functions import expr, col

def get_rules(tag):
  """
    loads data quality rules from a table
    :param tag: tag to match
    :return: dictionary of rules that matched the tag
  """
  df = spark.read.table("rules").filter(col("tag") == tag).collect()
  return {
      row['name']: row['constraint']
      for row in df
  }

@dp.table
@dp.expect_all_or_drop(get_rules('validity'))
def raw_farmers_market():
  return (
    spark.read.format('csv').option("header", "true")
      .load('/databricks-datasets/data.gov/farmers_markets_geographic_data/data-001/')
  )

@dp.table
@dp.expect_all_or_drop(get_rules('maintained'))
def organic_farmers_market():
  return (
    spark.read.table("raw_farmers_market")
      .filter(expr("Organic = 'Y'"))
  )&lt;/LI-CODE&gt;&lt;P&gt;Check following page for more details:&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/ldp/expectation-patterns?language=SQL#portable-and-reusable-expectations" target="_blank"&gt;Expectation recommendations and advanced patterns | Databricks on AWS&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jan 2026 13:17:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/146053#M52657</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2026-01-30T13:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rules dynamically in LDP</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/146123#M52658</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Is there a way that we can implement expect all or dynamic rules using sql in LDP instead of python&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jan 2026 15:09:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/146123#M52658</guid>
      <dc:creator>IM_01</dc:creator>
      <dc:date>2026-01-30T15:09:56Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rules dynamically in LDP</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/146205#M52659</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/193958"&gt;@IM_01&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;To be honest I don't know. I've always used python API when working with declarative pipelines &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jan 2026 20:12:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/146205#M52659</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2026-01-30T20:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rules dynamically in LDP</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/150184#M53293</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/193958"&gt;@IM_01&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;The feature you are looking for, storing data quality rules in a table and applying them dynamically, is fully supported in Lakeflow Spark Declarative Pipelines (SDP) through the Python API. Unfortunately, there is currently no equivalent SQL-only mechanism for loading expectations dynamically from a table. The SQL CONSTRAINT ... EXPECT syntax requires rules to be defined statically inline in the table definition.&lt;/P&gt;
&lt;P&gt;That said, here is the recommended approach using Python, and a hybrid workaround if you prefer to keep your transformation logic in SQL.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;OPTION 1: FULLY DYNAMIC RULES WITH PYTHON&lt;/P&gt;
&lt;P&gt;Step 1. Create a rules table that stores your expectations:&lt;/P&gt;
&lt;P&gt;CREATE OR REPLACE TABLE rules AS&lt;BR /&gt;SELECT col1 AS name, col2 AS constraint, col3 AS tag&lt;BR /&gt;FROM (&lt;BR /&gt;VALUES&lt;BR /&gt;("website_not_null", "Website IS NOT NULL", "validity"),&lt;BR /&gt;("fresh_data", "to_date(updateTime,'M/d/yyyy h:m:s a') &amp;gt; '2010-01-01'", "maintained"),&lt;BR /&gt;("social_media_access", "NOT(Facebook IS NULL AND Twitter IS NULL AND Youtube IS NULL)", "maintained")&lt;BR /&gt;)&lt;/P&gt;
&lt;P&gt;Step 2. Create a helper function in your pipeline notebook that reads rules from the table:&lt;/P&gt;
&lt;P&gt;from pyspark import pipelines as dp&lt;BR /&gt;from pyspark.sql.functions import expr, col&lt;/P&gt;
&lt;P&gt;def get_rules(tag):&lt;BR /&gt;"""Load data quality rules from a Delta table filtered by tag."""&lt;BR /&gt;df = spark.read.table("rules").filter(col("tag") == tag).collect()&lt;BR /&gt;return {row['name']: row['constraint'] for row in df}&lt;/P&gt;
&lt;P&gt;Step 3. Apply the rules dynamically using expect_all, expect_all_or_drop, or expect_all_or_fail:&lt;/P&gt;
&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/25059"&gt;@DP&lt;/a&gt;.table&lt;BR /&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/25059"&gt;@DP&lt;/a&gt;.expect_all_or_drop(get_rules('validity'))&lt;BR /&gt;def raw_farmers_market():&lt;BR /&gt;return (&lt;BR /&gt;spark.read.format('csv')&lt;BR /&gt;.option("header", "true")&lt;BR /&gt;.load('/databricks-datasets/data.gov/farmers_markets_geographic_data/data-001/')&lt;BR /&gt;)&lt;/P&gt;
&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/25059"&gt;@DP&lt;/a&gt;.table&lt;BR /&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/25059"&gt;@DP&lt;/a&gt;.expect_all_or_drop(get_rules('maintained'))&lt;BR /&gt;def organic_farmers_market():&lt;BR /&gt;return (&lt;BR /&gt;spark.read.table("raw_farmers_market")&lt;BR /&gt;.filter(expr("Organic = 'Y'"))&lt;BR /&gt;)&lt;/P&gt;
&lt;P&gt;This way, you can add, update, or remove rules by simply modifying the rules table, with no changes needed to the pipeline code itself.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;OPTION 2: HYBRID APPROACH (SQL TRANSFORMATIONS WITH PYTHON EXPECTATIONS)&lt;/P&gt;
&lt;P&gt;If you prefer writing your transformation logic in SQL but still want dynamic expectations, you can use a hybrid pattern. Define your transformation as a SQL temporary view, then wrap it in a Python table definition where you apply dynamic expectations:&lt;/P&gt;
&lt;P&gt;CREATE TEMPORARY VIEW raw_farmers_market_sql AS&lt;BR /&gt;SELECT * FROM csv.`/databricks-datasets/data.gov/farmers_markets_geographic_data/data-001/`&lt;/P&gt;
&lt;P&gt;Then in a Python cell in the same pipeline notebook:&lt;/P&gt;
&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/25059"&gt;@DP&lt;/a&gt;.table&lt;BR /&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/25059"&gt;@DP&lt;/a&gt;.expect_all_or_drop(get_rules('validity'))&lt;BR /&gt;def raw_farmers_market():&lt;BR /&gt;return spark.read.table("raw_farmers_market_sql")&lt;/P&gt;
&lt;P&gt;This gives you the flexibility of SQL for data transformations while leveraging Python for dynamic rule application.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;OPTION 3: PYTHON MODULE FOR RULES (ALTERNATIVE TO TABLE)&lt;/P&gt;
&lt;P&gt;If you prefer storing rules in code rather than a table, you can create a Python module with your rules and import them:&lt;/P&gt;
&lt;P&gt;# rules_module.py&lt;BR /&gt;def get_rules_as_list_of_dict():&lt;BR /&gt;return [&lt;BR /&gt;{"name": "website_not_null", "constraint": "Website IS NOT NULL", "tag": "validity"},&lt;BR /&gt;{"name": "fresh_data", "constraint": "to_date(updateTime,'M/d/yyyy h:m:s a') &amp;gt; '2010-01-01'", "tag": "maintained"},&lt;BR /&gt;]&lt;/P&gt;
&lt;P&gt;Then in your pipeline:&lt;/P&gt;
&lt;P&gt;from rules_module import get_rules_as_list_of_dict&lt;/P&gt;
&lt;P&gt;def get_rules(tag):&lt;BR /&gt;return {&lt;BR /&gt;row['name']: row['constraint']&lt;BR /&gt;for row in get_rules_as_list_of_dict()&lt;BR /&gt;if row['tag'] == tag&lt;BR /&gt;}&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;WHY SQL DOES NOT SUPPORT DYNAMIC EXPECTATIONS&lt;/P&gt;
&lt;P&gt;In the SQL syntax for Lakeflow Spark Declarative Pipelines, expectations are defined as static CONSTRAINT clauses:&lt;/P&gt;
&lt;P&gt;CREATE OR REFRESH STREAMING TABLE my_table (&lt;BR /&gt;CONSTRAINT valid_age EXPECT (age BETWEEN 0 AND 120) ON VIOLATION DROP ROW&lt;BR /&gt;) AS SELECT * FROM STREAM(source_table)&lt;/P&gt;
&lt;P&gt;There is no SQL syntax to reference or load constraints from another table at runtime. This is a limitation of the declarative SQL approach. The Python API, with its expect_all family of decorators that accept dictionaries, is the supported path for dynamic rule management.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;DOCUMENTATION REFERENCES&lt;/P&gt;
&lt;P&gt;For full details, see the official docs:&lt;BR /&gt;&lt;A href="https://docs.databricks.com/en/ldp/expectation-patterns.html" target="_blank"&gt;https://docs.databricks.com/en/ldp/expectation-patterns.html&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://docs.databricks.com/en/dlt/expectations.html" target="_blank"&gt;https://docs.databricks.com/en/dlt/expectations.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;* This reply used an agent system I built to research and draft this response based on the wide set of documentation I have available and previous memory. I personally review the draft for any obvious issues and for monitoring system reliability and update it when I detect any drift, but there is still a small chance that something is inaccurate, especially if you are experimenting with brand new features.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Mar 2026 07:31:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rules-dynamically-in-ldp/m-p/150184#M53293</guid>
      <dc:creator>SteveOstrowski</dc:creator>
      <dc:date>2026-03-08T07:31:56Z</dc:date>
    </item>
  </channel>
</rss>

