Skip to content

Commit

Permalink
Improve column comment handling when persist_docs is enabled (#161)
Browse files Browse the repository at this point in the history
* Persist docs for quoted column names

* Delete BigQuery tests

* Add cast col without name

* Update changelog

* Update changelog

Co-authored-by: Jeremy Cohen <jeremy@dbtlabs.com>
  • Loading branch information
LewisDavies and jtcohen6 authored Jul 5, 2022
1 parent 31e3890 commit 74bbae3
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### Fixes
- In multi-query statements, prepend all queries with query comments. Use the last non-`COMMIT` query to store metadata about the model result. **Note:** this restores previous (pre-v0.21) behavior for incremental models and snapshots, which will again correctly reflect the number of rows modified in `adapter_response.rows_affected` ([#140](https://github.com/dbt-labs/dbt-snowflake/issues/140), [#147](https://github.com/dbt-labs/dbt-snowflake/issues147140), [#153](https://github.com/dbt-labs/dbt-snowflake/pull/153))
- Improve column comment handling when `persist_docs` is enabled ([#161](https://github.com/dbt-labs/dbt-snowflake/pull/161))

### Contributors
- [@LewisDavies](https://github.com/LewisDavies) ([#161](https://github.com/dbt-labs/dbt-snowflake/pull/161))

## dbt-snowflake 1.2.0b1 (June 24, 2022)

Expand Down
29 changes: 19 additions & 10 deletions dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,28 @@
{%- endif -%}
{% endmacro %}

{% macro get_column_comment_sql(column_name, column_dict) %}
{{ adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name }} COMMENT $${{ column_dict[column_name]['description'] | replace('$', '[$]') }}$$
{% macro get_column_comment_sql(column_name, column_dict) -%}
{% if (column_name|upper in column_dict) -%}
{% set matched_column = column_name|upper -%}
{% elif (column_name|lower in column_dict) -%}
{% set matched_column = column_name|lower -%}
{% elif (column_name in column_dict) -%}
{% set matched_column = column_name -%}
{% else -%}
{% set matched_column = None -%}
{% endif -%}
{% if matched_column -%}
{{ adapter.quote(column_name) }} COMMENT $${{ column_dict[matched_column]['description'] | replace('$', '[$]') }}$$
{%- else -%}
{{ adapter.quote(column_name) }} COMMENT $$$$
{%- endif -%}
{% endmacro %}

{% macro get_persist_docs_column_list(model_columns, query_columns) %}
(
{% for column_name in query_columns %}
{% if (column_name|upper in model_columns) or (column_name in model_columns) %}
{{ get_column_comment_sql(column_name, model_columns) }}
{% else %}
{{column_name}}
{% endif %}
{{ ", " if not loop.last else "" }}
{{ get_column_comment_sql(column_name, model_columns) }}
{{- ", " if not loop.last else "" }}
{% endfor %}
)
{% endmacro %}
Expand Down Expand Up @@ -181,8 +190,8 @@
{% macro snowflake__alter_column_comment(relation, column_dict) -%}
{% set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list %}
alter {{ relation.type }} {{ relation }} alter
{% for column_name in column_dict if (column_name in existing_columns) or (column_name|upper in existing_columns) %}
{{ get_column_comment_sql(column_name, column_dict) }} {{ ',' if not loop.last else ';' }}
{% for column_name in existing_columns if (column_name in existing_columns) or (column_name|lower in existing_columns) %}
{{ get_column_comment_sql(column_name, column_dict) }} {{- ',' if not loop.last else ';' }}
{% endfor %}
{% endmacro %}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

18 changes: 18 additions & 0 deletions tests/integration/persist_docs_tests/models/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ models:
description: |
Some stuff here and then a call to
{{ doc('my_fun_doc')}}
- name: order
description: |
Order keyword lowercase column description
- name: ORDER
description: |
Order keyword uppercase column description
- name: OrDer
description: |
Order keyword mixed case column description
- name: view_model
description: |
View model description "with double quotes"
Expand All @@ -43,6 +52,15 @@ models:
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
- name: order
description: |
Order keyword lowercase column description
- name: ORDER
description: |
Order keyword uppercase column description
- name: OrDer
description: |
Order keyword mixed case column description
seeds:
- name: seed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{{ config(materialized='table') }}
select 1 as id, 'Joe' as name
select 1 as id, 'Joe' as name, 1 as "order", 1 as "ORDER", 1 as "OrDer"
2 changes: 1 addition & 1 deletion tests/integration/persist_docs_tests/models/view_model.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{{ config(materialized='view') }}
select 2 as id, 'Bob' as name
select 2 as id, 'Bob' as name, 1 as "order", 1 as "ORDER", 1 as "OrDer", cast(1 as text)

0 comments on commit 74bbae3

Please sign in to comment.