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

Update adapter so temporary tables are not created when foreign keys #927

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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-20241011-103317.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Foreign key constraint on incremental model results in Database Error
time: 2024-10-11T10:33:17.860744-04:00
custom:
Author: bdgeise
Issue: "628"
33 changes: 12 additions & 21 deletions dbt/include/redshift/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,28 @@
{%- set backup = config.get('backup') -%}

{{ sql_header if sql_header is not none }}

{%- set contract_config = config.get('contract') -%}
{%- if contract_config.enforced -%}

create {% if temporary -%}temporary{%- endif %} table
{{ relation.include(database=(not temporary), schema=(not temporary)) }}
{{ get_table_columns_and_constraints() }}
{{ get_assert_columns_equivalent(sql) }}
{%- set sql = get_select_subquery(sql) %}
{% if backup == false -%}backup no{%- endif %}
{{ dist(_dist) }}
{{ sort(_sort_type, _sort) }}
;

insert into {{ relation.include(database=(not temporary), schema=(not temporary)) }}
(
{{ sql }}
)
;

{%- set contract_config = config.get('contract') -%}
{%- if contract_config.enforced -%}
{{ get_assert_columns_equivalent(sql) }}
{% endif -%}
{% if contract_config.enforced and (not temporary) -%}
{{ get_table_columns_and_constraints() }};
{%- set sql = get_select_subquery(sql) %}
insert into {{ relation.include(database=(not temporary), schema=(not temporary)) }}
(
{{ sql }}
)
;
{%- else %}

create {% if temporary -%}temporary{%- endif %} table
{{ relation.include(database=(not temporary), schema=(not temporary)) }}
{% if backup == false -%}backup no{%- endif %}
{{ dist(_dist) }}
{{ sort(_sort_type, _sort) }}
as (
{{ sql }}
);

{%- endif %}
{%- endmacro %}

Expand Down
52 changes: 52 additions & 0 deletions tests/functional/adapter/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
BaseIncrementalConstraintsRollback,
BaseModelConstraintsRuntimeEnforcement,
BaseConstraintQuotedColumn,
TestIncrementalForeignKeyConstraint,
)

_expected_sql_redshift = """
Expand Down Expand Up @@ -147,3 +148,54 @@ def expected_sql(self):
) as model_subq
);
"""


class TestRedshiftIncrementalForeignKeyConstraint(TestIncrementalForeignKeyConstraint):
@pytest.fixture(scope="class")
def macros(self):
return {
"create_table.sql": """
{% macro create_table_macro() %}
create table if not exists numbers (n int not null primary key)
{% endmacro %}
""",
}

@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": """
version: 2
models:
- name: raw_numbers
config:
contract:
enforced: true
materialized: table
columns:
- name: n
data_type: integer
constraints:
- type: primary_key
- type: not_null
- name: stg_numbers
config:
contract:
enforced: true
materialized: incremental
on_schema_change: append_new_columns
unique_key: n
columns:
- name: n
data_type: integer
constraints:
- type: foreign_key
expression: {schema}.raw_numbers (n)
""",
"raw_numbers.sql": """
select 1 as n
""",
"stg_numbers.sql": """
select * from {{ ref('raw_numbers') }}
""",
}