<?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 column masks to a column using the DLT Python create_streaming_table API in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/adding-column-masks-to-a-column-using-the-dlt-python-create/m-p/116388#M45300</link>
    <description>&lt;P&gt;Thanks that was very quick. I'll try this in the morning and revert.&lt;/P&gt;</description>
    <pubDate>Wed, 23 Apr 2025 17:40:57 GMT</pubDate>
    <dc:creator>NamNguyenCypher</dc:creator>
    <dc:date>2025-04-23T17:40:57Z</dc:date>
    <item>
      <title>Adding column masks to a column using the DLT Python create_streaming_table API</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-column-masks-to-a-column-using-the-dlt-python-create/m-p/116366#M45296</link>
      <description>&lt;P&gt;I'm having difficulty adding a mask function to columns while creating streaming tables using the DLT Python method &lt;FONT face="terminal,monaco"&gt;create_streaming_table()&lt;/FONT&gt; like this but it does not work, the streaming table is created but no column is masked:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def prepare_column_properties_struct(table_contract: dict) -&amp;gt; StructType:
    struct_fields = []

    for column_properties in table_contract["models"]["columns"]:
        column_name = column_properties["name"]
        column_type = column_properties["type"]
        column_nullable = not column_properties["required"]
        column_comment = column_properties["comment"]
        column_mask = column_properties["mask"]

        struct_fields.append(
            StructField(
                name=column_name,
                dataType=parse_data_type(column_type),
                nullable=column_nullable,
                metadata={"comment": column_comment, "mask": "mask_all"},
            )
        )

    return StructType(struct_fields)

dlt.create_streaming_table(
            name="account",
            schema=prepare_column_properties_struct(data_contract),
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;How do I go about this? Maybe I'm not using the correct metadata key in the StructField? The doc is not helping.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Apr 2025 16:51:26 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-column-masks-to-a-column-using-the-dlt-python-create/m-p/116366#M45296</guid>
      <dc:creator>NamNguyenCypher</dc:creator>
      <dc:date>2025-04-23T16:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: Adding column masks to a column using the DLT Python create_streaming_table API</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-column-masks-to-a-column-using-the-dlt-python-create/m-p/116370#M45297</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/132934"&gt;@NamNguyenCypher&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Delta Live Tables’ Python API does not currently honor column-mask metadata embedded in a PySpark StructType. Masking (and row filters) on DLT tables are only applied when you define your table with a DDL-style schema that includes a MASK clause (or via SQL).&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Why your StructField(... metadata={"mask": "mask_all"}) isn’t working&lt;/STRONG&gt;&lt;BR /&gt;The Python create_streaming_table(..., schema=StructType) call will publish the schema (data types, comments, nullability), but it does not inspect StructField.metadata for mask or maskingPolicy keys.&amp;nbsp;&lt;A href="https://docs.databricks.com/aws/en/dlt-ref/dlt-python-ref-streaming-table?utm_source=chatgpt.com" target="_blank" rel="noopener"&gt;https://docs.databricks.com/aws/en/dlt-ref/dlt-python-ref-streaming-table?utm_source=chatgpt.com&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Column masks in DLT are applied at the table definition level via SQL’s MASK clause, not via Spark schema metadata.&amp;nbsp;&lt;A href="https://docs.azure.cn/en-us/databricks/dlt/sql-ref?utm_source=chatgpt.com" target="_blank" rel="noopener"&gt;https://docs.azure.cn/en-us/databricks/dlt/sql-ref?utm_source=chatgpt.com&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Use a SQL-DDL string for your schema&lt;/STRONG&gt;&lt;BR /&gt;Pass a single string to the schema parameter that embeds the MASK expression inline, e.g.:&lt;/P&gt;&lt;P&gt;import dlt&lt;/P&gt;&lt;P&gt;dlt.create_streaming_table(&lt;BR /&gt;name="account",&lt;BR /&gt;schema="""&lt;BR /&gt;account_id STRING,&lt;BR /&gt;email STRING,&lt;BR /&gt;ssn STRING&lt;BR /&gt;MASK my_catalog.my_schema.ssn_mask_fn()&lt;BR /&gt;COMMENT 'SSN masked for privacy'&lt;BR /&gt;""",&lt;BR /&gt;comment="Masked account stream",&lt;BR /&gt;path="/mnt/dlt/account",&lt;BR /&gt;partition_cols=["account_id"]&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;Here, ssn gets masked by the UDF ssn_mask_fn() every time it’s read.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Apr 2025 17:20:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-column-masks-to-a-column-using-the-dlt-python-create/m-p/116370#M45297</guid>
      <dc:creator>lingareddy_Alva</dc:creator>
      <dc:date>2025-04-23T17:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: Adding column masks to a column using the DLT Python create_streaming_table API</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-column-masks-to-a-column-using-the-dlt-python-create/m-p/116388#M45300</link>
      <description>&lt;P&gt;Thanks that was very quick. I'll try this in the morning and revert.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Apr 2025 17:40:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-column-masks-to-a-column-using-the-dlt-python-create/m-p/116388#M45300</guid>
      <dc:creator>NamNguyenCypher</dc:creator>
      <dc:date>2025-04-23T17:40:57Z</dc:date>
    </item>
  </channel>
</rss>

