datacontract-cli icon indicating copy to clipboard operation
datacontract-cli copied to clipboard

Are tags not automatically applied to the Unity catalog tables?

Open khushbookaul opened this issue 9 months ago • 2 comments

Hi,

I tried running

full_sql_script = contract.export(export_format="sql")

and realised that the script that gets generated is a CREATE OR REPLACE TABLE .... How would the "tags" be automatically applied to columns in Unity catalog?

Thanks in advance,

khushbookaul avatar Feb 17 '26 21:02 khushbookaul

Hi @khushbookaul , I guess this is not an issue of datacontract-cli, but the "create table" doesn't support setting tags as far as I can see here: https://learn.microsoft.com/en-us/azure/databricks/sql/language-manual/sql-ref-syntax-ddl-create-table-using

I was also facing the same requirement of pushing down tags from the datacontract to UnityCatalog tables and columns....and solved it by coding it in python, to parse the contract and generate proper "SET TAG ON ...." commands (docs)

gkoenig avatar Feb 27 '26 09:02 gkoenig

Closing this as a Databricks limitation.

By the way, another workaround could be to use a custom exporter for this: datacontract export --format custom --template unity_tags.sql with a Jinja2 template like:

  {%- for schema in data_contract.schema_ %}
  -- Create table
  CREATE OR REPLACE TABLE {{ schema.name }} (
  {%- for field in schema.properties %}
    {{ field.name }} {{ field.physicalType }}{% if not loop.last %},{% endif %}
  {%- endfor %}
  );

  -- Apply schema-level tags
  {%- if schema.tags %}
  ALTER TABLE {{ schema.name }} SET TAGS (
  {%- for tag in schema.tags %}
    '{{ tag }}' = 'true'{% if not loop.last %},{% endif %}
  {%- endfor %}
  );
  {%- endif %}

  -- Apply column-level tags
  {%- for field in schema.properties %}
  {%- if field.tags %}
  ALTER TABLE {{ schema.name }} ALTER COLUMN {{ field.name }} SET TAGS (
  {%- for tag in field.tags %}
    '{{ tag }}' = 'true'{% if not loop.last %},{% endif %}
  {%- endfor %}
  );
  {%- endif %}
  {%- endfor %}
  {%- endfor %}

jschoedl avatar Apr 13 '26 11:04 jschoedl