Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist Column level comments when creating views #104

Closed
shuaahvee opened this issue May 25, 2022 · 4 comments
Closed

Persist Column level comments when creating views #104

shuaahvee opened this issue May 25, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@shuaahvee
Copy link

Describe the feature

The persist_docs config works for tables by materializing a model as a table and then running ALTER TABLE statements to update the comment on each column. This method does not work for views because ALTER VIEW doesn't support adding comments. Instead, comments need to be defined when running the CREATE OR REPLACE VIEW statement. There should be an option to handle column level comments for models materialized as views

Describe alternatives you've considered

I tried creating a macro that adds the necessary comment clause and put it at the top of my model but the SQL gets inserted after the AS in CREATE OR REPLACE VIEW AS

Who will this benefit?

This will benefit anyone using column level comments on views in databricks

Are you interested in contributing this feature?

Yes! I overwrote the adapters.sql macro to do this. I'll create and link to a PR!

@shuaahvee shuaahvee added the enhancement New feature or request label May 25, 2022
@shuaahvee
Copy link
Author

shuaahvee commented May 25, 2022

{% macro column_comment_clause() %}
{%- set raw_persist_docs = config.get('persist_docs', {}) -%}

{%- if raw_persist_docs is mapping -%}
    {%- set raw_relation_column_comments = raw_persist_docs.get('columns', false) -%}
    {%- if raw_relation_column_comments -%}
    (
    {% for col in model.columns.values() %}
    {{col["name"] + ' comment ' + "'" + col["description"] | replace("'", "\\'") + "'" }}
    {% if not loop.last %},{% endif %}
    {% endfor %}
    )
    {% 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 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 databricks__create_view_as(relation, sql) -%}
create or replace view {{ relation }}
{{ column_comment_clause() }}
{{ comment_clause() }}
as
    {{ sql }}
{% endmacro %}

@allisonwang-db
Copy link
Collaborator

Hi @shuaahvee thanks for opening the issue. I think it would be better to add this feature in dbt-spark. Feel free to create an issue in the dbt-spark repository.

@shuaahvee
Copy link
Author

Thanks! Will do!

@anairax28
Copy link

@shuaahvee Thank you for the macros

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants