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

Add null equality condition to equals macro #744

Merged
merged 49 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
a1006c1
Added equals macro to handle null value comparison
adrianburusdbt Dec 17, 2024
7e816be
Changelog update
adrianburusdbt Dec 17, 2024
70afd18
Update equals.sql macro
adrianburusdbt Dec 17, 2024
b8312d6
Remved unused test fixture
adrianburusdbt Dec 17, 2024
85c954f
Attempt to fix end of file error
adrianburusdbt Dec 17, 2024
7565dea
Trim trailing whitespace
adrianburusdbt Dec 17, 2024
d582ab1
PR comments
adrianburusdbt Dec 17, 2024
8937201
Avoid using nested {{ }}
adrianburusdbt Dec 17, 2024
087e64d
Update test seeds / match counts
adrianburusdbt Dec 18, 2024
cc9fb55
Merge branch 'main' into fix_null_equality_110
VersusFacit Dec 19, 2024
e8843fb
Add back macros__equals_sql test fixture
adrianburusdbt Dec 19, 2024
6fd7240
Merge branch 'fix_null_equality_110' of https://github.com/dbt-labs/d…
adrianburusdbt Dec 19, 2024
4e09e9c
Merge branch 'main' into fix_null_equality_110
adrianburusdbt Dec 19, 2024
5b9a2fd
Merge branch 'main' into fix_null_equality_110
adrianburusdbt Dec 20, 2024
fc448be
Add reference to test fixture for equals from adapter utils
adrianburusdbt Jan 8, 2025
75f3f46
Merge branch 'fix_null_equality_110' of https://github.com/dbt-labs/d…
adrianburusdbt Jan 8, 2025
11283d9
Add equals as an include in test_equals.py
adrianburusdbt Jan 8, 2025
9c2104c
Added base_utils ref
adrianburusdbt Jan 8, 2025
214feab
Merge remote-tracking branch 'origin/fix_null_equality_110' into add_…
VersusFacit Jan 27, 2025
16169fc
TEMPORARY test refs to this branch.
VersusFacit Jan 27, 2025
27602a3
reformat
VersusFacit Jan 27, 2025
e77f0a2
TEMPORARY Move refs of branches.
VersusFacit Jan 27, 2025
5911260
Fix git diff
VersusFacit Jan 27, 2025
3cb3117
Fix ws
VersusFacit Jan 27, 2025
e9e791e
Fix bad metadata directive
VersusFacit Jan 27, 2025
acf3b4d
Add behavior flag for the null equality macro.
VersusFacit Jan 28, 2025
548f086
Fix behavior flag.
VersusFacit Jan 28, 2025
f3e0620
Keep moving forward on fixing macros here.
VersusFacit Jan 28, 2025
f52aed2
Disable stubborn test about partitioning.
VersusFacit Jan 28, 2025
9d83c0b
merge main.
VersusFacit Jan 28, 2025
886000a
remove unneeded directive.
VersusFacit Jan 28, 2025
ecc941a
revert pyproject files.
VersusFacit Jan 28, 2025
d16a938
Prevent undesired change of behavior.
VersusFacit Jan 28, 2025
9b05a8c
Fix tests.
VersusFacit Jan 28, 2025
6abab94
restore macro definition headers.
VersusFacit Jan 28, 2025
0e8df0c
Fix that stubborn bug by retracting ansi equals to plain equals excep…
VersusFacit Jan 28, 2025
68c7067
revert alteration to functional equality macro.
VersusFacit Jan 28, 2025
e70e95a
Readd the change.
VersusFacit Jan 28, 2025
b9946bd
Keep macro in place for now. Unpredictable to modify.
VersusFacit Jan 28, 2025
68394fb
Merge branch 'main' into add_null_equality_condition_to_equals_macro
VersusFacit Feb 3, 2025
4883cdc
Push broken code to try proving a point.
VersusFacit Feb 3, 2025
88534dd
FINALLY figured it out.
VersusFacit Feb 3, 2025
8e238e9
Finish fixing tests.-
VersusFacit Feb 3, 2025
7fc9289
Add me author cus colin said so <(^-^<)
VersusFacit Feb 3, 2025
b8843b1
Adjust boolean logic of snapshot materializations
VersusFacit Feb 3, 2025
5851e07
More authentic mapping.
VersusFacit Feb 3, 2025
5f7aa8a
Still fixing core tests.
VersusFacit Feb 3, 2025
4b349c6
One more adjustment for core.
VersusFacit Feb 3, 2025
79929d9
Merge branch 'main' into add_null_equality_condition_to_equals_macro
VersusFacit Feb 3, 2025
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/Under the Hood-20241217-110536.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Added new equals macro that handles null value checks in sql
time: 2024-12-17T11:05:36.363421+02:00
custom:
Author: adrianburusdbt,versusfacit
Issue: "159"
7 changes: 6 additions & 1 deletion dbt-adapters/src/dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@
"name": "require_batched_execution_for_custom_microbatch_strategy",
"default": False,
"docs_url": "https://docs.getdbt.com/docs/build/incremental-microbatch",
}
},
{
"name": "enable_truthy_nulls_equals_macro",
"default": False,
"docs_url": "",
},
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
{% do predicates.append(this_key_match) %}
{% endfor %}
{% else %}
{% set unique_key_match %}
DBT_INTERNAL_SOURCE.{{ unique_key }} = DBT_INTERNAL_DEST.{{ unique_key }}
{% endset %}
{% set source_unique_key = ("DBT_INTERNAL_SOURCE." ~ unique_key) | trim %}
{% set target_unique_key = ("DBT_INTERNAL_DEST." ~ unique_key) | trim %}
{% set unique_key_match = equals(source_unique_key, target_unique_key) | trim %}
{% do predicates.append(unique_key_match) %}
{% endif %}
{% else %}
Expand Down Expand Up @@ -62,11 +62,13 @@

