text: {% macro dbt_databricks_file_format_clause() %}
    {%- set file_format = config.get('file_format', default='delta') -%}
    {%- if file_format is not none %}
      using {{ file_format }}
    {%- endif %}
  {%- endmacro -%}
  
  {% macro dbt_databricks_location_clause() %}
    {%- set location_root = config.get('location_root', validator=validation.any[basestring]) -%}
    {%- set identifier = model['alias'] -%}
    {%- if location_root is not none %}
      location '{{ location_root }}/{{ identifier }}'
    {%- endif %}
  {%- endmacro -%}
  
  {% macro dbt_databricks_options_clause() -%}
    {%- set options = config.get('options') -%}
    {%- if config.get('file_format', default='delta') == 'hudi' -%}
      {%- set unique_key = config.get('unique_key') -%}
      {%- if unique_key is not none and options is none -%}
        {%- set options = {'primaryKey': config.get('unique_key')} -%}
      {%- elif unique_key is not none and options is not none and 'primaryKey' not in options -%}
        {%- set _ = options.update({'primaryKey': config.get('unique_key')}) -%}
      {%- elif options is not none and 'primaryKey' in options and options['primaryKey'] != unique_key -%}
        {{ exceptions.raise_compiler_error("unique_key and options('primaryKey') should be the same column(s).") }}
      {%- endif %}
    {%- endif %}
  
    {%- if options is not none %}
      options (
        {%- for option in options -%}
        {{ option }} "{{ options[option] }}" {% if not loop.last %}, {% endif %}
        {%- endfor %}
      )
    {%- endif %}
  {%- endmacro -%}
  
  {% macro dbt_databricks_comment_clause() %}
    {%- set raw_persist_docs = config.get('persist_docs', {}) -%}
  
    {%- if raw_persist_docs is mapping -%}
      {%- set raw_relation = raw_persist_docs.get('relation', false) -%}
        {%- if raw_relation -%}
        comment '{{ model.description | replace("'", "\\'") }}'
        {% endif %}
    {%- elif raw_persist_docs -%}
      {{ exceptions.raise_compiler_error("Invalid value provided for 'persist_docs'. Expected dict but got value: " ~ raw_persist_docs) }}
    {% endif %}
  {%- endmacro -%}
  
  {% macro dbt_databricks_partition_cols(label, required=false) %}
    {%- set cols = config.get('partition_by', validator=validation.any[list, basestring]) -%}
    {%- if cols is not none %}
      {%- if cols is string -%}
        {%- set cols = [cols] -%}
      {%- endif -%}
      {{ label }} (
      {%- for item in cols -%}
        {{ item }}
        {%- if not loop.last -%},{%- endif -%}
      {%- endfor -%}
      )
    {%- endif %}
  {%- endmacro -%}
  
  {% macro dbt_databricks_clustered_cols(label, required=false) %}
    {%- set cols = config.get('clustered_by', validator=validation.any[list, basestring]) -%}
    {%- set buckets = config.get('buckets', validator=validation.any[int]) -%}
    {%- if (cols is not none) and (buckets is not none) %}
      {%- if cols is string -%}
        {%- set cols = [cols] -%}
      {%- endif -%}
      {{ label }} (
      {%- for item in cols -%}
        {{ item }}
        {%- if not loop.last -%},{%- endif -%}
      {%- endfor -%}
      ) into {{ buckets }} buckets
    {%- endif %}
  {%- endmacro -%}
  
  {% macro dbt_databricks_tblproperties_clause() -%}
    {%- set tblproperties = config.get('tblproperties') -%}
    {%- if tblproperties is not none %}
      tblproperties (
        {%- for prop in tblproperties -%}
        '{{ prop }}' = '{{ tblproperties[prop] }}' {% if not loop.last %}, {% endif %}
        {%- endfor %}
      )
    {%- endif %}
  {%- endmacro -%}
  
  {#-- We can't use temporary tables with `create ... as ()` syntax #}
  {% macro dbt_databricks_create_temporary_view(relation, sql) -%}
    create temporary view {{ relation.include(schema=false) }} as
      {{ sql }}
  {% endmacro %}
  
  {% macro databricks__create_table_as(temporary, relation, sql) -%}
    {% if temporary -%}
      {{ dbt_databricks_create_temporary_view(relation, sql) }}
    {%- else -%}
      {% if config.get('file_format', default='delta') == 'delta' %}
        create or replace table {{ relation }}
      {% else %}
        create table {{ relation }}
      {% endif %}
      {{ dbt_databricks_file_format_clause() }}
      {{ dbt_databricks_options_clause() }}
      {{ dbt_databricks_partition_cols(label="partitioned by") }}
      {{ dbt_databricks_clustered_cols(label="clustered by") }}
      {{ dbt_databricks_location_clause() }}
      {{ dbt_databricks_comment_clause() }}
      {{ dbt_databricks_tblproperties_clause() }}
      as
        {{ sql }}
    {%- endif %}
  {%- endmacro -%}
  
  {% macro databricks__create_view_as(relation, sql) -%}
    create or replace view {{ relation }}
    {{ dbt_databricks_comment_clause() }}
    {{ dbt_databricks_tblproperties_clause() }}
    as
      {{ sql }}
  {% endmacro %}
  
  {% macro databricks__alter_column_comment(relation, column_dict) %}
    {% if config.get('file_format', default='delta') in ['delta', 'hudi'] %}
      {% for column_name in column_dict %}
        {% set comment = column_dict[column_name]['description'] %}
        {% set escaped_comment = comment | replace('\'', '\\\'') %}
        {% set comment_query %}
          alter table {{ relation }} change column
              {{ adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name }}
              comment '{{ escaped_comment }}';
        {% endset %}
        {% do run_query(comment_query) %}
      {% endfor %}
    {% endif %}
  {% endmacro %}
  