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] Wrap constraint type 'check' expression in parentheses #750

Merged
merged 14 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230512-151453.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: wrap expression for check constraints in parentheses
time: 2023-05-12T15:14:53.151149-04:00
custom:
Author: michelleark
Issue: "7480"
2 changes: 1 addition & 1 deletion dbt/include/spark/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
{% if constraint.type == 'check' and not is_incremental() %}
{%- set constraint_hash = local_md5(column_name ~ ";" ~ constraint.expression ~ ";" ~ loop.index) -%}
{% call statement() %}
alter table {{ relation }} add constraint {{ constraint.name if constraint.name else constraint_hash }} check {{ constraint.expression }};
alter table {{ relation }} add constraint {{ constraint.name if constraint.name else constraint_hash }} check ({{ constraint.expression }});
{% endcall %}
{% endif %}
{% endfor %}
Expand Down
4 changes: 2 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# install latest changes in dbt-core
# TODO: how to automate switching from develop to version branches?
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-tests-adapter&subdirectory=tests/adapter
git+https://github.com/dbt-labs/dbt-core.git@fix-constraint-rendering#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-core.git@fix-constraint-rendering#egg=dbt-tests-adapter&subdirectory=tests/adapter

# if version 1.x or greater -> pin to major version
# if version 0.x -> pin to minor
Expand Down
32 changes: 23 additions & 9 deletions tests/functional/adapter/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
my_model_incremental_wrong_order_sql,
my_model_incremental_wrong_name_sql,
my_incremental_model_sql,
model_fk_constraint_schema_yml,
my_model_wrong_order_depends_on_fk_sql,
foreign_key_model_sql,
my_model_incremental_wrong_order_depends_on_fk_sql,
)

# constraints are enforced via 'alter' statements that run after table creation
Expand All @@ -33,7 +37,9 @@
date_day
from

( select
(
-- depends_on: <foreign_key_model_identifier>
select
'blue' as color,
1 as id,
'2019-01-01' as date_day ) as model_subq
Expand All @@ -49,15 +55,20 @@
date_day
from

( select
1 as id,
(
-- depends_on: <foreign_key_model_identifier>
select
'blue' as color,
1 as id,
'2019-01-01' as date_day ) as model_subq
"""

# Different on Spark:
# - does not support a data type named 'text' (TODO handle this in the base test classes using string_type
constraints_yml = model_schema_yml.replace("text", "string").replace("primary key", "")
model_fk_constraint_schema_yml = model_fk_constraint_schema_yml.replace("text", "string").replace(
"primary key", ""
)
model_constraints_yml = constrained_model_schema_yml.replace("text", "string")


Expand Down Expand Up @@ -234,8 +245,9 @@ class TestSparkTableConstraintsDdlEnforcement(
@pytest.fixture(scope="class")
def models(self):
return {
"my_model.sql": my_model_wrong_order_sql,
"constraints_schema.yml": constraints_yml,
"my_model.sql": my_model_wrong_order_depends_on_fk_sql,
"foreign_key_model.sql": foreign_key_model_sql,
"constraints_schema.yml": model_fk_constraint_schema_yml,
}


Expand All @@ -246,8 +258,9 @@ class TestSparkIncrementalConstraintsDdlEnforcement(
@pytest.fixture(scope="class")
def models(self):
return {
"my_model.sql": my_model_incremental_wrong_order_sql,
"constraints_schema.yml": constraints_yml,
"my_model.sql": my_model_incremental_wrong_order_depends_on_fk_sql,
"foreign_key_model.sql": foreign_key_model_sql,
"constraints_schema.yml": model_fk_constraint_schema_yml,
}


Expand Down Expand Up @@ -328,8 +341,9 @@ def project_config_update(self):
@pytest.fixture(scope="class")
def models(self):
return {
"my_model.sql": my_incremental_model_sql,
"constraints_schema.yml": model_constraints_yml,
"my_model.sql": my_model_wrong_order_depends_on_fk_sql,
"foreign_key_model.sql": foreign_key_model_sql,
"constraints_schema.yml": model_fk_constraint_schema_yml,
}

@pytest.fixture(scope="class")
Expand Down