{% if unique_key %}
{% if unique_key is sequence and unique_key is not string %}
delete from {{target }}
delete from {{ target }}
using {{ source }}
where (
{% for key in unique_key %}
{{ source }}.{{ key }} = {{ target }}.{{ key }}
{% set source_unique_key = (source ~ "." ~ key) | trim %}
{% set target_unique_key = (target ~ "." ~ key) | trim %}
{{ equals(source_unique_key, target_unique_key) }}
{{ "and " if not loop.last}}
{% endfor %}
{% if incremental_predicates %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@
from {{ target_relation }}
where
{% if config.get('dbt_valid_to_current') %}
{# Check for either dbt_valid_to_current OR null, in order to correctly update records with nulls #}
( {{ columns.dbt_valid_to }} = {{ config.get('dbt_valid_to_current') }} or {{ columns.dbt_valid_to }} is null)
{% set source_unique_key = columns.dbt_valid_to | trim %}
{% set target_unique_key = config.get('dbt_valid_to_current') | trim %}

{# The exact equals semantics between NULL values depends on the current behavior flag set. Also, update records if the source field is null #}
( {{ equals(source_unique_key, target_unique_key) }} or {{ source_unique_key }} is null )
{% else %}
{{ columns.dbt_valid_to }} is null
{% endif %}
Expand Down Expand Up @@ -279,7 +282,9 @@
{% macro unique_key_join_on(unique_key, identifier, from_identifier) %}
{% if unique_key | is_list %}
{% for key in unique_key %}
{{ identifier }}.dbt_unique_key_{{ loop.index }} = {{ from_identifier }}.dbt_unique_key_{{ loop.index }}
{% set source_unique_key = (identifier ~ ".dbt_unique_key_" ~ loop.index) | trim %}
{% set target_unique_key = (from_identifier ~ ".dbt_unique_key_" ~ loop.index) | trim %}
{{ equals(source_unique_key, target_unique_key) }}
{%- if not loop.last %} and {%- endif %}
{% endfor %}
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

when matched
{% if config.get("dbt_valid_to_current") %}
and (DBT_INTERNAL_DEST.{{ columns.dbt_valid_to }} = {{ config.get('dbt_valid_to_current') }} or
DBT_INTERNAL_DEST.{{ columns.dbt_valid_to }} is null)
{% set source_unique_key = ("DBT_INTERNAL_DEST." ~ columns.dbt_valid_to) | trim %}
{% set target_unique_key = config.get('dbt_valid_to_current') | trim %}
and ({{ equals(source_unique_key, target_unique_key) }} or {{ source_unique_key }} is null)

{% else %}
and DBT_INTERNAL_DEST.{{ columns.dbt_valid_to }} is null
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% macro equals(expr1, expr2) %}
{{ return(adapter.dispatch('equals', 'dbt') (expr1, expr2)) }}
{%- endmacro %}

{% macro default__equals(expr1, expr2) -%}
{%- if adapter.behavior.enable_truthy_nulls_equals_macro.no_warn %}
case when (({{ expr1 }} = {{ expr2 }}) or ({{ expr1 }} is null and {{ expr2 }} is null))
colin-rogers-dbt marked this conversation as resolved.
Show resolved Hide resolved
then 0
else 1
end = 0
{%- else -%}
({{ expr1 }} = {{ expr2 }})
{%- endif %}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -1,5 +1,150 @@
from dbt.tests.adapter.incremental.test_incremental_unique_id import BaseIncrementalUniqueKey
import pytest

from dbt.contracts.results import RunStatus

class TestUniqueKeyBigQuery(BaseIncrementalUniqueKey):
from dbt.tests.adapter.incremental.test_incremental_unique_id import SubBaseIncrementalUniqueKey
colin-rogers-dbt marked this conversation as resolved.
Show resolved Hide resolved


class IncrementalUniqueKeyFalseyNullsEquals(SubBaseIncrementalUniqueKey):
def test__bad_unique_key(self, project):
"""expect compilation error from unique key not being a column"""

(status, exc) = self.fail_to_build_inc_missing_unique_key_column(
incremental_model_name="not_found_unique_key"
)

assert status == RunStatus.Error
assert "thisisnotacolumn" in exc.lower()

# test unique_key as list
def test__empty_unique_key_list(self, project):
"""with no unique keys, seed and model should match"""

expected_fields = self.get_expected_fields(relation="seed", seed_rows=9)
test_case_fields = self.get_test_fields(
project,
seed="seed",
incremental_model="empty_unique_key_list",
update_sql_file="add_new_rows",
)
self.check_scenario_correctness(expected_fields, test_case_fields, project)

def test__one_unique_key(self, project):
"""with one unique key, model will overwrite existing row"""

expected_fields = self.get_expected_fields(
relation="one_str__overwrite", seed_rows=8, opt_model_count=1
)
test_case_fields = self.get_test_fields(
project,
seed="seed",
incremental_model="str_unique_key",
update_sql_file="duplicate_insert",
opt_model_count=self.update_incremental_model("one_str__overwrite"),
)
self.check_scenario_correctness(expected_fields, test_case_fields, project)

def test__bad_unique_key_list(self, project):
"""expect compilation error from unique key not being a column"""

(status, exc) = self.fail_to_build_inc_missing_unique_key_column(
incremental_model_name="not_found_unique_key_list"
)

assert status == RunStatus.Error
assert "thisisnotacolumn" in exc.lower()


class IncrementalUniqueKeyTruthyNullsEquals(SubBaseIncrementalUniqueKey):
@pytest.fixture(scope="class")
def project_config_update(self):
return {"flags": {"enable_truthy_nulls_equals_macro": True}}

# no unique_key test
def test__no_unique_keys(self, project):
"""with no unique keys, seed and model should match"""

expected_fields = self.get_expected_fields(relation="seed", seed_rows=9)
test_case_fields = self.get_test_fields(
project, seed="seed", incremental_model="no_unique_key", update_sql_file="add_new_rows"
)
self.check_scenario_correctness(expected_fields, test_case_fields, project)

# unique_key as str tests
def test__empty_str_unique_key(self, project):
"""with empty string for unique key, seed and model should match"""

expected_fields = self.get_expected_fields(relation="seed", seed_rows=9)
test_case_fields = self.get_test_fields(
project,
seed="seed",
incremental_model="empty_str_unique_key",
update_sql_file="add_new_rows",
)
self.check_scenario_correctness(expected_fields, test_case_fields, project)

def test__unary_unique_key_list(self, project):
"""with one unique key, model will overwrite existing row"""

expected_fields = self.get_expected_fields(
relation="unique_key_list__inplace_overwrite", seed_rows=8, opt_model_count=1
)
test_case_fields = self.get_test_fields(
project,
seed="seed",
incremental_model="unary_unique_key_list",
update_sql_file="duplicate_insert",
opt_model_count=self.update_incremental_model("unique_key_list__inplace_overwrite"),
)
self.check_scenario_correctness(expected_fields, test_case_fields, project)

def test__duplicated_unary_unique_key_list(self, project):
"""with two of the same unique key, model will overwrite existing row"""

expected_fields = self.get_expected_fields(
relation="unique_key_list__inplace_overwrite", seed_rows=8, opt_model_count=1
)
test_case_fields = self.get_test_fields(
project,
seed="seed",
incremental_model="duplicated_unary_unique_key_list",
update_sql_file="duplicate_insert",
opt_model_count=self.update_incremental_model("unique_key_list__inplace_overwrite"),
)
self.check_scenario_correctness(expected_fields, test_case_fields, project)

def test__trinary_unique_key_list(self, project):
"""with three unique keys, model will overwrite existing row"""

expected_fields = self.get_expected_fields(
relation="unique_key_list__inplace_overwrite", seed_rows=8, opt_model_count=1
)
test_case_fields = self.get_test_fields(
project,
seed="seed",
incremental_model="trinary_unique_key_list",
update_sql_file="duplicate_insert",
opt_model_count=self.update_incremental_model("unique_key_list__inplace_overwrite"),
)
self.check_scenario_correctness(expected_fields, test_case_fields, project)

def test__trinary_unique_key_list_no_update(self, project):
"""even with three unique keys, adding distinct rows to seed does not
cause seed and model to diverge"""

expected_fields = self.get_expected_fields(relation="seed", seed_rows=9)
test_case_fields = self.get_test_fields(
project,
seed="seed",
incremental_model="nontyped_trinary_unique_key_list",
update_sql_file="add_new_rows",
)
self.check_scenario_correctness(expected_fields, test_case_fields, project)


class TestIncrementalUniqueKeyFalseyNullsEquals(IncrementalUniqueKeyFalseyNullsEquals):
pass


class TestIncrementalUniqueKeyTruthyNullsEquals(IncrementalUniqueKeyTruthyNullsEquals):
pass
Loading