Skip to content

Commit

Permalink
Use single quotes in gets in templates (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-db authored Dec 20, 2024
1 parent 5f6412d commit b0ff51b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% set batch_size = get_batch_size() %}
{% set column_override = model['config'].get('column_types', {}) %}
{% set must_cast = model['config'].get("file_format", "delta") == "parquet" %}
{% set must_cast = model['config'].get('file_format', 'delta') == 'parquet' %}

{% set statements = [] %}

Expand Down
34 changes: 17 additions & 17 deletions dbt/include/databricks/macros/relations/constraints.sql
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,27 @@

{% macro get_constraint_sql(relation, constraint, model, column={}) %}
{% set statements = [] %}
{% set type = constraint.get("type", "") %}
{% set type = constraint.get('type', '') %}

{% if type == 'check' %}
{% set expression = constraint.get("expression", "") %}
{% set expression = constraint.get('expression', '') %}
{% if not expression %}
{{ exceptions.raise_compiler_error('Invalid check constraint expression') }}
{% endif %}

{% set name = constraint.get("name") %}
{% set name = constraint.get('name') %}
{% if not name %}
{% if local_md5 %}
{{ exceptions.warn("Constraint of type " ~ type ~ " with no `name` provided. Generating hash instead for relation " ~ relation.identifier) }}
{%- set name = local_md5 (relation.identifier ~ ";" ~ column.get("name", "") ~ ";" ~ expression ~ ";") -%}
{%- set name = local_md5 (relation.identifier ~ ";" ~ column.get('name', '') ~ ";" ~ expression ~ ";") -%}
{% else %}
{{ exceptions.raise_compiler_error("Constraint of type " ~ type ~ " with no `name` provided, and no md5 utility.") }}
{% endif %}
{% endif %}
{% set stmt = "alter table " ~ relation ~ " add constraint " ~ name ~ " check (" ~ expression ~ ");" %}
{% do statements.append(stmt) %}
{% elif type == 'not_null' %}
{% set column_names = constraint.get("columns", []) %}
{% set column_names = constraint.get('columns', []) %}
{% if column and not column_names %}
{% set column_names = [column['name']] %}
{% endif %}
Expand All @@ -144,7 +144,7 @@
{% if constraint.get('warn_unenforced') %}
{{ exceptions.warn("unenforced constraint type: " ~ type)}}
{% endif %}
{% set column_names = constraint.get("columns", []) %}
{% set column_names = constraint.get('columns', []) %}
{% if column and not column_names %}
{% set column_names = [column['name']] %}
{% endif %}
Expand All @@ -161,7 +161,7 @@

{% set joined_names = quoted_names|join(", ") %}

{% set name = constraint.get("name") %}
{% set name = constraint.get('name') %}
{% if not name %}
{% if local_md5 %}
{{ exceptions.warn("Constraint of type " ~ type ~ " with no `name` provided. Generating hash instead for relation " ~ relation.identifier) }}
Expand All @@ -178,7 +178,7 @@
{{ exceptions.warn("unenforced constraint type: " ~ constraint.type)}}
{% endif %}

{% set name = constraint.get("name") %}
{% set name = constraint.get('name') %}

{% if constraint.get('expression') %}

Expand All @@ -193,7 +193,7 @@

{% set stmt = "alter table " ~ relation ~ " add constraint " ~ name ~ " foreign key" ~ constraint.get('expression') %}
{% else %}
{% set column_names = constraint.get("columns", []) %}
{% set column_names = constraint.get('columns', []) %}
{% if column and not column_names %}
{% set column_names = [column['name']] %}
{% endif %}
Expand All @@ -210,7 +210,7 @@

{% set joined_names = quoted_names|join(", ") %}

{% set parent = constraint.get("to") %}
{% set parent = constraint.get('to') %}
{% if not parent %}
{{ exceptions.raise_compiler_error('No parent table defined for foreign key: ' ~ expression) }}
{% endif %}
Expand All @@ -228,21 +228,21 @@
{% endif %}

{% set stmt = "alter table " ~ relation ~ " add constraint " ~ name ~ " foreign key(" ~ joined_names ~ ") references " ~ parent %}
{% set parent_columns = constraint.get("to_columns") %}
{% set parent_columns = constraint.get('to_columns') %}
{% if parent_columns %}
{% set stmt = stmt ~ "(" ~ parent_columns|join(", ") ~ ")"%}
{% endif %}
{% endif %}
{% set stmt = stmt ~ ";" %}
{% do statements.append(stmt) %}
{% elif type == 'custom' %}
{% set expression = constraint.get("expression", "") %}
{% set expression = constraint.get('expression', '') %}
{% if not expression %}
{{ exceptions.raise_compiler_error('Missing custom constraint expression') }}
{% endif %}

{% set name = constraint.get("name") %}
{% set expression = constraint.get("expression") %}
{% set name = constraint.get('name') %}
{% set expression = constraint.get('expression') %}
{% if not name %}
{% if local_md5 %}
{{ exceptions.warn("Constraint of type " ~ type ~ " with no `name` provided. Generating hash instead for relation " ~ relation.identifier) }}
Expand All @@ -264,15 +264,15 @@
{# convert constraints defined using the original databricks format #}
{% set dbt_constraints = [] %}
{% for constraint in constraints %}
{% if constraint.get and constraint.get("type") %}
{% if constraint.get and constraint.get('type') %}
{# already in model contract format #}
{% do dbt_constraints.append(constraint) %}
{% else %}
{% if column %}
{% if constraint == "not_null" %}
{% do dbt_constraints.append({"type": "not_null", "columns": [column.get("name")]}) %}
{% do dbt_constraints.append({"type": "not_null", "columns": [column.get('name')]}) %}
{% else %}
{{ exceptions.raise_compiler_error('Invalid constraint for column ' ~ column.get("name", "") ~ '. Only `not_null` is supported.') }}
{{ exceptions.raise_compiler_error('Invalid constraint for column ' ~ column.get('name', "") ~ '. Only `not_null` is supported.') }}
{% endif %}
{% else %}
{% set name = constraint['name'] %}
Expand Down

0 comments on commit b0ff51b

Please sign in to comment.