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

Fix persist_docs for columns #180

Merged
merged 4 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions dbt/include/spark/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,23 @@
{% do return(None) %}
{%- endmacro %}

{% macro spark__persist_docs(relation, model, for_relation, for_columns) -%}
{% if for_columns and config.persist_column_docs() and model.columns %}
{% do alter_column_comment(relation, model.columns) %}
{% endif %}
{% endmacro %}

{% macro spark__alter_column_comment(relation, column_dict) %}
{% if config.get('file_format', validator=validation.any[basestring]) == 'delta' %}
{% 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 '{{ comment }}';
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 %}

12 changes: 4 additions & 8 deletions dbt/include/spark/macros/materializations/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@
{%- set agate_table = load_agate_table() -%}
{%- do store_result('agate_table', response='OK', agate_table=agate_table) -%}

{{ run_hooks(pre_hooks, inside_transaction=False) }}
Copy link
Contributor

@leahwicz leahwicz Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do the run_hooks calls change? Is it related to the persist_docs change or is this related to something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really related to the specific change in the PR at all, I just noticed because I was editing related lines. We clearly copied these lines from the default seed materialization a few years back. The default materialization, which was originally written for Redshift/Postgres, assumes a transactional database. Some hooks may want to run before the main transaction starts (begin), after it starts, before it ends (commit), or after it ends.

Spark doesn't have transactions, so there's no need to distinguish between inside_transaction = True|False. I figured I would simplify it while here, but I'd also be fine reverting those lines for PR cleanliness.


-- `BEGIN` happens here:
{{ run_hooks(pre_hooks, inside_transaction=True) }}
{{ run_hooks(pre_hooks) }}

-- build model
{% set create_table_sql = reset_csv_table(model, full_refresh_mode, old_relation, agate_table) %}
Expand All @@ -98,10 +95,9 @@
{{ sql }}
{% endcall %}

{{ run_hooks(post_hooks, inside_transaction=True) }}
-- `COMMIT` happens here
{{ adapter.commit() }}
{{ run_hooks(post_hooks, inside_transaction=False) }}
{% do persist_docs(target_relation, model) %}

{{ run_hooks(post_hooks) }}

{{ return({'relations': [target_relation]}) }}

Expand Down
2 changes: 2 additions & 0 deletions dbt/include/spark/macros/materializations/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
{% call statement('main') -%}
{{ create_table_as(False, target_relation, sql) }}
{%- endcall %}

{% do persist_docs(target_relation, model) %}

{{ run_hooks(post_hooks) }}

Expand Down
3 changes: 3 additions & 0 deletions test/custom/persist_docs/data/seed.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name
1,Alice
2,Bob
26 changes: 26 additions & 0 deletions test/custom/persist_docs/data/seeds.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 2

seeds:
- name: seed
description: |
Seed model description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
columns:
- name: id
description: |
id Column description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
- name: name
description: |
Some stuff here and then a call to
{{ doc('my_fun_doc')}}
10 changes: 10 additions & 0 deletions test/custom/persist_docs/models/my_fun_docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% docs my_fun_doc %}
name Column description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting

{% enddocs %}
1 change: 1 addition & 0 deletions test/custom/persist_docs/models/no_docs_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 1 as id, 'Alice' as name
71 changes: 71 additions & 0 deletions test/custom/persist_docs/models/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
version: 2

models:

- name: table_parquet_model
description: |
Table model description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
columns:
- name: id
description: |
id Column description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
- name: name
description: |
Some stuff here and then a call to
{{ doc('my_fun_doc')}}

- name: table_delta_model
description: |
Table model description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
columns:
- name: id
description: |
id Column description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
- name: name
description: |
Some stuff here and then a call to
{{ doc('my_fun_doc')}}

- name: view_model
description: |
View model description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
columns:
- name: id
description: |
id Column description "with double quotes"
and with 'single quotes' as welll as other;
'''abc123'''
reserved -- characters
--
/* comment */
Some $lbl$ labeled $lbl$ and $$ unlabeled $$ dollar-quoting
2 changes: 2 additions & 0 deletions test/custom/persist_docs/models/table_delta_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ config(materialized='table', file_format='delta') }}
select 1 as id, 'Joe' as name
2 changes: 2 additions & 0 deletions test/custom/persist_docs/models/table_parquet_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ config(materialized='table', file_format='parquet') }}
select 1 as id, 'Joe' as name
2 changes: 2 additions & 0 deletions test/custom/persist_docs/models/view_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ config(materialized='view') }}
select 2 as id, 'Bob' as name
72 changes: 72 additions & 0 deletions test/custom/persist_docs/test_persist_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from cProfile import run
from test.custom.base import DBTSparkIntegrationTest, use_profile
import dbt.exceptions

import json


class TestPersistDocsDelta(DBTSparkIntegrationTest):
@property
def schema(self):
return "persist_docs_columns"

@property
def models(self):
return "models"

@property
def project_config(self):
return {
'config-version': 2,
'models': {
'test': {
'+persist_docs': {
"relation": True,
"columns": True,
},
}
},
'seeds': {
'test': {
'+persist_docs': {
"relation": True,
"columns": True,
},
'+file_format': 'delta',
'+quote_columns': True
}
},
}

def test_delta_comments(self):
self.run_dbt(['seed'])
self.run_dbt(['run'])

for table in ['table_parquet_model', 'table_delta_model', 'seed']:
results = self.run_sql(
'describe extended {schema}.{table}'.format(schema=self.unique_schema(), table=table),
fetch='all'
)

for result in results:
# parquet model will have table-level comment only
if result[0] == 'Comment':
whatis = 'Seed' if table == 'seed' else 'Table'
assert result[1].startswith(f'{whatis} model description')

# delta tables will have table-level and column-level comments
if table in ['table_delta_model', 'seed']:
if result[0] == 'id':
assert result[2].startswith('id Column description')
if result[0] == 'name':
assert result[2].startswith('Some stuff here and then a call to')

# runs on Spark v3.0
@use_profile("databricks_cluster")
def test_delta_comments_databricks_cluster(self):
self.test_delta_comments()

# runs on Spark v3.0
@use_profile("databricks_sql_endpoint")
def test_delta_comments_databricks_sql_endpoint(self):
self.test_delta_comments()