-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic to enable iceberg incremental tables. (#1190)
* Add logic to enable iceberg incremental tables. * Add changelog. * Standardize existing_relation as name of existing model. * Improve error message for table formats. * Update error message and add round of tests. * Add more comprehensive tests for before/after. * Update identifier param in incremental materialization. * Import Mike's revision on the relation type change test. * Try adding iceberg incremental model scenarios. * Disable dynamic tests. * Refine booleans for more restricting when incremental models are built. * Update tests to reflect realities on the database. * Add additional metadata. * Update boolean logic for faster runs overall. * Last bit of cleanup per code review * Syntax error from lack of iterable.
- Loading branch information
1 parent
583ec5e
commit d7632eb
Showing
6 changed files
with
258 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Add Iceberg format Incremental Models | ||
time: 2024-09-23T20:32:04.783741-07:00 | ||
custom: | ||
Author: versusfacit | ||
Issue: "321" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import pytest | ||
|
||
from pathlib import Path | ||
|
||
from dbt.tests.util import run_dbt, run_dbt_and_capture | ||
|
||
|
||
_SEED_INCREMENTAL_STRATEGIES = """ | ||
world_id,world_name,boss | ||
1,Yoshi's Island,Iggy | ||
2,Donut Plains,Morton | ||
3,Vanilla Dome,Lemmy | ||
4,Cookie Mountain,Temmy | ||
5,Forest of Illusion,Roy | ||
""".strip() | ||
|
||
_MODEL_BASIC_TABLE_MODEL = """ | ||
{{ | ||
config( | ||
materialized = "table", | ||
) | ||
}} | ||
select * from {{ ref('seed') }} | ||
""" | ||
|
||
_MODEL_INCREMENTAL_ICEBERG_BASE = """ | ||
{{{{ | ||
config( | ||
materialized='incremental', | ||
table_format='iceberg', | ||
incremental_strategy='{strategy}', | ||
unique_key="world_id", | ||
external_volume = "s3_iceberg_snow", | ||
) | ||
}}}} | ||
select * from {{{{ ref('upstream_table') }}}} | ||
{{% if is_incremental() %}} | ||
where world_id > 2 | ||
{{% endif %}} | ||
""" | ||
|
||
_MODEL_INCREMENTAL_ICEBERG_APPEND = _MODEL_INCREMENTAL_ICEBERG_BASE.format(strategy="append") | ||
_MODEL_INCREMENTAL_ICEBERG_MERGE = _MODEL_INCREMENTAL_ICEBERG_BASE.format(strategy="merge") | ||
_MODEL_INCREMENTAL_ICEBERG_DELETE_INSERT = _MODEL_INCREMENTAL_ICEBERG_BASE.format( | ||
strategy="delete+insert" | ||
) | ||
|
||
|
||
_QUERY_UPDATE_UPSTREAM_TABLE = """ | ||
UPDATE {database}.{schema}.upstream_table set world_name = 'Twin Bridges', boss = 'Ludwig' where world_id = 4; | ||
""" | ||
|
||
_QUERY_UPDATE_UPSTREAM_TABLE_NO_EFFECT = """ | ||
UPDATE {database}.{schema}.upstream_table set world_name = 'Doughnut Plains' where world_id = 2; | ||
""" | ||
|
||
|
||
class TestIcebergIncrementalStrategies: | ||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return {"flags": {"enable_iceberg_materializations": True}} | ||
|
||
@pytest.fixture(scope="class") | ||
def seeds(self): | ||
return { | ||
"seed.csv": _SEED_INCREMENTAL_STRATEGIES, | ||
} | ||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
def setup_class(self, project): | ||
run_dbt(["seed"]) | ||
yield | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"upstream_table.sql": _MODEL_BASIC_TABLE_MODEL, | ||
"append.sql": _MODEL_INCREMENTAL_ICEBERG_APPEND, | ||
"merge.sql": _MODEL_INCREMENTAL_ICEBERG_MERGE, | ||
"delete_insert.sql": _MODEL_INCREMENTAL_ICEBERG_DELETE_INSERT, | ||
} | ||
|
||
def test_incremental_strategies_build(self, project, setup_class): | ||
run_results = run_dbt() | ||
assert len(run_results) == 4 | ||
|
||
def __check_correct_operations(self, model_name, /, rows_affected, status="SUCCESS"): | ||
run_results = run_dbt( | ||
["show", "--inline", f"select * from {{{{ ref('{model_name}') }}}} where world_id = 4"] | ||
) | ||
assert run_results[0].adapter_response["rows_affected"] == rows_affected | ||
assert run_results[0].adapter_response["code"] == status | ||
|
||
if model_name != "append": | ||
run_results, stdout = run_dbt_and_capture( | ||
[ | ||
"show", | ||
"--inline", | ||
f"select * from {{{{ ref('{model_name}') }}}} where world_id = 2", | ||
] | ||
) | ||
run_results[0].adapter_response["rows_affected"] == 1 | ||
assert "Doughnut" not in stdout | ||
|
||
def test_incremental_strategies_with_update(self, project, setup_class): | ||
run_results = run_dbt() | ||
assert len(run_results) == 4 | ||
|
||
project.run_sql( | ||
_QUERY_UPDATE_UPSTREAM_TABLE.format( | ||
database=project.database, schema=project.test_schema | ||
) | ||
) | ||
project.run_sql( | ||
_QUERY_UPDATE_UPSTREAM_TABLE_NO_EFFECT.format( | ||
database=project.database, schema=project.test_schema | ||
) | ||
) | ||
|
||
run_results = run_dbt(["run", "-s", "append", "merge", "delete_insert"]) | ||
assert len(run_results) == 3 | ||
|
||
self.__check_correct_operations("append", rows_affected=3) | ||
self.__check_correct_operations("merge", rows_affected=1) | ||
self.__check_correct_operations("delete_insert", rows_affected=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters